diff --git a/plugins/kubernetes-backend/src/kubernetes-auth-translator/DispatchingKubernetesAuthTranslator.test.ts b/plugins/kubernetes-backend/src/kubernetes-auth-translator/DispatchingKubernetesAuthTranslator.test.ts new file mode 100644 index 0000000000..02b60a031b --- /dev/null +++ b/plugins/kubernetes-backend/src/kubernetes-auth-translator/DispatchingKubernetesAuthTranslator.test.ts @@ -0,0 +1,72 @@ +/* + * 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 { DispatchingKubernetesAuthTranslator } from './DispatchingKubernetesAuthTranslator'; +import { ClusterDetails } from '../types'; +import { KubernetesRequestAuth } from '@backstage/plugin-kubernetes-common'; +import { KubernetesAuthTranslator } from './types'; + +describe('decorateClusterDetailsWithAuth', () => { + let authTranslator: DispatchingKubernetesAuthTranslator; + let mockTranslator: jest.Mocked; + const authObject: KubernetesRequestAuth = {}; + + beforeEach(() => { + mockTranslator = { decorateClusterDetailsWithAuth: jest.fn() }; + authTranslator = new DispatchingKubernetesAuthTranslator({ + authTranslatorMap: { google: mockTranslator }, + }); + }); + + it('can decorate cluster details if the auth provider is in the translator map', () => { + const expectedClusterDetails: ClusterDetails = { + url: 'notanything.com', + name: 'randomName', + authProvider: 'google', + serviceAccountToken: 'added by mock translator', + }; + + mockTranslator.decorateClusterDetailsWithAuth.mockReturnValue( + expectedClusterDetails as unknown as Promise, + ); + + const returnedValue = authTranslator.decorateClusterDetailsWithAuth( + { name: 'googleCluster', url: 'anything.com', authProvider: 'google' }, + authObject, + ); + + expect(mockTranslator.decorateClusterDetailsWithAuth).toHaveBeenCalledWith( + { name: 'googleCluster', url: 'anything.com', authProvider: 'google' }, + authObject, + ); + expect(returnedValue).toBe(expectedClusterDetails); + }); + + it('throws an error when asked for an auth translator for an unsupported auth type', () => { + expect(() => + authTranslator.decorateClusterDetailsWithAuth( + { + name: 'test-cluster', + url: 'anything.com', + authProvider: 'linode', + }, + authObject, + ), + ).toThrow( + 'authProvider "linode" has no KubernetesAuthTranslator associated with it', + ); + }); +}); diff --git a/plugins/kubernetes-backend/src/kubernetes-auth-translator/DispatchingKubernetesAuthTranslator.ts b/plugins/kubernetes-backend/src/kubernetes-auth-translator/DispatchingKubernetesAuthTranslator.ts new file mode 100644 index 0000000000..0290bc4c52 --- /dev/null +++ b/plugins/kubernetes-backend/src/kubernetes-auth-translator/DispatchingKubernetesAuthTranslator.ts @@ -0,0 +1,56 @@ +/* + * 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 { KubernetesAuthTranslator } from './types'; +import { ClusterDetails } from '../types'; +import { KubernetesRequestAuth } from '@backstage/plugin-kubernetes-common'; + +/** + * + * @public + */ +export type KubernetesAuthTranslatorGeneratorOptions = { + authTranslatorMap: { + [key: string]: KubernetesAuthTranslator; + }; +}; +/** + * used to direct a KubernetesAuthProvider to its corresponding KubernetesAuthTranslator + * @public + */ +export class DispatchingKubernetesAuthTranslator + implements KubernetesAuthTranslator +{ + private readonly translatorMap: { [key: string]: KubernetesAuthTranslator }; + + constructor(options: KubernetesAuthTranslatorGeneratorOptions) { + this.translatorMap = options.authTranslatorMap; + } + + public decorateClusterDetailsWithAuth( + clusterDetails: ClusterDetails, + auth: KubernetesRequestAuth, + ) { + if (this.translatorMap[clusterDetails.authProvider]) { + return this.translatorMap[ + clusterDetails.authProvider + ].decorateClusterDetailsWithAuth(clusterDetails, auth); + } + throw new Error( + `authProvider "${clusterDetails.authProvider}" has no KubernetesAuthTranslator associated with it`, + ); + } +} diff --git a/plugins/kubernetes-backend/src/kubernetes-auth-translator/KubernetesAuthTranslatorGenerator.test.ts b/plugins/kubernetes-backend/src/kubernetes-auth-translator/KubernetesAuthTranslatorGenerator.test.ts deleted file mode 100644 index f8abdde9cf..0000000000 --- a/plugins/kubernetes-backend/src/kubernetes-auth-translator/KubernetesAuthTranslatorGenerator.test.ts +++ /dev/null @@ -1,61 +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 { KubernetesAuthTranslator } from './types'; -import { GoogleKubernetesAuthTranslator } from './GoogleKubernetesAuthTranslator'; -import { KubernetesAuthTranslatorGenerator } from './KubernetesAuthTranslatorGenerator'; -import { NoopKubernetesAuthTranslator } from './NoopKubernetesAuthTranslator'; -import { AwsIamKubernetesAuthTranslator } from './AwsIamKubernetesAuthTranslator'; -import { OidcKubernetesAuthTranslator } from './OidcKubernetesAuthTranslator'; -import { getVoidLogger } from '@backstage/backend-common'; - -const logger = getVoidLogger(); - -describe('getKubernetesAuthTranslatorInstance', () => { - const sut = KubernetesAuthTranslatorGenerator; - - it('can return an auth translator for google auth', () => { - const authTranslator: KubernetesAuthTranslator = - sut.getKubernetesAuthTranslatorInstance('google', { logger }); - expect(authTranslator instanceof GoogleKubernetesAuthTranslator).toBe(true); - }); - - it('can return an auth translator for aws auth', () => { - const authTranslator: KubernetesAuthTranslator = - sut.getKubernetesAuthTranslatorInstance('aws', { logger }); - expect(authTranslator instanceof AwsIamKubernetesAuthTranslator).toBe(true); - }); - - it('can return an auth translator for serviceAccount auth', () => { - const authTranslator: KubernetesAuthTranslator = - sut.getKubernetesAuthTranslatorInstance('serviceAccount', { logger }); - expect(authTranslator instanceof NoopKubernetesAuthTranslator).toBe(true); - }); - - it('can return an auth translator for oidc auth', () => { - const authTranslator: KubernetesAuthTranslator = - sut.getKubernetesAuthTranslatorInstance('oidc', { logger }); - expect(authTranslator instanceof OidcKubernetesAuthTranslator).toBe(true); - }); - - it('throws an error when asked for an auth translator for an unsupported auth type', () => { - expect(() => - sut.getKubernetesAuthTranslatorInstance('linode', { logger }), - ).toThrow( - 'authProvider "linode" has no KubernetesAuthTranslator associated with it', - ); - }); -}); diff --git a/plugins/kubernetes-backend/src/kubernetes-auth-translator/KubernetesAuthTranslatorGenerator.ts b/plugins/kubernetes-backend/src/kubernetes-auth-translator/KubernetesAuthTranslatorGenerator.ts deleted file mode 100644 index f54e46efab..0000000000 --- a/plugins/kubernetes-backend/src/kubernetes-auth-translator/KubernetesAuthTranslatorGenerator.ts +++ /dev/null @@ -1,66 +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 { Logger } from 'winston'; -import { KubernetesAuthTranslator } from './types'; -import { GoogleKubernetesAuthTranslator } from './GoogleKubernetesAuthTranslator'; -import { NoopKubernetesAuthTranslator } from './NoopKubernetesAuthTranslator'; -import { AwsIamKubernetesAuthTranslator } from './AwsIamKubernetesAuthTranslator'; -import { GoogleServiceAccountAuthTranslator } from './GoogleServiceAccountAuthProvider'; -import { AzureIdentityKubernetesAuthTranslator } from './AzureIdentityKubernetesAuthTranslator'; -import { OidcKubernetesAuthTranslator } from './OidcKubernetesAuthTranslator'; - -/** - * - * @public - */ -export class KubernetesAuthTranslatorGenerator { - static getKubernetesAuthTranslatorInstance( - authProvider: string, - options: { - logger: Logger; - }, - ): KubernetesAuthTranslator { - switch (authProvider) { - case 'google': { - return new GoogleKubernetesAuthTranslator(); - } - case 'aws': { - return new AwsIamKubernetesAuthTranslator(); - } - case 'azure': { - return new AzureIdentityKubernetesAuthTranslator(options.logger); - } - case 'serviceAccount': { - return new NoopKubernetesAuthTranslator(); - } - case 'googleServiceAccount': { - return new GoogleServiceAccountAuthTranslator(); - } - case 'oidc': { - return new OidcKubernetesAuthTranslator(); - } - case 'localKubectlProxy': { - return new NoopKubernetesAuthTranslator(); - } - default: { - throw new Error( - `authProvider "${authProvider}" has no KubernetesAuthTranslator associated with it`, - ); - } - } - } -} diff --git a/plugins/kubernetes-backend/src/kubernetes-auth-translator/index.ts b/plugins/kubernetes-backend/src/kubernetes-auth-translator/index.ts index 19f6dd80eb..4ee83b0da0 100644 --- a/plugins/kubernetes-backend/src/kubernetes-auth-translator/index.ts +++ b/plugins/kubernetes-backend/src/kubernetes-auth-translator/index.ts @@ -18,7 +18,7 @@ export * from './AwsIamKubernetesAuthTranslator'; export * from './AzureIdentityKubernetesAuthTranslator'; export * from './GoogleKubernetesAuthTranslator'; export * from './GoogleServiceAccountAuthProvider'; -export * from './KubernetesAuthTranslatorGenerator'; +export * from './DispatchingKubernetesAuthTranslator'; export * from './NoopKubernetesAuthTranslator'; export * from './OidcKubernetesAuthTranslator'; export * from './types';