From 2d831012fce1c44a8a3e078856ba7d9e28cb4b34 Mon Sep 17 00:00:00 2001 From: blam Date: Thu, 12 Jan 2023 17:38:53 +0100 Subject: [PATCH] chore: fixing auth proxy login Signed-off-by: blam --- .../uffizzi.production.app-config.yaml | 3 + packages/app/src/AuthProxyDiscoveryApi.ts | 49 ++++++++++++++ packages/app/src/UrlPatternDiscovery.ts | 67 ------------------- packages/app/src/apis.ts | 7 +- 4 files changed, 54 insertions(+), 72 deletions(-) create mode 100644 packages/app/src/AuthProxyDiscoveryApi.ts delete mode 100644 packages/app/src/UrlPatternDiscovery.ts diff --git a/.github/uffizzi/uffizzi.production.app-config.yaml b/.github/uffizzi/uffizzi.production.app-config.yaml index 2e45740875..d4b70641da 100644 --- a/.github/uffizzi/uffizzi.production.app-config.yaml +++ b/.github/uffizzi/uffizzi.production.app-config.yaml @@ -30,6 +30,9 @@ backend: auth: environment: production providers: {} + proxy: + enabled: true + url: https://demo.backstage.io/api/auth catalog: locations: diff --git a/packages/app/src/AuthProxyDiscoveryApi.ts b/packages/app/src/AuthProxyDiscoveryApi.ts new file mode 100644 index 0000000000..0f29dc2c82 --- /dev/null +++ b/packages/app/src/AuthProxyDiscoveryApi.ts @@ -0,0 +1,49 @@ +/* + * 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. + */ + +import { Config } from '@backstage/config'; +import { UrlPatternDiscovery } from '@backstage/core-app-api'; +import { DiscoveryApi as IDiscoveryApi } from '@backstage/core-plugin-api'; + +export class AuthProxyDiscoveryApi implements IDiscoveryApi { + private urlPatternDiscovery: UrlPatternDiscovery; + + constructor( + baseUrl: string, + private readonly isAuthProxyingEnabled?: boolean, + private readonly authProxyUrl?: string, + ) { + this.urlPatternDiscovery = UrlPatternDiscovery.compile( + `${baseUrl}/api/{{ pluginId }}`, + ); + } + + async getBaseUrl(pluginId: string) { + if (this.isAuthProxyingEnabled && this.authProxyUrl) { + return this.authProxyUrl; + } + + return this.urlPatternDiscovery.getBaseUrl(pluginId); + } + + static fromConfig(config: Config) { + return new AuthProxyDiscoveryApi( + config.getString('backend.baseUrl'), + config.getOptionalBoolean('auth.proxy.enabled'), + config.getOptionalString('auth.proxy.url'), + ); + } +} diff --git a/packages/app/src/UrlPatternDiscovery.ts b/packages/app/src/UrlPatternDiscovery.ts deleted file mode 100644 index 8e352c7432..0000000000 --- a/packages/app/src/UrlPatternDiscovery.ts +++ /dev/null @@ -1,67 +0,0 @@ -/* - * 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. - */ - -import { DiscoveryApi } from '@backstage/core-plugin-api'; - -const ERROR_PREFIX = 'Invalid discovery URL pattern,'; - -/** - * UrlPatternDiscovery is a lightweight DiscoveryApi implementation. - * It uses a single template string to construct URLs for each plugin. - * - * @public - */ -export class UrlPatternDiscovery implements DiscoveryApi { - /** - * Creates a new UrlPatternDiscovery given a template. The the only - * interpolation done for the template is to replace instances of `{{pluginId}}` - * with the ID of the plugin being requested. - * - * Example pattern: `http://localhost:7007/api/{{ pluginId }}` - */ - static compile(pattern: string): UrlPatternDiscovery { - const parts = pattern.split(/\{\{\s*pluginId\s*\}\}/); - const urlStr = parts.join('pluginId'); - - let url; - try { - url = new URL(urlStr); - } catch { - throw new Error(`${ERROR_PREFIX} URL '${urlStr}' is invalid`); - } - if (url.hash) { - throw new Error(`${ERROR_PREFIX} URL must not have a hash`); - } - if (url.search) { - throw new Error(`${ERROR_PREFIX} URL must not have a query`); - } - if (urlStr.endsWith('/')) { - throw new Error(`${ERROR_PREFIX} URL must not end with a slash`); - } - - return new UrlPatternDiscovery(parts); - } - - private constructor(private readonly parts: string[]) {} - - async getBaseUrl(pluginId: string): Promise { - if (pluginId === 'auth') { - return 'https://demo.backstage.io/api/auth'; - } - - return this.parts.join(pluginId); - } -} diff --git a/packages/app/src/apis.ts b/packages/app/src/apis.ts index d080c980af..f348d24466 100644 --- a/packages/app/src/apis.ts +++ b/packages/app/src/apis.ts @@ -35,16 +35,13 @@ import { errorApiRef, githubAuthApiRef, } from '@backstage/core-plugin-api'; -import { UrlPatternDiscovery } from './UrlPatternDiscovery'; +import { AuthProxyDiscoveryApi } from './AuthProxyDiscoveryApi'; export const apis: AnyApiFactory[] = [ createApiFactory({ api: discoveryApiRef, deps: { configApi: configApiRef }, - factory: ({ configApi }) => - UrlPatternDiscovery.compile( - `${configApi.getString('backend.baseUrl')}/api/{{ pluginId }}`, - ), + factory: ({ configApi }) => AuthProxyDiscoveryApi.fromConfig(configApi), }), createApiFactory({ api: scmIntegrationsApiRef,