Skip to content

Commit 44d8582

Browse files
committed
add proxy support
1 parent 980765f commit 44d8582

File tree

5 files changed

+201
-8
lines changed

5 files changed

+201
-8
lines changed

package-lock.json

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

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@
7272
"tar": "^7.0.0",
7373
"tmp-promise": "^3.0.2",
7474
"tslib": "^2.5.0",
75-
"ws": "^8.18.0"
75+
"ws": "^8.18.0",
76+
"socks-proxy-agent": "^8.0.4",
77+
"hpagent": "^1.2.0"
7678
},
7779
"devDependencies": {
7880
"@types/chai": "^5.0.0",

src/config.ts

Lines changed: 25 additions & 1 deletion
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, HttpsProxyAgent } from 'hpagent';
3638

3739
const SERVICEACCOUNT_ROOT: string = '/var/run/secrets/kubernetes.io/serviceaccount';
3840
const SERVICEACCOUNT_CA_PATH: string = SERVICEACCOUNT_ROOT + '/ca.crt';
@@ -248,7 +250,29 @@ export class KubeConfig implements SecurityAuthentication {
248250
agentOptions.passphrase = httpsOptions.passphrase;
249251
agentOptions.rejectUnauthorized = httpsOptions.rejectUnauthorized;
250252

251-
context.setAgent(new https.Agent(agentOptions));
253+
let agent: https.Agent | SocksProxyAgent | HttpProxyAgent | HttpsProxyAgent;
254+
255+
if (cluster && cluster.proxyUrl) {
256+
if (cluster.proxyUrl.startsWith('socks')) {
257+
agent = new SocksProxyAgent(cluster.proxyUrl, agentOptions);
258+
} else if (cluster.proxyUrl.startsWith('http')) {
259+
agent = new HttpsProxyAgent({
260+
proxy: cluster.proxyUrl,
261+
...agentOptions,
262+
});
263+
} else if (cluster.proxyUrl.startsWith('https')) {
264+
agent = new HttpProxyAgent({
265+
proxy: cluster.proxyUrl,
266+
...agentOptions,
267+
});
268+
} else {
269+
throw new Error('Unsupported proxy type');
270+
}
271+
} else {
272+
agent = new https.Agent(agentOptions);
273+
}
274+
275+
context.setAgent(agent);
252276
}
253277

254278
/**

src/config_types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export interface Cluster {
2222
readonly server: string;
2323
readonly tlsServerName?: string;
2424
readonly skipTLSVerify: boolean;
25+
readonly proxyUrl?: string;
2526
}
2627

2728
export function newClusters(a: any, opts?: Partial<ConfigOptions>): Cluster[] {
@@ -43,6 +44,7 @@ export function exportCluster(cluster: Cluster): any {
4344
'certificate-authority': cluster.caFile,
4445
'insecure-skip-tls-verify': cluster.skipTLSVerify,
4546
'tls-server-name': cluster.tlsServerName,
47+
'proxy-url': cluster.proxyUrl,
4648
},
4749
};
4850
}
@@ -68,6 +70,7 @@ function clusterIterator(
6870
server: elt.cluster.server.replace(/\/$/, ''),
6971
skipTLSVerify: elt.cluster['insecure-skip-tls-verify'] === true,
7072
tlsServerName: elt.cluster['tls-server-name'],
73+
proxyUrl: elt.cluster['proxy-url'],
7174
};
7275
} catch (err) {
7376
switch (onInvalidEntry) {

testdata/kubeconfig-proxy-url.yaml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
apiVersion: v1
2+
clusters:
3+
- cluster:
4+
certificate-authority-data: Q0FEQVRA
5+
server: http://example2.com
6+
proxy-url: socks://example:1187
7+
name: clusterA
8+
9+
contexts:
10+
- context:
11+
cluster: clusterA
12+
user: userA
13+
name: contextA
14+
15+
current-context: contextA
16+
kind: Config
17+
preferences: {}
18+
users:
19+
- name: userA
20+
user:
21+
client-certificate-data: XVNFUl9DQURBVEE=
22+
client-key-data: XVNFUl9DS0RBVEE=
23+
- name: userB
24+
user:
25+
client-certificate-data: XVNFUjJfQ0FEQVRB
26+
client-key-data: XVNFUjJfQ0tEQVRB
27+
- name: userC
28+
user:
29+
username: foo
30+
password: bar

0 commit comments

Comments
 (0)