-
Notifications
You must be signed in to change notification settings - Fork 53
fix(permission-policy): preserve scoped capability paths for catalog entity visibility #767
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kavix
wants to merge
2
commits into
openchoreo:main
Choose a base branch
from
kavix:fix/issue-763-catalog-scoped-project-view
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,5 @@ | ||
| --- | ||
| '@openchoreo/backstage-plugin-permission-backend-module-openchoreo-policy': patch | ||
| --- | ||
|
|
||
| Preserve scoped capability paths for catalog entity visibility checks (Fixes #763) |
181 changes: 181 additions & 0 deletions
181
...permission-backend-module-openchoreo-policy/src/policy/OpenChoreoPermissionPolicy.test.ts
This file contains hidden or 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,181 @@ | ||
| import { mockServices } from '@backstage/backend-test-utils'; | ||
| import { AuthorizeResult } from '@backstage/plugin-permission-common'; | ||
| import { catalogEntityReadPermission } from '@backstage/plugin-catalog-common/alpha'; | ||
| import { Entity } from '@backstage/catalog-model'; | ||
| import { CHOREO_ANNOTATIONS } from '@openchoreo/backstage-plugin-common'; | ||
| import { PolicyQueryUser } from '@backstage/plugin-permission-node'; | ||
| import { OpenChoreoPermissionPolicy } from './OpenChoreoPermissionPolicy'; | ||
| import { matchesCatalogEntityCapability } from '../rules'; | ||
| import { AuthzProfileService } from '../services'; | ||
|
|
||
| function makeEntity( | ||
| kind: string, | ||
| annotations: Record<string, string | undefined> = {}, | ||
| ): Entity { | ||
| const cleanAnnotations: Record<string, string> = {}; | ||
| for (const [k, v] of Object.entries(annotations)) { | ||
| if (v !== undefined) { | ||
| cleanAnnotations[k] = v; | ||
| } | ||
| } | ||
| return { | ||
| apiVersion: 'backstage.io/v1alpha1', | ||
| kind, | ||
| metadata: { | ||
| name: 'test-entity', | ||
| annotations: cleanAnnotations, | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| describe('OpenChoreoPermissionPolicy - Catalog Permission Scoped Capabilities (Issue #763)', () => { | ||
| const mockLogger = mockServices.logger.mock(); | ||
|
|
||
| it('preserves scoped capability paths (e.g. with constraints) in kindCapabilities for System/Project entities', async () => { | ||
| const mockAuthzService = { | ||
| getCapabilitiesForUser: jest.fn().mockResolvedValue({ | ||
| capabilities: { | ||
| 'project:view': { | ||
| allowed: [ | ||
| { | ||
| path: 'ns/acme/project/project-a', | ||
| constraints: { | ||
| expressions: ['resource.environment == "prod"'], | ||
| }, | ||
| }, | ||
| ], | ||
| denied: [], | ||
| }, | ||
| }, | ||
| }), | ||
| } as unknown as AuthzProfileService; | ||
|
|
||
| const policy = new OpenChoreoPermissionPolicy({ | ||
| authzService: mockAuthzService, | ||
| logger: mockLogger, | ||
| }); | ||
|
|
||
| const user: PolicyQueryUser = { | ||
| info: { | ||
| userEntityRef: 'user:default/scoped-user', | ||
| }, | ||
| } as unknown as PolicyQueryUser; | ||
|
|
||
| const decision = await policy.handle( | ||
| { permission: catalogEntityReadPermission }, | ||
| user, | ||
| ); | ||
|
|
||
| expect(decision.result).toBe(AuthorizeResult.CONDITIONAL); | ||
| if (decision.result !== AuthorizeResult.CONDITIONAL) { | ||
| throw new Error('Expected CONDITIONAL decision'); | ||
| } | ||
|
|
||
| const ruleCondition = decision.conditions as any; | ||
| expect(ruleCondition).toBeDefined(); | ||
| expect(ruleCondition.rule).toBe('MATCHES_CATALOG_ENTITY_CAPABILITY'); | ||
|
|
||
| const kindCapabilities = JSON.parse( | ||
| ruleCondition.params.kindCapabilitiesJson, | ||
| ); | ||
|
|
||
| // Assert that kindCapabilities.system.allowedPaths contains the scoped project path | ||
| expect(kindCapabilities.system).toBeDefined(); | ||
| expect(kindCapabilities.system.allowedPaths).toEqual([ | ||
| 'ns/acme/project/project-a', | ||
| ]); | ||
|
|
||
| // Test apply against a matching System entity (OpenChoreo Project) | ||
| const matchingEntity = makeEntity('System', { | ||
| [CHOREO_ANNOTATIONS.NAMESPACE]: 'acme', | ||
| [CHOREO_ANNOTATIONS.PROJECT_ID]: 'project-a', | ||
| }); | ||
|
|
||
| const matchesMatching = matchesCatalogEntityCapability.apply( | ||
| matchingEntity, | ||
| ruleCondition.params, | ||
| ); | ||
| expect(matchesMatching).toBe(true); | ||
|
|
||
| // Test apply against a non-matching System entity (out-of-scope project) | ||
| const nonMatchingEntity = makeEntity('System', { | ||
| [CHOREO_ANNOTATIONS.NAMESPACE]: 'acme', | ||
| [CHOREO_ANNOTATIONS.PROJECT_ID]: 'project-b', | ||
| }); | ||
|
|
||
| const matchesNonMatching = matchesCatalogEntityCapability.apply( | ||
| nonMatchingEntity, | ||
| ruleCondition.params, | ||
| ); | ||
| expect(matchesNonMatching).toBe(false); | ||
| }); | ||
|
|
||
| it('allows plain unconstrained scoped capability paths', async () => { | ||
| const mockAuthzService = { | ||
| getCapabilitiesForUser: jest.fn().mockResolvedValue({ | ||
| capabilities: { | ||
| 'project:view': { | ||
| allowed: [ | ||
| { | ||
| path: 'ns/acme/project/project-a', | ||
| }, | ||
| ], | ||
| denied: [], | ||
| }, | ||
| }, | ||
| }), | ||
| } as unknown as AuthzProfileService; | ||
|
|
||
| const policy = new OpenChoreoPermissionPolicy({ | ||
| authzService: mockAuthzService, | ||
| logger: mockLogger, | ||
| }); | ||
|
|
||
| const user: PolicyQueryUser = { | ||
| info: { | ||
| userEntityRef: 'user:default/scoped-user', | ||
| }, | ||
| } as unknown as PolicyQueryUser; | ||
|
|
||
| const decision = await policy.handle( | ||
| { permission: catalogEntityReadPermission }, | ||
| user, | ||
| ); | ||
|
|
||
| expect(decision.result).toBe(AuthorizeResult.CONDITIONAL); | ||
| if (decision.result !== AuthorizeResult.CONDITIONAL) { | ||
| throw new Error('Expected CONDITIONAL decision'); | ||
| } | ||
|
|
||
| const ruleCondition = decision.conditions as any; | ||
| const kindCapabilities = JSON.parse( | ||
| ruleCondition.params.kindCapabilitiesJson, | ||
| ); | ||
|
|
||
| expect(kindCapabilities.system.allowedPaths).toEqual([ | ||
| 'ns/acme/project/project-a', | ||
| ]); | ||
|
|
||
| const matchingEntity = makeEntity('System', { | ||
| [CHOREO_ANNOTATIONS.NAMESPACE]: 'acme', | ||
| [CHOREO_ANNOTATIONS.PROJECT_ID]: 'project-a', | ||
| }); | ||
| expect( | ||
| matchesCatalogEntityCapability.apply( | ||
| matchingEntity, | ||
| ruleCondition.params, | ||
| ), | ||
| ).toBe(true); | ||
|
|
||
| const nonMatchingEntity = makeEntity('System', { | ||
| [CHOREO_ANNOTATIONS.NAMESPACE]: 'acme', | ||
| [CHOREO_ANNOTATIONS.PROJECT_ID]: 'project-b', | ||
| }); | ||
| expect( | ||
| matchesCatalogEntityCapability.apply( | ||
| nonMatchingEntity, | ||
| ruleCondition.params, | ||
| ), | ||
| ).toBe(false); | ||
| }); | ||
| }); |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Keep constrained denies out of the catalog path-only rule.
Line 311 discards each deny constraint.
matchesCatalogEntityCapabilityreceives onlydeniedPathsand denies matching paths before it checks allows. A deny such asns/acme/project/project-awith an environment CEL condition will therefore hide this project in the catalog even when the condition does not apply.Keep
unconstrainedPathsfor denied entries until this rule receives and evaluates the constraints. Unconstrained scoped deny paths remain supported.Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents