-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SALTO-5737 space deployment filter to handle permission changes (#5795)
- Loading branch information
1 parent
4cdad50
commit 5927782
Showing
12 changed files
with
1,057 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
packages/confluence-adapter/src/definitions/transformation_utils/space.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* Copyright 2024 Salto Labs Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { definitions } from '@salto-io/adapter-components' | ||
import _ from 'lodash' | ||
import { logger } from '@salto-io/logging' | ||
import { Value } from '@salto-io/adapter-api' | ||
import { values } from '@salto-io/lowerdash' | ||
import { assertValue } from './generic' | ||
|
||
const log = logger(module) | ||
|
||
export type PermissionObject = { | ||
type: string | ||
principalId: string | ||
key: string | ||
targetType: string | ||
} | ||
|
||
export const createPermissionUniqueKey = ({ type, principalId, key, targetType }: PermissionObject): string => | ||
`${type}_${principalId}_${key}_${targetType}` | ||
|
||
export const isPermissionObject = (value: unknown): value is PermissionObject => | ||
_.isString(_.get(value, 'type')) && | ||
_.isString(_.get(value, 'principalId')) && | ||
_.isString(_.get(value, 'key')) && | ||
_.isString(_.get(value, 'targetType')) | ||
|
||
/** | ||
* Restructures a single raw permission object from the service and updates permissionInternalIdMap with the relevant service id. | ||
* @param permission - raw permission from the service. | ||
* @param permissionInternalIdMap - serviceIds map to update. | ||
* @param onFetch - is raw permission came upon fetch or deploy (service returns different structures). | ||
*/ | ||
export const transformPermissionAndUpdateIdMap = ( | ||
permission: Value, | ||
permissionInternalIdMap: Record<string, string>, | ||
onFetch?: boolean, | ||
): PermissionObject | undefined => { | ||
const type = onFetch ? _.get(permission, 'principal.type') : _.get(permission, 'subject.type') | ||
const principalId = onFetch ? _.get(permission, 'principal.id') : _.get(permission, 'subject.identifier') | ||
const key = _.get(permission, 'operation.key') | ||
const targetType = onFetch ? _.get(permission, 'operation.targetType') : _.get(permission, 'operation.target') | ||
const internalId = _.get(permission, 'id') | ||
if ([type, principalId, key, targetType].some(x => !_.isString(x)) || internalId === undefined) { | ||
log.warn('permission is not in expected format: %o, skipping', permission) | ||
return undefined | ||
} | ||
permissionInternalIdMap[createPermissionUniqueKey({ type, principalId, key, targetType })] = String(internalId) | ||
return { type, principalId, key, targetType } | ||
} | ||
|
||
/** | ||
* Restructures permissions array on space instance value and creates an internal ID map. | ||
* To be used on deploy. We need this as we cannot hide fields inside arrays | ||
* @param value - value containing raw permissions array from the service. | ||
*/ | ||
export const restructurePermissionsAndCreateInternalIdMap = (value: Record<string, Value>): void => { | ||
const permissions = _.get(value, 'permissions') | ||
if (!Array.isArray(permissions)) { | ||
log.warn('permissions is not an array: %o, skipping space adjust function', permissions) | ||
return | ||
} | ||
const permissionInternalIdMap: Record<string, string> = {} | ||
const transformedPermissions = permissions | ||
.map(per => transformPermissionAndUpdateIdMap(per, permissionInternalIdMap, true)) | ||
.filter(values.isDefined) | ||
value.permissions = transformedPermissions | ||
value.permissionInternalIdMap = { ...permissionInternalIdMap } | ||
} | ||
|
||
/** | ||
* Adjust function for transforming space instances upon fetch. | ||
* We reconstruct the permissions so we use this function on resource and not on request. | ||
* @param item - The item to adjust. | ||
*/ | ||
export const spaceMergeAndTransformAdjust: definitions.AdjustFunction<{ | ||
fragments: definitions.GeneratedItem[] | ||
}> = item => { | ||
const value = assertValue(item.value) | ||
restructurePermissionsAndCreateInternalIdMap(value) | ||
return { value } | ||
} |
Oops, something went wrong.