Skip to content

Commit

Permalink
feat(rbac)!: migrate to new backend and remove deprecations (#2225)
Browse files Browse the repository at this point in the history
* feat(rbac)!: migrate to new backend and remove deprecations

Signed-off-by: Jessica He <jhe@redhat.com>

* avoid manually mocking services

Signed-off-by: Jessica He <jhe@redhat.com>

---------

Signed-off-by: Jessica He <jhe@redhat.com>
  • Loading branch information
JessicaJHee authored Oct 3, 2024
1 parent ea3bbfa commit c209cde
Show file tree
Hide file tree
Showing 47 changed files with 642 additions and 636 deletions.
8 changes: 8 additions & 0 deletions plugins/bulk-import-backend/src/openapi.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions plugins/rbac-backend-module-test/config.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TaskScheduleDefinitionConfig } from '@backstage/backend-tasks';
import { SchedulerServiceTaskScheduleDefinitionConfig } from '@backstage/backend-plugin-api';

export interface Config {
permission?: {
Expand All @@ -7,7 +7,7 @@ export interface Config {
test?: {
baseUrl: string;
accessToken: string;
schedule?: TaskScheduleDefinitionConfig;
schedule?: SchedulerServiceTaskScheduleDefinitionConfig;
};
};
};
Expand Down
12 changes: 2 additions & 10 deletions plugins/rbac-backend-module-test/dist-dynamic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@
"require": "./dist/index.cjs.js",
"default": "./dist/index.cjs.js"
},
"./alpha": {
"require": "./dist/alpha.cjs.js",
"default": "./dist/alpha.cjs.js"
},
"./package.json": "./package.json"
},
"scripts": {},
Expand All @@ -34,8 +30,7 @@
"files": [
"dist",
"config.d.ts",
"app-config.janus-idp.yaml",
"alpha"
"app-config.janus-idp.yaml"
],
"configSchema": "config.d.ts",
"repository": {
Expand All @@ -51,11 +46,8 @@
"bugs": "https://github.com/janus-idp/backstage-plugins/issues",
"bundleDependencies": true,
"peerDependencies": {
"@backstage/backend-common": "^0.23.3",
"@backstage/backend-dynamic-feature-service": "^0.2.15",
"@backstage/backend-plugin-api": "^0.7.0",
"@backstage/backend-tasks": "^0.5.27",
"@backstage/config": "^1.2.0"
"@backstage/errors": "^1.2.4"
},
"overrides": {
"@aws-sdk/util-utf8-browser": {
Expand Down
16 changes: 5 additions & 11 deletions plugins/rbac-backend-module-test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,10 @@
},
"exports": {
".": "./src/index.ts",
"./alpha": "./src/alpha.ts",
"./package.json": "./package.json"
},
"typesVersions": {
"*": {
"alpha": [
"src/alpha.ts"
],
"package.json": [
"package.json"
]
Expand All @@ -42,25 +38,23 @@
"tsc": "tsc"
},
"dependencies": {
"@backstage/backend-common": "^0.23.3",
"@backstage/backend-dynamic-feature-service": "^0.2.15",
"@backstage/backend-plugin-api": "^0.7.0",
"@backstage/backend-tasks": "^0.5.27",
"@backstage/config": "^1.2.0",
"@janus-idp/backstage-plugin-rbac-node": "1.5.0",
"@backstage/backend-plugin-api": "^0.7.0",
"@backstage/errors": "^1.2.4",
"csv-parse": "^5.5.6"
},
"devDependencies": {
"@backstage/config": "^1.2.0",
"@backstage/backend-test-utils": "0.4.4",
"@backstage/cli": "0.26.11",
"@janus-idp/cli": "1.14.0"
"@janus-idp/cli": "1.14.0",
"@backstage/backend-dynamic-feature-service": "^0.2.15"
},
"files": [
"dist",
"config.d.ts",
"dist-dynamic/*.*",
"dist-dynamic/dist/**",
"dist-dynamic/alpha/*",
"app-config.janus-idp.yaml"
],
"configSchema": "config.d.ts",
Expand Down
1 change: 0 additions & 1 deletion plugins/rbac-backend-module-test/src/alpha.ts

This file was deleted.

5 changes: 0 additions & 5 deletions plugins/rbac-backend-module-test/src/dynamic/index.ts

This file was deleted.

4 changes: 1 addition & 3 deletions plugins/rbac-backend-module-test/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@
*
* @packageDocumentation
*/

export * from './dynamic/index';
export * from './module';
export { rbacModuleTest as default } from './module';
18 changes: 10 additions & 8 deletions plugins/rbac-backend-module-test/src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@ export const rbacModuleTest = createBackendModule({
},
async init({ logger, rbac, scheduler, config }) {
rbac.addRBACProvider(
TestProvider.fromConfig(config, {
logger,
scheduler: scheduler,
schedule: scheduler.createScheduledTaskRunner({
frequency: { minutes: 30 },
timeout: { minutes: 3 },
}),
}),
TestProvider.fromConfig(
{ config, logger },
{
scheduler: scheduler,
schedule: scheduler.createScheduledTaskRunner({
frequency: { minutes: 30 },
timeout: { minutes: 3 },
}),
},
),
);
},
});
Expand Down
34 changes: 20 additions & 14 deletions plugins/rbac-backend-module-test/src/provider/TestProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import {
SchedulerServiceTaskRunner,
SchedulerServiceTaskScheduleDefinition,
} from '@backstage/backend-plugin-api';
import { Config } from '@backstage/config';
import type { Config } from '@backstage/config';
import { InputError, isError, NotFoundError } from '@backstage/errors';

import { parse } from 'csv-parse/sync';

import {
import type {
RBACProvider,
RBACProviderConnection,
} from '@janus-idp/backstage-plugin-rbac-node';
Expand All @@ -28,27 +29,30 @@ export class TestProvider implements RBACProvider {
private connection?: RBACProviderConnection;

static fromConfig(
config: Config,
options: {
deps: {
config: Config;
logger: LoggerService;
schedule?: SchedulerServiceTaskRunner;
scheduler?: SchedulerService;
},
options:
| { schedule: SchedulerServiceTaskRunner }
| { scheduler: SchedulerService },
): TestProvider {
const providerConfig = readProviderConfig(config);
const providerConfig = readProviderConfig(deps.config);
let schedulerServiceTaskRunner;

if (options.scheduler && providerConfig.schedule) {
if ('scheduler' in options && providerConfig.schedule) {
schedulerServiceTaskRunner = options.scheduler.createScheduledTaskRunner(
providerConfig.schedule,
);
} else if (options.schedule) {
} else if ('schedule' in options) {
schedulerServiceTaskRunner = options.schedule;
} else {
throw new Error('Neither schedule nor scheduler is provided.');
throw new InputError(
`No schedule provided via config for RBACTestProvider.`,
);
}

return new TestProvider(schedulerServiceTaskRunner, options.logger);
return new TestProvider(schedulerServiceTaskRunner, deps.logger);
}

private constructor(
Expand Down Expand Up @@ -89,8 +93,10 @@ export class TestProvider implements RBACProvider {
fn: async () => {
try {
await this.run();
} catch (error: any) {
this.logger.error(`Error occurred, here is the error ${error}`);
} catch (error) {
if (isError(error)) {
this.logger.error(`Error occurred, here is the error ${error}`);
}
}
},
});
Expand All @@ -99,7 +105,7 @@ export class TestProvider implements RBACProvider {

private async run(): Promise<void> {
if (!this.connection) {
throw new Error('Not initialized');
throw new NotFoundError('Not initialized');
}
const permissions: string[][] = [];
const roles: string[][] = [];
Expand Down
58 changes: 0 additions & 58 deletions plugins/rbac-backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,64 +38,6 @@ yarn workspace backend add @janus-idp/backstage-plugin-rbac-backend

### Configuring the Backend

#### Old Backend System

To connect the RBAC framework to your backend use the `PolicyBuilder` class in your backend permissions plugin (typically `packages/backend/src/plugins/permissions.ts`) as follows:

```ts
/* highlight-add-start */
import { Router } from 'express';

import {
PluginIdProvider,
PolicyBuilder,
} from '@janus-idp/backstage-plugin-rbac-backend';

import { PluginEnvironment } from '../types';

export default async function createPlugin(
env: PluginEnvironment,
pluginIdProvider: PluginIdProvider,
): Promise<Router> {
return PolicyBuilder.build(
{
config: env.config,
logger: env.logger,
discovery: env.discovery,
identity: env.identity,
permissions: env.permissions,
},
pluginIdProvider,
);
}
/* highlight-add-end */
```

Secondly, in your backend router (typically `packages/backend/src/index.ts`) add a route for `/permission` specifying the list of plugin id's that support permissions:

```ts
// ...
/* highlight-add-next-line */
import permission from './plugins/permissions';

async function main() {
// ...
/* highlight-add-next-line */
const permissionEnv = useHotMemoize(module, () => createEnv('permission'));

// ...
/* highlight-add-start */
apiRouter.use(
'/permission',
await permission(permissionEnv, {
// return list static plugin which supports Backstage permissions.
getPluginIds: () => ['catalog', 'scaffolder', 'permission'],
}),
);
/* highlight-add-end */
}
```

#### New Backend System

The RBAC plugin supports the integration with the new backend system.
Expand Down
13 changes: 6 additions & 7 deletions plugins/rbac-backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,11 @@
"@backstage/backend-plugin-api": "^0.7.0",
"@backstage/catalog-client": "^1.6.5",
"@backstage/catalog-model": "^1.5.0",
"@backstage/config": "^1.2.0",
"@backstage/core-plugin-api": "^1.9.3",
"@backstage/errors": "^1.2.4",
"@backstage/plugin-auth-node": "^0.4.17",
"@backstage/plugin-permission-backend": "^0.5.46",
"@backstage/plugin-permission-common": "^0.8.0",
"@backstage/plugin-permission-node": "^0.8.0",
"@backstage/types": "^1.1.1",
"@dagrejs/graphlib": "^2.1.13",
"@janus-idp/backstage-plugin-audit-log-node": "1.5.0",
"@janus-idp/backstage-plugin-rbac-common": "1.10.0",
Expand All @@ -55,9 +52,7 @@
"js-yaml": "^4.1.0",
"knex": "^3.0.0",
"lodash": "^4.17.21",
"qs": "^6.11.2",
"typeorm-adapter": "^1.6.1",
"yn": "^4.0.0"
"typeorm-adapter": "^1.6.1"
},
"devDependencies": {
"@backstage/backend-test-utils": "0.4.4",
Expand All @@ -67,7 +62,11 @@
"@types/supertest": "2.0.16",
"knex-mock-client": "2.0.1",
"msw": "1.3.3",
"supertest": "6.3.4"
"supertest": "6.3.4",
"@backstage/core-plugin-api": "^1.9.3",
"@backstage/types": "^1.1.1",
"qs": "^6.11.2",
"@backstage/config": "^1.2.0"
},
"files": [
"dist",
Expand Down
4 changes: 2 additions & 2 deletions plugins/rbac-backend/src/audit-log/audit-logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import {
PolicyDecision,
ResourcePermission,
} from '@backstage/plugin-permission-common';
import { PolicyQuery } from '@backstage/plugin-permission-node';
import type { PolicyQuery } from '@backstage/plugin-permission-node';

import { AuditLogOptions } from '@janus-idp/backstage-plugin-audit-log-node';
import type { AuditLogOptions } from '@janus-idp/backstage-plugin-audit-log-node';
import {
PermissionAction,
RoleConditionalPolicyDecision,
Expand Down
9 changes: 7 additions & 2 deletions plugins/rbac-backend/src/audit-log/rest-errors-interceptor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { ErrorRequestHandler, NextFunction, Request, Response } from 'express';
import type {
ErrorRequestHandler,
NextFunction,
Request,
Response,
} from 'express';

import { AuditLogger } from '@janus-idp/backstage-plugin-audit-log-node';
import type { AuditLogger } from '@janus-idp/backstage-plugin-audit-log-node';

import {
ConditionEvents,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {
import type {
PermissionCondition,
PermissionCriteria,
PermissionRuleParams,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { BackstageUserInfo } from '@backstage/backend-plugin-api';
import {
import type { BackstageUserInfo } from '@backstage/backend-plugin-api';
import type {
PermissionCondition,
PermissionCriteria,
PermissionRuleParam,
PermissionRuleParams,
} from '@backstage/plugin-permission-common';
import { JsonPrimitive } from '@backstage/types';
import type { JsonPrimitive } from '@backstage/types';

import {
CONDITION_ALIAS_SIGN,
Expand Down
Loading

0 comments on commit c209cde

Please sign in to comment.