From c014ad31668f34d771c50e2a51beb702643f8978 Mon Sep 17 00:00:00 2001 From: Raghunandan Balachandran Date: Tue, 12 Aug 2025 16:29:18 +0200 Subject: [PATCH] consume config through options Signed-off-by: Raghunandan Balachandran --- plugins/kubernetes-backend/report.api.md | 5 +--- .../auth/GoogleServiceAccountStrategy.test.ts | 28 +++++++++---------- .../src/auth/GoogleServiceAccountStrategy.ts | 4 +-- .../src/service/KubernetesBuilder.ts | 4 ++- 4 files changed, 20 insertions(+), 21 deletions(-) diff --git a/plugins/kubernetes-backend/report.api.md b/plugins/kubernetes-backend/report.api.md index d16fc103a1..798d5d7d29 100644 --- a/plugins/kubernetes-backend/report.api.md +++ b/plugins/kubernetes-backend/report.api.md @@ -90,12 +90,9 @@ export type DispatchStrategyOptions = { }; }; -// @public @deprecated (undocumented) -export type FetchResponseWrapper = k8sAuthTypes.FetchResponseWrapper; - // @public export class GoogleServiceAccountStrategy implements AuthenticationStrategy { - constructor(config: Config); + constructor(opts: { config: Config }); // (undocumented) getCredential(): Promise; // (undocumented) diff --git a/plugins/kubernetes-backend/src/auth/GoogleServiceAccountStrategy.test.ts b/plugins/kubernetes-backend/src/auth/GoogleServiceAccountStrategy.test.ts index d70cba784d..34022556b8 100644 --- a/plugins/kubernetes-backend/src/auth/GoogleServiceAccountStrategy.test.ts +++ b/plugins/kubernetes-backend/src/auth/GoogleServiceAccountStrategy.test.ts @@ -52,7 +52,7 @@ describe('GoogleServiceAccountStrategy', () => { }, }); - const strategy = new GoogleServiceAccountStrategy(config); + const strategy = new GoogleServiceAccountStrategy({ config }); expect(strategy).toBeDefined(); }); @@ -61,14 +61,14 @@ describe('GoogleServiceAccountStrategy', () => { kubernetes: {}, }); - const strategy = new GoogleServiceAccountStrategy(config); + const strategy = new GoogleServiceAccountStrategy({ config }); expect(strategy).toBeDefined(); }); it('should work with empty config', () => { const config = new ConfigReader({}); - const strategy = new GoogleServiceAccountStrategy(config); + const strategy = new GoogleServiceAccountStrategy({ config }); expect(strategy).toBeDefined(); }); }); @@ -95,7 +95,7 @@ describe('GoogleServiceAccountStrategy', () => { mockGetAccessToken.mockResolvedValue('test-access-token'); - const strategy = new GoogleServiceAccountStrategy(config); + const strategy = new GoogleServiceAccountStrategy({ config }); const credential = await strategy.getCredential(); expect(MockedClusterManagerClient).toHaveBeenCalledWith({ @@ -115,7 +115,7 @@ describe('GoogleServiceAccountStrategy', () => { mockGetAccessToken.mockResolvedValue('default-access-token'); - const strategy = new GoogleServiceAccountStrategy(config); + const strategy = new GoogleServiceAccountStrategy({ config }); const credential = await strategy.getCredential(); expect(MockedClusterManagerClient).toHaveBeenCalledWith(); @@ -133,7 +133,7 @@ describe('GoogleServiceAccountStrategy', () => { }, }); - const strategy = new GoogleServiceAccountStrategy(config); + const strategy = new GoogleServiceAccountStrategy({ config }); await expect(strategy.getCredential()).rejects.toThrow( 'Failed to parse Google Service Account credentials from config', @@ -150,7 +150,7 @@ describe('GoogleServiceAccountStrategy', () => { mockGetAccessToken.mockResolvedValue(null); - const strategy = new GoogleServiceAccountStrategy(config); + const strategy = new GoogleServiceAccountStrategy({ config }); await expect(strategy.getCredential()).rejects.toThrow( 'Unable to obtain access token for Google Cloud authentication', @@ -167,7 +167,7 @@ describe('GoogleServiceAccountStrategy', () => { mockGetAccessToken.mockResolvedValue(undefined); - const strategy = new GoogleServiceAccountStrategy(config); + const strategy = new GoogleServiceAccountStrategy({ config }); await expect(strategy.getCredential()).rejects.toThrow( 'Unable to obtain access token for Google Cloud authentication', @@ -184,7 +184,7 @@ describe('GoogleServiceAccountStrategy', () => { mockGetAccessToken.mockResolvedValue(''); - const strategy = new GoogleServiceAccountStrategy(config); + const strategy = new GoogleServiceAccountStrategy({ config }); await expect(strategy.getCredential()).rejects.toThrow( 'Unable to obtain access token for Google Cloud authentication', @@ -201,7 +201,7 @@ describe('GoogleServiceAccountStrategy', () => { }, }); - const strategy = new GoogleServiceAccountStrategy(config); + const strategy = new GoogleServiceAccountStrategy({ config }); await expect(strategy.getCredential()).rejects.toThrow( /Failed to parse Google Service Account credentials from config: Unexpected token/, @@ -224,7 +224,7 @@ describe('GoogleServiceAccountStrategy', () => { throw new Error('Client creation failed'); }); - const strategy = new GoogleServiceAccountStrategy(config); + const strategy = new GoogleServiceAccountStrategy({ config }); await expect(strategy.getCredential()).rejects.toThrow( 'Client creation failed', @@ -238,7 +238,7 @@ describe('GoogleServiceAccountStrategy', () => { mockGetAccessToken.mockRejectedValue(new Error('Token fetch failed')); - const strategy = new GoogleServiceAccountStrategy(config); + const strategy = new GoogleServiceAccountStrategy({ config }); await expect(strategy.getCredential()).rejects.toThrow( 'Token fetch failed', @@ -252,7 +252,7 @@ describe('GoogleServiceAccountStrategy', () => { describe('#validateCluster', () => { it('should return empty array', () => { const config = new ConfigReader({}); - const strategy = new GoogleServiceAccountStrategy(config); + const strategy = new GoogleServiceAccountStrategy({ config }); const result = strategy.validateCluster(); @@ -263,7 +263,7 @@ describe('GoogleServiceAccountStrategy', () => { describe('#presentAuthMetadata', () => { it('should return empty object', () => { const config = new ConfigReader({}); - const strategy = new GoogleServiceAccountStrategy(config); + const strategy = new GoogleServiceAccountStrategy({ config }); const result = strategy.presentAuthMetadata({ test: 'metadata' }); diff --git a/plugins/kubernetes-backend/src/auth/GoogleServiceAccountStrategy.ts b/plugins/kubernetes-backend/src/auth/GoogleServiceAccountStrategy.ts index 2e2af018bc..6bb7f8d7ee 100644 --- a/plugins/kubernetes-backend/src/auth/GoogleServiceAccountStrategy.ts +++ b/plugins/kubernetes-backend/src/auth/GoogleServiceAccountStrategy.ts @@ -50,8 +50,8 @@ import { Config } from '@backstage/config'; export class GoogleServiceAccountStrategy implements AuthenticationStrategy { private readonly credentials?: string; - constructor(config: Config) { - this.credentials = config.getOptionalString( + constructor(opts: { config: Config }) { + this.credentials = opts.config.getOptionalString( 'kubernetes.googleServiceAccountCredentials', ); } diff --git a/plugins/kubernetes-backend/src/service/KubernetesBuilder.ts b/plugins/kubernetes-backend/src/service/KubernetesBuilder.ts index 9f87514eb4..2cf0bcf04c 100644 --- a/plugins/kubernetes-backend/src/service/KubernetesBuilder.ts +++ b/plugins/kubernetes-backend/src/service/KubernetesBuilder.ts @@ -462,7 +462,9 @@ export class KubernetesBuilder { aws: new AwsIamStrategy({ config: this.env.config }), azure: new AzureIdentityStrategy(this.env.logger), google: new GoogleStrategy(), - googleServiceAccount: new GoogleServiceAccountStrategy(this.env.config), + googleServiceAccount: new GoogleServiceAccountStrategy({ + config: this.env.config, + }), localKubectlProxy: new AnonymousStrategy(), oidc: new OidcStrategy(), serviceAccount: new ServiceAccountStrategy(),