forked from kubernetes-client/javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_test.ts
1181 lines (1075 loc) · 41.3 KB
/
config_test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { readFileSync } from 'fs';
import * as https from 'https';
import { dirname, join } from 'path';
import { expect } from 'chai';
import mockfs = require('mock-fs');
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';
const kcFileName = 'testdata/kubeconfig.yaml';
const kc2FileName = 'testdata/kubeconfig-2.yaml';
const kcDupeCluster = 'testdata/kubeconfig-dupe-cluster.yaml';
const kcDupeContext = 'testdata/kubeconfig-dupe-context.yaml';
const kcDupeUser = 'testdata/kubeconfig-dupe-user.yaml';
const kcNoUserFileName = 'testdata/empty-user-kubeconfig.yaml';
/* tslint:disable: no-empty */
describe('Config', () => {});
function validateFileLoad(kc: KubeConfig) {
// check clusters
expect(kc.clusters.length).to.equal(2, 'there are 2 clusters');
const cluster1 = kc.clusters[0];
const cluster2 = kc.clusters[1];
expect(cluster1.name).to.equal('cluster1');
expect(cluster1.caData).to.equal('Q0FEQVRB');
expect(cluster1.server).to.equal('http://example.com');
expect(cluster2.name).to.equal('cluster2');
expect(cluster2.caData).to.equal('Q0FEQVRBMg==');
expect(cluster2.server).to.equal('http://example2.com');
expect(cluster2.skipTLSVerify).to.equal(true);
// check users
expect(kc.users.length).to.equal(3, 'there are 3 users');
const user1 = kc.users[0];
const user2 = kc.users[1];
const user3 = kc.users[2];
expect(user1.name).to.equal('user1');
expect(user1.certData).to.equal('VVNFUl9DQURBVEE=');
expect(user1.keyData).to.equal('VVNFUl9DS0RBVEE=');
expect(user2.name).to.equal('user2');
expect(user2.certData).to.equal('VVNFUjJfQ0FEQVRB');
expect(user2.keyData).to.equal('VVNFUjJfQ0tEQVRB');
expect(user3.name).to.equal('user3');
expect(user3.username).to.equal('foo');
expect(user3.password).to.equal('bar');
// check contexts
expect(kc.contexts.length).to.equal(3, 'there are three contexts');
const context1 = kc.contexts[0];
const context2 = kc.contexts[1];
const context3 = kc.contexts[2];
expect(context1.name).to.equal('context1');
expect(context1.user).to.equal('user1');
expect(context1.namespace).to.equal(undefined);
expect(context1.cluster).to.equal('cluster1');
expect(context2.name).to.equal('context2');
expect(context2.user).to.equal('user2');
expect(context2.namespace).to.equal('namespace2');
expect(context2.cluster).to.equal('cluster2');
expect(context3.name).to.equal('passwd');
expect(context3.user).to.equal('user3');
expect(context3.cluster).to.equal('cluster2');
expect(kc.getCurrentContext()).to.equal('context2');
}
describe('KubeConfig', () => {
describe('findObject', () => {
it('should find objects', () => {
interface MyNamed {
name: string;
some: any;
cluster: any;
}
const list: MyNamed[] = [
{
name: 'foo',
cluster: {
some: 'sub-object',
},
some: 'object',
} as MyNamed,
{
name: 'bar',
some: 'object',
cluster: {
some: 'sub-object',
},
} as MyNamed,
];
// Validate that if the named object ('cluster' in this case) is inside we pick it out
const obj1 = findObject(list, 'foo', 'cluster');
expect(obj1).to.not.equal(null);
if (obj1) {
expect(obj1.some).to.equal('sub-object');
}
// Validate that if the named object is missing, we just return the full object
const obj2 = findObject(list, 'bar', 'context');
expect(obj2).to.not.equal(null);
if (obj2) {
expect(obj2.some).to.equal('object');
}
// validate that we do the right thing if it is missing
const obj3 = findObject(list, 'nonexistent', 'context');
expect(obj3).to.equal(null);
});
});
describe('loadFromClusterAndUser', () => {
it('should load from cluster and user', () => {
const kc = new KubeConfig();
const cluster = {
name: 'foo',
server: 'http://server.com',
} as Cluster;
const user = {
name: 'my-user',
password: 'some-password',
} as User;
kc.loadFromClusterAndUser(cluster, user);
const clusterOut = kc.getCurrentCluster();
expect(clusterOut).to.equal(cluster);
const userOut = kc.getCurrentUser();
expect(userOut).to.equal(user);
});
});
describe('clusterConstructor', () => {
it('should load from options', () => {
const cluster = {
name: 'foo',
server: 'http://server.com',
} as Cluster;
const user = {
name: 'my-user',
password: 'some-password',
} as User;
const context = {
name: 'my-context',
user: user.name,
cluster: cluster.name,
};
const kc = new KubeConfig();
kc.loadFromOptions({
clusters: [cluster],
users: [user],
contexts: [context],
currentContext: context.name,
});
const clusterOut = kc.getCurrentCluster();
expect(clusterOut).to.equal(cluster);
const userOut = kc.getCurrentUser();
expect(userOut).to.equal(user);
});
});
describe('loadFromString', () => {
it('should throw with a bad version', () => {
const kc = new KubeConfig();
expect(() => kc.loadFromString('apiVersion: v2')).to.throw('unknown version: v2');
});
});
describe('loadFromFile', () => {
it('should load the kubeconfig file properly', () => {
const kc = new KubeConfig();
kc.loadFromFile(kcFileName);
validateFileLoad(kc);
});
it('should fail to load a missing kubeconfig file', () => {
// TODO: make the error check work
// let kc = new KubeConfig();
// expect(kc.loadFromFile("missing.yaml")).to.throw();
});
});
describe('loadEmptyUser', () => {
it('should load a kubeconfig with an empty user', () => {
const kc = new KubeConfig();
kc.loadFromFile(kcNoUserFileName);
});
});
describe('applyHTTPSOptions', () => {
it('should apply cert configs', () => {
const kc = new KubeConfig();
kc.loadFromFile(kcFileName);
const opts: https.RequestOptions = {};
kc.applytoHTTPSOptions(opts);
expect(opts).to.deep.equal({
headers: [],
ca: new Buffer('CADATA2', 'utf-8'),
cert: new Buffer('USER2_CADATA', 'utf-8'),
key: new Buffer('USER2_CKDATA', 'utf-8'),
rejectUnauthorized: false,
});
});
it('should apply password', async () => {
const kc = new KubeConfig();
kc.loadFromFile(kcFileName);
kc.setCurrentContext('passwd');
const opts: requestlib.Options = {
url: 'https://company.com',
};
await kc.applyToRequest(opts);
expect(opts).to.deep.equal({
headers: [],
ca: new Buffer('CADATA2', 'utf-8'),
auth: {
username: 'foo',
password: 'bar',
},
url: 'https://company.com',
strictSSL: false,
rejectUnauthorized: false,
});
});
});
describe('loadClusterConfigObjects', () => {
it('should fail if name is missing from cluster', () => {
expect(() => {
newClusters([
{
name: 'some-cluster',
cluster: {
server: 'some.server.com',
},
},
{
foo: 'bar',
},
]);
}).to.throw('clusters[1].name is missing');
});
it('should fail if cluster is missing from cluster', () => {
expect(() => {
newClusters([
{
name: 'some-cluster',
cluster: {
server: 'some.server.com',
},
},
{
name: 'bar',
},
]);
}).to.throw('clusters[1].cluster is missing');
});
it('should fail if cluster.server is missing from cluster', () => {
expect(() => {
newClusters([
{
name: 'some-cluster',
cluster: {
server: 'some.server.com',
},
},
{
name: 'bar',
cluster: {},
},
]);
}).to.throw('clusters[1].cluster.server is missing');
});
});
describe('loadUserConfigObjects', () => {
it('should fail if name is missing from user', () => {
expect(() => {
newUsers([
{
name: 'some-user',
user: {},
},
{
foo: 'bar',
},
]);
}).to.throw('users[1].name is missing');
});
it('should load correctly with just name', () => {
const name = 'some-name';
const users = newUsers([
{
name,
},
]);
expect(name).to.equal(users[0].name);
});
it('should load token correctly', () => {
const name = 'some-name';
const token = 'token';
const users = newUsers([
{
name,
user: {
token: 'token',
},
},
]);
expect(name).to.equal(users[0].name);
expect(token).to.equal(users[0].token);
});
it('should load token file correctly', () => {
const name = 'some-name';
const token = 'token-file-data';
mockfs({
'/path/to/fake/dir': {
'token.txt': token,
},
});
const users = newUsers([
{
name,
user: {
'token-file': '/path/to/fake/dir/token.txt',
},
},
]);
mockfs.restore();
expect(name).to.equal(users[0].name);
expect(token).to.equal(users[0].token);
});
it('should load extra auth stuff correctly', () => {
const authProvider = 'authProvider';
const certData = 'certData';
const certFile = 'certFile';
const keyData = 'keyData';
const keyFile = 'keyFile';
const password = 'password';
const username = 'username';
const name = 'some-name';
const users = newUsers([
{
name,
user: {
'auth-provider': authProvider,
'client-certificate-data': certData,
'client-certificate': certFile,
'client-key-data': keyData,
'client-key': keyFile,
password,
username,
},
},
]);
expect(authProvider).to.equal(users[0].authProvider);
expect(certData).to.equal(users[0].certData);
expect(certFile).to.equal(users[0].certFile);
expect(keyData).to.equal(users[0].keyData);
expect(keyFile).to.equal(users[0].keyFile);
expect(password).to.equal(users[0].password);
expect(username).to.equal(users[0].username);
expect(name).to.equal(users[0].name);
});
});
describe('findHome', () => {
it('should load from HOME if present', () => {
const currentHome = process.env.HOME;
const expectedHome = 'foobar';
process.env.HOME = expectedHome;
const dir = join(process.env.HOME, '.kube');
const arg = {};
arg[dir] = { config: 'data' };
mockfs(arg);
const home = findHomeDir();
mockfs.restore();
process.env.HOME = currentHome;
expect(home).to.equal(expectedHome);
});
});
describe('win32HomeDirTests', () => {
let originalPlatform: string;
const originalEnvVars: any = {};
before(() => {
originalPlatform = process.platform;
Object.defineProperty(process, 'platform', {
value: 'win32',
});
originalEnvVars.HOME = process.env.HOME;
originalEnvVars.USERPROFILE = process.env.USERPROFILE;
originalEnvVars.HOMEDRIVE = process.env.HOMEDRIVE;
originalEnvVars.HOMEPATH = process.env.HOMEPATH;
delete process.env.HOME;
delete process.env.USERPROFILE;
delete process.env.HOMEDRIVE;
delete process.env.HOMEPATH;
});
after(() => {
Object.defineProperty(process, 'platform', {
value: originalPlatform,
});
process.env.HOME = originalEnvVars.HOME;
process.env.USERPROFILE = originalEnvVars.USERPROFILE;
process.env.HOMEDRIVE = originalEnvVars.HOMEDRIVE;
process.env.HOMEPATH = originalEnvVars.HOMEPATH;
});
it('should return null if no home is present', () => {
const dir = findHomeDir();
expect(dir).to.equal(null);
});
it('should load from HOMEDRIVE/HOMEPATH if present', () => {
process.env.HOMEDRIVE = 'foo';
process.env.HOMEPATH = 'bar';
const dir = join(process.env.HOMEDRIVE, process.env.HOMEPATH);
const arg = {};
arg[dir] = { config: 'data' };
mockfs(arg);
const home = findHomeDir();
mockfs.restore();
expect(home).to.equal(dir);
});
it('should load from USERPROFILE if present', () => {
const dir = 'someplace';
process.env.HOMEDRIVE = 'foo';
process.env.HOMEPATH = 'bar';
process.env.USERPROFILE = dir;
const arg = {};
arg[dir] = { config: 'data' };
mockfs(arg);
const home = findHomeDir();
mockfs.restore();
expect(home).to.equal(dir);
});
});
describe('loadContextConfigObjects', () => {
it('should fail if name is missing from context', () => {
expect(() => {
newContexts([
{
name: 'some-cluster',
context: {
cluster: 'foo',
user: 'bar',
},
},
{
foo: 'bar',
},
]);
}).to.throw('contexts[1].name is missing');
});
it('should fail if context is missing from context', () => {
expect(() => {
newContexts([
{
name: 'some-cluster',
context: {
cluster: 'foo',
user: 'bar',
},
},
{
name: 'bar',
},
]);
}).to.throw('contexts[1].context is missing');
});
it('should fail if context is missing from context', () => {
expect(() => {
newContexts([
{
name: 'some-cluster',
context: {
cluster: 'foo',
user: 'bar',
},
},
{
name: 'bar',
context: {
user: 'user',
},
},
]);
}).to.throw('contexts[1].context.cluster is missing');
});
});
describe('auth options', () => {
it('should populate basic-auth for https', async () => {
const config = new KubeConfig();
const user = 'user';
const passwd = 'password';
config.loadFromClusterAndUser({} as Cluster, { username: user, password: passwd } as User);
const opts = {} as https.RequestOptions;
await config.applytoHTTPSOptions(opts);
expect(opts.auth).to.equal(`${user}:${passwd}`);
});
it('should populate options for request', async () => {
const config = new KubeConfig();
const user = 'user';
const passwd = 'password';
config.loadFromClusterAndUser(
{
skipTLSVerify: true,
} as Cluster,
{
username: user,
password: passwd,
} as User,
);
const opts = {} as requestlib.Options;
await config.applyToRequest(opts);
/* tslint:disable no-unused-expression*/
expect(opts.auth).to.not.be.undefined;
if (opts.auth) {
expect(opts.auth.username).to.equal(user);
expect(opts.auth.password).to.equal(passwd);
}
expect(opts.strictSSL).to.equal(false);
});
it('should not populate strict ssl', async () => {
const config = new KubeConfig();
config.loadFromClusterAndUser({ skipTLSVerify: false } as Cluster, {} as User);
const opts = {} as requestlib.Options;
await config.applyToRequest(opts);
expect(opts.strictSSL).to.equal(undefined);
});
it('should populate from token', async () => {
const config = new KubeConfig();
const token = 'token';
config.loadFromClusterAndUser(
{ skipTLSVerify: false } as Cluster,
{
token,
} 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 populate from auth provider', async () => {
const config = new KubeConfig();
const token = 'token';
config.loadFromClusterAndUser(
{ skipTLSVerify: false } as Cluster,
{
authProvider: {
name: 'azure',
config: {
'access-token': token,
expiry: 'Fri Aug 24 07:32:05 PDT 3018',
},
},
} 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}`);
}
opts.headers = [];
opts.headers.Host = 'foo.com';
await config.applyToRequest(opts);
expect(opts.headers.Authorization).to.equal(`Bearer ${token}`);
});
it('should populate from auth provider without expirty', async () => {
const config = new KubeConfig();
const token = 'token';
config.loadFromClusterAndUser(
{ skipTLSVerify: false } as Cluster,
{
authProvider: {
name: 'azure',
config: {
'access-token': token,
},
},
} 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 populate rejectUnauthorized=false when skipTLSVerify is set', async () => {
const config = new KubeConfig();
const token = 'token';
config.loadFromClusterAndUser(
{ skipTLSVerify: true } as Cluster,
{
authProvider: {
name: 'azure',
config: {
'access-token': token,
},
},
} as User,
);
const opts = {} as requestlib.Options;
await config.applyToRequest(opts);
expect(opts.rejectUnauthorized).to.equal(false);
});
it('should not set rejectUnauthorized if skipTLSVerify is not set', async () => {
// This test is just making 100% sure we validate certs unless we explictly set
// skipTLSVerify = true
const config = new KubeConfig();
const token = 'token';
config.loadFromClusterAndUser(
{} as Cluster,
{
authProvider: {
name: 'azure',
config: {
'access-token': token,
},
},
} as User,
);
const opts = {} as requestlib.Options;
await config.applyToRequest(opts);
expect(opts.rejectUnauthorized).to.equal(undefined);
});
it('should throw with expired token and no cmd', () => {
const config = new KubeConfig();
config.loadFromClusterAndUser(
{ skipTLSVerify: false } as Cluster,
{
authProvider: {
name: 'azure',
config: {
expiry: 'Aug 24 07:32:05 PDT 2017',
},
},
} as User,
);
const opts = {} as requestlib.Options;
return expect(config.applyToRequest(opts)).to.eventually.be.rejectedWith('Token is expired!');
});
it('should throw with bad command', () => {
const config = new KubeConfig();
config.loadFromClusterAndUser(
{ skipTLSVerify: false } as Cluster,
{
authProvider: {
name: 'azure',
config: {
'access-token': 'token',
expiry: 'Aug 24 07:32:05 PDT 2017',
'cmd-path': 'non-existent-command',
},
},
} as User,
);
const opts = {} as requestlib.Options;
return expect(config.applyToRequest(opts)).to.eventually.be.rejectedWith(
/Failed to refresh token/,
);
});
it('should exec with expired token', async () => {
const config = new KubeConfig();
const token = 'token';
const responseStr = `{"token":{"accessToken":"${token}"}}`;
config.loadFromClusterAndUser(
{ skipTLSVerify: false } as Cluster,
{
authProvider: {
name: 'azure',
config: {
expiry: 'Aug 24 07:32:05 PDT 2017',
'cmd-path': '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 without access-token', async () => {
const config = new KubeConfig();
const token = 'token';
const responseStr = `{"token":{"accessToken":"${token}"}}`;
config.loadFromClusterAndUser(
{ skipTLSVerify: false } as Cluster,
{
authProvider: {
name: 'azure',
config: {
'cmd-path': '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 without access-token', async () => {
const config = new KubeConfig();
const token = 'token';
const responseStr = `{"token":{"accessToken":"${token}"}}`;
config.loadFromClusterAndUser(
{ skipTLSVerify: false } as Cluster,
{
authProvider: {
name: 'azure',
config: {
'cmd-path': '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 succesfully with spaces in cmd', async () => {
const config = new KubeConfig();
const token = 'token';
const responseStr = `{"token":{"accessToken":"${token}"}}`;
config.loadFromClusterAndUser(
{ skipTLSVerify: false } as Cluster,
{
authProvider: {
name: 'azure', // applies to gcp too as they are both handled by CloudAuth class
config: {
'cmd-path': path.join(__dirname, '..', 'test', 'echo space.js'),
'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';
const responseStr = `{"status": { "token": "${token}" }}`;
config.loadFromClusterAndUser(
{ skipTLSVerify: false } as Cluster,
{
authProvider: {
name: 'exec',
config: {
exec: {
command: 'echo',
args: [`${responseStr}`],
env: [
{
name: 'foo',
value: 'bar',
},
],
},
},
},
} as User,
);
// TODO: inject the exec command here and validate env vars?
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', async () => {
const config = new KubeConfig();
const token = 'token';
const responseStr = `{
"apiVersion": "client.authentication.k8s.io/v1beta1",
"kind": "ExecCredential",
"status": {
"token": "${token}"
}
}`;
config.loadFromClusterAndUser(
{ skipTLSVerify: false } as Cluster,
{
authProvider: {
name: 'exec',
config: {
exec: {
command: 'echo',
args: [`${responseStr}`],
},
},
},
} as User,
);
// TODO: inject the exec command here?
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 (other location)', async () => {
const config = new KubeConfig();
const token = 'token';
const responseStr = `{
"apiVersion": "client.authentication.k8s.io/v1beta1",
"kind": "ExecCredential",
"status": {
"token": "${token}"
}
}`;
config.loadFromClusterAndUser(
{ skipTLSVerify: false } as Cluster,
{
exec: {
command: 'echo',
args: [`${responseStr}`],
},
} as User,
);
// TODO: inject the exec command here?
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 throw with no command.', () => {
const config = new KubeConfig();
config.loadFromClusterAndUser(
{ skipTLSVerify: false } as Cluster,
{
authProvider: {
name: 'exec',
config: {
exec: {},
},
},
} as User,
);
const opts = {} as requestlib.Options;
return expect(config.applyToRequest(opts)).to.eventually.be.rejectedWith(
'No command was specified for exec authProvider!',
);
});
});
describe('load from multi $KUBECONFIG', () => {
it('should load from multiple files', () => {
process.env.KUBECONFIG = kcFileName + path.delimiter + kc2FileName;
const kc = new KubeConfig();
kc.loadFromDefault();
// 2 in the first config, 1 in the second config
expect(kc.clusters.length).to.equal(3);
expect(kc.users.length).to.equal(6);
expect(kc.contexts.length).to.equal(4);
expect(kc.getCurrentContext()).to.equal('contextA');
});
it('should throw with duplicate clusters', () => {
process.env.KUBECONFIG = kcFileName + path.delimiter + kcDupeCluster;
const kc = new KubeConfig();
expect(() => kc.loadFromDefault()).to.throw('Duplicate cluster: cluster1');
});
it('should throw with duplicate contexts', () => {
process.env.KUBECONFIG = kcFileName + path.delimiter + kcDupeContext;
const kc = new KubeConfig();
expect(() => kc.loadFromDefault()).to.throw('Duplicate context: context1');
});
it('should throw with duplicate users', () => {
process.env.KUBECONFIG = kcFileName + path.delimiter + kcDupeUser;
const kc = new KubeConfig();
expect(() => kc.loadFromDefault()).to.throw('Duplicate user: user1');
});
});
describe('MakeAbsolutePaths', () => {
it('should correctly make absolute paths', () => {
const relative = 'foo/bar';
const absolute = '/tmp/foo/bar';
const root = '/usr/';
expect(makeAbsolutePath(root, relative)).to.equal('/usr/foo/bar');
expect(makeAbsolutePath(root, absolute)).to.equal(absolute);
});
});
describe('loadFromDefault', () => {
it('should load from $KUBECONFIG', () => {
process.env.KUBECONFIG = kcFileName;
const kc = new KubeConfig();
kc.loadFromDefault();
delete process.env.KUBECONFIG;
validateFileLoad(kc);
});
it('should load from $HOME/.kube/config', () => {
const currentHome = process.env.HOME;
process.env.HOME = 'foobar';
const data = readFileSync(kcFileName);
const dir = join(process.env.HOME, '.kube');
const arg = {};
arg[dir] = { config: data };
mockfs(arg);
const kc = new KubeConfig();
kc.loadFromDefault();
mockfs.restore();
process.env.HOME = currentHome;