Skip to content

Commit

Permalink
Fix env variables to include existing environment variables, then ove…
Browse files Browse the repository at this point in the history
…rride.
  • Loading branch information
brendandburns committed May 24, 2019
1 parent e5ec945 commit 3fe1b53
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ coverage/*
examples/package-lock.json
.nyc_output
kubernetes-client-node-*.tgz
**/*.swp

9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions src/exec_auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { User } from './config_types';

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

public isAuthProvider(user: User) {
if (!user) {
Expand Down Expand Up @@ -53,11 +54,11 @@ export class ExecAuth implements Authenticator {
}
let opts: shell.ExecOpts;
if (exec.env) {
const env = {};
const env = process.env;
exec.env.forEach((elt) => (env[elt.name] = elt.value));
opts = { env };
}
const result = shell.exec(cmd, opts);
const result = this.execFn(cmd, opts);
if (result.code === 0) {
const obj = JSON.parse(result.stdout);
this.tokenCache[user.name] = obj;
Expand Down
57 changes: 57 additions & 0 deletions src/exec_auth_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { expect } from 'chai';
import { ExecAuth } from './exec_auth';
import * as shell from 'shelljs';

describe('ExecAuth', () => {
it('should correctly exec', async () => {
const auth = new ExecAuth();
(auth as any).execFn = (command: string, opts: shell.ExecOpts): shell.ShellReturnValue => {
return {
code: 0,
stdout: JSON.stringify({ status: { token: 'foo' } })
} as shell.ShellReturnValue;
};

const token = auth.getToken({
name: 'user',
authProvider: {
config: {
exec: {
command: 'echo'
}
}
}
});
expect(token).to.equal('Bearer foo');
});

it('should exec with env vars', async () => {
const auth = new ExecAuth();
var optsOut: shell.ExecOpts = {};
(auth as any).execFn = (command: string, opts: shell.ExecOpts): shell.ShellReturnValue => {
optsOut = opts
return {
code: 0,
stdout: JSON.stringify({ status: { token: 'foo' } })
} as shell.ShellReturnValue;
};
process.env.BLABBLE = 'flubble';
const token = auth.getToken({
name: 'user',
authProvider: {
config: {
exec: {
command: 'echo',
env: [{
name: 'foo',
value: 'bar'
}]
}
}
}
});
expect(optsOut.env.foo).to.equal('bar');
expect(optsOut.env.PATH).to.equal(process.env.PATH);
expect(optsOut.env.BLABBLE).to.equal(process.env.BLABBLE);
});
});

0 comments on commit 3fe1b53

Please sign in to comment.