consume config through options

Signed-off-by: Raghunandan Balachandran <raghunandan@spotify.com>
This commit is contained in:
Raghunandan Balachandran
2025-08-12 16:29:18 +02:00
parent 02045fa299
commit c014ad3166
4 changed files with 20 additions and 21 deletions
+1 -4
View File
@@ -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<KubernetesCredential>;
// (undocumented)
@@ -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' });
@@ -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',
);
}
@@ -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(),