Skip to content

Commit 0f5694e

Browse files
authored
Merge pull request #2111 from krmodelski/proxy-url-release-1.x
Add proxy support
2 parents be116d7 + b7a2509 commit 0f5694e

File tree

7 files changed

+347
-9
lines changed

7 files changed

+347
-9
lines changed

package-lock.json

Lines changed: 175 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,14 @@
6565
"@types/tar": "^6.1.1",
6666
"@types/ws": "^8.5.4",
6767
"form-data": "^4.0.0",
68+
"hpagent": "^1.2.0",
6869
"isomorphic-ws": "^5.0.0",
6970
"js-yaml": "^4.1.0",
7071
"jsonpath-plus": "^10.2.0",
7172
"node-fetch": "^2.6.9",
7273
"openid-client": "^6.1.3",
7374
"rfc4648": "^1.3.0",
75+
"socks-proxy-agent": "^8.0.4",
7476
"stream-buffers": "^3.0.2",
7577
"tar": "^7.0.0",
7678
"tmp-promise": "^3.0.2",

src/config.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ import {
3333
import { OpenIDConnectAuth } from './oidc_auth.js';
3434
import WebSocket from 'isomorphic-ws';
3535
import child_process from 'node:child_process';
36+
import { SocksProxyAgent } from 'socks-proxy-agent';
37+
import { HttpProxyAgent, HttpProxyAgentOptions, HttpsProxyAgent, HttpsProxyAgentOptions } from 'hpagent';
3638

3739
const SERVICEACCOUNT_ROOT: string = '/var/run/secrets/kubernetes.io/serviceaccount';
3840
const SERVICEACCOUNT_CA_PATH: string = SERVICEACCOUNT_ROOT + '/ca.crt';
@@ -171,6 +173,7 @@ export class KubeConfig implements SecurityAuthentication {
171173

172174
public async applyToHTTPSOptions(opts: https.RequestOptions | WebSocket.ClientOptions): Promise<void> {
173175
const user = this.getCurrentUser();
176+
const cluster = this.getCurrentCluster();
174177

175178
await this.applyOptions(opts);
176179

@@ -205,7 +208,7 @@ export class KubeConfig implements SecurityAuthentication {
205208
agentOptions.secureProtocol = opts.secureProtocol;
206209
agentOptions.sessionIdContext = opts.sessionIdContext;
207210

208-
opts.agent = new https.Agent(agentOptions);
211+
opts.agent = this.createAgent(cluster, agentOptions);
209212
}
210213

211214
/**
@@ -248,7 +251,7 @@ export class KubeConfig implements SecurityAuthentication {
248251
agentOptions.passphrase = httpsOptions.passphrase;
249252
agentOptions.rejectUnauthorized = httpsOptions.rejectUnauthorized;
250253

251-
context.setAgent(new https.Agent(agentOptions));
254+
context.setAgent(this.createAgent(cluster, agentOptions));
252255
}
253256

254257
/**
@@ -509,6 +512,32 @@ export class KubeConfig implements SecurityAuthentication {
509512
return this.getContextObject(this.currentContext);
510513
}
511514

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+
512541
private applyHTTPSOptions(opts: https.RequestOptions | WebSocket.ClientOptions): void {
513542
const cluster = this.getCurrentCluster();
514543
const user = this.getCurrentUser();

0 commit comments

Comments
 (0)