Skip to content
Open
Show file tree
Hide file tree
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
24 changes: 24 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
"nock": "^14.0.5",
"prettier": "^3.0.0",
"pretty-quick": "^4.0.0",
"selfsigned": "^3.0.1",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, selfsigned looks to be about 1.7MB, and it is only used in one test. It might be simpler to use a fixture certificate.

"ts-mockito": "^2.3.1",
"tsx": "^4.19.1",
"typedoc": "^0.28.0",
Expand Down
8 changes: 6 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,9 @@ export class KubeConfig implements SecurityAuthentication {
agentOptions.key = opts.key;
agentOptions.pfx = opts.pfx;
agentOptions.passphrase = opts.passphrase;
agentOptions.rejectUnauthorized = opts.rejectUnauthorized;
if (opts.rejectUnauthorized !== undefined) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes in this file are very likely to be refactored in the future by someone missing the context of this PR.

Can you either add comments to the two code blocks in this file, or rewrite as something along the lines of agentOptions.rejectUnauthorized = opts.rejectUnauthorized ?? process.env.NODE_TLS_REJECT_UNAUTHORIZED !== '0';. If you go with the second option, it probably makes sense to put the logic in a function, similar to what Node core does.

agentOptions.rejectUnauthorized = opts.rejectUnauthorized;
}
// The ws docs say that it accepts anything that https.RequestOptions accepts,
// but Typescript doesn't understand that idea (yet) probably could be fixed in
// the typings, but for now just cast to any
Expand Down Expand Up @@ -259,7 +261,9 @@ export class KubeConfig implements SecurityAuthentication {
agentOptions.key = httpsOptions.key;
agentOptions.pfx = httpsOptions.pfx;
agentOptions.passphrase = httpsOptions.passphrase;
agentOptions.rejectUnauthorized = httpsOptions.rejectUnauthorized;
if (httpsOptions.rejectUnauthorized !== undefined) {
agentOptions.rejectUnauthorized = httpsOptions.rejectUnauthorized;
}

context.setAgent(this.createAgent(cluster, agentOptions));
}
Expand Down
94 changes: 93 additions & 1 deletion src/config_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import mockfs from 'mock-fs';

import { Authenticator } from './auth.js';
import { Headers } from 'node-fetch';
import fetch, { Headers } from 'node-fetch';
import { HttpMethod } from './index.js';
import { assertRequestAgentsEqual, assertRequestOptionsEqual } from './test/match-buffer.js';
import { CoreV1Api, RequestContext } from './api.js';
Expand All @@ -27,6 +27,8 @@
import { ExecAuth } from './exec_auth.js';
import { HttpProxyAgent, HttpsProxyAgent } from 'hpagent';
import { SocksProxyAgent } from 'socks-proxy-agent';
import { AddressInfo } from 'node:net';
import selfsigned from 'selfsigned';

const kcFileName = 'testdata/kubeconfig.yaml';
const kc2FileName = 'testdata/kubeconfig-2.yaml';
Expand Down Expand Up @@ -491,6 +493,63 @@

strictEqual(rc.getAgent() instanceof https.Agent, true);
});

it('should apply NODE_TLS_REJECT_UNAUTHORIZED from environment to agent', async () => {
const { server, host, port } = await createTestHttpsServer((req, res) => {
res.setHeader('Content-Type', 'application/json');
if (req.url?.includes('/api/v1/namespaces')) {
res.writeHead(200);
res.end(
JSON.stringify({
apiVersion: 'v1',
kind: 'NamespaceList',
items: [
{
apiVersion: 'v1',
kind: 'Namespace',
metadata: { name: 'default' },
},
],
}),
);
} else {
res.writeHead(200);
res.end('ok');
}
});

const originalValue = process.env.NODE_TLS_REJECT_UNAUTHORIZED;
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
after(() => {
server.close();
process.env.NODE_TLS_REJECT_UNAUTHORIZED = originalValue;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would reverse the order of these two lines in case server.close() throws for some reason.

});

const kc = new KubeConfig();
kc.loadFromClusterAndUser(
{
name: 'test-cluster',
server: `https://${host}:${port}`,
// ignore skipTLSVerify specified from environment variables
} as Cluster,
{
name: 'test-user',
token: 'test-token',
},
);
const coreV1Api = kc.makeApiClient(CoreV1Api);
const namespaceList = await coreV1Api.listNamespace();

strictEqual(namespaceList.kind, 'NamespaceList');
strictEqual(namespaceList.items.length, 1);
strictEqual(namespaceList.items[0].metadata?.name, 'default');

const res2 = await fetch(`https://${host}:${port}`, await kc.applyToFetchOptions({}));
strictEqual(res2.status, 200);
strictEqual(await res2.text(), 'ok');

delete process.env.NODE_TLS_REJECT_UNAUTHORIZED;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this line necessary since the after() hook is assigning a value to process.env.NODE_TLS_REJECT_UNAUTHORIZED?

});
});

describe('loadClusterConfigObjects', () => {
Expand Down Expand Up @@ -1827,3 +1886,36 @@
});
});
});

// create a self-signed HTTPS test server
async function createTestHttpsServer(
requestHandler?: (req: http.IncomingMessage, res: http.ServerResponse) => void,
): Promise<{
server: https.Server;
host: string;
port: number;
ca: string;
}> {
const host = 'localhost';
const { private: key, cert } = selfsigned.generate([{ name: 'commonName', value: host }]);

const defaultHandler = (req: http.IncomingMessage, res: http.ServerResponse) => {
res.writeHead(200);
res.end('ok');
};

const server = https.createServer({ key, cert }, requestHandler ?? defaultHandler);

const port = await new Promise<number>((resolve) => {
server.listen(0, () => {
resolve((server.address() as AddressInfo).port);
});
});

return {
server,
host,
port,
ca: cert, // ca is the same as cert here
};
}
Loading