From edfcd7e04b84896f7f02b6ff58e35442971d0ba4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Adel=C3=B6w?= Date: Tue, 15 Apr 2025 22:39:03 +0200 Subject: [PATCH] removed backend-common MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fredrik Adelöw --- plugins/auth-backend/package.json | 1 - plugins/auth-backend/src/authPlugin.ts | 3 - .../auth-backend/src/database/AuthDatabase.ts | 17 --- .../src/identity/KeyStores.test.ts | 126 ++++++++++-------- .../lib/catalog/CatalogIdentityClient.test.ts | 31 ++--- .../src/lib/catalog/CatalogIdentityClient.ts | 32 +---- .../CatalogAuthResolverContext.test.ts | 3 - .../resolvers/CatalogAuthResolverContext.ts | 14 +- plugins/auth-backend/src/providers/index.ts | 2 +- plugins/auth-backend/src/providers/router.ts | 13 -- plugins/auth-backend/src/service/router.ts | 16 +-- yarn.lock | 1 - 12 files changed, 95 insertions(+), 164 deletions(-) diff --git a/plugins/auth-backend/package.json b/plugins/auth-backend/package.json index 3040d03516..656a19cf89 100644 --- a/plugins/auth-backend/package.json +++ b/plugins/auth-backend/package.json @@ -43,7 +43,6 @@ "test": "backstage-cli package test" }, "dependencies": { - "@backstage/backend-common": "^0.25.0", "@backstage/backend-plugin-api": "workspace:^", "@backstage/catalog-client": "workspace:^", "@backstage/catalog-model": "workspace:^", diff --git a/plugins/auth-backend/src/authPlugin.ts b/plugins/auth-backend/src/authPlugin.ts index bc02243446..8e55fccdea 100644 --- a/plugins/auth-backend/src/authPlugin.ts +++ b/plugins/auth-backend/src/authPlugin.ts @@ -66,7 +66,6 @@ export const authPlugin = createBackendPlugin({ database: coreServices.database, discovery: coreServices.discovery, auth: coreServices.auth, - httpAuth: coreServices.httpAuth, catalogApi: catalogServiceRef, }, async init({ @@ -76,7 +75,6 @@ export const authPlugin = createBackendPlugin({ database, discovery, auth, - httpAuth, catalogApi, }) { const router = await createRouter({ @@ -85,7 +83,6 @@ export const authPlugin = createBackendPlugin({ database, discovery, auth, - httpAuth, catalogApi, providerFactories: Object.fromEntries(providers), ownershipResolver, diff --git a/plugins/auth-backend/src/database/AuthDatabase.ts b/plugins/auth-backend/src/database/AuthDatabase.ts index 43b48401e2..a09f28c1cd 100644 --- a/plugins/auth-backend/src/database/AuthDatabase.ts +++ b/plugins/auth-backend/src/database/AuthDatabase.ts @@ -14,12 +14,10 @@ * limitations under the License. */ -import { DatabaseManager } from '@backstage/backend-common'; import { DatabaseService, resolvePackagePath, } from '@backstage/backend-plugin-api'; -import { ConfigReader } from '@backstage/config'; import { Knex } from 'knex'; const migrationsDir = resolvePackagePath( @@ -39,21 +37,6 @@ export class AuthDatabase { return new AuthDatabase(database); } - /** @internal */ - static forTesting(): AuthDatabase { - const config = new ConfigReader({ - backend: { - database: { - client: 'better-sqlite3', - connection: ':memory:', - useNullAsDefault: true, - }, - }, - }); - const database = DatabaseManager.fromConfig(config).forPlugin('auth'); - return new AuthDatabase(database); - } - static async runMigrations(knex: Knex): Promise { await knex.migrate.latest({ directory: migrationsDir, diff --git a/plugins/auth-backend/src/identity/KeyStores.test.ts b/plugins/auth-backend/src/identity/KeyStores.test.ts index 0d4ffac4d8..13818ff79c 100644 --- a/plugins/auth-backend/src/identity/KeyStores.test.ts +++ b/plugins/auth-backend/src/identity/KeyStores.test.ts @@ -20,9 +20,13 @@ import { DatabaseKeyStore } from './DatabaseKeyStore'; import { FirestoreKeyStore } from './FirestoreKeyStore'; import { KeyStores } from './KeyStores'; import { MemoryKeyStore } from './MemoryKeyStore'; -import { mockServices } from '@backstage/backend-test-utils'; +import { mockServices, TestDatabases } from '@backstage/backend-test-utils'; + +jest.setTimeout(60_000); describe('KeyStores', () => { + const databases = TestDatabases.create(); + const defaultConfigOptions = { auth: { keyStore: { @@ -32,65 +36,77 @@ describe('KeyStores', () => { }; const defaultConfig = new ConfigReader(defaultConfigOptions); - it('reads auth section from config', async () => { - const configSpy = jest.spyOn(defaultConfig, 'getOptionalConfig'); - const keyStore = await KeyStores.fromConfig(defaultConfig, { - logger: mockServices.logger.mock(), - database: AuthDatabase.forTesting(), - }); + it.each(databases.eachSupportedId())( + 'reads auth section from config, %p', + async databaseId => { + const knex = await databases.init(databaseId); + const configSpy = jest.spyOn(defaultConfig, 'getOptionalConfig'); + const keyStore = await KeyStores.fromConfig(defaultConfig, { + logger: mockServices.logger.mock(), + database: AuthDatabase.create(mockServices.database({ knex })), + }); - expect(keyStore).toBeInstanceOf(MemoryKeyStore); - expect(configSpy).toHaveBeenCalledWith('auth.keyStore'); - expect( - defaultConfig - .getOptionalConfig('auth.keyStore') - ?.getOptionalString('provider'), - ).toBe(defaultConfigOptions.auth.keyStore.provider); - }); + expect(keyStore).toBeInstanceOf(MemoryKeyStore); + expect(configSpy).toHaveBeenCalledWith('auth.keyStore'); + expect( + defaultConfig + .getOptionalConfig('auth.keyStore') + ?.getOptionalString('provider'), + ).toBe(defaultConfigOptions.auth.keyStore.provider); + }, + ); - it('can handle without auth config', async () => { - const keyStore = await KeyStores.fromConfig(new ConfigReader({}), { - logger: mockServices.logger.mock(), - database: AuthDatabase.forTesting(), - }); - expect(keyStore).toBeInstanceOf(DatabaseKeyStore); - }); + it.each(databases.eachSupportedId())( + 'can handle without auth config, %p', + async databaseId => { + const knex = await databases.init(databaseId); + const keyStore = await KeyStores.fromConfig(new ConfigReader({}), { + logger: mockServices.logger.mock(), + database: AuthDatabase.create(mockServices.database({ knex })), + }); + expect(keyStore).toBeInstanceOf(DatabaseKeyStore); + }, + ); - it('can handle additional provider config', async () => { - jest.spyOn(FirestoreKeyStore, 'verifyConnection').mockResolvedValue(); - const createSpy = jest.spyOn(FirestoreKeyStore, 'create'); + it.each(databases.eachSupportedId())( + 'can handle additional provider config, %p', + async databaseId => { + const knex = await databases.init(databaseId); + jest.spyOn(FirestoreKeyStore, 'verifyConnection').mockResolvedValue(); + const createSpy = jest.spyOn(FirestoreKeyStore, 'create'); - const configOptions = { - auth: { - keyStore: { - provider: 'firestore', - firestore: { - projectId: 'my-project', - keyFilename: 'cred.json', - path: 'my-path', - timeout: 100, - host: 'localhost', - port: 8088, - ssl: false, + const configOptions = { + auth: { + keyStore: { + provider: 'firestore', + firestore: { + projectId: 'my-project', + keyFilename: 'cred.json', + path: 'my-path', + timeout: 100, + host: 'localhost', + port: 8088, + ssl: false, + }, }, }, - }, - }; - const config = new ConfigReader(configOptions); - const keyStore = await KeyStores.fromConfig(config, { - logger: mockServices.logger.mock(), - database: AuthDatabase.forTesting(), - }); + }; + const config = new ConfigReader(configOptions); + const keyStore = await KeyStores.fromConfig(config, { + logger: mockServices.logger.mock(), + database: AuthDatabase.create(mockServices.database({ knex })), + }); - expect(keyStore).toBeInstanceOf(FirestoreKeyStore); - expect(createSpy).toHaveBeenCalledWith( - configOptions.auth.keyStore.firestore, - ); - expect( - config - .getOptionalConfig('auth.keyStore') - ?.getOptionalConfig('firestore') - ?.getOptionalString('projectId'), - ).toBe(configOptions.auth.keyStore.firestore.projectId); - }); + expect(keyStore).toBeInstanceOf(FirestoreKeyStore); + expect(createSpy).toHaveBeenCalledWith( + configOptions.auth.keyStore.firestore, + ); + expect( + config + .getOptionalConfig('auth.keyStore') + ?.getOptionalConfig('firestore') + ?.getOptionalString('projectId'), + ).toBe(configOptions.auth.keyStore.firestore.projectId); + }, + ); }); diff --git a/plugins/auth-backend/src/lib/catalog/CatalogIdentityClient.test.ts b/plugins/auth-backend/src/lib/catalog/CatalogIdentityClient.test.ts index 749b684d6e..b873b043aa 100644 --- a/plugins/auth-backend/src/lib/catalog/CatalogIdentityClient.test.ts +++ b/plugins/auth-backend/src/lib/catalog/CatalogIdentityClient.test.ts @@ -14,20 +14,16 @@ * limitations under the License. */ -import { TokenManager } from '@backstage/backend-common'; import { RELATION_MEMBER_OF, UserEntityV1alpha1, } from '@backstage/catalog-model'; import { catalogServiceMock } from '@backstage/plugin-catalog-node/testUtils'; import { CatalogIdentityClient } from './CatalogIdentityClient'; -import { mockServices } from '@backstage/backend-test-utils'; +import { mockCredentials, mockServices } from '@backstage/backend-test-utils'; describe('CatalogIdentityClient', () => { - const tokenManager: jest.Mocked = { - getToken: jest.fn(), - authenticate: jest.fn(), - }; + const auth = mockServices.auth(); afterEach(() => jest.resetAllMocks()); @@ -48,11 +44,9 @@ describe('CatalogIdentityClient', () => { }); jest.spyOn(catalogApi, 'getEntities'); - tokenManager.getToken.mockResolvedValue({ token: 'my-token' }); const client = new CatalogIdentityClient({ - discovery: mockServices.discovery(), catalogApi, - tokenManager, + auth, }); await client.findUser({ annotations: { key: 'value' } }); @@ -64,9 +58,13 @@ describe('CatalogIdentityClient', () => { 'metadata.annotations.key': 'value', }, }, - { token: 'my-token' }, + { + token: mockCredentials.service.token({ + onBehalfOf: mockCredentials.service('plugin:test'), + targetPluginId: 'catalog', + }), + }, ); - expect(tokenManager.getToken).toHaveBeenCalledWith(); }); it('resolveCatalogMembership resolves membership', async () => { @@ -107,12 +105,10 @@ describe('CatalogIdentityClient', () => { ]; const catalogApi = catalogServiceMock({ entities: mockUsers }); jest.spyOn(catalogApi, 'getEntities'); - tokenManager.getToken.mockResolvedValue({ token: 'my-token' }); const client = new CatalogIdentityClient({ - discovery: {} as any, catalogApi, - tokenManager, + auth, }); const claims = await client.resolveCatalogMembership({ @@ -139,7 +135,12 @@ describe('CatalogIdentityClient', () => { }, ], }, - { token: 'my-token' }, + { + token: mockCredentials.service.token({ + onBehalfOf: mockCredentials.service('plugin:test'), + targetPluginId: 'catalog', + }), + }, ); expect(claims).toMatchObject([ diff --git a/plugins/auth-backend/src/lib/catalog/CatalogIdentityClient.ts b/plugins/auth-backend/src/lib/catalog/CatalogIdentityClient.ts index 252cae88af..074a402bca 100644 --- a/plugins/auth-backend/src/lib/catalog/CatalogIdentityClient.ts +++ b/plugins/auth-backend/src/lib/catalog/CatalogIdentityClient.ts @@ -14,12 +14,7 @@ * limitations under the License. */ -import { - AuthService, - DiscoveryService, - HttpAuthService, - LoggerService, -} from '@backstage/backend-plugin-api'; +import { AuthService, LoggerService } from '@backstage/backend-plugin-api'; import { ConflictError, NotFoundError } from '@backstage/errors'; import { CatalogApi } from '@backstage/catalog-client'; import { @@ -29,38 +24,17 @@ import { stringifyEntityRef, UserEntity, } from '@backstage/catalog-model'; -import { - TokenManager, - createLegacyAuthAdapters, -} from '@backstage/backend-common'; /** * A catalog client tailored for reading out identity data from the catalog. - * - * @public - * @deprecated Use the provided `AuthResolverContext` instead, see https://backstage.io/docs/auth/identity-resolver#building-custom-resolvers */ export class CatalogIdentityClient { private readonly catalogApi: CatalogApi; private readonly auth: AuthService; - constructor(options: { - catalogApi: CatalogApi; - tokenManager?: TokenManager; - discovery: DiscoveryService; - auth?: AuthService; - httpAuth?: HttpAuthService; - }) { + constructor(options: { catalogApi: CatalogApi; auth: AuthService }) { this.catalogApi = options.catalogApi; - - const { auth } = createLegacyAuthAdapters({ - auth: options.auth, - httpAuth: options.httpAuth, - discovery: options.discovery, - tokenManager: options.tokenManager, - }); - - this.auth = auth; + this.auth = options.auth; } /** diff --git a/plugins/auth-backend/src/lib/resolvers/CatalogAuthResolverContext.test.ts b/plugins/auth-backend/src/lib/resolvers/CatalogAuthResolverContext.test.ts index c51d99a558..e79b4ee75d 100644 --- a/plugins/auth-backend/src/lib/resolvers/CatalogAuthResolverContext.test.ts +++ b/plugins/auth-backend/src/lib/resolvers/CatalogAuthResolverContext.test.ts @@ -17,7 +17,6 @@ import { CatalogAuthResolverContext } from './CatalogAuthResolverContext'; import { mockServices } from '@backstage/backend-test-utils'; import { TokenIssuer } from '../../identity/types'; -import { DiscoveryService } from '@backstage/backend-plugin-api'; import { catalogServiceMock } from '@backstage/plugin-catalog-node/testUtils'; import { NotFoundError } from '@backstage/errors'; @@ -34,9 +33,7 @@ describe('CatalogAuthResolverContext', () => { logger: mockServices.logger.mock(), catalogApi, tokenIssuer: {} as TokenIssuer, - discovery: {} as DiscoveryService, auth: mockServices.auth(), - httpAuth: mockServices.httpAuth(), }); await expect( diff --git a/plugins/auth-backend/src/lib/resolvers/CatalogAuthResolverContext.ts b/plugins/auth-backend/src/lib/resolvers/CatalogAuthResolverContext.ts index 0917d916c7..31db763d09 100644 --- a/plugins/auth-backend/src/lib/resolvers/CatalogAuthResolverContext.ts +++ b/plugins/auth-backend/src/lib/resolvers/CatalogAuthResolverContext.ts @@ -14,7 +14,6 @@ * limitations under the License. */ -import { TokenManager } from '@backstage/backend-common'; import { CatalogApi } from '@backstage/catalog-client'; import { DEFAULT_NAMESPACE, @@ -24,12 +23,7 @@ import { stringifyEntityRef, } from '@backstage/catalog-model'; import { ConflictError, InputError, NotFoundError } from '@backstage/errors'; -import { - AuthService, - DiscoveryService, - HttpAuthService, - LoggerService, -} from '@backstage/backend-plugin-api'; +import { AuthService, LoggerService } from '@backstage/backend-plugin-api'; import { TokenIssuer } from '../../identity/types'; import { AuthOwnershipResolver, @@ -55,18 +49,12 @@ export class CatalogAuthResolverContext implements AuthResolverContext { logger: LoggerService; catalogApi: CatalogApi; tokenIssuer: TokenIssuer; - tokenManager?: TokenManager; - discovery: DiscoveryService; auth: AuthService; - httpAuth: HttpAuthService; ownershipResolver?: AuthOwnershipResolver; }): CatalogAuthResolverContext { const catalogIdentityClient = new CatalogIdentityClient({ catalogApi: options.catalogApi, - tokenManager: options.tokenManager, - discovery: options.discovery, auth: options.auth, - httpAuth: options.httpAuth, }); return new CatalogAuthResolverContext( diff --git a/plugins/auth-backend/src/providers/index.ts b/plugins/auth-backend/src/providers/index.ts index abe3e77736..772a913024 100644 --- a/plugins/auth-backend/src/providers/index.ts +++ b/plugins/auth-backend/src/providers/index.ts @@ -14,4 +14,4 @@ * limitations under the License. */ -export { createOriginFilter, type ProviderFactories } from './router'; +export { type ProviderFactories } from './router'; diff --git a/plugins/auth-backend/src/providers/router.ts b/plugins/auth-backend/src/providers/router.ts index ceab20553d..63891c38dc 100644 --- a/plugins/auth-backend/src/providers/router.ts +++ b/plugins/auth-backend/src/providers/router.ts @@ -14,11 +14,9 @@ * limitations under the License. */ -import { TokenManager } from '@backstage/backend-common'; import { AuthService, DiscoveryService, - HttpAuthService, LoggerService, } from '@backstage/backend-plugin-api'; import { CatalogApi, CatalogClient } from '@backstage/catalog-client'; @@ -50,8 +48,6 @@ export function bindProviderRouters( logger: LoggerService; discovery: DiscoveryService; auth: AuthService; - httpAuth: HttpAuthService; - tokenManager?: TokenManager; tokenIssuer: TokenIssuer; ownershipResolver?: AuthOwnershipResolver; catalogApi?: CatalogApi; @@ -65,8 +61,6 @@ export function bindProviderRouters( logger, discovery, auth, - httpAuth, - tokenManager, tokenIssuer, catalogApi, ownershipResolver, @@ -97,10 +91,7 @@ export function bindProviderRouters( catalogApi: catalogApi ?? new CatalogClient({ discoveryApi: discovery }), tokenIssuer, - tokenManager, - discovery, auth, - httpAuth, ownershipResolver, }), }); @@ -148,10 +139,6 @@ export function bindProviderRouters( } } -/** - * @public - * @deprecated this export will be removed - */ export function createOriginFilter( config: Config, ): (origin: string) => boolean { diff --git a/plugins/auth-backend/src/service/router.ts b/plugins/auth-backend/src/service/router.ts index 070c9f905f..9c5a6b7711 100644 --- a/plugins/auth-backend/src/service/router.ts +++ b/plugins/auth-backend/src/service/router.ts @@ -21,15 +21,10 @@ import { AuthService, DatabaseService, DiscoveryService, - HttpAuthService, LoggerService, RootConfigService, } from '@backstage/backend-plugin-api'; import { AuthOwnershipResolver } from '@backstage/plugin-auth-node'; -import { - TokenManager, - createLegacyAuthAdapters, -} from '@backstage/backend-common'; import { NotFoundError } from '@backstage/errors'; import { CatalogApi } from '@backstage/catalog-client'; import { @@ -53,9 +48,7 @@ export interface RouterOptions { database: DatabaseService; config: RootConfigService; discovery: DiscoveryService; - tokenManager?: TokenManager; - auth?: AuthService; - httpAuth?: HttpAuthService; + auth: AuthService; tokenFactoryAlgorithm?: string; providerFactories?: ProviderFactories; catalogApi?: CatalogApi; @@ -74,8 +67,6 @@ export async function createRouter( providerFactories = {}, } = options; - const { auth, httpAuth } = createLegacyAuthAdapters(options); - const router = Router(); const appUrl = config.getString('app.baseUrl'); @@ -147,12 +138,11 @@ export async function createRouter( baseUrl: authUrl, tokenIssuer, ...options, - auth, - httpAuth, + auth: options.auth, }); bindOidcRouter(router, { - auth, + auth: options.auth, tokenIssuer, baseUrl: authUrl, userInfoDatabaseHandler, diff --git a/yarn.lock b/yarn.lock index 3337a6ee2f..4ff5e976ad 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5277,7 +5277,6 @@ __metadata: version: 0.0.0-use.local resolution: "@backstage/plugin-auth-backend@workspace:plugins/auth-backend" dependencies: - "@backstage/backend-common": "npm:^0.25.0" "@backstage/backend-defaults": "workspace:^" "@backstage/backend-plugin-api": "workspace:^" "@backstage/backend-test-utils": "workspace:^"