linguist-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-15 18:43:38 +01:00
parent 72572b2fe1
commit 61ff58f1ef
6 changed files with 50 additions and 15 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-linguist-backend': patch
---
Migrated to support new auth services.
+6
View File
@@ -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;
@@ -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,
@@ -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,
});
+6
View File
@@ -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,
+12 -2
View File
@@ -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<express.Router> {
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,