@@ -33,6 +33,8 @@ import {
33
33
import { OpenIDConnectAuth } from './oidc_auth.js' ;
34
34
import WebSocket from 'isomorphic-ws' ;
35
35
import child_process from 'node:child_process' ;
36
+ import { SocksProxyAgent } from 'socks-proxy-agent' ;
37
+ import { HttpProxyAgent , HttpProxyAgentOptions , HttpsProxyAgent , HttpsProxyAgentOptions } from 'hpagent' ;
36
38
37
39
const SERVICEACCOUNT_ROOT : string = '/var/run/secrets/kubernetes.io/serviceaccount' ;
38
40
const SERVICEACCOUNT_CA_PATH : string = SERVICEACCOUNT_ROOT + '/ca.crt' ;
@@ -171,6 +173,7 @@ export class KubeConfig implements SecurityAuthentication {
171
173
172
174
public async applyToHTTPSOptions ( opts : https . RequestOptions | WebSocket . ClientOptions ) : Promise < void > {
173
175
const user = this . getCurrentUser ( ) ;
176
+ const cluster = this . getCurrentCluster ( ) ;
174
177
175
178
await this . applyOptions ( opts ) ;
176
179
@@ -205,7 +208,7 @@ export class KubeConfig implements SecurityAuthentication {
205
208
agentOptions . secureProtocol = opts . secureProtocol ;
206
209
agentOptions . sessionIdContext = opts . sessionIdContext ;
207
210
208
- opts . agent = new https . Agent ( agentOptions ) ;
211
+ opts . agent = this . createAgent ( cluster , agentOptions ) ;
209
212
}
210
213
211
214
/**
@@ -248,7 +251,7 @@ export class KubeConfig implements SecurityAuthentication {
248
251
agentOptions . passphrase = httpsOptions . passphrase ;
249
252
agentOptions . rejectUnauthorized = httpsOptions . rejectUnauthorized ;
250
253
251
- context . setAgent ( new https . Agent ( agentOptions ) ) ;
254
+ context . setAgent ( this . createAgent ( cluster , agentOptions ) ) ;
252
255
}
253
256
254
257
/**
@@ -509,6 +512,32 @@ export class KubeConfig implements SecurityAuthentication {
509
512
return this . getContextObject ( this . currentContext ) ;
510
513
}
511
514
515
+ private createAgent (
516
+ cluster : Cluster | null ,
517
+ agentOptions : https . AgentOptions ,
518
+ ) : https . Agent | SocksProxyAgent | HttpProxyAgent | HttpsProxyAgent {
519
+ let agent : https . Agent | SocksProxyAgent | HttpProxyAgent | HttpsProxyAgent ;
520
+
521
+ if ( cluster && cluster . proxyUrl ) {
522
+ if ( cluster . proxyUrl . startsWith ( 'socks' ) ) {
523
+ agent = new SocksProxyAgent ( cluster . proxyUrl , agentOptions ) ;
524
+ } else if ( cluster . server . startsWith ( 'https' ) ) {
525
+ const httpsProxyAgentOptions : HttpsProxyAgentOptions = agentOptions as HttpsProxyAgentOptions ;
526
+ httpsProxyAgentOptions . proxy = cluster . proxyUrl ;
527
+ agent = new HttpsProxyAgent ( httpsProxyAgentOptions ) ;
528
+ } else if ( cluster . server . startsWith ( 'http' ) ) {
529
+ const httpProxyAgentOptions : HttpProxyAgentOptions = agentOptions as HttpProxyAgentOptions ;
530
+ httpProxyAgentOptions . proxy = cluster . proxyUrl ;
531
+ agent = new HttpProxyAgent ( httpProxyAgentOptions ) ;
532
+ } else {
533
+ throw new Error ( 'Unsupported proxy type' ) ;
534
+ }
535
+ } else {
536
+ agent = new https . Agent ( agentOptions ) ;
537
+ }
538
+ return agent ;
539
+ }
540
+
512
541
private applyHTTPSOptions ( opts : https . RequestOptions | WebSocket . ClientOptions ) : void {
513
542
const cluster = this . getCurrentCluster ( ) ;
514
543
const user = this . getCurrentUser ( ) ;
0 commit comments