Skip to content

Commit 6c3c488

Browse files
committed
Add the ability to inject authenticators.
1 parent 0a9e601 commit 6c3c488

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

src/config.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ export class KubeConfig implements SecurityAuthentication {
6565
new OpenIDConnectAuth(),
6666
];
6767

68+
// Optionally add additional external authenticators, you must do this
69+
// before you load a kubeconfig file that references them.
70+
public static addAuthenticator(authenticator: Authenticator): void {
71+
this.authenticators.push(authenticator);
72+
}
73+
6874
/**
6975
* The list of all known clusters
7076
*/

src/config_test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { mock } from 'node:test';
99

1010
import mockfs from 'mock-fs';
1111

12+
import { Authenticator } from './auth.js';
1213
import { Headers } from 'node-fetch';
1314
import { HttpMethod } from './index.js';
1415
import { assertRequestAgentsEqual, assertRequestOptionsEqual } from './test/match-buffer.js';
@@ -1703,5 +1704,45 @@ describe('KubeConfig', () => {
17031704
}
17041705
validateFileLoad(kc);
17051706
});
1707+
1708+
it('should inject a custom Authenticator', async () => {
1709+
class CustomAuthenticator implements Authenticator {
1710+
public isAuthProvider(user: User): boolean {
1711+
return user.authProvider === 'custom';
1712+
}
1713+
1714+
public async applyAuthentication(user: User, opts: RequestOptions): Promise<void> {
1715+
if (user.authProvider === 'custom') {
1716+
// Simulate token retrieval
1717+
const token = 'test-token';
1718+
opts.headers = opts.headers || {};
1719+
opts.headers.Authorization = `Bearer ${token}`;
1720+
} else {
1721+
throw new Error('No custom configuration found');
1722+
}
1723+
}
1724+
}
1725+
1726+
const customAuthenticator = new CustomAuthenticator();
1727+
KubeConfig.addAuthenticator(customAuthenticator);
1728+
const kc = new KubeConfig();
1729+
1730+
const cluster: Cluster = {
1731+
name: 'test-cluster',
1732+
server: 'https://localhost:6443',
1733+
skipTLSVerify: false,
1734+
};
1735+
const user: User = {
1736+
name: 'test-user',
1737+
authProvider: 'custom',
1738+
};
1739+
1740+
kc.loadFromClusterAndUser(cluster, user);
1741+
1742+
const opts: RequestOptions = {};
1743+
await kc.applyToHTTPSOptions(opts);
1744+
1745+
strictEqual(opts.headers!.Authorization, 'Bearer test-token');
1746+
});
17061747
});
17071748
});

0 commit comments

Comments
 (0)