From 8d722ae1f3360dd386bb66f91f6844f1b85680ec Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Wed, 8 May 2024 10:28:35 +0200 Subject: [PATCH] backend-common: add test for legacy plugin auth Signed-off-by: Patrik Oldsberg --- packages/backend-common/src/legacy.test.ts | 118 +++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 packages/backend-common/src/legacy.test.ts diff --git a/packages/backend-common/src/legacy.test.ts b/packages/backend-common/src/legacy.test.ts new file mode 100644 index 0000000000..1ddebce5c4 --- /dev/null +++ b/packages/backend-common/src/legacy.test.ts @@ -0,0 +1,118 @@ +/* + * Copyright 2023 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { + coreServices, + createBackendPlugin, +} from '@backstage/backend-plugin-api'; +import { mockServices, startTestBackend } from '@backstage/backend-test-utils'; +import { EventEmitter } from 'events'; +import { Router } from 'express'; +import { createLegacyAuthAdapters } from './auth'; +import { legacyPlugin } from './legacy'; +import { + authServiceFactory, + tokenManagerServiceFactory, +} from '@backstage/backend-app-api'; + +describe('legacyPlugin', () => { + it('can auth across the new and old systems', async () => { + const emitter = new EventEmitter(); + + const done = new Promise(resolve => { + emitter.once('done', () => { + emitter.once('done', resolve); + }); + }); + + await startTestBackend({ + features: [ + authServiceFactory, + tokenManagerServiceFactory, + mockServices.rootConfig.factory({ + data: { + backend: { + auth: { + keys: [ + { + secret: 'test', + }, + ], + }, + }, + }, + }), + createBackendPlugin({ + pluginId: 'new', + register(reg) { + reg.registerInit({ + deps: { + auth: coreServices.auth, + discovery: coreServices.discovery, + }, + async init({ auth }) { + emitter.once('legacy-token', async otherToken => { + const credentials = await auth.authenticate(otherToken); + expect(credentials.principal).toEqual({ + type: 'service', + subject: 'external:backstage-plugin', + }); + emitter.emit('done'); + }); + + const { token } = await auth.getPluginRequestToken({ + onBehalfOf: await auth.getOwnServiceCredentials(), + targetPluginId: 'old', + }); + emitter.emit('new-token', token); + }, + }); + }, + }), + legacyPlugin( + 'old', + Promise.resolve({ + async default({ tokenManager, identity, discovery }) { + const { auth } = createLegacyAuthAdapters({ + tokenManager, + identity, + discovery, + auth: undefined as any as typeof coreServices.auth.T, + httpAuth: undefined as any as typeof coreServices.httpAuth.T, + }); + + emitter.once('new-token', async otherToken => { + const credentials = await auth.authenticate(otherToken); + expect(credentials.principal).toEqual({ + type: 'service', + subject: 'external:backstage-plugin', + }); + emitter.emit('done'); + }); + + const { token } = await tokenManager.getToken(); + emitter.emit('legacy-token', token); + + return Router(); + }, + }), + ), + ], + }); + + await done; + }); +});