From a86f5c17016632908296e6a8d7498350b3705c01 Mon Sep 17 00:00:00 2001 From: Erik Larsson Date: Wed, 22 Dec 2021 22:48:07 +0100 Subject: [PATCH 1/3] fixes api auth bug in tech-insights plugin Signed-off-by: Erik Larsson --- .changeset/fluffy-toys-tease.md | 5 +++++ .../tech-insights/src/api/TechInsightsClient.ts | 16 ++++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 .changeset/fluffy-toys-tease.md diff --git a/.changeset/fluffy-toys-tease.md b/.changeset/fluffy-toys-tease.md new file mode 100644 index 0000000000..f20eaebb2c --- /dev/null +++ b/.changeset/fluffy-toys-tease.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-tech-insights': minor +--- + +fixes api auth in tech-insights plugin diff --git a/plugins/tech-insights/src/api/TechInsightsClient.ts b/plugins/tech-insights/src/api/TechInsightsClient.ts index 25002e30a1..43979e60b0 100644 --- a/plugins/tech-insights/src/api/TechInsightsClient.ts +++ b/plugins/tech-insights/src/api/TechInsightsClient.ts @@ -17,7 +17,7 @@ import { TechInsightsApi } from './TechInsightsApi'; import { CheckResult } from '@backstage/plugin-tech-insights-common'; import { Check } from './types'; -import { DiscoveryApi } from '@backstage/core-plugin-api'; +import { DiscoveryApi, IdentityApi } from '@backstage/core-plugin-api'; import { ResponseError } from '@backstage/errors'; import { EntityName } from '@backstage/catalog-model'; @@ -28,13 +28,16 @@ import { export type Options = { discoveryApi: DiscoveryApi; + identityApi: IdentityApi; }; export class TechInsightsClient implements TechInsightsApi { private readonly discoveryApi: DiscoveryApi; + private readonly identityApi: IdentityApi; constructor(options: Options) { this.discoveryApi = options.discoveryApi; + this.identityApi = options.identityApi; } getScorecardsDefinition( @@ -47,7 +50,14 @@ export class TechInsightsClient implements TechInsightsApi { async getAllChecks(): Promise { const url = await this.discoveryApi.getBaseUrl('tech-insights'); - const response = await fetch(`${url}/checks`); + const token = await this.identityApi.getIdToken(); + const response = await fetch(`${url}/checks`, { + headers: token + ? { + Authorization: `Bearer ${token}`, + } + : undefined, + }); if (!response.ok) { throw await ResponseError.fromResponse(response); } @@ -59,6 +69,7 @@ export class TechInsightsClient implements TechInsightsApi { checks: Check[], ): Promise { const url = await this.discoveryApi.getBaseUrl('tech-insights'); + const token = await this.identityApi.getIdToken(); const { namespace, kind, name } = entityParams; const allChecks = checks ? checks : await this.getAllChecks(); const checkIds = allChecks.map((check: Check) => check.id); @@ -71,6 +82,7 @@ export class TechInsightsClient implements TechInsightsApi { body: JSON.stringify({ checks: checkIds }), headers: { 'Content-Type': 'application/json', + ...(token && { Authorization: `Bearer ${token}` }), }, }, ); From da5751717e6daf9d0c9443850cbb647b49a55119 Mon Sep 17 00:00:00 2001 From: Erik Larsson Date: Wed, 22 Dec 2021 23:05:09 +0100 Subject: [PATCH 2/3] tsc Signed-off-by: Erik Larsson --- plugins/tech-insights/src/plugin.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/plugins/tech-insights/src/plugin.ts b/plugins/tech-insights/src/plugin.ts index f229b654f3..b4382d8361 100644 --- a/plugins/tech-insights/src/plugin.ts +++ b/plugins/tech-insights/src/plugin.ts @@ -31,8 +31,9 @@ export const techInsightsPlugin = createPlugin({ apis: [ createApiFactory({ api: techInsightsApiRef, - deps: { discoveryApi: discoveryApiRef }, - factory: ({ discoveryApi }) => new TechInsightsClient({ discoveryApi }), + deps: { discoveryApi: discoveryApiRef, identityApi: identityApiRef }, + factory: ({ discoveryApi, identityApi }) => + new TechInsightsClient({ discoveryApi, identityApi }), }), ], routes: { From 57a54eaed4ca87cfe7447ef9a07548290e9cbaca Mon Sep 17 00:00:00 2001 From: Erik Larsson Date: Wed, 22 Dec 2021 23:09:34 +0100 Subject: [PATCH 3/3] tsc Signed-off-by: Erik Larsson --- plugins/tech-insights/src/plugin.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/plugins/tech-insights/src/plugin.ts b/plugins/tech-insights/src/plugin.ts index b4382d8361..0c2da63670 100644 --- a/plugins/tech-insights/src/plugin.ts +++ b/plugins/tech-insights/src/plugin.ts @@ -18,6 +18,7 @@ import { createRoutableExtension, createApiFactory, discoveryApiRef, + identityApiRef, } from '@backstage/core-plugin-api'; import { rootRouteRef } from './routes'; import { techInsightsApiRef } from './api/TechInsightsApi';