Skip to content

Commit a01b704

Browse files
authored
Merge pull request #888 from tigrato/tls-server-name
Add `tls-server-name` to https requests if provided
2 parents 718f06c + 52c383a commit a01b704

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

src/config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,10 @@ export class KubeConfig {
161161
username: user.username,
162162
};
163163
}
164+
165+
if (cluster && cluster.tlsServerName) {
166+
opts.agentOptions = { servername: cluster.tlsServerName } as https.RequestOptions;
167+
}
164168
}
165169

166170
public loadFromString(config: string, opts?: Partial<ConfigOptions>): void {

src/config_test.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { CoreV1Api } from './api';
1111
import { bufferFromFileOrString, findHomeDir, findObject, KubeConfig, makeAbsolutePath } from './config';
1212
import { Cluster, newClusters, newContexts, newUsers, User, ActionOnInvalid } from './config_types';
1313
import { ExecAuth } from './exec_auth';
14+
import { request } from 'http';
1415

1516
const kcFileName = 'testdata/kubeconfig.yaml';
1617
const kc2FileName = 'testdata/kubeconfig-2.yaml';
@@ -240,6 +241,44 @@ describe('KubeConfig', () => {
240241
});
241242

242243
describe('applyHTTPSOptions', () => {
244+
it('should apply tls-server-name to https.RequestOptions', async () => {
245+
const kc = new KubeConfig();
246+
kc.loadFromFile(kcTlsServerNameFileName);
247+
248+
const opts: https.RequestOptions = {};
249+
await kc.applytoHTTPSOptions(opts);
250+
251+
expect(opts).to.deep.equal({
252+
headers: {},
253+
ca: new Buffer('CADATA2', 'utf-8'),
254+
cert: new Buffer('USER_CADATA', 'utf-8'),
255+
key: new Buffer('USER_CKDATA', 'utf-8'),
256+
rejectUnauthorized: false,
257+
servername: 'kube.example2.com',
258+
});
259+
});
260+
it('should apply tls-server-name to request.Options', async () => {
261+
const kc = new KubeConfig();
262+
kc.loadFromFile(kcTlsServerNameFileName);
263+
264+
const opts: requestlib.Options = {
265+
url: 'https://company.com',
266+
};
267+
await kc.applyToRequest(opts);
268+
269+
expect(opts).to.deep.equal({
270+
url: 'https://company.com',
271+
headers: {},
272+
ca: new Buffer('CADATA2', 'utf-8'),
273+
cert: new Buffer('USER_CADATA', 'utf-8'),
274+
key: new Buffer('USER_CKDATA', 'utf-8'),
275+
rejectUnauthorized: false,
276+
strictSSL: false,
277+
agentOptions: {
278+
servername: 'kube.example2.com',
279+
},
280+
});
281+
});
243282
it('should apply cert configs', () => {
244283
const kc = new KubeConfig();
245284
kc.loadFromFile(kcFileName);

0 commit comments

Comments
 (0)