From 33d7bd0baaac3fa2d3b0b34d27b270f109953578 Mon Sep 17 00:00:00 2001 From: rodion Date: Tue, 26 Oct 2021 21:57:04 +0300 Subject: [PATCH] fix: sentry plugin can pass id token Signed-off-by: rodion --- .changeset/nasty-actors-push.md | 5 +++++ plugins/sentry/api-report.md | 18 +++++++++++++++++- plugins/sentry/src/api/production-api.ts | 21 ++++++++++++++++++++- plugins/sentry/src/plugin.ts | 10 ++++++++-- 4 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 .changeset/nasty-actors-push.md diff --git a/.changeset/nasty-actors-push.md b/.changeset/nasty-actors-push.md new file mode 100644 index 0000000000..8f4d0d92a8 --- /dev/null +++ b/.changeset/nasty-actors-push.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-sentry': patch +--- + +fix: sentry-plugin can forward identity token to backend (for case when it requires authorization) diff --git a/plugins/sentry/api-report.md b/plugins/sentry/api-report.md index a23fa112c1..1b7a248e48 100644 --- a/plugins/sentry/api-report.md +++ b/plugins/sentry/api-report.md @@ -9,6 +9,7 @@ import { ApiRef } from '@backstage/core-plugin-api'; import { BackstagePlugin } from '@backstage/core-plugin-api'; import { DiscoveryApi } from '@backstage/core-plugin-api'; import { Entity } from '@backstage/catalog-model'; +import { IdentityApi } from '@backstage/core-plugin-api'; import { InfoCardVariants } from '@backstage/core-components'; import { RouteRef } from '@backstage/core-plugin-api'; @@ -34,7 +35,22 @@ export class MockSentryApi implements SentryApi { // // @public (undocumented) export class ProductionSentryApi implements SentryApi { - constructor(discoveryApi: DiscoveryApi, organization: string); + constructor( + discoveryApi: DiscoveryApi, + organization: string, + identityApi?: IdentityApi | undefined, + ); + // (undocumented) + authOptions(): Promise< + | { + headers?: undefined; + } + | { + headers: { + authorization: string; + }; + } + >; // (undocumented) fetchIssues( project: string, diff --git a/plugins/sentry/src/api/production-api.ts b/plugins/sentry/src/api/production-api.ts index 5968d20011..0632da711e 100644 --- a/plugins/sentry/src/api/production-api.ts +++ b/plugins/sentry/src/api/production-api.ts @@ -16,12 +16,13 @@ import { SentryIssue } from './sentry-issue'; import { SentryApi } from './sentry-api'; -import { DiscoveryApi } from '@backstage/core-plugin-api'; +import { DiscoveryApi, IdentityApi } from '@backstage/core-plugin-api'; export class ProductionSentryApi implements SentryApi { constructor( private readonly discoveryApi: DiscoveryApi, private readonly organization: string, + private readonly identityApi?: IdentityApi, ) {} async fetchIssues( @@ -34,11 +35,13 @@ export class ProductionSentryApi implements SentryApi { } const apiUrl = `${await this.discoveryApi.getBaseUrl('proxy')}/sentry/api`; + const options = await this.authOptions(); const queryPart = query ? `&query=${query}` : ''; const response = await fetch( `${apiUrl}/0/projects/${this.organization}/${project}/issues/?statsPeriod=${statsFor}${queryPart}`, + options, ); if (response.status >= 400 && response.status < 600) { @@ -47,4 +50,20 @@ export class ProductionSentryApi implements SentryApi { return (await response.json()) as SentryIssue[]; } + + async authOptions() { + if (!this.identityApi) { + return {}; + } + try { + const token = await this.identityApi.getIdToken(); + return { + headers: { + authorization: `Bearer ${token}`, + }, + }; + } catch (e) { + return {}; + } + } } diff --git a/plugins/sentry/src/plugin.ts b/plugins/sentry/src/plugin.ts index 75de6ba6dc..454c1f02a6 100644 --- a/plugins/sentry/src/plugin.ts +++ b/plugins/sentry/src/plugin.ts @@ -21,6 +21,7 @@ import { createPlugin, createRouteRef, discoveryApiRef, + identityApiRef, } from '@backstage/core-plugin-api'; export const rootRouteRef = createRouteRef({ @@ -33,11 +34,16 @@ export const sentryPlugin = createPlugin({ apis: [ createApiFactory({ api: sentryApiRef, - deps: { configApi: configApiRef, discoveryApi: discoveryApiRef }, - factory: ({ configApi, discoveryApi }) => + deps: { + configApi: configApiRef, + discoveryApi: discoveryApiRef, + identityApi: identityApiRef, + }, + factory: ({ configApi, discoveryApi, identityApi }) => new ProductionSentryApi( discoveryApi, configApi.getString('sentry.organization'), + identityApi, ), }), ],