diff --git a/.changeset/bright-bulldogs-whisper.md b/.changeset/bright-bulldogs-whisper.md new file mode 100644 index 0000000000..bedc367856 --- /dev/null +++ b/.changeset/bright-bulldogs-whisper.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-linguist-backend': patch +--- + +Migrated to support new auth services. diff --git a/plugins/linguist-backend/api-report.md b/plugins/linguist-backend/api-report.md index 95bdc858ee..31892fcdb1 100644 --- a/plugins/linguist-backend/api-report.md +++ b/plugins/linguist-backend/api-report.md @@ -3,6 +3,7 @@ > Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/). ```ts +import { AuthService } from '@backstage/backend-plugin-api'; import { BackendFeature } from '@backstage/backend-plugin-api'; import { CatalogProcessor } from '@backstage/plugin-catalog-node'; import { CatalogProcessorCache } from '@backstage/plugin-catalog-node'; @@ -10,6 +11,7 @@ import { Config } from '@backstage/config'; import { DiscoveryService } from '@backstage/backend-plugin-api'; import { Entity } from '@backstage/catalog-model'; import express from 'express'; +import { HttpAuthService } from '@backstage/backend-plugin-api'; import { HumanDuration } from '@backstage/types'; import { Languages } from '@backstage/plugin-linguist-common'; import { LanguageType } from '@backstage/plugin-linguist-common'; @@ -94,6 +96,8 @@ export interface PluginOptions { // @public (undocumented) export interface RouterOptions { + // (undocumented) + auth?: AuthService; // (undocumented) config?: Config; // (undocumented) @@ -101,6 +105,8 @@ export interface RouterOptions { // (undocumented) discovery: PluginEndpointDiscovery; // (undocumented) + httpAuth?: HttpAuthService; + // (undocumented) linguistBackendApi?: LinguistBackendApi; // (undocumented) logger: Logger; diff --git a/plugins/linguist-backend/src/api/LinguistBackendClient.test.ts b/plugins/linguist-backend/src/api/LinguistBackendClient.test.ts index 55c74df424..be25fa9821 100644 --- a/plugins/linguist-backend/src/api/LinguistBackendClient.test.ts +++ b/plugins/linguist-backend/src/api/LinguistBackendClient.test.ts @@ -17,7 +17,6 @@ import { getVoidLogger, ReadTreeResponse, - ServerTokenManager, UrlReader, } from '@backstage/backend-common'; import { CatalogApi, GetEntitiesResponse } from '@backstage/catalog-client'; @@ -27,6 +26,7 @@ import { LinguistBackendStore } from '../db'; import { kindOrDefault, LinguistBackendClient } from './LinguistBackendClient'; import fs from 'fs-extra'; import { LINGUIST_ANNOTATION } from '@backstage/plugin-linguist-common'; +import { mockServices } from '@backstage/backend-test-utils'; const linguistResultMock = Promise.resolve({ files: { @@ -97,13 +97,11 @@ describe('Linguist backend API', () => { getEntityByRef: jest.fn(), } as any; - const tokenManager = ServerTokenManager.noop(); - const api = new LinguistBackendClient( logger, store, urlReader, - tokenManager, + mockServices.auth(), catalogApi, ); @@ -230,7 +228,7 @@ describe('Linguist backend API', () => { logger, store, urlReader, - tokenManager, + mockServices.auth(), catalogApi, { days: 5 }, ); @@ -353,7 +351,7 @@ describe('Linguist backend API', () => { logger, store, urlReader, - tokenManager, + mockServices.auth(), catalogApi, undefined, 2, diff --git a/plugins/linguist-backend/src/api/LinguistBackendClient.ts b/plugins/linguist-backend/src/api/LinguistBackendClient.ts index 1be4a7d288..d2c2a9124a 100644 --- a/plugins/linguist-backend/src/api/LinguistBackendClient.ts +++ b/plugins/linguist-backend/src/api/LinguistBackendClient.ts @@ -25,7 +25,7 @@ import { GetEntitiesRequest, CatalogApi, } from '@backstage/catalog-client'; -import { TokenManager, UrlReader } from '@backstage/backend-common'; +import { UrlReader } from '@backstage/backend-common'; import { DateTime } from 'luxon'; import { LINGUIST_ANNOTATION } from '@backstage/plugin-linguist-common'; @@ -40,6 +40,7 @@ import { import { assertError } from '@backstage/errors'; import { HumanDuration } from '@backstage/types'; import { Results } from 'linguist-js/dist/types'; +import { type AuthService } from '@backstage/backend-plugin-api'; /** @public */ export interface LinguistBackendApi { @@ -52,7 +53,7 @@ export class LinguistBackendClient implements LinguistBackendApi { private readonly logger: Logger; private readonly store: LinguistBackendStore; private readonly urlReader: UrlReader; - private readonly tokenManager: TokenManager; + private readonly auth: AuthService; private readonly catalogApi: CatalogApi; private readonly age?: HumanDuration; @@ -64,7 +65,7 @@ export class LinguistBackendClient implements LinguistBackendApi { logger: Logger, store: LinguistBackendStore, urlReader: UrlReader, - tokenManager: TokenManager, + auth: AuthService, catalogApi: CatalogApi, age?: HumanDuration, batchSize?: number, @@ -75,7 +76,7 @@ export class LinguistBackendClient implements LinguistBackendApi { this.logger = logger; this.store = store; this.urlReader = urlReader; - this.tokenManager = tokenManager; + this.auth = auth; this.catalogApi = catalogApi; this.batchSize = batchSize; this.age = age; @@ -114,7 +115,10 @@ export class LinguistBackendClient implements LinguistBackendApi { fields: ['kind', 'metadata'], }; - const { token } = await this.tokenManager.getToken(); + const { token } = await this.auth.getPluginRequestToken({ + onBehalfOf: await this.auth.getOwnServiceCredentials(), + targetPluginId: 'catalog', + }); const response = await this.catalogApi.getEntities(request, { token }); const entities = response.items; @@ -130,7 +134,10 @@ export class LinguistBackendClient implements LinguistBackendApi { const allEntities = await this.store.getAllEntities(); for (const entityRef of allEntities) { - const { token } = await this.tokenManager.getToken(); + const { token } = await this.auth.getPluginRequestToken({ + onBehalfOf: await this.auth.getOwnServiceCredentials(), + targetPluginId: 'catalog', + }); const result = await this.catalogApi.getEntityByRef(entityRef, { token }); if (!result) { @@ -155,7 +162,10 @@ export class LinguistBackendClient implements LinguistBackendApi { ); for (const entityRef of entities) { - const { token } = await this.tokenManager.getToken(); + const { token } = await this.auth.getPluginRequestToken({ + onBehalfOf: await this.auth.getOwnServiceCredentials(), + targetPluginId: 'catalog', + }); const entity = await this.catalogApi.getEntityByRef(entityRef, { token, }); diff --git a/plugins/linguist-backend/src/plugin.ts b/plugins/linguist-backend/src/plugin.ts index cbf18d9ebe..e569b00233 100644 --- a/plugins/linguist-backend/src/plugin.ts +++ b/plugins/linguist-backend/src/plugin.ts @@ -32,6 +32,8 @@ export const linguistPlugin = createBackendPlugin({ register(env) { env.registerInit({ deps: { + auth: coreServices.auth, + httpAuth: coreServices.httpAuth, logger: coreServices.logger, config: coreServices.rootConfig, reader: coreServices.urlReader, @@ -42,6 +44,8 @@ export const linguistPlugin = createBackendPlugin({ httpRouter: coreServices.httpRouter, }, async init({ + auth, + httpAuth, logger, config, reader, @@ -53,6 +57,8 @@ export const linguistPlugin = createBackendPlugin({ }) { httpRouter.use( await createRouterFromConfig({ + auth, + httpAuth, logger: loggerToWinstonLogger(logger), config, reader, diff --git a/plugins/linguist-backend/src/service/router.ts b/plugins/linguist-backend/src/service/router.ts index d29943c38b..7d64edc82e 100644 --- a/plugins/linguist-backend/src/service/router.ts +++ b/plugins/linguist-backend/src/service/router.ts @@ -15,6 +15,7 @@ */ import { + createLegacyAuthAdapters, errorHandler, PluginDatabaseManager, PluginEndpointDiscovery, @@ -35,6 +36,7 @@ import { HumanDuration } from '@backstage/types'; import { CatalogClient } from '@backstage/catalog-client'; import { LinguistBackendClient } from '../api/LinguistBackendClient'; import { Config } from '@backstage/config'; +import { AuthService, HttpAuthService } from '@backstage/backend-plugin-api'; /** @public */ export interface PluginOptions { @@ -56,6 +58,8 @@ export interface RouterOptions { discovery: PluginEndpointDiscovery; scheduler?: PluginTaskScheduler; config?: Config; + auth?: AuthService; + httpAuth?: HttpAuthService; } /** @public */ @@ -63,7 +67,7 @@ export async function createRouter( pluginOptions: PluginOptions, routerOptions: RouterOptions, ): Promise { - const { logger, reader, database, discovery, scheduler, tokenManager } = + const { logger, reader, database, discovery, scheduler, tokenManager, auth } = routerOptions; const { @@ -81,13 +85,19 @@ export async function createRouter( const catalogClient = new CatalogClient({ discoveryApi: discovery }); + const { auth: adaptedAuth } = createLegacyAuthAdapters({ + auth, + tokenManager: tokenManager, + discovery: discovery, + }); + const linguistBackendClient = routerOptions.linguistBackendApi || new LinguistBackendClient( logger, linguistBackendStore, reader, - tokenManager, + adaptedAuth, catalogClient, age, batchSize,