-
Notifications
You must be signed in to change notification settings - Fork 6.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22568 from sennyeya/catalog-entity-policies
feat(catalog): add support for entity policies in the new backend.
- Loading branch information
Showing
9 changed files
with
221 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
--- | ||
'@backstage/plugin-catalog-backend': minor | ||
'@backstage/plugin-catalog-node': minor | ||
--- | ||
|
||
Adds support for supplying field validators to the new backend's catalog plugin. If you're using entity policies, you should use the new `transformLegacyPolicyToProcessor` function to install them as processors instead. | ||
|
||
```ts | ||
import { | ||
catalogProcessingExtensionPoint, | ||
catalogModelExtensionPoint, | ||
} from '@backstage/plugin-catalog-node/alpha'; | ||
import {myPolicy} from './my-policy'; | ||
|
||
export const catalogModulePolicyProvider = createBackendModule({ | ||
pluginId: 'catalog', | ||
moduleId: 'internal-policy-provider', | ||
register(reg) { | ||
reg.registerInit({ | ||
deps: { | ||
modelExtensions: catalogModelExtensionPoint, | ||
processingExtensions: catalogProcessingExtensionPoint, | ||
}, | ||
async init({ modelExtensions, processingExtensions }) { | ||
modelExtensions.setFieldValidators({ | ||
... | ||
}); | ||
processingExtensions.addProcessors(transformLegacyPolicyToProcessor(myPolicy)) | ||
}, | ||
}); | ||
}, | ||
}); | ||
``` |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
90 changes: 90 additions & 0 deletions
90
plugins/catalog-backend/src/modules/core/transformLegacyPolicyToProcessor.test.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,90 @@ | ||
/* | ||
* Copyright 2024 The Backstage Authors | ||
* | ||
* 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 { Entity, EntityPolicy } from '@backstage/catalog-model'; | ||
import { transformLegacyPolicyToProcessor } from './transformLegacyPolicyToProcessor'; | ||
import { clone } from 'lodash'; | ||
|
||
describe('transformLegacyPolicyToProcessor', () => { | ||
const entityToProcess: Entity = { | ||
apiVersion: 'backstage.io/v1alpha', | ||
kind: 'Component', | ||
metadata: { | ||
name: 'test', | ||
}, | ||
}; | ||
it('modifies the entity if the policy modifies the entity', async () => { | ||
const policy: EntityPolicy = { | ||
async enforce(entity) { | ||
entity.kind = 'Group'; | ||
return entity; | ||
}, | ||
}; | ||
const processor = transformLegacyPolicyToProcessor(policy); | ||
const clonedEntity = clone(entityToProcess); | ||
const entity = await processor.preProcessEntity?.( | ||
clonedEntity, | ||
{} as any, | ||
jest.fn(), | ||
{} as any, | ||
{} as any, | ||
); | ||
expect(entity).toBeTruthy(); | ||
expect(entity?.kind).toBe('Group'); | ||
expect(entity?.apiVersion).toBe('backstage.io/v1alpha'); | ||
expect(entity?.metadata.name).toBe('test'); | ||
}); | ||
|
||
it('does not modify the entity if the policy returns undefined', async () => { | ||
const policy: EntityPolicy = { | ||
async enforce() { | ||
return undefined; | ||
}, | ||
}; | ||
const processor = transformLegacyPolicyToProcessor(policy); | ||
const clonedEntity = clone(entityToProcess); | ||
const entity = await processor.preProcessEntity?.( | ||
clonedEntity, | ||
{} as any, | ||
jest.fn(), | ||
{} as any, | ||
{} as any, | ||
); | ||
expect(entity).toBeTruthy(); | ||
expect(entity?.kind).toBe('Component'); | ||
expect(entity?.apiVersion).toBe('backstage.io/v1alpha'); | ||
expect(entity?.metadata.name).toBe('test'); | ||
}); | ||
|
||
it('bubbles up processor error', async () => { | ||
const policy: EntityPolicy = { | ||
async enforce() { | ||
throw new TypeError('Invalid value for metadata.name'); | ||
}, | ||
}; | ||
const processor = transformLegacyPolicyToProcessor(policy); | ||
const clonedEntity = clone(entityToProcess); | ||
await expect( | ||
processor.preProcessEntity?.( | ||
clonedEntity, | ||
{} as any, | ||
jest.fn(), | ||
{} as any, | ||
{} as any, | ||
), | ||
).rejects.toThrow(/Invalid value for metadata.name/); | ||
}); | ||
}); |
42 changes: 42 additions & 0 deletions
42
plugins/catalog-backend/src/modules/core/transformLegacyPolicyToProcessor.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,42 @@ | ||
/* | ||
* Copyright 2024 The Backstage Authors | ||
* | ||
* 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 { EntityPolicy } from '@backstage/catalog-model'; | ||
import { CatalogProcessor } from '@backstage/plugin-catalog-node'; | ||
|
||
/** | ||
* Transform a given entity policy to an entity processor. | ||
* @param policy - The policy to transform | ||
* @returns A new entity processor that uses the entity policy. | ||
* @public | ||
*/ | ||
export function transformLegacyPolicyToProcessor( | ||
policy: EntityPolicy, | ||
): CatalogProcessor { | ||
return { | ||
getProcessorName() { | ||
return policy.constructor.name; | ||
}, | ||
async preProcessEntity(entity) { | ||
// If enforcing the policy fails, throw the policy error. | ||
const result = await policy.enforce(entity); | ||
if (!result) { | ||
return entity; | ||
} | ||
return result; | ||
}, | ||
}; | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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