Skip to content

Commit a2c68d6

Browse files
committed
Fix https types
Fix indentation on user.token Rename applyAuthOptions to applyAuthorizationHeader
1 parent 8242346 commit a2c68d6

File tree

3 files changed

+51
-13
lines changed

3 files changed

+51
-13
lines changed

node-client/src/config.ts

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import fs = require('fs');
22
import os = require('os');
33
import path = require('path');
4+
import https = require('https');
45

56
import base64 = require('base-64');
67
import jsonpath = require('jsonpath');
@@ -80,15 +81,15 @@ export class KubeConfig {
8081
return this.getCluster(this.getCurrentContextObject()['cluster']);
8182
}
8283

83-
public getCluster(name: string) {
84+
public getCluster(name: string): Cluster {
8485
return KubeConfig.findObject(this.clusters, name, 'cluster');
8586
}
8687

8788
public getCurrentUser() {
8889
return this.getUser(this.getCurrentContextObject()['user']);
8990
}
9091

91-
public getUser(name: string) {
92+
public getUser(name: string): User {
9293
return KubeConfig.findObject(this.users, name, 'user');
9394
}
9495

@@ -106,22 +107,25 @@ export class KubeConfig {
106107
return null;
107108
}
108109

109-
public applyToRequest(opts: request.Options) {
110-
let cluster = this.getCurrentCluster();
111-
let user = this.getCurrentUser();
110+
private applyHTTPSOptions(opts: request.Options | https.RequestOptions) {
111+
const cluster = this.getCurrentCluster();
112+
const user = this.getCurrentUser();
112113

113-
if (cluster.skipTLSVerify) {
114-
opts.strictSSL = false
115-
}
116114
opts.ca = this.bufferFromFileOrString(cluster.caFile, cluster.caData);
117115
opts.cert = this.bufferFromFileOrString(user.certFile, user.certData);
118116
opts.key = this.bufferFromFileOrString(user.keyFile, user.keyData);
117+
}
118+
119+
private applyAuthorizationHeader(opts: request.Options | https.RequestOptions) {
120+
const user = this.getCurrentUser();
119121
let token = null;
122+
120123
if (user.authProvider && user.authProvider.config) {
121-
let config = user.authProvider.config;
124+
const config = user.authProvider.config;
122125
// This should probably be extracted as auth-provider specific plugins...
123126
token = 'Bearer ' + config['access-token'];
124-
let expiry = config['expiry'];
127+
const expiry = config['expiry'];
128+
125129
if (expiry) {
126130
let expiration = Date.parse(expiry);
127131
if (expiration < Date.now()) {
@@ -131,7 +135,7 @@ export class KubeConfig {
131135
cmd = cmd + ' ' + config['cmd-args'];
132136
}
133137
// TODO: Cache to file?
134-
let result = shelljs.exec(cmd, { silent: true });
138+
const result = shelljs.exec(cmd, { silent: true });
135139
if (result['code'] != 0) {
136140
throw new Error('Failed to refresh token: ' + result);
137141
}
@@ -148,13 +152,43 @@ export class KubeConfig {
148152
}
149153
}
150154
}
155+
151156
}
157+
152158
if (user.token) {
153159
token = 'Bearer ' + user.token;
154160
}
161+
155162
if (token) {
156163
opts.headers['Authorization'] = token;
157164
}
165+
}
166+
167+
private applyOptions(opts: request.Options | https.RequestOptions) {
168+
this.applyHTTPSOptions(opts);
169+
this.applyAuthorizationHeader(opts);
170+
}
171+
172+
public applytoHTTPsOptions(opts: https.RequestOptions) {
173+
const user = this.getCurrentUser();
174+
175+
this.applyOptions(opts);
176+
177+
if (user.username) {
178+
opts.auth = `${user.username}:${user.password}`;
179+
}
180+
}
181+
182+
public applyToRequest(opts: request.Options) {
183+
const cluster = this.getCurrentCluster();
184+
const user = this.getCurrentUser();
185+
186+
this.applyOptions(opts);
187+
188+
if (cluster.skipTLSVerify) {
189+
opts.strictSSL = false
190+
}
191+
158192
if (user.username) {
159193
opts.auth = {
160194
username: user.username,

node-client/src/config_types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export interface User {
3737
readonly certFile: string
3838
readonly keyData: string
3939
readonly keyFile: string
40-
readonly authProvider: Object
40+
readonly authProvider: any
4141
readonly token: string
4242
readonly username: string
4343
readonly password: string

node-client/src/web-socket-handler.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import stream = require('stream');
2+
import https = require('https');
23

34
import ws = require('websocket');
45
import { KubeConfig } from './config';
@@ -32,7 +33,10 @@ export class WebSocketHandler {
3233
const target = server.startsWith('https://') ? server.substr(8) : server.substr(7);
3334
const uri = `wss://${target}${path}`;
3435

35-
const client = new ws.client();
36+
const opts : https.RequestOptions = {};
37+
this.config.applytoHTTPsOptions(opts)
38+
39+
const client = new ws.client({ tlsOptions: opts } );
3640

3741
return new Promise((resolve, reject) => {
3842
client.on('connect', (connection) => {

0 commit comments

Comments
 (0)