Skip to content

Commit ae1c897

Browse files
authored
Merge pull request kubernetes-client#354 from brendandburns/headers
Fix a bug if the headers array is null or undefined in exec auth.
2 parents 9e10c20 + 6bf2b75 commit ae1c897

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/exec_auth.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ export class ExecAuth implements Authenticator {
4949
}
5050
const token = this.getToken(credential);
5151
if (token) {
52+
if (!opts.headers) {
53+
opts.headers = [];
54+
}
5255
opts.headers!.Authorization = `Bearer ${token}`;
5356
}
5457
}

src/exec_auth_test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ import * as shell from 'shelljs';
66

77
import execa = require('execa');
88
import request = require('request');
9+
import https = require('https');
910

1011
import { ExecAuth } from './exec_auth';
1112
import { User } from './config_types';
13+
import { fail } from 'assert';
1214

1315
describe('ExecAuth', () => {
1416
it('should claim correctly', () => {
@@ -246,4 +248,37 @@ describe('ExecAuth', () => {
246248
expect(optsOut.env.PATH).to.equal(process.env.PATH);
247249
expect(optsOut.env.BLABBLE).to.equal(process.env.BLABBLE);
248250
});
251+
252+
it('should handle empty headers array correctly', async () => {
253+
const auth = new ExecAuth();
254+
(auth as any).execFn = (
255+
command: string,
256+
args: string[],
257+
opts: execa.SyncOptions,
258+
): execa.ExecaSyncReturnValue => {
259+
return {
260+
code: 0,
261+
stdout: JSON.stringify({ status: { token: 'foo' } }),
262+
} as execa.ExecaSyncReturnValue;
263+
};
264+
const opts = {} as https.RequestOptions;
265+
auth.applyAuthentication(
266+
{
267+
name: 'user',
268+
authProvider: {
269+
config: {
270+
exec: {
271+
command: 'echo',
272+
},
273+
},
274+
},
275+
},
276+
opts,
277+
);
278+
if (!opts.headers) {
279+
fail('unexpected null headers!');
280+
} else {
281+
expect(opts.headers.Authorization).to.equal('Bearer foo');
282+
}
283+
});
249284
});

0 commit comments

Comments
 (0)