Skip to content

Commit 3765992

Browse files
committed
fix: check for skipTlsVerify before allowing http connection
1 parent 692e449 commit 3765992

File tree

5 files changed

+49
-5
lines changed

5 files changed

+49
-5
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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,8 +545,10 @@ export class KubeConfig implements SecurityAuthentication {
545545
} else {
546546
throw new Error('Unsupported proxy type');
547547
}
548-
} else if (cluster?.server?.startsWith('http:')) {
548+
} else if (cluster?.server?.startsWith('http:') && cluster.skipTLSVerify) {
549549
agent = new http.Agent(agentOptions);
550+
} else if (cluster?.server?.startsWith('http:') && !cluster.skipTLSVerify) {
551+
throw new Error('HTTP protocol is not allowed when skipTLSVerify is not set or false');
550552
} else {
551553
agent = new https.Agent(agentOptions);
552554
}

src/config_test.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
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';
@@ -461,11 +468,21 @@ describe('KubeConfig', () => {
461468

462469
strictEqual(rc.getAgent() instanceof http.Agent, true);
463470
});
464-
it('should apply https agent if cluster.server starts with https and no proxy-url is provided', async () => {
471+
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 () => {
465472
const kc = new KubeConfig();
466473
kc.loadFromFile(kcProxyUrl);
467474
kc.setCurrentContext('contextF');
468475

476+
const testServerName = 'http://example.com';
477+
const rc = new RequestContext(testServerName, HttpMethod.GET);
478+
479+
await assert.rejects(kc.applySecurityAuthentication(rc), Error);
480+
});
481+
it('should apply https agent if cluster.server starts with https and no proxy-url is provided', async () => {
482+
const kc = new KubeConfig();
483+
kc.loadFromFile(kcProxyUrl);
484+
kc.setCurrentContext('contextG');
485+
469486
const testServerName = 'https://example.com';
470487
const rc = new RequestContext(testServerName, HttpMethod.GET);
471488

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)