From 74ea94e1498c4fe14e0bb5a71c8a93180ae7f441 Mon Sep 17 00:00:00 2001 From: Niklas Aronsson Date: Thu, 12 Sep 2024 11:19:38 +0200 Subject: [PATCH] fix(bazaar-backend): use the new auth system Signed-off-by: Niklas Aronsson --- .../bazaar/.changeset/light-dots-relax.md | 24 +++++++ .../bazaar/plugins/bazaar-backend/README.md | 1 + .../plugins/bazaar-backend/dev/index.ts | 5 ++ .../plugins/bazaar-backend/package.json | 6 +- .../plugins/bazaar-backend/src/alpha.ts | 19 +++-- .../bazaar/plugins/bazaar-backend/src/run.ts | 33 --------- .../bazaar-backend/src/service/router.ts | 31 ++++++--- .../src/service/standaloneServer.ts | 69 ------------------- workspaces/bazaar/yarn.lock | 4 +- 9 files changed, 69 insertions(+), 123 deletions(-) create mode 100644 workspaces/bazaar/.changeset/light-dots-relax.md create mode 100644 workspaces/bazaar/plugins/bazaar-backend/dev/index.ts delete mode 100644 workspaces/bazaar/plugins/bazaar-backend/src/run.ts delete mode 100644 workspaces/bazaar/plugins/bazaar-backend/src/service/standaloneServer.ts diff --git a/workspaces/bazaar/.changeset/light-dots-relax.md b/workspaces/bazaar/.changeset/light-dots-relax.md new file mode 100644 index 0000000000..b7bcd50278 --- /dev/null +++ b/workspaces/bazaar/.changeset/light-dots-relax.md @@ -0,0 +1,24 @@ +--- +'@backstage-community/plugin-bazaar-backend': minor +--- + +**BREAKING**: The `discovery` service is now required if using the old backend +system. Migrated to support new auth services. + +```diff +import { PluginEnvironment } from '../types'; +import { createRouter } from '@backstage-community/plugin-bazaar-backend'; +import { Router } from 'express'; + +export default async function createPlugin( + env: PluginEnvironment, +): Promise { + return await createRouter({ + logger: env.logger, + config: env.config, + database: env.database, ++ discovery: env.discovery, + identity: env.identity, + }); +} +``` diff --git a/workspaces/bazaar/plugins/bazaar-backend/README.md b/workspaces/bazaar/plugins/bazaar-backend/README.md index b69b869806..3156b62db0 100644 --- a/workspaces/bazaar/plugins/bazaar-backend/README.md +++ b/workspaces/bazaar/plugins/bazaar-backend/README.md @@ -27,6 +27,7 @@ export default async function createPlugin( logger: env.logger, config: env.config, database: env.database, + discovery: env.discovery, identity: env.identity, }); } diff --git a/workspaces/bazaar/plugins/bazaar-backend/dev/index.ts b/workspaces/bazaar/plugins/bazaar-backend/dev/index.ts new file mode 100644 index 0000000000..d86daaf341 --- /dev/null +++ b/workspaces/bazaar/plugins/bazaar-backend/dev/index.ts @@ -0,0 +1,5 @@ +import { createBackend } from '@backstage/backend-defaults'; + +const backend = createBackend(); +backend.add(import('../src/alpha')); +backend.start(); diff --git a/workspaces/bazaar/plugins/bazaar-backend/package.json b/workspaces/bazaar/plugins/bazaar-backend/package.json index 52bc5d36e0..4a28e7c243 100644 --- a/workspaces/bazaar/plugins/bazaar-backend/package.json +++ b/workspaces/bazaar/plugins/bazaar-backend/package.json @@ -52,14 +52,12 @@ }, "dependencies": { "@backstage/backend-common": "^0.24.0", + "@backstage/backend-defaults": "^0.4.2", "@backstage/backend-plugin-api": "^0.8.0", - "@backstage/config": "^1.2.0", - "@backstage/plugin-auth-node": "^0.5.0", "@types/express": "^4.17.6", "express": "^4.17.1", "express-promise-router": "^4.1.0", - "knex": "^3.0.0", - "yn": "^4.0.0" + "knex": "^3.0.0" }, "devDependencies": { "@backstage/backend-test-utils": "^0.5.0", diff --git a/workspaces/bazaar/plugins/bazaar-backend/src/alpha.ts b/workspaces/bazaar/plugins/bazaar-backend/src/alpha.ts index ff1c78bac3..6279035fd9 100644 --- a/workspaces/bazaar/plugins/bazaar-backend/src/alpha.ts +++ b/workspaces/bazaar/plugins/bazaar-backend/src/alpha.ts @@ -32,16 +32,25 @@ export default createBackendPlugin({ deps: { config: coreServices.rootConfig, database: coreServices.database, - identity: coreServices.identity, - logger: coreServices.logger, + discovery: coreServices.discovery, + httpAuth: coreServices.httpAuth, httpRouter: coreServices.httpRouter, + logger: coreServices.logger, }, - async init({ database, config, identity, logger, httpRouter }) { + async init({ + config, + database, + discovery, + httpAuth, + httpRouter, + logger, + }) { httpRouter.use( await createRouter({ - database, config, - identity, + database, + discovery, + httpAuth, logger, }), ); diff --git a/workspaces/bazaar/plugins/bazaar-backend/src/run.ts b/workspaces/bazaar/plugins/bazaar-backend/src/run.ts deleted file mode 100644 index 0a3ed2b7f0..0000000000 --- a/workspaces/bazaar/plugins/bazaar-backend/src/run.ts +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright 2020 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 { getRootLogger } from '@backstage/backend-common'; -import yn from 'yn'; -import { startStandaloneServer } from './service/standaloneServer'; - -const port = process.env.PLUGIN_PORT ? Number(process.env.PLUGIN_PORT) : 7007; -const enableCors = yn(process.env.PLUGIN_CORS, { default: false }); -const logger = getRootLogger(); - -startStandaloneServer({ port, enableCors, logger }).catch(err => { - logger.error(err); - process.exit(1); -}); - -process.on('SIGINT', () => { - logger.info('CTRL+C pressed; exiting.'); - process.exit(0); -}); diff --git a/workspaces/bazaar/plugins/bazaar-backend/src/service/router.ts b/workspaces/bazaar/plugins/bazaar-backend/src/service/router.ts index cb4dcec0d5..2e1bf50040 100644 --- a/workspaces/bazaar/plugins/bazaar-backend/src/service/router.ts +++ b/workspaces/bazaar/plugins/bazaar-backend/src/service/router.ts @@ -14,27 +14,38 @@ * limitations under the License. */ -import { errorHandler, PluginDatabaseManager } from '@backstage/backend-common'; +import { + errorHandler, + PluginDatabaseManager, + createLegacyAuthAdapters, +} from '@backstage/backend-common'; import express from 'express'; import Router from 'express-promise-router'; -import { Config } from '@backstage/config'; -import { IdentityApi } from '@backstage/plugin-auth-node'; import { DatabaseHandler } from './DatabaseHandler'; -import { LoggerService } from '@backstage/backend-plugin-api'; +import { + DiscoveryService, + HttpAuthService, + IdentityService, + LoggerService, + RootConfigService, +} from '@backstage/backend-plugin-api'; /** @public */ export interface RouterOptions { logger: LoggerService; database: PluginDatabaseManager; - config: Config; - identity: IdentityApi; + discovery: DiscoveryService; + config: RootConfigService; + identity?: IdentityService; + httpAuth?: HttpAuthService; } /** @public */ export async function createRouter( options: RouterOptions, ): Promise { - const { logger, database, identity } = options; + const { logger, database } = options; + const { httpAuth } = createLegacyAuthAdapters(options); const dbHandler = await DatabaseHandler.create({ database }); @@ -55,12 +66,14 @@ export async function createRouter( router.put('/projects/:id/member/:userId', async (request, response) => { const { id, userId } = request.params; - const user = await identity.getIdentity({ request: request }); + const credentials = await httpAuth.credentials(request, { + allow: ['user'], + }); await dbHandler.addMember( parseInt(id, 10), userId, - user?.identity.userEntityRef, + credentials.principal.userEntityRef, request.body?.picture, ); diff --git a/workspaces/bazaar/plugins/bazaar-backend/src/service/standaloneServer.ts b/workspaces/bazaar/plugins/bazaar-backend/src/service/standaloneServer.ts deleted file mode 100644 index b4aabe074f..0000000000 --- a/workspaces/bazaar/plugins/bazaar-backend/src/service/standaloneServer.ts +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright 2020 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 { - createServiceBuilder, - DatabaseManager, - loadBackendConfig, -} from '@backstage/backend-common'; -import { IdentityApi } from '@backstage/plugin-auth-node'; -import { Server } from 'http'; -import { createRouter } from './router'; -import { ConfigReader } from '@backstage/config'; -import { LoggerService } from '@backstage/backend-plugin-api'; - -export interface ServerOptions { - port: number; - enableCors: boolean; - logger: LoggerService; -} - -export async function startStandaloneServer( - options: ServerOptions, -): Promise { - const logger = options.logger.child({ service: 'bazaar-backend' }); - const config = await loadBackendConfig({ logger, argv: process.argv }); - - const manager = DatabaseManager.fromConfig( - new ConfigReader({ - backend: { - database: { client: 'better-sqlite3', connection: ':memory:' }, - }, - }), - ); - const database = manager.forPlugin('bazaar'); - - const router = await createRouter({ - logger, - database, - config: config, - identity: {} as IdentityApi, - }); - - let service = createServiceBuilder(module) - .setPort(options.port) - .addRouter('/bazaar', router); - if (options.enableCors) { - service = service.enableCors({ origin: 'http://localhost:3000' }); - } - - return await service.start().catch(err => { - logger.error(err); - process.exit(1); - }); -} - -module.hot?.accept(); diff --git a/workspaces/bazaar/yarn.lock b/workspaces/bazaar/yarn.lock index 5fe14b2eec..93c0cd1d7e 100644 --- a/workspaces/bazaar/yarn.lock +++ b/workspaces/bazaar/yarn.lock @@ -2517,16 +2517,14 @@ __metadata: resolution: "@backstage-community/plugin-bazaar-backend@workspace:plugins/bazaar-backend" dependencies: "@backstage/backend-common": ^0.24.0 + "@backstage/backend-defaults": ^0.4.2 "@backstage/backend-plugin-api": ^0.8.0 "@backstage/backend-test-utils": ^0.5.0 "@backstage/cli": ^0.27.0 - "@backstage/config": ^1.2.0 - "@backstage/plugin-auth-node": ^0.5.0 "@types/express": ^4.17.6 express: ^4.17.1 express-promise-router: ^4.1.0 knex: ^3.0.0 - yn: ^4.0.0 languageName: unknown linkType: soft