lighthouse-backend: migrate to support new auth services

Co-authored-by: Fredrik Adelöw <freben@gmail.com>
Co-authored-by: Carl-Erik Bergström <cbergstrom@spotify.com>
Co-authored-by: blam <ben@blam.sh>
Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2024-02-14 12:37:54 +01:00
parent 72572b2fe1
commit 9f9ba708da
6 changed files with 45 additions and 8 deletions
+5
View File
@@ -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.
+2 -1
View File
@@ -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,
});
}
+6
View File
@@ -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;
+13 -1
View File
@@ -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,
});
},
});
@@ -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<string, symbol | string> = {
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(
{
@@ -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],