From 84946a580c460ca01a81d25895052cce6a51d6e5 Mon Sep 17 00:00:00 2001 From: eipc16 Date: Mon, 3 Apr 2023 20:36:35 +0200 Subject: [PATCH] expose the permission plugin using the new backend system Signed-off-by: eipc16 --- .changeset/famous-beds-repair.md | 5 ++ packages/backend-next/package.json | 4 ++ .../src/ExamplePermissionPolicy.ts | 35 ++++++++++ packages/backend-next/src/index.ts | 6 ++ .../permission-backend/alpha-api-report.md | 20 ++++++ plugins/permission-backend/package.json | 16 +++++ plugins/permission-backend/src/alpha.ts | 17 +++++ plugins/permission-backend/src/plugin.ts | 66 +++++++++++++++++++ yarn.lock | 5 ++ 9 files changed, 174 insertions(+) create mode 100644 .changeset/famous-beds-repair.md create mode 100644 packages/backend-next/src/ExamplePermissionPolicy.ts create mode 100644 plugins/permission-backend/alpha-api-report.md create mode 100644 plugins/permission-backend/src/alpha.ts create mode 100644 plugins/permission-backend/src/plugin.ts diff --git a/.changeset/famous-beds-repair.md b/.changeset/famous-beds-repair.md new file mode 100644 index 0000000000..54adce96b5 --- /dev/null +++ b/.changeset/famous-beds-repair.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-permission-backend': minor +--- + +Introduced alpha export of the `permissionPlugin` using the new backend system diff --git a/packages/backend-next/package.json b/packages/backend-next/package.json index 2fccf12a85..48b4cd10a6 100644 --- a/packages/backend-next/package.json +++ b/packages/backend-next/package.json @@ -27,8 +27,12 @@ "dependencies": { "@backstage/backend-defaults": "workspace:^", "@backstage/plugin-app-backend": "workspace:^", + "@backstage/plugin-auth-node": "workspace:^", "@backstage/plugin-catalog-backend": "workspace:^", "@backstage/plugin-kubernetes-backend": "workspace:^", + "@backstage/plugin-permission-backend": "workspace:^", + "@backstage/plugin-permission-common": "workspace:^", + "@backstage/plugin-permission-node": "workspace:^", "@backstage/plugin-scaffolder-backend": "workspace:^", "@backstage/plugin-search-backend": "workspace:^", "@backstage/plugin-search-backend-module-catalog": "workspace:^", diff --git a/packages/backend-next/src/ExamplePermissionPolicy.ts b/packages/backend-next/src/ExamplePermissionPolicy.ts new file mode 100644 index 0000000000..cf7c024e01 --- /dev/null +++ b/packages/backend-next/src/ExamplePermissionPolicy.ts @@ -0,0 +1,35 @@ +/* + * 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 { BackstageIdentityResponse } from '@backstage/plugin-auth-node'; +import { + AuthorizeResult, + PolicyDecision, +} from '@backstage/plugin-permission-common'; +import { + PermissionPolicy, + PolicyQuery, +} from '@backstage/plugin-permission-node'; + +export class ExamplePermissionPolicy implements PermissionPolicy { + async handle( + _request: PolicyQuery, + _user?: BackstageIdentityResponse, + ): Promise { + return { + result: AuthorizeResult.ALLOW, + }; + } +} diff --git a/packages/backend-next/src/index.ts b/packages/backend-next/src/index.ts index 1ce63a8d51..12437c8ab5 100644 --- a/packages/backend-next/src/index.ts +++ b/packages/backend-next/src/index.ts @@ -25,6 +25,8 @@ import { searchModuleCatalogCollator } from '@backstage/plugin-search-backend-mo import { searchModuleTechDocsCollator } from '@backstage/plugin-search-backend-module-techdocs/alpha'; import { searchModuleExploreCollator } from '@backstage/plugin-search-backend-module-explore/alpha'; import { kubernetesPlugin } from '@backstage/plugin-kubernetes-backend/alpha'; +import { permissionPlugin } from '@backstage/plugin-permission-backend/alpha'; +import { ExamplePermissionPolicy } from './ExamplePermissionPolicy'; const backend = createBackend(); @@ -48,4 +50,8 @@ backend.add(searchModuleExploreCollator()); // Kubernetes backend.add(kubernetesPlugin()); + +// Permissions +backend.add(permissionPlugin({ policy: new ExamplePermissionPolicy() })); + backend.start(); diff --git a/plugins/permission-backend/alpha-api-report.md b/plugins/permission-backend/alpha-api-report.md new file mode 100644 index 0000000000..a86d8903ad --- /dev/null +++ b/plugins/permission-backend/alpha-api-report.md @@ -0,0 +1,20 @@ +## API Report File for "@backstage/plugin-permission-backend" + +> Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/). + +```ts +import { BackendFeature } from '@backstage/backend-plugin-api'; +import { PermissionPolicy } from '@backstage/plugin-permission-node'; + +// @alpha +export const permissionPlugin: ( + options: PermissionPluginOptions, +) => BackendFeature; + +// @alpha +export type PermissionPluginOptions = { + policy: PermissionPolicy; +}; + +// (No @packageDocumentation comment for this package) +``` diff --git a/plugins/permission-backend/package.json b/plugins/permission-backend/package.json index 2fcee7ee19..0d2c0a840e 100644 --- a/plugins/permission-backend/package.json +++ b/plugins/permission-backend/package.json @@ -9,6 +9,21 @@ "main": "dist/index.cjs.js", "types": "dist/index.d.ts" }, + "exports": { + ".": "./src/index.ts", + "./alpha": "./src/alpha.ts", + "./package.json": "./package.json" + }, + "typesVersions": { + "*": { + "alpha": [ + "src/alpha.ts" + ], + "package.json": [ + "package.json" + ] + } + }, "backstage": { "role": "backend-plugin" }, @@ -23,6 +38,7 @@ }, "dependencies": { "@backstage/backend-common": "workspace:^", + "@backstage/backend-plugin-api": "workspace:^", "@backstage/config": "workspace:^", "@backstage/errors": "workspace:^", "@backstage/plugin-auth-node": "workspace:^", diff --git a/plugins/permission-backend/src/alpha.ts b/plugins/permission-backend/src/alpha.ts new file mode 100644 index 0000000000..a03fa0cee6 --- /dev/null +++ b/plugins/permission-backend/src/alpha.ts @@ -0,0 +1,17 @@ +/* + * 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. + */ + +export { permissionPlugin, type PermissionPluginOptions } from './plugin'; diff --git a/plugins/permission-backend/src/plugin.ts b/plugins/permission-backend/src/plugin.ts new file mode 100644 index 0000000000..9a6e5a13af --- /dev/null +++ b/plugins/permission-backend/src/plugin.ts @@ -0,0 +1,66 @@ +/* + * Copyright 2021 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 { loggerToWinstonLogger } from '@backstage/backend-common'; +import { + coreServices, + createBackendPlugin, +} from '@backstage/backend-plugin-api'; +import { PermissionPolicy } from '@backstage/plugin-permission-node'; +import { createRouter } from './service'; + +/** + * Permission plugin options + * + * @alpha + */ +export type PermissionPluginOptions = { + policy: PermissionPolicy; +}; + +/** + * Permission plugin + * + * @alpha + */ +export const permissionPlugin = createBackendPlugin( + (options: PermissionPluginOptions) => ({ + pluginId: 'permission', + register(env) { + env.registerInit({ + deps: { + http: coreServices.httpRouter, + config: coreServices.config, + logger: coreServices.logger, + discovery: coreServices.discovery, + identity: coreServices.identity, + }, + async init({ http, config, logger, discovery, identity }) { + const winstonLogger = loggerToWinstonLogger(logger); + http.use( + await createRouter({ + config, + discovery, + identity, + logger: winstonLogger, + policy: options.policy, + }), + ); + }, + }); + }, + }), +); diff --git a/yarn.lock b/yarn.lock index 7b0ba7f3e5..87fc76a056 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7593,6 +7593,7 @@ __metadata: resolution: "@backstage/plugin-permission-backend@workspace:plugins/permission-backend" dependencies: "@backstage/backend-common": "workspace:^" + "@backstage/backend-plugin-api": "workspace:^" "@backstage/cli": "workspace:^" "@backstage/config": "workspace:^" "@backstage/errors": "workspace:^" @@ -23441,8 +23442,12 @@ __metadata: "@backstage/backend-defaults": "workspace:^" "@backstage/cli": "workspace:^" "@backstage/plugin-app-backend": "workspace:^" + "@backstage/plugin-auth-node": "workspace:^" "@backstage/plugin-catalog-backend": "workspace:^" "@backstage/plugin-kubernetes-backend": "workspace:^" + "@backstage/plugin-permission-backend": "workspace:^" + "@backstage/plugin-permission-common": "workspace:^" + "@backstage/plugin-permission-node": "workspace:^" "@backstage/plugin-scaffolder-backend": "workspace:^" "@backstage/plugin-search-backend": "workspace:^" "@backstage/plugin-search-backend-module-catalog": "workspace:^"