1
1
import { readFileSync } from 'fs' ;
2
2
import * as https from 'https' ;
3
3
import { join } from 'path' ;
4
+ import { RequestOptions } from 'https' ;
4
5
5
6
import { expect } from 'chai' ;
6
7
import mockfs = require( 'mock-fs' ) ;
7
8
import * as path from 'path' ;
8
- import * as requestlib from 'request' ;
9
9
10
- import { CoreV1Api } from './api' ;
10
+ import { CoreV1Api , RequestContext } from './api' ;
11
11
import { bufferFromFileOrString , findHomeDir , findObject , KubeConfig , makeAbsolutePath } from './config' ;
12
12
import { Cluster , newClusters , newContexts , newUsers , User , ActionOnInvalid } from './config_types' ;
13
13
import { ExecAuth } from './exec_auth' ;
14
+ import { HttpMethod } from '.' ;
14
15
15
16
const kcFileName = 'testdata/kubeconfig.yaml' ;
16
17
const kc2FileName = 'testdata/kubeconfig-2.yaml' ;
@@ -23,7 +24,7 @@ const kcInvalidContextFileName = 'testdata/empty-context-kubeconfig.yaml';
23
24
const kcInvalidClusterFileName = 'testdata/empty-cluster-kubeconfig.yaml' ;
24
25
25
26
/* tslint:disable: no-empty */
26
- describe ( 'Config' , ( ) => { } ) ;
27
+ describe ( 'Config' , ( ) => { } ) ;
27
28
28
29
function validateFileLoad ( kc : KubeConfig ) {
29
30
// check clusters
@@ -252,11 +253,14 @@ describe('KubeConfig', () => {
252
253
const kc = new KubeConfig ( ) ;
253
254
kc . loadFromFile ( kcFileName ) ;
254
255
kc . setCurrentContext ( 'passwd' ) ;
256
+ const testServerName1 = 'https://company.com' ;
255
257
256
- const opts : requestlib . Options = {
257
- url : 'https://company.com' ,
258
+ const opts : RequestOptions = {
259
+ servername : 'https://company.com' ,
258
260
} ;
259
- await kc . applyToRequest ( opts ) ;
261
+ const rc = new RequestContext ( testServerName1 , HttpMethod . GET ) ;
262
+ await kc . applySecurityAuthentication ( rc ) ;
263
+ await kc . applytoHTTPSOptions ( opts ) ;
260
264
expect ( opts ) . to . deep . equal ( {
261
265
headers : { } ,
262
266
ca : new Buffer ( 'CADATA2' , 'utf-8' ) ,
@@ -672,27 +676,26 @@ describe('KubeConfig', () => {
672
676
password : passwd ,
673
677
} as User ,
674
678
) ;
675
- const opts = { } as requestlib . Options ;
679
+ const opts = { } as RequestOptions ;
676
680
677
- await config . applyToRequest ( opts ) ;
681
+ await config . applytoHTTPSOptions ( opts ) ;
678
682
679
683
/* tslint:disable no-unused-expression*/
680
684
expect ( opts . auth ) . to . not . be . undefined ;
681
685
if ( opts . auth ) {
682
- expect ( opts . auth . username ) . to . equal ( user ) ;
683
- expect ( opts . auth . password ) . to . equal ( passwd ) ;
686
+ expect ( opts . auth ) . to . equal ( `${ user } :${ passwd } ` ) ;
684
687
}
685
- expect ( opts . strictSSL ) . to . equal ( false ) ;
688
+ expect ( opts . rejectUnauthorized ) . to . equal ( false ) ;
686
689
} ) ;
687
690
it ( 'should not populate strict ssl' , async ( ) => {
688
691
const config = new KubeConfig ( ) ;
689
692
690
693
config . loadFromClusterAndUser ( { skipTLSVerify : false } as Cluster , { } as User ) ;
691
- const opts = { } as requestlib . Options ;
694
+ const opts = { } as RequestOptions ;
692
695
693
- await config . applyToRequest ( opts ) ;
696
+ await config . applytoHTTPSOptions ( opts ) ;
694
697
695
- expect ( opts . strictSSL ) . to . equal ( undefined ) ;
698
+ expect ( opts . rejectUnauthorized ) . to . equal ( undefined ) ;
696
699
} ) ;
697
700
it ( 'should populate from token' , async ( ) => {
698
701
const config = new KubeConfig ( ) ;
@@ -703,9 +706,9 @@ describe('KubeConfig', () => {
703
706
token,
704
707
} as User ,
705
708
) ;
706
- const opts = { } as requestlib . Options ;
709
+ const opts = { } as RequestOptions ;
707
710
708
- await config . applyToRequest ( opts ) ;
711
+ await config . applytoHTTPSOptions ( opts ) ;
709
712
expect ( opts . headers ) . to . not . be . undefined ;
710
713
if ( opts . headers ) {
711
714
expect ( opts . headers . Authorization ) . to . equal ( `Bearer ${ token } ` ) ;
@@ -726,16 +729,16 @@ describe('KubeConfig', () => {
726
729
} ,
727
730
} as User ,
728
731
) ;
729
- const opts = { } as requestlib . Options ;
732
+ const opts = { } as RequestOptions ;
730
733
731
- await config . applyToRequest ( opts ) ;
734
+ await config . applytoHTTPSOptions ( opts ) ;
732
735
expect ( opts . headers ) . to . not . be . undefined ;
733
736
if ( opts . headers ) {
734
737
expect ( opts . headers . Authorization ) . to . equal ( `Bearer ${ token } ` ) ;
735
738
}
736
- opts . headers = [ ] ;
739
+ opts . headers = { }
737
740
opts . headers . Host = 'foo.com' ;
738
- await config . applyToRequest ( opts ) ;
741
+ await config . applytoHTTPSOptions ( opts ) ;
739
742
expect ( opts . headers . Authorization ) . to . equal ( `Bearer ${ token } ` ) ;
740
743
} ) ;
741
744
@@ -753,9 +756,9 @@ describe('KubeConfig', () => {
753
756
} ,
754
757
} as User ,
755
758
) ;
756
- const opts = { } as requestlib . Options ;
759
+ const opts = { } as RequestOptions ;
757
760
758
- await config . applyToRequest ( opts ) ;
761
+ await config . applytoHTTPSOptions ( opts ) ;
759
762
expect ( opts . headers ) . to . not . be . undefined ;
760
763
if ( opts . headers ) {
761
764
expect ( opts . headers . Authorization ) . to . equal ( `Bearer ${ token } ` ) ;
@@ -776,9 +779,9 @@ describe('KubeConfig', () => {
776
779
} ,
777
780
} as User ,
778
781
) ;
779
- const opts = { } as requestlib . Options ;
782
+ const opts = { } as RequestOptions ;
780
783
781
- await config . applyToRequest ( opts ) ;
784
+ await config . applytoHTTPSOptions ( opts ) ;
782
785
expect ( opts . rejectUnauthorized ) . to . equal ( false ) ;
783
786
} ) ;
784
787
@@ -798,9 +801,9 @@ describe('KubeConfig', () => {
798
801
} ,
799
802
} as User ,
800
803
) ;
801
- const opts = { } as requestlib . Options ;
804
+ const opts = { } as RequestOptions ;
802
805
803
- await config . applyToRequest ( opts ) ;
806
+ await config . applytoHTTPSOptions ( opts ) ;
804
807
expect ( opts . rejectUnauthorized ) . to . equal ( undefined ) ;
805
808
} ) ;
806
809
@@ -817,9 +820,9 @@ describe('KubeConfig', () => {
817
820
} ,
818
821
} as User ,
819
822
) ;
820
- const opts = { } as requestlib . Options ;
823
+ const opts = { } as RequestOptions ;
821
824
822
- return expect ( config . applyToRequest ( opts ) ) . to . eventually . be . rejectedWith ( 'Token is expired!' ) ;
825
+ return expect ( config . applytoHTTPSOptions ( opts ) ) . to . eventually . be . rejectedWith ( 'Token is expired!' ) ;
823
826
} ) ;
824
827
825
828
it ( 'should throw with bad command' , ( ) => {
@@ -837,8 +840,8 @@ describe('KubeConfig', () => {
837
840
} ,
838
841
} as User ,
839
842
) ;
840
- const opts = { } as requestlib . Options ;
841
- return expect ( config . applyToRequest ( opts ) ) . to . eventually . be . rejectedWith (
843
+ const opts = { } as RequestOptions ;
844
+ return expect ( config . applytoHTTPSOptions ( opts ) ) . to . eventually . be . rejectedWith (
842
845
/ F a i l e d t o r e f r e s h t o k e n / ,
843
846
) ;
844
847
} ) ;
@@ -866,8 +869,8 @@ describe('KubeConfig', () => {
866
869
} ,
867
870
} as User ,
868
871
) ;
869
- const opts = { } as requestlib . Options ;
870
- await config . applyToRequest ( opts ) ;
872
+ const opts = { } as RequestOptions ;
873
+ await config . applytoHTTPSOptions ( opts ) ;
871
874
expect ( opts . headers ) . to . not . be . undefined ;
872
875
if ( opts . headers ) {
873
876
expect ( opts . headers . Authorization ) . to . equal ( `Bearer ${ token } ` ) ;
@@ -897,8 +900,8 @@ describe('KubeConfig', () => {
897
900
} ,
898
901
} as User ,
899
902
) ;
900
- const opts = { } as requestlib . Options ;
901
- await config . applyToRequest ( opts ) ;
903
+ const opts = { } as RequestOptions ;
904
+ await config . applytoHTTPSOptions ( opts ) ;
902
905
expect ( opts . headers ) . to . not . be . undefined ;
903
906
if ( opts . headers ) {
904
907
expect ( opts . headers . Authorization ) . to . equal ( `Bearer ${ token } ` ) ;
@@ -927,8 +930,8 @@ describe('KubeConfig', () => {
927
930
} ,
928
931
} as User ,
929
932
) ;
930
- const opts = { } as requestlib . Options ;
931
- await config . applyToRequest ( opts ) ;
933
+ const opts = { } as RequestOptions ;
934
+ await config . applytoHTTPSOptions ( opts ) ;
932
935
expect ( opts . headers ) . to . not . be . undefined ;
933
936
if ( opts . headers ) {
934
937
expect ( opts . headers . Authorization ) . to . equal ( `Bearer ${ token } ` ) ;
@@ -956,8 +959,8 @@ describe('KubeConfig', () => {
956
959
} ,
957
960
} as User ,
958
961
) ;
959
- const opts = { } as requestlib . Options ;
960
- await config . applyToRequest ( opts ) ;
962
+ const opts = { } as RequestOptions ;
963
+ await config . applytoHTTPSOptions ( opts ) ;
961
964
expect ( opts . headers ) . to . not . be . undefined ;
962
965
if ( opts . headers ) {
963
966
expect ( opts . headers . Authorization ) . to . equal ( `Bearer ${ token } ` ) ;
@@ -985,8 +988,8 @@ describe('KubeConfig', () => {
985
988
} ,
986
989
} as User ,
987
990
) ;
988
- const opts = { } as requestlib . Options ;
989
- await config . applyToRequest ( opts ) ;
991
+ const opts = { } as RequestOptions ;
992
+ await config . applytoHTTPSOptions ( opts ) ;
990
993
expect ( opts . headers ) . to . not . be . undefined ;
991
994
if ( opts . headers ) {
992
995
expect ( opts . headers . Authorization ) . to . equal ( `Bearer ${ token } ` ) ;
@@ -1021,8 +1024,8 @@ describe('KubeConfig', () => {
1021
1024
} as User ,
1022
1025
) ;
1023
1026
// TODO: inject the exec command here and validate env vars?
1024
- const opts = { } as requestlib . Options ;
1025
- await config . applyToRequest ( opts ) ;
1027
+ const opts = { } as RequestOptions ;
1028
+ await config . applytoHTTPSOptions ( opts ) ;
1026
1029
expect ( opts . headers ) . to . not . be . undefined ;
1027
1030
if ( opts . headers ) {
1028
1031
expect ( opts . headers . Authorization ) . to . equal ( `Bearer ${ token } ` ) ;
@@ -1057,8 +1060,8 @@ describe('KubeConfig', () => {
1057
1060
} as User ,
1058
1061
) ;
1059
1062
// TODO: inject the exec command here?
1060
- const opts = { } as requestlib . Options ;
1061
- await config . applyToRequest ( opts ) ;
1063
+ const opts = { } as RequestOptions ;
1064
+ await config . applytoHTTPSOptions ( opts ) ;
1062
1065
expect ( opts . headers ) . to . not . be . undefined ;
1063
1066
if ( opts . headers ) {
1064
1067
expect ( opts . headers . Authorization ) . to . equal ( `Bearer ${ token } ` ) ;
@@ -1088,8 +1091,8 @@ describe('KubeConfig', () => {
1088
1091
} as User ,
1089
1092
) ;
1090
1093
// TODO: inject the exec command here?
1091
- const opts = { } as requestlib . Options ;
1092
- await config . applyToRequest ( opts ) ;
1094
+ const opts = { } as RequestOptions ;
1095
+ await config . applytoHTTPSOptions ( opts ) ;
1093
1096
expect ( opts . headers ) . to . not . be . undefined ;
1094
1097
if ( opts . headers ) {
1095
1098
expect ( opts . headers . Authorization ) . to . equal ( `Bearer ${ token } ` ) ;
@@ -1120,8 +1123,8 @@ describe('KubeConfig', () => {
1120
1123
} as User ,
1121
1124
) ;
1122
1125
// TODO: inject the exec command here?
1123
- const opts = { } as requestlib . Options ;
1124
- await config . applyToRequest ( opts ) ;
1126
+ const opts = { } as RequestOptions ;
1127
+ await config . applytoHTTPSOptions ( opts ) ;
1125
1128
let execAuthenticator = ( KubeConfig as any ) . authenticators . find (
1126
1129
( authenticator ) => authenticator instanceof ExecAuth ,
1127
1130
) ;
@@ -1141,8 +1144,8 @@ describe('KubeConfig', () => {
1141
1144
} ,
1142
1145
} as User ,
1143
1146
) ;
1144
- const opts = { } as requestlib . Options ;
1145
- return expect ( config . applyToRequest ( opts ) ) . to . eventually . be . rejectedWith (
1147
+ const opts = { } as RequestOptions ;
1148
+ return expect ( config . applytoHTTPSOptions ( opts ) ) . to . eventually . be . rejectedWith (
1146
1149
'No command was specified for exec authProvider!' ,
1147
1150
) ;
1148
1151
} ) ;
@@ -1440,8 +1443,8 @@ describe('KubeConfig', () => {
1440
1443
} ) ;
1441
1444
1442
1445
it ( 'should apply to request' , async ( ) => {
1443
- const opts = { } as requestlib . Options ;
1444
- await emptyConfig . applyToRequest ( opts ) ;
1446
+ const opts = { } as RequestOptions ;
1447
+ await emptyConfig . applytoHTTPSOptions ( opts ) ;
1445
1448
} ) ;
1446
1449
} ) ;
1447
1450
@@ -1503,4 +1506,4 @@ describe('KubeConfig', () => {
1503
1506
mockfs . restore ( ) ;
1504
1507
} ) ;
1505
1508
} ) ;
1506
- } ) ;
1509
+ } ) ;
0 commit comments