Skip to content

Commit 0aee44e

Browse files
authored
Merge pull request #217 from brendandburns/auth
Fix exec.
2 parents cc5cbf8 + 3a8403e commit 0aee44e

File tree

5 files changed

+62
-14
lines changed

5 files changed

+62
-14
lines changed

src/cloud_auth.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ interface Config {
1919
}
2020
export class CloudAuth implements Authenticator {
2121
public isAuthProvider(user: User): boolean {
22+
if (!user || !user.authProvider) {
23+
return false;
24+
}
2225
return user.authProvider.name === 'azure' || user.authProvider.name === 'gcp';
2326
}
2427

src/config.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -276,13 +276,11 @@ export class KubeConfig {
276276
}
277277
let token: string | null = null;
278278

279-
if (user.authProvider && user.authProvider.config) {
280-
KubeConfig.authenticators.forEach((authenticator: Authenticator) => {
281-
if (authenticator.isAuthProvider(user)) {
282-
token = authenticator.getToken(user);
283-
}
284-
});
285-
}
279+
KubeConfig.authenticators.forEach((authenticator: Authenticator) => {
280+
if (authenticator.isAuthProvider(user)) {
281+
token = authenticator.getToken(user);
282+
}
283+
});
286284

287285
if (user.token) {
288286
token = 'Bearer ' + user.token;

src/config_test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,33 @@ describe('KubeConfig', () => {
816816
expect(opts.headers.Authorization).to.equal(`Bearer ${token}`);
817817
}
818818
});
819+
it('should exec with exec auth (other location)', () => {
820+
const config = new KubeConfig();
821+
const token = 'token';
822+
const responseStr = `'{
823+
"apiVersion": "client.authentication.k8s.io/v1beta1",
824+
"kind": "ExecCredential",
825+
"status": {
826+
"token": "${token}"
827+
}
828+
}'`;
829+
config.loadFromClusterAndUser(
830+
{ skipTLSVerify: false } as Cluster,
831+
{
832+
exec: {
833+
command: 'echo',
834+
args: [`${responseStr}`],
835+
},
836+
} as User,
837+
);
838+
// TODO: inject the exec command here?
839+
const opts = {} as requestlib.Options;
840+
config.applyToRequest(opts);
841+
expect(opts.headers).to.not.be.undefined;
842+
if (opts.headers) {
843+
expect(opts.headers.Authorization).to.equal(`Bearer ${token}`);
844+
}
845+
});
819846
it('should throw with no command.', () => {
820847
const config = new KubeConfig();
821848
config.loadFromClusterAndUser(

src/config_types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as fs from 'fs';
22
import * as u from 'underscore';
3+
import { ExtensionsV1beta1RollbackConfig } from './api';
34

45
export interface Cluster {
56
readonly name: string;
@@ -38,6 +39,7 @@ export interface User {
3839
readonly name: string;
3940
readonly certData?: string;
4041
readonly certFile?: string;
42+
readonly exec?: any;
4143
readonly keyData?: string;
4244
readonly keyFile?: string;
4345
readonly authProvider?: any;

src/exec_auth.ts

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ export class ExecAuth implements Authenticator {
77
private readonly tokenCache: { [key: string]: any } = {};
88

99
public isAuthProvider(user: User) {
10+
if (!user) {
11+
return false;
12+
}
13+
if (user.exec) {
14+
return true;
15+
}
16+
if (!user.authProvider) {
17+
return false;
18+
}
1019
return (
1120
user.authProvider.name === 'exec' || (user.authProvider.config && user.authProvider.config.exec)
1221
);
@@ -25,18 +34,27 @@ export class ExecAuth implements Authenticator {
2534
}
2635
this.tokenCache[user.name] = null;
2736
}
28-
const config = user.authProvider.config;
29-
if (!config.exec.command) {
37+
let exec: any = null;
38+
if (user.authProvider && user.authProvider.config) {
39+
exec = user.authProvider.config.exec;
40+
}
41+
if (user.exec) {
42+
exec = user.exec;
43+
}
44+
if (!exec) {
45+
return null;
46+
}
47+
if (!exec.command) {
3048
throw new Error('No command was specified for exec authProvider!');
3149
}
32-
let cmd = config.exec.command;
33-
if (config.exec.args) {
34-
cmd = `${cmd} ${config.exec.args.join(' ')}`;
50+
let cmd = exec.command;
51+
if (exec.args) {
52+
cmd = `${cmd} ${exec.args.join(' ')}`;
3553
}
3654
let opts: shell.ExecOpts;
37-
if (config.exec.env) {
55+
if (exec.env) {
3856
const env = {};
39-
config.exec.env.forEach((elt) => (env[elt.name] = elt.value));
57+
exec.env.forEach((elt) => (env[elt.name] = elt.value));
4058
opts = { env };
4159
}
4260
const result = shell.exec(cmd, opts);

0 commit comments

Comments
 (0)