@@ -18,7 +18,7 @@ import { fileURLToPath } from 'node:url';
18
18
import mockfs from 'mock-fs' ;
19
19
20
20
import { Authenticator } from './auth.js' ;
21
- import { Headers } from 'node-fetch' ;
21
+ import fetch , { Headers } from 'node-fetch' ;
22
22
import { HttpMethod } from './index.js' ;
23
23
import { assertRequestAgentsEqual , assertRequestOptionsEqual } from './test/match-buffer.js' ;
24
24
import { CoreV1Api , RequestContext } from './api.js' ;
@@ -27,6 +27,8 @@ import { ActionOnInvalid, Cluster, newClusters, newContexts, newUsers, User } fr
27
27
import { ExecAuth } from './exec_auth.js' ;
28
28
import { HttpProxyAgent , HttpsProxyAgent } from 'hpagent' ;
29
29
import { SocksProxyAgent } from 'socks-proxy-agent' ;
30
+ import { AddressInfo } from 'node:net' ;
31
+ import selfsigned from 'selfsigned' ;
30
32
31
33
const kcFileName = 'testdata/kubeconfig.yaml' ;
32
34
const kc2FileName = 'testdata/kubeconfig-2.yaml' ;
@@ -491,6 +493,28 @@ describe('KubeConfig', () => {
491
493
492
494
strictEqual ( rc . getAgent ( ) instanceof https . Agent , true ) ;
493
495
} ) ;
496
+
497
+ it ( 'should apply NODE_TLS_REJECT_UNAUTHORIZED from environment to agent' , async ( ) => {
498
+ const { server, host, port } = await createTestHttpsServer ( ) ;
499
+ const originalValue = process . env . NODE_TLS_REJECT_UNAUTHORIZED ;
500
+ process . env . NODE_TLS_REJECT_UNAUTHORIZED = '0' ;
501
+ after ( ( ) => {
502
+ server . close ( ) ;
503
+ process . env . NODE_TLS_REJECT_UNAUTHORIZED = originalValue ;
504
+ } ) ;
505
+
506
+ 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' ) ;
512
+
513
+ const res2 = await fetch ( `https://${ host } :${ port } ` , await kc . applyToFetchOptions ( { } ) ) ;
514
+ strictEqual ( res2 . status , 200 ) ;
515
+ strictEqual ( await res2 . text ( ) , 'OK' ) ;
516
+ delete process . env . NODE_TLS_REJECT_UNAUTHORIZED ;
517
+ } ) ;
494
518
} ) ;
495
519
496
520
describe ( 'loadClusterConfigObjects' , ( ) => {
@@ -1827,3 +1851,32 @@ describe('KubeConfig', () => {
1827
1851
} ) ;
1828
1852
} ) ;
1829
1853
} ) ;
1854
+
1855
+ // create a self-signed HTTPS test server
1856
+ async function createTestHttpsServer ( ) : Promise < {
1857
+ server : https . Server ;
1858
+ host : string ;
1859
+ port : number ;
1860
+ ca : string ;
1861
+ } > {
1862
+ const host = 'localhost' ;
1863
+ const { private : key , cert } = selfsigned . generate ( [ { name : 'commonName' , value : host } ] ) ;
1864
+
1865
+ const server = https . createServer ( { key, cert } , ( _req , res ) => {
1866
+ res . writeHead ( 200 ) ;
1867
+ res . end ( 'OK' ) ;
1868
+ } ) ;
1869
+
1870
+ const port = await new Promise < number > ( ( resolve ) => {
1871
+ server . listen ( 0 , ( ) => {
1872
+ resolve ( ( server . address ( ) as AddressInfo ) . port ) ;
1873
+ } ) ;
1874
+ } ) ;
1875
+
1876
+ return {
1877
+ server,
1878
+ host,
1879
+ port,
1880
+ ca : cert , // ca is the same as cert here
1881
+ } ;
1882
+ }
0 commit comments