Skip to content

Commit 82c82bd

Browse files
committed
fix: remove unused request dependency
1 parent 465a920 commit 82c82bd

File tree

6 files changed

+76
-12
lines changed

6 files changed

+76
-12
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
"isomorphic-ws": "^4.0.1",
6969
"js-yaml": "^4.1.0",
7070
"jsonpath-plus": "^0.19.0",
71-
"node-fetch": "^2.6.0",
71+
"node-fetch": "^2.6.9",
7272
"openid-client": "^4.1.1",
7373
"rfc4648": "^1.3.0",
7474
"shelljs": "^0.8.5",

src/config.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,9 @@ export class KubeConfig implements SecurityAuthentication {
159159
headers.set(key, val.toString());
160160
}
161161
}
162+
if (opts.auth) {
163+
headers.set('Authorization', 'Basic ' + Buffer.from(opts.auth).toString('base64'));
164+
}
162165
return {
163166
agent: opts.agent,
164167
headers,
@@ -185,6 +188,23 @@ export class KubeConfig implements SecurityAuthentication {
185188
agentOptions.pfx = opts.pfx;
186189
agentOptions.passphrase = opts.passphrase;
187190
agentOptions.rejectUnauthorized = opts.rejectUnauthorized;
191+
agentOptions.timeout = opts.timeout;
192+
agentOptions.host = opts.host;
193+
agentOptions.path = opts.path;
194+
agentOptions.servername = opts.servername;
195+
agentOptions.ciphers = opts.ciphers;
196+
agentOptions.honorCipherOrder = opts.honorCipherOrder;
197+
agentOptions.ecdhCurve = opts.ecdhCurve;
198+
agentOptions.clientCertEngine = opts.clientCertEngine;
199+
agentOptions.crl = opts.crl;
200+
agentOptions.dhparam = opts.dhparam;
201+
agentOptions.secureOptions = opts.secureOptions;
202+
agentOptions.secureProtocol = opts.secureProtocol;
203+
agentOptions.sessionIdContext = opts.sessionIdContext;
204+
// setting undefined will cause tls error
205+
if (opts.hasOwnProperty('port')) {
206+
agentOptions.port = typeof opts.port === 'string' ? parseInt(opts.port, 10) : opts.port;
207+
}
188208

189209
opts.agent = new https.Agent(agentOptions);
190210
}

src/config_test.ts

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { readFileSync } from 'fs';
22
import * as https from 'https';
33
import { join } from 'path';
4-
import { RequestOptions } from 'https';
4+
import { RequestOptions, Agent } from 'https';
55

66
import { expect, use } from 'chai';
77
import chaiAsPromised from 'chai-as-promised';
@@ -13,7 +13,8 @@ import { bufferFromFileOrString, findHomeDir, findObject, KubeConfig, makeAbsolu
1313
import { Cluster, newClusters, newContexts, newUsers, User, ActionOnInvalid } from './config_types';
1414
import { ExecAuth } from './exec_auth';
1515
import { HttpMethod } from '.';
16-
import { assertRequestOptionsEqual } from '../test/match-buffer';
16+
import { assertRequestOptionsEqual, assertRequestAgentsEqual } from '../test/match-buffer';
17+
import { Headers } from 'node-fetch';
1718

1819
const kcFileName = 'testdata/kubeconfig.yaml';
1920
const kc2FileName = 'testdata/kubeconfig-2.yaml';
@@ -237,6 +238,47 @@ describe('KubeConfig', () => {
237238
});
238239
});
239240

241+
describe('applytoFetchOptions', () => {
242+
it('should apply cert configs', async () => {
243+
const kc = new KubeConfig();
244+
kc.loadFromFile(kcFileName);
245+
kc.setCurrentContext('passwd');
246+
247+
const opts: https.RequestOptions = {
248+
method: 'POST',
249+
timeout: 5,
250+
headers: {
251+
number: 5,
252+
string: 'str',
253+
empty: undefined,
254+
list: ['a', 'b'],
255+
},
256+
};
257+
const requestInit = await kc.applytoFetchOptions(opts);
258+
const expectedCA = Buffer.from('CADATA2', 'utf-8');
259+
const expectedAgent = new https.Agent({
260+
ca: expectedCA,
261+
rejectUnauthorized: false,
262+
});
263+
264+
expect(requestInit.method).to.equal('POST');
265+
expect(requestInit.timeout).to.equal(5);
266+
expect((requestInit.headers as Headers).raw()).to.deep.equal({
267+
"list": [
268+
"a",
269+
"b",
270+
],
271+
"number": [
272+
"5",
273+
],
274+
"string": [
275+
"str",
276+
],
277+
});
278+
assertRequestAgentsEqual(requestInit.agent as Agent, expectedAgent);
279+
});
280+
});
281+
240282
describe('applyHTTPSOptions', () => {
241283
it('should apply cert configs', () => {
242284
const kc = new KubeConfig();

src/metrics.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ export class Metrics {
8686

8787
const requestInit = await this.config.applytoFetchOptions({
8888
method: 'GET',
89-
servername: cluster.server + path,
9089
});
9190

9291
return fetch(requestURL, requestInit)

test/match-buffer.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,7 @@ class StringBufferMatcher extends Matcher {
2828
}
2929
}
3030

31-
export function assertRequestOptionsEqual(options1: RequestOptions, options2: RequestOptions): void {
32-
//@ts-ignore agent has type Agent | Boolean which we expect to be populated with Agent here
33-
const agent1: Agent = options1.agent
34-
//@ts-ignore
35-
const agent2: Agent = options2.agent
36-
31+
export function assertRequestAgentsEqual(agent1: Agent, agent2: Agent): void {
3732
const BUFFER_EQUAL_TRUE = 0
3833
const ca1 = agent1.options.ca
3934
const ca2 = agent2.options.ca
@@ -55,10 +50,18 @@ export function assertRequestOptionsEqual(options1: RequestOptions, options2: Re
5550
if(key1 !== key2 && Buffer.compare(key1,key2) !== BUFFER_EQUAL_TRUE){
5651
throw("unequal agent key buffer")
5752
}
58-
53+
5954
expect(agent1.options.passphrase).to.equal(agent2.options.passphrase);
6055
expect(agent1.options.pfx).to.equal(agent2.options.pfx);
6156
expect(agent1.options.rejectUnauthorized).to.equal(agent2.options.rejectUnauthorized);
57+
}
58+
59+
export function assertRequestOptionsEqual(options1: RequestOptions, options2: RequestOptions): void {
60+
//@ts-ignore agent has type Agent | Boolean which we expect to be populated with Agent here
61+
const agent1: Agent = options1.agent
62+
//@ts-ignore
63+
const agent2: Agent = options2.agent
64+
assertRequestAgentsEqual(agent1, agent2)
6265

6366
expect(options1.auth).to.equal(options2.auth)
6467
expect(options1.headers).to.deep.equal(options2.headers)

0 commit comments

Comments
 (0)