From 9f9ba708daef7337dc8fd902a1418681e6594a55 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Wed, 14 Feb 2024 12:37:54 +0100 Subject: [PATCH] lighthouse-backend: migrate to support new auth services MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Fredrik Adelöw Co-authored-by: Carl-Erik Bergström Co-authored-by: blam Signed-off-by: Patrik Oldsberg --- .changeset/chilled-goats-matter.md | 5 +++++ packages/backend/src/plugins/lighthouse.ts | 3 ++- plugins/lighthouse-backend/api-report.md | 6 ++++++ plugins/lighthouse-backend/src/plugin.ts | 14 +++++++++++++- .../src/service/EntitiesLoader.ts | 9 ++++++--- .../src/service/createScheduler.ts | 16 +++++++++++++--- 6 files changed, 45 insertions(+), 8 deletions(-) create mode 100644 .changeset/chilled-goats-matter.md diff --git a/.changeset/chilled-goats-matter.md b/.changeset/chilled-goats-matter.md new file mode 100644 index 0000000000..b66381452f --- /dev/null +++ b/.changeset/chilled-goats-matter.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-lighthouse-backend': patch +--- + +**BREAKING**: The `createScheduler` function now requires the `discovery` service to be forwarded from the plugin environment. This is part of the migration to support new auth services. diff --git a/packages/backend/src/plugins/lighthouse.ts b/packages/backend/src/plugins/lighthouse.ts index 00a67409e3..77204159cb 100644 --- a/packages/backend/src/plugins/lighthouse.ts +++ b/packages/backend/src/plugins/lighthouse.ts @@ -19,7 +19,7 @@ import { PluginEnvironment } from '../types'; import { CatalogClient } from '@backstage/catalog-client'; export default async function createPlugin(env: PluginEnvironment) { - const { logger, scheduler, config, tokenManager } = env; + const { logger, scheduler, config, tokenManager, discovery } = env; const catalogClient = new CatalogClient({ discoveryApi: env.discovery, @@ -31,5 +31,6 @@ export default async function createPlugin(env: PluginEnvironment) { config, catalogClient, tokenManager, + discovery, }); } diff --git a/plugins/lighthouse-backend/api-report.md b/plugins/lighthouse-backend/api-report.md index d382e40def..44379d6569 100644 --- a/plugins/lighthouse-backend/api-report.md +++ b/plugins/lighthouse-backend/api-report.md @@ -3,20 +3,26 @@ > 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 { CatalogApi } from '@backstage/catalog-client'; import { Config } from '@backstage/config'; +import { DiscoveryService } from '@backstage/backend-plugin-api'; import { Logger } from 'winston'; import { PluginTaskScheduler } from '@backstage/backend-tasks'; import { TokenManager } from '@backstage/backend-common'; // @public (undocumented) export interface CreateLighthouseSchedulerOptions { + // (undocumented) + auth?: AuthService; // (undocumented) catalogClient: CatalogApi; // (undocumented) config: Config; // (undocumented) + discovery: DiscoveryService; + // (undocumented) logger: Logger; // (undocumented) scheduler?: PluginTaskScheduler; diff --git a/plugins/lighthouse-backend/src/plugin.ts b/plugins/lighthouse-backend/src/plugin.ts index 3d5c8dac72..6a36e3f189 100644 --- a/plugins/lighthouse-backend/src/plugin.ts +++ b/plugins/lighthouse-backend/src/plugin.ts @@ -38,8 +38,18 @@ export const lighthousePlugin = createBackendPlugin({ logger: coreServices.logger, scheduler: coreServices.scheduler, tokenManager: coreServices.tokenManager, + discovery: coreServices.discovery, + auth: coreServices.auth, }, - async init({ catalogClient, config, logger, scheduler, tokenManager }) { + async init({ + catalogClient, + config, + logger, + scheduler, + tokenManager, + discovery, + auth, + }) { const winstonLogger = loggerToWinstonLogger(logger); await createScheduler({ @@ -48,6 +58,8 @@ export const lighthousePlugin = createBackendPlugin({ logger: winstonLogger, scheduler, tokenManager, + discovery, + auth, }); }, }); diff --git a/plugins/lighthouse-backend/src/service/EntitiesLoader.ts b/plugins/lighthouse-backend/src/service/EntitiesLoader.ts index c20b95beae..bee00cd4d9 100644 --- a/plugins/lighthouse-backend/src/service/EntitiesLoader.ts +++ b/plugins/lighthouse-backend/src/service/EntitiesLoader.ts @@ -18,11 +18,11 @@ import { CatalogClient, CATALOG_FILTER_EXISTS, } from '@backstage/catalog-client'; -import { TokenManager } from '@backstage/backend-common'; +import { AuthService } from '@backstage/backend-plugin-api'; export async function loadLighthouseEntities( catalogClient: CatalogClient, - tokenManager: TokenManager, + auth: AuthService, ) { const filter: Record = { kind: 'Component', @@ -30,7 +30,10 @@ export async function loadLighthouseEntities( ['lighthouse.com/website-url']: CATALOG_FILTER_EXISTS, }; - const { token } = await tokenManager.getToken(); + const { token } = await auth.getPluginRequestToken({ + onBehalfOf: await auth.getOwnServiceCredentials(), + targetPluginId: 'catalog', + }); return await catalogClient.getEntities( { diff --git a/plugins/lighthouse-backend/src/service/createScheduler.ts b/plugins/lighthouse-backend/src/service/createScheduler.ts index 45a3e88149..6f0ef497a5 100644 --- a/plugins/lighthouse-backend/src/service/createScheduler.ts +++ b/plugins/lighthouse-backend/src/service/createScheduler.ts @@ -21,22 +21,29 @@ import { Config } from '@backstage/config'; import { LighthouseRestApi } from '@backstage/plugin-lighthouse-common'; import { stringifyEntityRef } from '@backstage/catalog-model'; import { LighthouseAuditScheduleImpl } from '../config'; -import { TokenManager } from '@backstage/backend-common'; +import { + TokenManager, + createLegacyAuthAdapters, +} from '@backstage/backend-common'; +import { AuthService, DiscoveryService } from '@backstage/backend-plugin-api'; /** @public **/ export interface CreateLighthouseSchedulerOptions { logger: Logger; config: Config; + discovery: DiscoveryService; scheduler?: PluginTaskScheduler; catalogClient: CatalogApi; tokenManager: TokenManager; + auth?: AuthService; } /** @public **/ export async function createScheduler( options: CreateLighthouseSchedulerOptions, ) { - const { logger, scheduler, catalogClient, config, tokenManager } = options; + const { logger, scheduler, catalogClient, config } = options; + const { auth } = createLegacyAuthAdapters(options); const lighthouseApi = LighthouseRestApi.fromConfig(config); const lighthouseAuditConfig = LighthouseAuditScheduleImpl.fromConfig(config, { @@ -78,7 +85,10 @@ export async function createScheduler( logger.info('Running Lighthouse Audit Task'); - const { token } = await tokenManager.getToken(); + const { token } = await auth.getPluginRequestToken({ + onBehalfOf: await auth.getOwnServiceCredentials(), + targetPluginId: 'catalog', + }); const websitesWithUrl = await catalogClient.getEntities( { filter: [filter],