Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions src/config_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import { bufferFromFileOrString, findHomeDir, findObject, KubeConfig, makeAbsolu
import { ActionOnInvalid, Cluster, newClusters, newContexts, newUsers, User } from './config_types.js';
import { ExecAuth } from './exec_auth.js';
import { Agent as UndiciAgent, ProxyAgent as UndiciProxyAgent } from 'undici';
import { SocksProxyAgent } from 'socks-proxy-agent';
import { HttpProxyAgent, HttpsProxyAgent } from 'hpagent';
import { AddressInfo } from 'node:net';

const kcFileName = 'testdata/kubeconfig.yaml';
Expand Down Expand Up @@ -506,6 +508,56 @@ describe('KubeConfig', () => {

strictEqual(rc.getDispatcher() instanceof UndiciAgent, true);
});
it('should apply socks proxy agent via applyToHTTPSOptions', async () => {
const kc = new KubeConfig();
kc.loadFromFile(kcProxyUrl);
kc.setCurrentContext('contextA');

const opts: https.RequestOptions = {};
await kc.applyToHTTPSOptions(opts);

strictEqual(opts.agent instanceof SocksProxyAgent, true);
});
it('should apply https proxy agent via applyToHTTPSOptions', async () => {
const kc = new KubeConfig();
kc.loadFromFile(kcProxyUrl);
kc.setCurrentContext('contextB');

const opts: https.RequestOptions = {};
await kc.applyToHTTPSOptions(opts);

strictEqual(opts.agent instanceof HttpsProxyAgent, true);
});
it('should apply http proxy agent via applyToHTTPSOptions', async () => {
const kc = new KubeConfig();
kc.loadFromFile(kcProxyUrl);
kc.setCurrentContext('contextC');

const opts: https.RequestOptions = {};
await kc.applyToHTTPSOptions(opts);

strictEqual(opts.agent instanceof HttpProxyAgent, true);
});
it('should throw unsupported proxy type via applyToHTTPSOptions', async () => {
const kc = new KubeConfig();
kc.loadFromFile(kcProxyUrl);
kc.setCurrentContext('contextD');

const opts: https.RequestOptions = {};
await rejects(kc.applyToHTTPSOptions(opts), {
message: 'Unsupported proxy type',
});
});
it('should throw if cluster.server starts with http and skipTLSVerify is not set via applyToHTTPSOptions', async () => {
const kc = new KubeConfig();
kc.loadFromFile(kcProxyUrl);
kc.setCurrentContext('contextF');

const opts: https.RequestOptions = {};
await rejects(kc.applyToHTTPSOptions(opts), {
message: 'HTTP protocol is not allowed when skipTLSVerify is not set or false',
});
});

it('should apply NODE_TLS_REJECT_UNAUTHORIZED from environment to agent', async () => {
const { server, host, port } = await createTestHttpsServer((req, res) => {
Expand Down