Skip to content

Commit

Permalink
Update Keycloak plugin for dynamic backend
Browse files Browse the repository at this point in the history
Add dynamic backend module capabilities for the Keycloak backend plugin.

Signed-off-by: Craig Tracey <[email protected]>
  • Loading branch information
craigtracey committed Nov 7, 2023
1 parent 8ee7398 commit 5d89751
Show file tree
Hide file tree
Showing 6 changed files with 2,906 additions and 141 deletions.
27 changes: 25 additions & 2 deletions plugins/keycloak-backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,22 @@
"types": "dist/index.d.ts"
},
"backstage": {
"role": "backend-plugin"
"role": "backend-plugin-module"
},
"exports": {
".": "./src/index.ts",
"./alpha": "./src/alpha.ts",
"./package.json": "./package.json"
},
"typesVersions": {
"*": {
"alpha": [
"src/alpha.ts"
],
"package.json": [
"package.json"
]
}
},
"scripts": {
"start": "opener http://localhost:8080/admin/master/console/#/janus-realm && opener http://localhost:7007/catalog/entities && turbo run start:plugin start:keycloak",
Expand All @@ -23,10 +38,12 @@
"test": "backstage-cli package test --passWithNoTests --coverage",
"clean": "backstage-cli package clean",
"prepack": "backstage-cli package prepack",
"postpack": "backstage-cli package postpack"
"postpack": "backstage-cli package postpack",
"export-dynamic": "janus-cli package export-dynamic-plugin"
},
"dependencies": {
"@backstage/backend-common": "^0.19.8",
"@backstage/backend-plugin-api": "^0.6.6",
"@backstage/backend-tasks": "^0.5.11",
"@backstage/catalog-model": "^1.4.3",
"@backstage/config": "^1.1.1",
Expand All @@ -38,22 +55,28 @@
},
"devDependencies": {
"@backstage/backend-app-api": "0.5.6",
"@backstage/backend-test-utils": "^0.2.7",
"@backstage/cli": "0.23.0",
"@backstage/plugin-auth-node": "0.4.0",
"@backstage/plugin-catalog-backend": "1.14.0",
"@backstage/plugin-permission-common": "0.7.9",
"@backstage/plugin-permission-node": "0.7.17",
"@janus-idp/cli": "1.1.0",
"@types/lodash": "4.14.200",
"@types/supertest": "2.0.14",
"@types/uuid": "9.0.5",
"deepmerge": "4.3.1",
"express": "4.18.2",
"luxon": "^3.4.3",
"msw": "1.3.2",
"opener": "1.5.2",
"supertest": "6.3.3"
},
"files": [
"dist",
"dist-dynamic/*.*",
"dist-dynamic/dist/**",
"dist-dynamic/alpha/*",
"config.d.ts"
],
"configSchema": "config.d.ts",
Expand Down
24 changes: 24 additions & 0 deletions plugins/keycloak-backend/src/alpha.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2023 The Janus IDP 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.
*/

/**
* The Keycloak backend plugin integrates Keycloak into Backstage
*
* @packageDocumentation
*/

export * from './module';
export { default } from './module';
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright 2023 The Janus IDP 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 { TaskScheduleDefinition } from '@backstage/backend-tasks';
import { mockServices, startTestBackend } from '@backstage/backend-test-utils';
import { EntityProvider } from '@backstage/plugin-catalog-node';
import { catalogProcessingExtensionPoint } from '@backstage/plugin-catalog-node/alpha';

import { Duration } from 'luxon';

import { catalogModuleKeycloakEntityProvider } from './catalogModuleKeycloakEntityProvider';

describe('catalogModuleKeycloakEntityProvider', () => {
it('should register provider at the catalog extension point', async () => {
let addedProviders: Array<EntityProvider> | undefined;
let usedSchedule: TaskScheduleDefinition | undefined;

const extensionPoint = {
addEntityProvider: (providers: any) => {
addedProviders = providers;
},
};
const runner = jest.fn();
const scheduler = mockServices.scheduler.mock({
createScheduledTaskRunner(schedule) {
usedSchedule = schedule;
return { run: runner };
},
});

const config = {
catalog: {
providers: {
keycloakOrg: {
default: {
baseUrl: 'https://example.com/auth',
schedule: {
frequency: 'P1M',
timeout: 'PT3M',
},
},
},
},
},
};

await startTestBackend({
extensionPoints: [[catalogProcessingExtensionPoint, extensionPoint]],
features: [
catalogModuleKeycloakEntityProvider(),
mockServices.rootConfig.factory({ data: config }),
scheduler.factory,
],
});

expect(usedSchedule?.frequency).toEqual(Duration.fromISO('P1M'));
expect(usedSchedule?.timeout).toEqual(Duration.fromISO('PT3M'));
expect(addedProviders?.length).toEqual(1);
expect(addedProviders?.pop()?.getProviderName()).toEqual(
'KeycloakOrgEntityProvider:default',
);
expect(runner).not.toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2023 The Janus IDP 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,
createBackendModule,
} from '@backstage/backend-plugin-api';
import { catalogProcessingExtensionPoint } from '@backstage/plugin-catalog-node/alpha';

import { KeycloakOrgEntityProvider } from '../providers';

/**
* Registers the `KeycloakEntityProvider` with the catalog processing extension point.
*
* @alpha
*/
export const catalogModuleKeycloakEntityProvider = createBackendModule({
pluginId: 'catalog',
moduleId: 'keycloakEntityProvider',
register(env) {
env.registerInit({
deps: {
catalog: catalogProcessingExtensionPoint,
config: coreServices.rootConfig,
logger: coreServices.logger,
scheduler: coreServices.scheduler,
},
async init({ catalog, config, logger, scheduler }) {
catalog.addEntityProvider(
KeycloakOrgEntityProvider.fromConfig(config, {
id: 'development',
logger: loggerToWinstonLogger(logger),
scheduler,
}),
);
},
});
},
});
17 changes: 17 additions & 0 deletions plugins/keycloak-backend/src/module/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright 2023 The Janus IDP 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 { catalogModuleKeycloakEntityProvider as default } from './catalogModuleKeycloakEntityProvider';
Loading

0 comments on commit 5d89751

Please sign in to comment.