From ca523c1056a2a577d4bc761f7cbd88c60c83a445 Mon Sep 17 00:00:00 2001 From: Jussi Nummelin Date: Thu, 10 Oct 2019 10:48:58 +0300 Subject: [PATCH 1/2] use command as quoted string in CloudAuth --- src/cloud_auth.ts | 2 ++ src/config_test.ts | 38 +++++++++++++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/cloud_auth.ts b/src/cloud_auth.ts index 097aeef4bf..bc175f1ade 100644 --- a/src/cloud_auth.ts +++ b/src/cloud_auth.ts @@ -63,6 +63,8 @@ export class CloudAuth implements Authenticator { if (!cmd) { throw new Error('Token is expired!'); } + // Wrap cmd in quotes to make it cope with spaces in path + cmd = `"${cmd}"`; const args = config['cmd-args']; if (args) { cmd = cmd + ' ' + args; diff --git a/src/config_test.ts b/src/config_test.ts index 3a1a9419fc..2c556004e7 100644 --- a/src/config_test.ts +++ b/src/config_test.ts @@ -4,9 +4,12 @@ import { dirname, join } from 'path'; import { expect } from 'chai'; import mockfs = require('mock-fs'); -import * as requestlib from 'request'; import * as path from 'path'; +import * as requestlib from 'request'; +import * as filesystem from 'fs'; +import { fs } from 'mock-fs'; +import * as os from 'os'; import { CoreV1Api } from './api'; import { bufferFromFileOrString, findHomeDir, findObject, KubeConfig, makeAbsolutePath } from './config'; import { Cluster, newClusters, newContexts, newUsers, User } from './config_types'; @@ -788,6 +791,39 @@ describe('KubeConfig', () => { expect(opts.headers.Authorization).to.equal(`Bearer ${token}`); } }); + it('should exec succesfully with spaces in cmd', async () => { + /** + * + * to test this, symlink echo to dir that has spaces: + * mkdir -p /tmp/foo\ bar/ + * ln -s /bin/echo /tmp/foo\ bar/echo + * + * FIXME: Figure out a "portable" way to dynamically do this sort of symlink as part of the test case + */ + const config = new KubeConfig(); + const token = 'token'; + const responseStr = `{"token":{"accessToken":"${token}"}}`; + config.loadFromClusterAndUser( + { skipTLSVerify: false } as Cluster, + { + authProvider: { + name: 'azure', // aplias to gcp too as they are both handled by CloudAuth class + config: { + 'cmd-path': '/tmp/foo bar/echo', + 'cmd-args': `'${responseStr}'`, + 'token-key': '{.token.accessToken}', + 'expiry-key': '{.token.token_expiry}', + }, + }, + } as User, + ); + const opts = {} as requestlib.Options; + await config.applyToRequest(opts); + expect(opts.headers).to.not.be.undefined; + if (opts.headers) { + expect(opts.headers.Authorization).to.equal(`Bearer ${token}`); + } + }); it('should exec with exec auth and env vars', async () => { const config = new KubeConfig(); const token = 'token'; From 2e9dbfa68da106e17823f4b791e6e90dfa3fcd39 Mon Sep 17 00:00:00 2001 From: Jussi Nummelin Date: Fri, 11 Oct 2019 13:42:41 +0300 Subject: [PATCH 2/2] use test script with spaces in the name for echoing the token json --- src/config_test.ts | 12 ++---------- test/echo space.js | 4 ++++ 2 files changed, 6 insertions(+), 10 deletions(-) create mode 100755 test/echo space.js diff --git a/src/config_test.ts b/src/config_test.ts index 2c556004e7..bffe24eabd 100644 --- a/src/config_test.ts +++ b/src/config_test.ts @@ -792,14 +792,6 @@ describe('KubeConfig', () => { } }); it('should exec succesfully with spaces in cmd', async () => { - /** - * - * to test this, symlink echo to dir that has spaces: - * mkdir -p /tmp/foo\ bar/ - * ln -s /bin/echo /tmp/foo\ bar/echo - * - * FIXME: Figure out a "portable" way to dynamically do this sort of symlink as part of the test case - */ const config = new KubeConfig(); const token = 'token'; const responseStr = `{"token":{"accessToken":"${token}"}}`; @@ -807,9 +799,9 @@ describe('KubeConfig', () => { { skipTLSVerify: false } as Cluster, { authProvider: { - name: 'azure', // aplias to gcp too as they are both handled by CloudAuth class + name: 'azure', // applies to gcp too as they are both handled by CloudAuth class config: { - 'cmd-path': '/tmp/foo bar/echo', + 'cmd-path': path.join(__dirname, '..', 'test', 'echo space.js'), 'cmd-args': `'${responseStr}'`, 'token-key': '{.token.accessToken}', 'expiry-key': '{.token.token_expiry}', diff --git a/test/echo space.js b/test/echo space.js new file mode 100755 index 0000000000..a36d8b7bdc --- /dev/null +++ b/test/echo space.js @@ -0,0 +1,4 @@ +#!/usr/bin/env node + +// Just echo back all the args +console.log(process.argv.slice(2).join(' ')) \ No newline at end of file