Linguist-backend: Add token to catalogClient

The catalog client calls will fail for backstage deployments with
authentication.

Signed-off-by: Niklas Aronsson <niklasar@axis.com>
This commit is contained in:
Niklas Aronsson
2023-02-17 09:54:24 +01:00
parent b25b4bda4e
commit 4a1c318853
6 changed files with 38 additions and 5 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-linguist-backend': minor
---
**BREAKING** The linguist-backend `createRouter` now requires that the `tokenManger` is passed to the router.
+4
View File
@@ -14,6 +14,7 @@ import { PluginEndpointDiscovery } from '@backstage/backend-common';
import { PluginTaskScheduler } from '@backstage/backend-tasks';
import { ProcessedEntity } from '@backstage/plugin-linguist-common';
import { TaskScheduleDefinition } from '@backstage/backend-tasks';
import { TokenManager } from '@backstage/backend-common';
import { UrlReader } from '@backstage/backend-common';
// @public (undocumented)
@@ -29,6 +30,7 @@ export class LinguistBackendApi {
store: LinguistBackendStore,
urlReader: UrlReader,
discovery: PluginEndpointDiscovery,
tokenManager: TokenManager,
age?: HumanDuration,
batchSize?: number,
useSourceLocation?: boolean,
@@ -96,6 +98,8 @@ export interface RouterOptions {
reader: UrlReader;
// (undocumented)
scheduler?: PluginTaskScheduler;
// (undocumented)
tokenManager: TokenManager;
}
// (No @packageDocumentation comment for this package)
@@ -25,7 +25,11 @@ import {
CatalogClient,
GetEntitiesRequest,
} from '@backstage/catalog-client';
import { PluginEndpointDiscovery, UrlReader } from '@backstage/backend-common';
import {
PluginEndpointDiscovery,
TokenManager,
UrlReader,
} from '@backstage/backend-common';
import { DateTime } from 'luxon';
import { LINGUIST_ANNOTATION } from '@backstage/plugin-linguist-common';
@@ -46,6 +50,8 @@ export class LinguistBackendApi {
private readonly store: LinguistBackendStore;
private readonly urlReader: UrlReader;
private readonly discovery: PluginEndpointDiscovery;
private readonly tokenManager: TokenManager;
private readonly catalogClient: CatalogClient;
private readonly age?: HumanDuration;
private readonly batchSize?: number;
@@ -55,6 +61,7 @@ export class LinguistBackendApi {
store: LinguistBackendStore,
urlReader: UrlReader,
discovery: PluginEndpointDiscovery,
tokenManager: TokenManager,
age?: HumanDuration,
batchSize?: number,
useSourceLocation?: boolean,
@@ -63,8 +70,8 @@ export class LinguistBackendApi {
this.store = store;
this.urlReader = urlReader;
this.discovery = discovery;
this.tokenManager = tokenManager;
this.catalogClient = new CatalogClient({ discoveryApi: this.discovery });
this.batchSize = batchSize;
this.age = age;
this.useSourceLocation = useSourceLocation;
@@ -98,7 +105,8 @@ export class LinguistBackendApi {
fields: ['kind', 'metadata'],
};
const response = await this.catalogClient.getEntities(request);
const { token } = await this.tokenManager.getToken();
const response = await this.catalogClient.getEntities(request, { token });
const entities = response.items;
entities.forEach(entity => {
@@ -118,7 +126,10 @@ export class LinguistBackendApi {
this.batchSize ?? 20,
);
entities.forEach(async entityRef => {
const entity = await this.catalogClient.getEntityByRef(entityRef);
const { token } = await this.tokenManager.getToken();
const entity = await this.catalogClient.getEntityByRef(entityRef, {
token,
});
const annotationKey = this.useSourceLocation
? ANNOTATION_SOURCE_LOCATION
: LINGUIST_ANNOTATION;
@@ -19,6 +19,7 @@ import {
getVoidLogger,
PluginDatabaseManager,
PluginEndpointDiscovery,
TokenManager,
UrlReaders,
} from '@backstage/backend-common';
import express from 'express';
@@ -48,6 +49,11 @@ const testDiscovery: jest.Mocked<PluginEndpointDiscovery> = {
getExternalBaseUrl: jest.fn(),
};
const mockedTokenManager: jest.Mocked<TokenManager> = {
getToken: jest.fn(),
authenticate: jest.fn(),
};
const mockUrlReader = UrlReaders.default({
logger: getVoidLogger(),
config: new ConfigReader({}),
@@ -72,6 +78,7 @@ describe('createRouter', () => {
database: createDatabase(),
reader: mockUrlReader,
logger: getVoidLogger(),
tokenManager: mockedTokenManager,
},
);
app = express().use(router);
@@ -18,6 +18,7 @@ import {
errorHandler,
PluginDatabaseManager,
PluginEndpointDiscovery,
TokenManager,
UrlReader,
} from '@backstage/backend-common';
import express from 'express';
@@ -44,6 +45,7 @@ export interface RouterOptions {
linguistBackendApi?: LinguistBackendApi;
logger: Logger;
reader: UrlReader;
tokenManager: TokenManager;
database: PluginDatabaseManager;
discovery: PluginEndpointDiscovery;
scheduler?: PluginTaskScheduler;
@@ -56,7 +58,8 @@ export async function createRouter(
): Promise<express.Router> {
const { schedule, age, batchSize, useSourceLocation } = pluginOptions;
const { logger, reader, database, discovery, scheduler } = routerOptions;
const { logger, reader, database, discovery, scheduler, tokenManager } =
routerOptions;
const linguistBackendStore = await LinguistBackendDatabase.create(
await database.getClient(),
@@ -69,6 +72,7 @@ export async function createRouter(
linguistBackendStore,
reader,
discovery,
tokenManager,
age,
batchSize,
useSourceLocation,
@@ -20,6 +20,7 @@ import {
SingleHostDiscovery,
UrlReaders,
useHotMemoize,
ServerTokenManager,
} from '@backstage/backend-common';
import { Server } from 'http';
import { Logger } from 'winston';
@@ -66,6 +67,7 @@ export async function startStandaloneServer(
discovery: SingleHostDiscovery.fromConfig(config),
reader: UrlReaders.default({ logger, config }),
logger,
tokenManager: ServerTokenManager.noop(),
},
);