Merge pull request #8151 from backstage/permission-client-pass-config
permission-common: accept configApi in PermissionClient constructor
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
---
|
||||
'@backstage/plugin-permission-common': minor
|
||||
---
|
||||
|
||||
Accept configApi rather than enabled flag in PermissionClient constructor.
|
||||
@@ -3,6 +3,8 @@
|
||||
> Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/).
|
||||
|
||||
```ts
|
||||
import { Config } from '@backstage/config';
|
||||
|
||||
// @public
|
||||
export type AuthorizeRequest = {
|
||||
permission: Permission;
|
||||
@@ -67,7 +69,7 @@ export type PermissionAttributes = {
|
||||
|
||||
// @public
|
||||
export class PermissionClient {
|
||||
constructor(options: { discoveryApi: DiscoveryApi; enabled?: boolean });
|
||||
constructor(options: { discoveryApi: DiscoveryApi; configApi: Config });
|
||||
authorize(
|
||||
requests: AuthorizeRequest[],
|
||||
options?: AuthorizeRequestOptions,
|
||||
|
||||
@@ -38,6 +38,7 @@
|
||||
"url": "https://github.com/backstage/backstage/issues"
|
||||
},
|
||||
"dependencies": {
|
||||
"@backstage/config": "^0.1.11",
|
||||
"@backstage/errors": "^0.1.2",
|
||||
"cross-fetch": "^3.0.6",
|
||||
"uuid": "^8.0.0",
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
import { RestContext, rest } from 'msw';
|
||||
import { setupServer } from 'msw/node';
|
||||
import { ConfigReader } from '@backstage/config';
|
||||
import { PermissionClient } from './PermissionClient';
|
||||
import { AuthorizeRequest, AuthorizeResult, Identified } from './types/api';
|
||||
import { DiscoveryApi } from './types/discovery';
|
||||
@@ -32,7 +33,7 @@ const discoveryApi: DiscoveryApi = {
|
||||
};
|
||||
const client: PermissionClient = new PermissionClient({
|
||||
discoveryApi,
|
||||
enabled: true,
|
||||
configApi: new ConfigReader({ permission: { enabled: true } }),
|
||||
});
|
||||
|
||||
const mockPermission: Permission = {
|
||||
@@ -145,7 +146,7 @@ describe('PermissionClient', () => {
|
||||
).rejects.toThrowError(/invalid input/i);
|
||||
});
|
||||
|
||||
it('should allow all when authorization is not enabled', async () => {
|
||||
it('should allow all when permission.enabled is false', async () => {
|
||||
mockAuthorizeHandler.mockImplementationOnce(
|
||||
(req, res, { json }: RestContext) => {
|
||||
const responses = req.body.map((a: Identified<AuthorizeRequest>) => ({
|
||||
@@ -156,7 +157,32 @@ describe('PermissionClient', () => {
|
||||
return res(json(responses));
|
||||
},
|
||||
);
|
||||
const disabled = new PermissionClient({ discoveryApi, enabled: false });
|
||||
const disabled = new PermissionClient({
|
||||
discoveryApi,
|
||||
configApi: new ConfigReader({ permission: { enabled: false } }),
|
||||
});
|
||||
const response = await disabled.authorize([mockAuthorizeRequest]);
|
||||
expect(response[0]).toEqual(
|
||||
expect.objectContaining({ result: AuthorizeResult.ALLOW }),
|
||||
);
|
||||
expect(mockAuthorizeHandler).not.toBeCalled();
|
||||
});
|
||||
|
||||
it('should allow all when permission.enabled is not configured', async () => {
|
||||
mockAuthorizeHandler.mockImplementationOnce(
|
||||
(req, res, { json }: RestContext) => {
|
||||
const responses = req.body.map((a: Identified<AuthorizeRequest>) => ({
|
||||
id: a.id,
|
||||
outcome: AuthorizeResult.DENY,
|
||||
}));
|
||||
|
||||
return res(json(responses));
|
||||
},
|
||||
);
|
||||
const disabled = new PermissionClient({
|
||||
discoveryApi,
|
||||
configApi: new ConfigReader({}),
|
||||
});
|
||||
const response = await disabled.authorize([mockAuthorizeRequest]);
|
||||
expect(response[0]).toEqual(
|
||||
expect.objectContaining({ result: AuthorizeResult.ALLOW }),
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { Config } from '@backstage/config';
|
||||
import { ResponseError } from '@backstage/errors';
|
||||
import fetch from 'cross-fetch';
|
||||
import * as uuid from 'uuid';
|
||||
@@ -74,9 +75,10 @@ export class PermissionClient {
|
||||
private readonly enabled: boolean;
|
||||
private readonly discoveryApi: DiscoveryApi;
|
||||
|
||||
constructor(options: { discoveryApi: DiscoveryApi; enabled?: boolean }) {
|
||||
constructor(options: { discoveryApi: DiscoveryApi; configApi: Config }) {
|
||||
this.discoveryApi = options.discoveryApi;
|
||||
this.enabled = options.enabled ?? false;
|
||||
this.enabled =
|
||||
options.configApi.getOptionalBoolean('permission.enabled') ?? false;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user