Skip to content

Commit eaa072d

Browse files
authored
Merge pull request #2351 from kubernetes-client/max/http-agent
fix: apply http agent if needed
2 parents d7cfec6 + 3765992 commit eaa072d

File tree

5 files changed

+75
-3
lines changed

5 files changed

+75
-3
lines changed

src/cache_test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { ListPromise } from './informer.js';
1111
import nock from 'nock';
1212
import { Watch } from './watch.js';
1313

14-
const server = 'http://foo.company.com';
14+
const server = 'https://foo.company.com';
1515

1616
const fakeConfig: {
1717
clusters: Cluster[];

src/config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import fs from 'node:fs';
22
import https from 'node:https';
3+
import http from 'node:http';
34
import yaml from 'js-yaml';
45
import net from 'node:net';
56
import path from 'node:path';
@@ -545,6 +546,10 @@ export class KubeConfig implements SecurityAuthentication {
545546
} else {
546547
throw new Error('Unsupported proxy type');
547548
}
549+
} else if (cluster?.server?.startsWith('http:') && cluster.skipTLSVerify) {
550+
agent = new http.Agent(agentOptions);
551+
} else if (cluster?.server?.startsWith('http:') && !cluster.skipTLSVerify) {
552+
throw new Error('HTTP protocol is not allowed when skipTLSVerify is not set or false');
548553
} else {
549554
agent = new https.Agent(agentOptions);
550555
}

src/config_test.ts

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
import { after, before, beforeEach, describe, it, mock } from 'node:test';
2-
import { deepEqual, deepStrictEqual, notStrictEqual, rejects, strictEqual, throws } from 'node:assert';
2+
import assert, {
3+
deepEqual,
4+
deepStrictEqual,
5+
notStrictEqual,
6+
rejects,
7+
strictEqual,
8+
throws,
9+
} from 'node:assert';
310
import child_process from 'node:child_process';
411
import { readFileSync } from 'node:fs';
512
import https from 'node:https';
13+
import http from 'node:http';
614
import { Agent, RequestOptions } from 'node:https';
715
import path, { dirname, join } from 'node:path';
816
import { fileURLToPath } from 'node:url';
@@ -449,6 +457,40 @@ describe('KubeConfig', () => {
449457
message: 'Unsupported proxy type',
450458
});
451459
});
460+
it('should apply http agent if cluster.server starts with http and no proxy-url is provided', async () => {
461+
const kc = new KubeConfig();
462+
kc.loadFromFile(kcProxyUrl);
463+
kc.setCurrentContext('contextE');
464+
465+
const testServerName = 'http://example.com';
466+
const rc = new RequestContext(testServerName, HttpMethod.GET);
467+
468+
await kc.applySecurityAuthentication(rc);
469+
470+
strictEqual(rc.getAgent() instanceof http.Agent, true);
471+
});
472+
it('should throw an error if cluster.server starts with http, no proxy-url is provided and insecure-skip-tls-verify is not set', async () => {
473+
const kc = new KubeConfig();
474+
kc.loadFromFile(kcProxyUrl);
475+
kc.setCurrentContext('contextF');
476+
477+
const testServerName = 'http://example.com';
478+
const rc = new RequestContext(testServerName, HttpMethod.GET);
479+
480+
await assert.rejects(kc.applySecurityAuthentication(rc), Error);
481+
});
482+
it('should apply https agent if cluster.server starts with https and no proxy-url is provided', async () => {
483+
const kc = new KubeConfig();
484+
kc.loadFromFile(kcProxyUrl);
485+
kc.setCurrentContext('contextG');
486+
487+
const testServerName = 'https://example.com';
488+
const rc = new RequestContext(testServerName, HttpMethod.GET);
489+
490+
await kc.applySecurityAuthentication(rc);
491+
492+
strictEqual(rc.getAgent() instanceof https.Agent, true);
493+
});
452494
});
453495

454496
describe('loadClusterConfigObjects', () => {

src/watch_test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { Cluster, Context, User } from './config_types.js';
77
import { Watch } from './watch.js';
88
import { IncomingMessage } from 'node:http';
99

10-
const server = 'http://foo.company.com';
10+
const server = 'https://foo.company.com';
1111

1212
const fakeConfig: {
1313
clusters: Cluster[];

testdata/kubeconfig-proxy-url.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ clusters:
2020
server: htto://exampleerror.com
2121
proxy-url: http://example:8080
2222
name: clusterD
23+
- cluster:
24+
certificate-authority-data: Q0FEQVRA
25+
server: http://exampleerror.com
26+
insecure-skip-tls-verify: true
27+
name: clusterE
28+
- cluster:
29+
certificate-authority-data: Q0FEQVRA
30+
server: http://exampleerror.com
31+
name: clusterF
2332

2433
contexts:
2534
- context:
@@ -38,6 +47,14 @@ contexts:
3847
cluster: clusterD
3948
user: userD
4049
name: contextD
50+
- context:
51+
cluster: clusterE
52+
user: userE
53+
name: contextE
54+
- context:
55+
cluster: clusterF
56+
user: userF
57+
name: contextF
4158

4259
current-context: contextA
4360
kind: Config
@@ -59,3 +76,11 @@ users:
5976
user:
6077
client-certificate-data: XVNFUl9DQURBVEE=
6178
client-key-data: XVNFUl9DS0RBVEE=
79+
- name: userE
80+
user:
81+
client-certificate-data: XVNFUl9DQURBVEE=
82+
client-key-data: XVNFUl9DS0RBVEE=
83+
- name: userF
84+
user:
85+
client-certificate-data: XVNFUl9DQURBVEE=
86+
client-key-data: XVNFUl9DS0RBVEE=

0 commit comments

Comments
 (0)