From 6c59abc74e2148c9e794e990cfd298528fbbc821 Mon Sep 17 00:00:00 2001 From: Eric Peterson Date: Wed, 19 Jan 2022 10:31:05 +0100 Subject: [PATCH] Revert app manager protections and let identity API hang. Signed-off-by: Eric Peterson --- .../IdentityApi/AppIdentityProxy.ts | 16 +-- .../src/apis/system/ApiProvider.tsx | 18 --- .../core-app-api/src/app/AppManager.test.tsx | 105 ------------------ packages/core-app-api/src/app/AppManager.tsx | 25 +---- 4 files changed, 10 insertions(+), 154 deletions(-) diff --git a/packages/core-app-api/src/apis/implementations/IdentityApi/AppIdentityProxy.ts b/packages/core-app-api/src/apis/implementations/IdentityApi/AppIdentityProxy.ts index cf69ead361..3ef3a1f6d3 100644 --- a/packages/core-app-api/src/apis/implementations/IdentityApi/AppIdentityProxy.ts +++ b/packages/core-app-api/src/apis/implementations/IdentityApi/AppIdentityProxy.ts @@ -46,12 +46,12 @@ type CompatibilityIdentityApi = IdentityApi & { * and sign-in page. */ export class AppIdentityProxy implements IdentityApi { -private target?: CompatibilityIdentityApi; -private waitForTarget: Promise; - private resolveTarget: (api: IdentityApi) => void = () => {}; + private target?: CompatibilityIdentityApi; + private waitForTarget: Promise; + private resolveTarget: (api: CompatibilityIdentityApi) => void = () => {}; constructor() { - this.waitForTarget = new Promise(resolve => { + this.waitForTarget = new Promise(resolve => { this.resolveTarget = resolve; }); } @@ -89,7 +89,9 @@ private waitForTarget: Promise; } async getBackstageIdentity(): Promise { - const identity = await this.waitForTarget.then(target => target.getBackstageIdentity()); + const identity = await this.waitForTarget.then(target => + target.getBackstageIdentity(), + ); if (!identity.userEntityRef.match(/^.*:.*\/.*$/)) { // eslint-disable-next-line no-console console.warn( @@ -106,12 +108,12 @@ private waitForTarget: Promise; } async getIdToken(): Promise { - return this.waitForTarget.then((target: CompatibilityIdentityApi) => { + return this.waitForTarget.then(target => { if (!target.getIdToken) { throw new Error('IdentityApi does not implement getIdToken'); } logDeprecation('getIdToken'); - return target.getIdToken() + return target.getIdToken(); }); } diff --git a/packages/core-app-api/src/apis/system/ApiProvider.tsx b/packages/core-app-api/src/apis/system/ApiProvider.tsx index 681c741afb..c75f883a51 100644 --- a/packages/core-app-api/src/apis/system/ApiProvider.tsx +++ b/packages/core-app-api/src/apis/system/ApiProvider.tsx @@ -53,24 +53,6 @@ export const ApiProvider = (props: PropsWithChildren) => { ); }; -/** - * Same as above, but does not inherit APIs from API providers further up in - * the react tree. - * - * Private to (and only used in) this package. - */ -export const IsolatedApiProvider = ( - props: PropsWithChildren, -) => { - const { apis, children } = props; - return ( - - ); -}; - ApiProvider.propTypes = { apis: PropTypes.shape({ get: PropTypes.func.isRequired }).isRequired, children: PropTypes.node, diff --git a/packages/core-app-api/src/app/AppManager.test.tsx b/packages/core-app-api/src/app/AppManager.test.tsx index 718a90dabe..467145acfc 100644 --- a/packages/core-app-api/src/app/AppManager.test.tsx +++ b/packages/core-app-api/src/app/AppManager.test.tsx @@ -34,9 +34,6 @@ import { createSubRouteRef, createRoutableExtension, analyticsApiRef, - useApi, - identityApiRef, - fetchApiRef, } from '@backstage/core-plugin-api'; import { AppManager } from './AppManager'; import { AppComponents, AppIcons } from './types'; @@ -572,106 +569,4 @@ describe('Integration Test', () => { ), ]); }); - - it('explodes if identityApi is directly used in a given SignInPage', async () => { - // Sign-in page that uses APIs it oughtn't to directly! - const OffendingSignInPage = () => { - try { - useApi(identityApiRef); - return <>No problem.; - } catch (e) { - return <>{(e as any).message}; - } - }; - - const app = new AppManager({ - themes: [ - { - id: 'light', - title: 'Light Theme', - variant: 'light', - Provider: ({ children }) => <>{children}, - }, - ], - icons, - components: { - ...components, - SignInPage: OffendingSignInPage, - }, - configLoader: async () => [], - }); - - const Provider = app.getProvider(); - const Router = app.getRouter(); - const { getByText } = await renderWithEffects( - - - - } /> - - - , - ); - - expect( - getByText('No implementation available for apiRef{core.identity}'), - ).toBeInTheDocument(); - }); - - it('explodes if identityApi is indirectly used in a given SignInPage', async () => { - // Set up fetchApi with a dependency on identityApi. - const apis = [ - createApiFactory({ - api: fetchApiRef, - deps: { identityApiRef }, - factory: () => ({} as any), - }), - ]; - - // Sign-in page that uses APIs it oughtn't to, but indirectly! - const OffendingSignInPage = () => { - try { - useApi(fetchApiRef); - return <>No problem.; - } catch (e) { - return <>{(e as any).message}; - } - }; - - const app = new AppManager({ - apis, - themes: [ - { - id: 'light', - title: 'Light Theme', - variant: 'light', - Provider: ({ children }) => <>{children}, - }, - ], - icons, - components: { - ...components, - SignInPage: OffendingSignInPage, - }, - configLoader: async () => [], - }); - - const Provider = app.getProvider(); - const Router = app.getRouter(); - const { getByText } = await renderWithEffects( - - - - } /> - - - , - ); - - expect( - getByText( - 'No API factory available for dependency apiRef{core.identity} of dependent apiRef{core.fetch}', - ), - ).toBeInTheDocument(); - }); }); diff --git a/packages/core-app-api/src/app/AppManager.tsx b/packages/core-app-api/src/app/AppManager.tsx index 484a63396d..0262ece8f4 100644 --- a/packages/core-app-api/src/app/AppManager.tsx +++ b/packages/core-app-api/src/app/AppManager.tsx @@ -79,7 +79,6 @@ import { AppThemeProvider } from './AppThemeProvider'; import { defaultConfigLoader } from './defaultConfigLoader'; import { ApiRegistry } from '../apis/system/ApiRegistry'; import { resolveRouteBindings } from './resolveRouteBindings'; -import { IsolatedApiProvider } from '../apis/system/ApiProvider'; type CompatiblePlugin = | BackstagePlugin @@ -336,14 +335,7 @@ export class AppManager implements BackstageApp { const [identityApi, setIdentityApi] = useState(); if (!identityApi) { - // Encapsulate the sign in page component in an API provider which - // contains all APIs except the identity API. Provides fast feedback in - // case the identity API is accidentally required on the sign-in page. - return ( - - - - ); + return ; } this.appIdentityProxy.setTarget(identityApi); @@ -481,21 +473,6 @@ export class AppManager implements BackstageApp { return this.apiHolder; } - private getIdentitylessApiHolder() { - const registrySansIdentity = new ApiFactoryRegistry(); - - // Assume all APIs in the current registry are valid. - this.apiFactoryRegistry.getAllApis().forEach(apiRef => { - const factory = this.apiFactoryRegistry.get(apiRef); - // Add all API factories except identity! - if (factory && apiRef !== identityApiRef) { - registrySansIdentity.register('default', factory); - } - }); - - return new ApiResolver(registrySansIdentity); - } - private verifyPlugins(plugins: Iterable) { const pluginIds = new Set();