Skip to content

Commit 08fc25a

Browse files
committed
Fix environment variable handling in exec auth.
1 parent e5ec945 commit 08fc25a

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ coverage/*
1010
examples/package-lock.json
1111
.nyc_output
1212
kubernetes-client-node-*.tgz
13+
**/*.swp
1314

src/exec_auth.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { User } from './config_types';
55

66
export class ExecAuth implements Authenticator {
77
private readonly tokenCache: { [key: string]: any } = {};
8+
private execFn: (cmd: string, opts: shell.ExecOpts) => shell.ShellReturnValue = shell.exec;
89

910
public isAuthProvider(user: User) {
1011
if (!user) {
@@ -53,11 +54,11 @@ export class ExecAuth implements Authenticator {
5354
}
5455
let opts: shell.ExecOpts;
5556
if (exec.env) {
56-
const env = {};
57+
const env = process.env;
5758
exec.env.forEach((elt) => (env[elt.name] = elt.value));
5859
opts = { env };
5960
}
60-
const result = shell.exec(cmd, opts);
61+
const result = this.execFn(cmd, opts);
6162
if (result.code === 0) {
6263
const obj = JSON.parse(result.stdout);
6364
this.tokenCache[user.name] = obj;

src/exec_auth_test.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { expect } from 'chai';
2+
import * as shell from 'shelljs';
3+
4+
import { ExecAuth } from './exec_auth';
5+
6+
describe('ExecAuth', () => {
7+
it('should correctly exec', async () => {
8+
const auth = new ExecAuth();
9+
(auth as any).execFn = (command: string, opts: shell.ExecOpts): shell.ShellReturnValue => {
10+
return {
11+
code: 0,
12+
stdout: JSON.stringify({ status: { token: 'foo' } }),
13+
} as shell.ShellReturnValue;
14+
};
15+
16+
const token = auth.getToken({
17+
name: 'user',
18+
authProvider: {
19+
config: {
20+
exec: {
21+
command: 'echo',
22+
},
23+
},
24+
},
25+
});
26+
expect(token).to.equal('Bearer foo');
27+
});
28+
29+
it('should exec with env vars', async () => {
30+
const auth = new ExecAuth();
31+
let optsOut: shell.ExecOpts = {};
32+
(auth as any).execFn = (command: string, opts: shell.ExecOpts): shell.ShellReturnValue => {
33+
optsOut = opts;
34+
return {
35+
code: 0,
36+
stdout: JSON.stringify({ status: { token: 'foo' } }),
37+
} as shell.ShellReturnValue;
38+
};
39+
process.env.BLABBLE = 'flubble';
40+
const token = auth.getToken({
41+
name: 'user',
42+
authProvider: {
43+
config: {
44+
exec: {
45+
command: 'echo',
46+
env: [
47+
{
48+
name: 'foo',
49+
value: 'bar',
50+
},
51+
],
52+
},
53+
},
54+
},
55+
});
56+
expect(optsOut.env.foo).to.equal('bar');
57+
expect(optsOut.env.PATH).to.equal(process.env.PATH);
58+
expect(optsOut.env.BLABBLE).to.equal(process.env.BLABBLE);
59+
});
60+
});

0 commit comments

Comments
 (0)