-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(rbac): prevent policy creation when rbac plugin is disabled
- Loading branch information
1 parent
7a28963
commit 67e48b9
Showing
4 changed files
with
108 additions
and
15 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
plugins/rbac-backend/src/policies/allow-all-policy.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,67 @@ | ||
import { | ||
AuthorizeResult, | ||
createPermission, | ||
} from '@backstage/plugin-permission-common'; | ||
import { | ||
PermissionPolicy, | ||
PolicyQuery, | ||
PolicyQueryUser, | ||
} from '@backstage/plugin-permission-node'; | ||
|
||
import { AllowAllPolicy } from './allow-all-policy'; | ||
|
||
describe('Allow All Policy', () => { | ||
describe('Allow all policy should allow all', () => { | ||
let policy: PermissionPolicy; | ||
beforeEach(() => { | ||
policy = new AllowAllPolicy(); | ||
}); | ||
|
||
it('should be able to create an allow all permission policy', () => { | ||
expect(policy).not.toBeNull(); | ||
}); | ||
|
||
it('should allow all when handle is called', async () => { | ||
const result = await policy.handle( | ||
newPolicyQueryWithBasicPermission('catalog.entity.create'), | ||
newPolicyQueryUser('user:default/guest'), | ||
); | ||
|
||
expect(result).toStrictEqual({ result: AuthorizeResult.ALLOW }); | ||
}); | ||
}); | ||
}); | ||
|
||
function newPolicyQueryWithBasicPermission(name: string): PolicyQuery { | ||
const mockPermission = createPermission({ | ||
name: name, | ||
attributes: {}, | ||
}); | ||
return { permission: mockPermission }; | ||
} | ||
|
||
function newPolicyQueryUser( | ||
user?: string, | ||
ownershipEntityRefs?: string[], | ||
): PolicyQueryUser | undefined { | ||
if (user) { | ||
return { | ||
identity: { | ||
ownershipEntityRefs: ownershipEntityRefs ?? [], | ||
type: 'user', | ||
userEntityRef: user, | ||
}, | ||
credentials: { | ||
$$type: '@backstage/BackstageCredentials', | ||
principal: true, | ||
expiresAt: new Date('2021-01-01T00:00:00Z'), | ||
}, | ||
info: { | ||
userEntityRef: user, | ||
ownershipEntityRefs: ownershipEntityRefs ?? [], | ||
}, | ||
token: 'token', | ||
}; | ||
} | ||
return undefined; | ||
} |
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,18 @@ | ||
import { | ||
AuthorizeResult, | ||
PolicyDecision, | ||
} from '@backstage/plugin-permission-common'; | ||
import { | ||
PermissionPolicy, | ||
PolicyQuery, | ||
PolicyQueryUser, | ||
} from '@backstage/plugin-permission-node'; | ||
|
||
export class AllowAllPolicy implements PermissionPolicy { | ||
async handle( | ||
_request: PolicyQuery, | ||
_user?: PolicyQueryUser, | ||
): Promise<PolicyDecision> { | ||
return { result: AuthorizeResult.ALLOW }; | ||
} | ||
} |
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