From a19cb2b8a53ab78bdbf71a8f5f492523df769135 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Thu, 2 Jan 2025 11:01:17 +0100 Subject: [PATCH] backend-defaults: implement permissionIntegrations service Signed-off-by: Patrik Oldsberg --- .changeset/hungry-mirrors-sniff.md | 5 ++ packages/backend-defaults/package.json | 20 +++--- .../report-permissionIntegrations.api.md | 17 +++++ .../backend-defaults/src/CreateBackend.ts | 2 + .../permissionIntegrations/index.ts | 17 +++++ .../permissionIntegrationsServiceFactory.ts | 67 +++++++++++++++++++ 6 files changed, 120 insertions(+), 8 deletions(-) create mode 100644 .changeset/hungry-mirrors-sniff.md create mode 100644 packages/backend-defaults/report-permissionIntegrations.api.md create mode 100644 packages/backend-defaults/src/entrypoints/permissionIntegrations/index.ts create mode 100644 packages/backend-defaults/src/entrypoints/permissionIntegrations/permissionIntegrationsServiceFactory.ts diff --git a/.changeset/hungry-mirrors-sniff.md b/.changeset/hungry-mirrors-sniff.md new file mode 100644 index 0000000000..27ebf9d896 --- /dev/null +++ b/.changeset/hungry-mirrors-sniff.md @@ -0,0 +1,5 @@ +--- +'@backstage/backend-defaults': patch +--- + +Added default implementation for the new `PermissionIntegrationsService`. diff --git a/packages/backend-defaults/package.json b/packages/backend-defaults/package.json index e29bb0da81..7ef216af3a 100644 --- a/packages/backend-defaults/package.json +++ b/packages/backend-defaults/package.json @@ -28,6 +28,7 @@ "./httpRouter": "./src/entrypoints/httpRouter/index.ts", "./lifecycle": "./src/entrypoints/lifecycle/index.ts", "./logger": "./src/entrypoints/logger/index.ts", + "./permissionIntegrations": "./src/entrypoints/permissionIntegrations/index.ts", "./permissions": "./src/entrypoints/permissions/index.ts", "./rootConfig": "./src/entrypoints/rootConfig/index.ts", "./rootHealth": "./src/entrypoints/rootHealth/index.ts", @@ -67,6 +68,9 @@ "logger": [ "src/entrypoints/logger/index.ts" ], + "permissionIntegrations": [ + "src/entrypoints/permissionIntegrations/index.ts" + ], "permissions": [ "src/entrypoints/permissions/index.ts" ], @@ -185,14 +189,6 @@ "yn": "^4.0.0", "zod": "^3.22.4" }, - "peerDependencies": { - "@google-cloud/cloud-sql-connector": "^1.4.0" - }, - "peerDependenciesMeta": { - "@google-cloud/cloud-sql-connector": { - "optional": true - } - }, "devDependencies": { "@aws-sdk/util-stream-node": "^3.350.0", "@backstage/backend-plugin-api": "workspace:^", @@ -214,5 +210,13 @@ "supertest": "^7.0.0", "wait-for-expect": "^3.0.2" }, + "peerDependencies": { + "@google-cloud/cloud-sql-connector": "^1.4.0" + }, + "peerDependenciesMeta": { + "@google-cloud/cloud-sql-connector": { + "optional": true + } + }, "configSchema": "config.d.ts" } diff --git a/packages/backend-defaults/report-permissionIntegrations.api.md b/packages/backend-defaults/report-permissionIntegrations.api.md new file mode 100644 index 0000000000..db24c04d65 --- /dev/null +++ b/packages/backend-defaults/report-permissionIntegrations.api.md @@ -0,0 +1,17 @@ +## API Report File for "@backstage/backend-defaults" + +> Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/). + +```ts +import { PermissionIntegrationsService } from '@backstage/backend-plugin-api'; +import { ServiceFactory } from '@backstage/backend-plugin-api'; + +// @public +export const permissionIntegrationsServiceFactory: ServiceFactory< + PermissionIntegrationsService, + 'plugin', + 'singleton' +>; + +// (No @packageDocumentation comment for this package) +``` diff --git a/packages/backend-defaults/src/CreateBackend.ts b/packages/backend-defaults/src/CreateBackend.ts index 4b0f497980..3cfb2b24ea 100644 --- a/packages/backend-defaults/src/CreateBackend.ts +++ b/packages/backend-defaults/src/CreateBackend.ts @@ -23,6 +23,7 @@ import { httpAuthServiceFactory } from '@backstage/backend-defaults/httpAuth'; import { httpRouterServiceFactory } from '@backstage/backend-defaults/httpRouter'; import { lifecycleServiceFactory } from '@backstage/backend-defaults/lifecycle'; import { loggerServiceFactory } from '@backstage/backend-defaults/logger'; +import { permissionIntegrationsServiceFactory } from '@backstage/backend-defaults/permissionIntegrations'; import { permissionsServiceFactory } from '@backstage/backend-defaults/permissions'; import { rootConfigServiceFactory } from '@backstage/backend-defaults/rootConfig'; import { rootHealthServiceFactory } from '@backstage/backend-defaults/rootHealth'; @@ -44,6 +45,7 @@ export const defaultServiceFactories = [ httpRouterServiceFactory, lifecycleServiceFactory, loggerServiceFactory, + permissionIntegrationsServiceFactory, permissionsServiceFactory, rootHealthServiceFactory, rootHttpRouterServiceFactory, diff --git a/packages/backend-defaults/src/entrypoints/permissionIntegrations/index.ts b/packages/backend-defaults/src/entrypoints/permissionIntegrations/index.ts new file mode 100644 index 0000000000..8e1daa15a1 --- /dev/null +++ b/packages/backend-defaults/src/entrypoints/permissionIntegrations/index.ts @@ -0,0 +1,17 @@ +/* + * 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. + */ + +export { permissionIntegrationsServiceFactory } from './permissionIntegrationsServiceFactory'; diff --git a/packages/backend-defaults/src/entrypoints/permissionIntegrations/permissionIntegrationsServiceFactory.ts b/packages/backend-defaults/src/entrypoints/permissionIntegrations/permissionIntegrationsServiceFactory.ts new file mode 100644 index 0000000000..3b668514ce --- /dev/null +++ b/packages/backend-defaults/src/entrypoints/permissionIntegrations/permissionIntegrationsServiceFactory.ts @@ -0,0 +1,67 @@ +/* + * Copyright 2022 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, + createServiceFactory, +} from '@backstage/backend-plugin-api'; +import { createPermissionIntegrationRouter } from '@backstage/plugin-permission-node'; + +/** + * Permission system integration for registering resources and permissions. + * + * See {@link @backstage/code-plugin-api#PermissionIntegrationsService} + * and {@link https://backstage.io/docs/backend-system/core-services/permission-integrations | the service docs} + * for more information. + * + * @public + */ +export const permissionIntegrationsServiceFactory = createServiceFactory({ + service: coreServices.permissionIntegrations, + deps: { + lifecycle: coreServices.lifecycle, + httpRouter: coreServices.httpRouter, + }, + async factory({ httpRouter, lifecycle }) { + const router = createPermissionIntegrationRouter(); + + httpRouter.use(router); + + let started = false; + lifecycle.addStartupHook(() => { + started = true; + }); + + return { + addResourceType(resource) { + if (started) { + throw new Error( + 'Cannot add permission resource types after the plugin has started', + ); + } + router.addResourceType(resource); + }, + addPermissions(permissions) { + if (started) { + throw new Error( + 'Cannot add permissions after the plugin has started', + ); + } + router.addPermissions(permissions); + }, + }; + }, +});