fix: sentry plugin can pass id token
Signed-off-by: rodion <rodiongork@github.com>
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
---
|
||||
'@backstage/plugin-sentry': patch
|
||||
---
|
||||
|
||||
fix: sentry-plugin can forward identity token to backend (for case when it requires authorization)
|
||||
@@ -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,
|
||||
|
||||
@@ -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 {};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
),
|
||||
}),
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user