Skip to content

Commit 3fe1b53

Browse files
committed
Fix env variables to include existing environment variables, then override.
1 parent e5ec945 commit 3fe1b53

File tree

4 files changed

+70
-2
lines changed

4 files changed

+70
-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

package-lock.json

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

0 commit comments

Comments
 (0)