Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(app): dynamic authentication provider support (release-1.4) #2217

Open
wants to merge 1 commit into
base: release-1.4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 39 additions & 8 deletions packages/app/src/components/DynamicRoot/DynamicRoot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import React, { useCallback, useEffect, useRef, useState } from 'react';

import { createApp } from '@backstage/app-defaults';
import { BackstageApp } from '@backstage/core-app-api';
import { AnyApiFactory, BackstagePlugin } from '@backstage/core-plugin-api';
import {
AnyApiFactory,
AppComponents,
BackstagePlugin,
} from '@backstage/core-plugin-api';

import { useThemes } from '@redhat-developer/red-hat-developer-hub-theme';
import { AppsConfig } from '@scalprum/core';
Expand Down Expand Up @@ -61,7 +65,9 @@ export const DynamicRoot = ({
React.ComponentType | undefined
>(undefined);
// registry of remote components loaded at bootstrap
const [components, setComponents] = useState<ComponentRegistry | undefined>();
const [componentRegistry, setComponentRegistry] = useState<
ComponentRegistry | undefined
>();
const { initialized, pluginStore, api: scalprumApi } = useScalprum();

const themes = useThemes();
Expand All @@ -72,6 +78,7 @@ export const DynamicRoot = ({
pluginModules,
apiFactories,
appIcons,
components,
dynamicRoutes,
menuItems,
entityTabs,
Expand All @@ -86,6 +93,10 @@ export const DynamicRoot = ({
scope,
module,
})),
...components.map(({ scope, module }) => ({
scope,
module,
})),
...routeBindingTargets.map(({ scope, module }) => ({
scope,
module,
Expand Down Expand Up @@ -172,6 +183,23 @@ export const DynamicRoot = ({
),
);

const appComponents = components.reduce<Partial<AppComponents>>(
(componentMap, { scope, module, importName, name }) => {
if (typeof allPlugins[scope]?.[module]?.[importName] !== 'undefined') {
componentMap[name] = allPlugins[scope]?.[module]?.[
importName
] as React.ComponentType<any>;
} else {
// eslint-disable-next-line no-console
console.warn(
`Plugin ${scope} is not configured properly: ${module}.${importName} not found, ignoring AppComponent: ${name}`,
);
}
return componentMap;
},
{},
);

let icons = Object.fromEntries(
appIcons.reduce<[string, React.ComponentType<{}>][]>(
(acc, { scope, module, importName, name }) => {
Expand Down Expand Up @@ -408,7 +436,10 @@ export const DynamicRoot = ({
...remoteBackstagePlugins,
],
themes: [...filteredStaticThemes, ...dynamicThemeProviders],
components: defaultAppComponents,
components: {
...defaultAppComponents,
...appComponents,
} as Partial<AppComponents>,
});
}

Expand All @@ -424,7 +455,7 @@ export const DynamicRoot = ({
scaffolderFieldExtensionComponents;

// make the dynamic UI configuration available to DynamicRootContext consumers
setComponents({
setComponentRegistry({
AppProvider: app.current.getProvider(),
AppRouter: app.current.getRouter(),
dynamicRoutes: dynamicRoutesComponents,
Expand All @@ -449,17 +480,17 @@ export const DynamicRoot = ({
]);

useEffect(() => {
if (initialized && !components) {
if (initialized && !componentRegistry) {
initializeRemoteModules();
}
}, [initialized, components, initializeRemoteModules]);
}, [initialized, componentRegistry, initializeRemoteModules]);

if (!initialized || !components) {
if (!initialized || !componentRegistry) {
return <Loader />;
}

return (
<DynamicRootContext.Provider value={components}>
<DynamicRootContext.Provider value={componentRegistry}>
{ChildComponent ? <ChildComponent /> : <Loader />}
</DynamicRootContext.Provider>
);
Expand Down
16 changes: 16 additions & 0 deletions packages/app/src/utils/dynamicUI/extractDynamicConfig.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ describe('extractDynamicConfig', () => {
const config = extractDynamicConfig(source as DynamicPluginConfig);
expect(config).toEqual({
pluginModules: [],
components: [],
routeBindings: [],
dynamicRoutes: [],
entityTabs: [],
Expand All @@ -162,6 +163,20 @@ describe('extractDynamicConfig', () => {
});

it.each([
[
'a component',
{ components: [{ name: 'foo', importName: 'blah' }] },
{
components: [
{
importName: 'blah',
module: 'PluginRoot',
name: 'foo',
scope: 'janus-idp.plugin-foo',
},
],
},
],
[
'a dynamicRoute',
{ dynamicRoutes: [{ path: '/foo' }] },
Expand Down Expand Up @@ -506,6 +521,7 @@ describe('extractDynamicConfig', () => {
scope: 'janus-idp.plugin-foo',
},
],
components: [],
routeBindings: [],
routeBindingTargets: [],
dynamicRoutes: [],
Expand Down
27 changes: 26 additions & 1 deletion packages/app/src/utils/dynamicUI/extractDynamicConfig.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Entity } from '@backstage/catalog-model';
import { ApiHolder } from '@backstage/core-plugin-api';
import { ApiHolder, AppComponents } from '@backstage/core-plugin-api';
import { isKind } from '@backstage/plugin-catalog';

import { hasAnnotation, isType } from '../../components/catalog/utils';
Expand Down Expand Up @@ -116,8 +116,17 @@ type ThemeEntry = {
importName: string;
};

type ComponentEntry = {
scope: string;
module: string;
id: string;
importName: string;
name: keyof AppComponents;
};

type CustomProperties = {
pluginModule?: string;
components: ComponentEntry[];
dynamicRoutes?: (DynamicModuleEntry & {
importName?: string;
module?: string;
Expand Down Expand Up @@ -149,6 +158,7 @@ type DynamicConfig = {
pluginModules: PluginModule[];
apiFactories: ApiFactory[];
appIcons: AppIcon[];
components: ComponentEntry[];
dynamicRoutes: DynamicRoute[];
menuItems: MenuItem[];
entityTabs: EntityTabEntry[];
Expand All @@ -171,6 +181,7 @@ function extractDynamicConfig(
pluginModules: [],
apiFactories: [],
appIcons: [],
components: [],
dynamicRoutes: [],
menuItems: [],
entityTabs: [],
Expand All @@ -190,6 +201,20 @@ function extractDynamicConfig(
},
[],
);
config.components = Object.entries(frontend).reduce<ComponentEntry[]>(
(pluginSet, [scope, customProperties]) => {
pluginSet.push(
...(customProperties.components ?? []).map(component => ({
...component,
module: component.module ?? 'PluginRoot',
importName: component.importName ?? 'default',
scope,
})),
);
return pluginSet;
},
[],
);
config.dynamicRoutes = Object.entries(frontend).reduce<DynamicRoute[]>(
(pluginSet, [scope, customProperties]) => {
pluginSet.push(
Expand Down
6 changes: 5 additions & 1 deletion packages/backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,11 @@ backend.add(rbacDynamicPluginsProvider);

backend.add(import('@backstage/plugin-auth-backend'));
backend.add(import('@backstage/plugin-auth-backend-module-guest-provider'));
backend.add(import('./modules/authProvidersModule'));
if (process.env.ENABLE_AUTH_PROVIDER_MODULE_OVERRIDE !== 'true') {
backend.add(import('./modules/authProvidersModule'));
} else {
staticLogger.info(`Default authentication provider module disabled`);
}

backend.add(import('@internal/plugin-dynamic-plugins-info-backend'));
backend.add(import('@internal/plugin-scalprum-backend'));
Expand Down
Loading