Skip to content

Commit

Permalink
fix(rbac): pass token to readUrl for well-known permission endpoint (j…
Browse files Browse the repository at this point in the history
  • Loading branch information
PatAKnight authored Mar 18, 2024
1 parent 1c86a96 commit 36b7c77
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 24 deletions.
19 changes: 11 additions & 8 deletions plugins/rbac-backend/src/service/plugin-endpoint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ jest.mock('@backstage/backend-common', () => {
});

describe('plugin-endpoint', () => {
const fakeToken = 'fakeToken';
const mockPluginEndpointDiscovery = {
getBaseUrl: jest.fn().mockImplementation(async (pluginId: string) => {
return `https://localhost:7007/api/${pluginId}`;
Expand Down Expand Up @@ -63,7 +64,7 @@ describe('plugin-endpoint', () => {
logger,
config,
);
const policiesMetadata = await collector.getPluginPolicies();
const policiesMetadata = await collector.getPluginPolicies(fakeToken);

expect(policiesMetadata.length).toEqual(0);
});
Expand All @@ -82,7 +83,7 @@ describe('plugin-endpoint', () => {
logger,
config,
);
const policiesMetadata = await collector.getPluginPolicies();
const policiesMetadata = await collector.getPluginPolicies(fakeToken);

expect(policiesMetadata.length).toEqual(1);
expect(policiesMetadata[0].pluginId).toEqual('permission');
Expand Down Expand Up @@ -112,7 +113,7 @@ describe('plugin-endpoint', () => {
logger,
config,
);
const policiesMetadata = await collector.getPluginPolicies();
const policiesMetadata = await collector.getPluginPolicies(fakeToken);

expect(policiesMetadata.length).toEqual(1);
expect(policiesMetadata[0].pluginId).toEqual('permission');
Expand Down Expand Up @@ -151,7 +152,7 @@ describe('plugin-endpoint', () => {
logger,
config,
);
const policiesMetadata = await collector.getPluginPolicies();
const policiesMetadata = await collector.getPluginPolicies(fakeToken);

expect(policiesMetadata.length).toEqual(1);
expect(policiesMetadata[0].pluginId).toEqual('permission');
Expand Down Expand Up @@ -192,7 +193,7 @@ describe('plugin-endpoint', () => {
config,
);

const policiesMetadata = await collector.getPluginPolicies();
const policiesMetadata = await collector.getPluginPolicies(fakeToken);

expect(policiesMetadata.length).toEqual(1);
expect(policiesMetadata[0].pluginId).toEqual('permission');
Expand Down Expand Up @@ -233,7 +234,7 @@ describe('plugin-endpoint', () => {
logger,
config,
);
const policiesMetadata = await collector.getPluginPolicies();
const policiesMetadata = await collector.getPluginPolicies(fakeToken);

expect(policiesMetadata.length).toEqual(1);
expect(policiesMetadata[0].pluginId).toEqual('permission');
Expand All @@ -259,7 +260,8 @@ describe('plugin-endpoint', () => {
logger,
config,
);
const conditionRulesMetadata = await collector.getPluginConditionRules();
const conditionRulesMetadata =
await collector.getPluginConditionRules(fakeToken);

expect(conditionRulesMetadata.length).toEqual(0);
});
Expand All @@ -278,7 +280,8 @@ describe('plugin-endpoint', () => {
logger,
config,
);
const conditionRulesMetadata = await collector.getPluginConditionRules();
const conditionRulesMetadata =
await collector.getPluginConditionRules(fakeToken);

expect(conditionRulesMetadata.length).toEqual(1);
expect(conditionRulesMetadata[0].pluginId).toEqual('catalog');
Expand Down
20 changes: 12 additions & 8 deletions plugins/rbac-backend/src/service/plugin-endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ export class PluginPermissionMetadataCollector {
});
}

async getPluginConditionRules(): Promise<
PluginMetadataResponseSerializedRule[]
> {
const pluginMetadata = await this.getPluginMetaData();
async getPluginConditionRules(
token: string | undefined,
): Promise<PluginMetadataResponseSerializedRule[]> {
const pluginMetadata = await this.getPluginMetaData(token);

return pluginMetadata
.filter(metadata => metadata.metaDataResponse.rules.length > 0)
Expand All @@ -69,8 +69,10 @@ export class PluginPermissionMetadataCollector {
});
}

async getPluginPolicies(): Promise<PluginPermissionMetaData[]> {
const pluginMetadata = await this.getPluginMetaData();
async getPluginPolicies(
token: string | undefined,
): Promise<PluginPermissionMetaData[]> {
const pluginMetadata = await this.getPluginMetaData(token);

return pluginMetadata
.filter(metadata => metadata.metaDataResponse.permissions !== undefined)
Expand All @@ -88,14 +90,16 @@ export class PluginPermissionMetadataCollector {
return [{ reader: new FetchUrlReader(), predicate: (_url: URL) => true }];
};

private async getPluginMetaData(): Promise<PluginMetadataResponse[]> {
private async getPluginMetaData(
token: string | undefined,
): Promise<PluginMetadataResponse[]> {
let pluginResponses: PluginMetadataResponse[] = [];

for (const pluginId of this.pluginIds) {
const baseEndpoint = await this.discovery.getBaseUrl(pluginId);
const wellKnownURL = `${baseEndpoint}/.well-known/backstage/permissions/metadata`;
try {
const permResp = await this.urlReader.readUrl(wellKnownURL);
const permResp = await this.urlReader.readUrl(wellKnownURL, { token });
const permMetaDataRaw = (await permResp.buffer()).toString();
let permMetaData;
try {
Expand Down
22 changes: 14 additions & 8 deletions plugins/rbac-backend/src/service/policies-rest-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -551,30 +551,36 @@ export class PolicesServer {
},
);

router.get('/plugins/policies', async (req, resp) => {
const decision = await this.authorize(req, {
router.get('/plugins/policies', async (request, response) => {
const decision = await this.authorize(request, {
permission: policyEntityReadPermission,
});

if (decision.result === AuthorizeResult.DENY) {
throw new NotAllowedError(); // 403
}

const policies = await pluginPermMetaData.getPluginPolicies();
resp.json(policies);
const authHeader = request.header('authorization');
const token = getBearerTokenFromAuthorizationHeader(authHeader);

const policies = await pluginPermMetaData.getPluginPolicies(token);
response.json(policies);
});

router.get('/plugins/condition-rules', async (req, resp) => {
const decision = await this.authorize(req, {
router.get('/plugins/condition-rules', async (request, response) => {
const decision = await this.authorize(request, {
permission: policyEntityReadPermission,
});

if (decision.result === AuthorizeResult.DENY) {
throw new NotAllowedError(); // 403
}

const rules = await pluginPermMetaData.getPluginConditionRules();
resp.json(rules);
const authHeader = request.header('authorization');
const token = getBearerTokenFromAuthorizationHeader(authHeader);

const rules = await pluginPermMetaData.getPluginConditionRules(token);
response.json(rules);
});

router.get('/conditions', async (req, resp) => {
Expand Down

0 comments on commit 36b7c77

Please sign in to comment.