@@ -495,7 +495,29 @@ describe('KubeConfig', () => {
495
495
} ) ;
496
496
497
497
it ( 'should apply NODE_TLS_REJECT_UNAUTHORIZED from environment to agent' , async ( ) => {
498
- const { server, host, port } = await createTestHttpsServer ( ) ;
498
+ const { server, host, port } = await createTestHttpsServer ( ( req , res ) => {
499
+ res . setHeader ( 'Content-Type' , 'application/json' ) ;
500
+ if ( req . url ?. includes ( '/api/v1/namespaces' ) ) {
501
+ res . writeHead ( 200 ) ;
502
+ res . end (
503
+ JSON . stringify ( {
504
+ apiVersion : 'v1' ,
505
+ kind : 'NamespaceList' ,
506
+ items : [
507
+ {
508
+ apiVersion : 'v1' ,
509
+ kind : 'Namespace' ,
510
+ metadata : { name : 'default' } ,
511
+ } ,
512
+ ] ,
513
+ } ) ,
514
+ ) ;
515
+ } else {
516
+ res . writeHead ( 200 ) ;
517
+ res . end ( 'ok' ) ;
518
+ }
519
+ } ) ;
520
+
499
521
const originalValue = process . env . NODE_TLS_REJECT_UNAUTHORIZED ;
500
522
process . env . NODE_TLS_REJECT_UNAUTHORIZED = '0' ;
501
523
after ( ( ) => {
@@ -504,15 +526,28 @@ describe('KubeConfig', () => {
504
526
} ) ;
505
527
506
528
const kc = new KubeConfig ( ) ;
507
- const rc = new RequestContext ( `https://${ host } :${ port } ` , HttpMethod . GET ) ;
508
- await kc . applySecurityAuthentication ( rc ) ;
509
- const res = await fetch ( `https://${ host } :${ port } ` , { agent : rc . getAgent ( ) } ) ;
510
- strictEqual ( res . status , 200 ) ;
511
- strictEqual ( await res . text ( ) , 'OK' ) ;
529
+ kc . loadFromClusterAndUser (
530
+ {
531
+ name : 'test-cluster' ,
532
+ server : `https://${ host } :${ port } ` ,
533
+ // ignore skipTLSVerify specified from environment variables
534
+ } as Cluster ,
535
+ {
536
+ name : 'test-user' ,
537
+ token : 'test-token' ,
538
+ } ,
539
+ ) ;
540
+ const coreV1Api = kc . makeApiClient ( CoreV1Api ) ;
541
+ const namespaceList = await coreV1Api . listNamespace ( ) ;
542
+
543
+ strictEqual ( namespaceList . kind , 'NamespaceList' ) ;
544
+ strictEqual ( namespaceList . items . length , 1 ) ;
545
+ strictEqual ( namespaceList . items [ 0 ] . metadata ?. name , 'default' ) ;
512
546
513
547
const res2 = await fetch ( `https://${ host } :${ port } ` , await kc . applyToFetchOptions ( { } ) ) ;
514
548
strictEqual ( res2 . status , 200 ) ;
515
- strictEqual ( await res2 . text ( ) , 'OK' ) ;
549
+ strictEqual ( await res2 . text ( ) , 'ok' ) ;
550
+
516
551
delete process . env . NODE_TLS_REJECT_UNAUTHORIZED ;
517
552
} ) ;
518
553
} ) ;
@@ -1853,7 +1888,9 @@ describe('KubeConfig', () => {
1853
1888
} ) ;
1854
1889
1855
1890
// create a self-signed HTTPS test server
1856
- async function createTestHttpsServer ( ) : Promise < {
1891
+ async function createTestHttpsServer (
1892
+ requestHandler ?: ( req : http . IncomingMessage , res : http . ServerResponse ) => void ,
1893
+ ) : Promise < {
1857
1894
server : https . Server ;
1858
1895
host : string ;
1859
1896
port : number ;
@@ -1862,10 +1899,12 @@ async function createTestHttpsServer(): Promise<{
1862
1899
const host = 'localhost' ;
1863
1900
const { private : key , cert } = selfsigned . generate ( [ { name : 'commonName' , value : host } ] ) ;
1864
1901
1865
- const server = https . createServer ( { key , cert } , ( _req , res ) => {
1902
+ const defaultHandler = ( req : http . IncomingMessage , res : http . ServerResponse ) => {
1866
1903
res . writeHead ( 200 ) ;
1867
- res . end ( 'OK' ) ;
1868
- } ) ;
1904
+ res . end ( 'ok' ) ;
1905
+ } ;
1906
+
1907
+ const server = https . createServer ( { key, cert } , requestHandler ?? defaultHandler ) ;
1869
1908
1870
1909
const port = await new Promise < number > ( ( resolve ) => {
1871
1910
server . listen ( 0 , ( ) => {
0 commit comments