@@ -18,7 +18,7 @@ import { fileURLToPath } from 'node:url';
1818import mockfs from 'mock-fs' ;
1919
2020import { Authenticator } from './auth.js' ;
21- import { Headers } from 'node-fetch' ;
21+ import fetch , { Headers } from 'node-fetch' ;
2222import { HttpMethod } from './index.js' ;
2323import { assertRequestAgentsEqual , assertRequestOptionsEqual } from './test/match-buffer.js' ;
2424import { CoreV1Api , RequestContext } from './api.js' ;
@@ -27,6 +27,8 @@ import { ActionOnInvalid, Cluster, newClusters, newContexts, newUsers, User } fr
2727import { ExecAuth } from './exec_auth.js' ;
2828import { HttpProxyAgent , HttpsProxyAgent } from 'hpagent' ;
2929import { SocksProxyAgent } from 'socks-proxy-agent' ;
30+ import { AddressInfo } from 'node:net' ;
31+ import selfsigned from 'selfsigned' ;
3032
3133const kcFileName = 'testdata/kubeconfig.yaml' ;
3234const kc2FileName = 'testdata/kubeconfig-2.yaml' ;
@@ -491,6 +493,28 @@ describe('KubeConfig', () => {
491493
492494 strictEqual ( rc . getAgent ( ) instanceof https . Agent , true ) ;
493495 } ) ;
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+ } ) ;
494518 } ) ;
495519
496520 describe ( 'loadClusterConfigObjects' , ( ) => {
@@ -1827,3 +1851,32 @@ describe('KubeConfig', () => {
18271851 } ) ;
18281852 } ) ;
18291853} ) ;
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