From 08fc25aa2548d9c0e54413dedf3b69848e073cc1 Mon Sep 17 00:00:00 2001 From: Brendan Burns Date: Thu, 23 May 2019 20:18:07 -0700 Subject: [PATCH] Fix environment variable handling in exec auth. --- .gitignore | 1 + src/exec_auth.ts | 5 ++-- src/exec_auth_test.ts | 60 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 src/exec_auth_test.ts diff --git a/.gitignore b/.gitignore index 4a00b6c2ac..b1a1a619d6 100644 --- a/.gitignore +++ b/.gitignore @@ -10,4 +10,5 @@ coverage/* examples/package-lock.json .nyc_output kubernetes-client-node-*.tgz +**/*.swp diff --git a/src/exec_auth.ts b/src/exec_auth.ts index 171b6de8dc..75958af474 100644 --- a/src/exec_auth.ts +++ b/src/exec_auth.ts @@ -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) { @@ -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; diff --git a/src/exec_auth_test.ts b/src/exec_auth_test.ts new file mode 100644 index 0000000000..c1c346c034 --- /dev/null +++ b/src/exec_auth_test.ts @@ -0,0 +1,60 @@ +import { expect } from 'chai'; +import * as shell from 'shelljs'; + +import { ExecAuth } from './exec_auth'; + +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(); + let 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); + }); +});