Skip to content
Closed
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
2 changes: 2 additions & 0 deletions node-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@
"@types/js-yaml": "^3.5.31",
"@types/mocha": "^2.2.41",
"@types/node": "^8.0.2",
"@types/request": "^2.47.0",
"@types/underscore": "^1.8.1",
"@types/websocket": "0.0.38",
"chai": "^4.0.2",
"jasmine": "^2.8.0",
"mocha": "^3.4.2",
Expand Down
52 changes: 43 additions & 9 deletions node-client/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import https = require('https');
import fs = require('fs');
import os = require('os');
import path = require('path');
Expand Down Expand Up @@ -106,16 +107,8 @@ export class KubeConfig {
return null;
}

public applyToRequest(opts: request.Options) {
let cluster = this.getCurrentCluster();
private getAuthorizationToken(): string | null {
let user = this.getCurrentUser();

if (cluster.skipTLSVerify) {
opts.strictSSL = false
}
opts.ca = this.bufferFromFileOrString(cluster.caFile, cluster.caData);
opts.cert = this.bufferFromFileOrString(user.certFile, user.certData);
opts.key = this.bufferFromFileOrString(user.keyFile, user.keyData);
let token = null;
if (user.authProvider && user.authProvider.config) {
let config = user.authProvider.config;
Expand Down Expand Up @@ -152,6 +145,47 @@ export class KubeConfig {
if (user.token) {
token = 'Bearer ' + user.token;
}
return token;
}

private getHttpsCredentials() {
const cluster = this.getCurrentCluster();
const user = this.getCurrentUser();

return {
ca: this.bufferFromFileOrString(cluster.caFile, cluster.caData),
cert: this.bufferFromFileOrString(user.certFile, user.certData),
key: this.bufferFromFileOrString(user.keyFile, user.keyData),
};
}

public applyToHttpsOptions(opts: https.RequestOptions) {
const user = this.getCurrentUser();
const { ca, cert, key } = this.getHttpsCredentials();
opts.ca = ca;
opts.cert = cert;
opts.key = key;
const token = this.getAuthorizationToken();
if (token) {
opts.headers['Authorization'] = token;
}
if (user.username) {
opts.auth = `${user.username}:${user.password}`;
}
}

public applyToRequest(opts: request.Options) {
let cluster = this.getCurrentCluster();
let user = this.getCurrentUser();

if (cluster.skipTLSVerify) {
opts.strictSSL = false
}
const { ca, cert, key } = this.getHttpsCredentials();
opts.ca = ca;
opts.cert = cert;
opts.key = key;
const token = this.getAuthorizationToken();
if (token) {
opts.headers['Authorization'] = token;
}
Expand Down
5 changes: 3 additions & 2 deletions node-client/src/exec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import querystring = require('querystring');
import stream = require('stream');

import { connection as wsConnection } from 'websocket';
import { WebSocketHandler } from './web-socket-handler';
import { KubeConfig } from './config';

Expand All @@ -12,7 +13,7 @@ export class Exec {
}

// TODO: make command an array and support multiple args
public async exec(namespace: string, podName: string, containerName: string, command: string, stdout: stream.Writable | any, stderr: stream.Writable | any, stdin: stream.Readable | any, tty: boolean): Promise<WebSocket> {
public async exec(namespace: string, podName: string, containerName: string, command: string, stdout: stream.Writable | any, stderr: stream.Writable | any, stdin: stream.Readable | any, tty: boolean): Promise<wsConnection> {
var query = {
stdout: stdout != null,
stderr: stderr != null,
Expand All @@ -29,6 +30,6 @@ export class Exec {
if (stdin != null) {
WebSocketHandler.handleStandardInput(conn, stdin);
}
return conn as WebSocket;
return conn;
}
}
17 changes: 9 additions & 8 deletions node-client/src/web-socket-handler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import https = require('https');
import stream = require('stream');

import ws = require('websocket');
import { connection as wsConnection, client as wsClient } from 'websocket';
import { KubeConfig } from './config';
import { V1Status } from './api';

Expand All @@ -26,10 +27,10 @@ export class WebSocketHandler {

public connect(path: string,
textHandler: (text: string) => void,
binaryHandler: (stream: number, buff: Buffer) => void): Promise<ws.connection> {
let opts = {};
this.config.applyToRequest(opts);
let client = new ws.client({ 'tlsOptions': opts });
binaryHandler: (stream: number, buff: Buffer) => void): Promise<wsConnection> {
let opts: https.RequestOptions = {};
this.config.applyToHttpsOptions(opts);
let client = new wsClient({ 'tlsOptions': opts });

return new Promise((resolve, reject) => {
client.on('connect', (connection) => {
Expand All @@ -41,7 +42,7 @@ export class WebSocketHandler {
}
else if (message.type === 'binary') {
if (binaryHandler) {
let stream = message.binaryData.readInt8();
let stream = message.binaryData.readInt8(0);
binaryHandler(stream, message.binaryData.slice(1));
}
}
Expand Down Expand Up @@ -87,7 +88,7 @@ export class WebSocketHandler {
return null;
}

public static handleStandardInput(conn: ws.connection, stdin: stream.Readable | any) {
public static handleStandardInput(conn: wsConnection, stdin: stream.Readable | any) {
stdin.on('data', (data) => {
let buff = new Buffer(data.length + 1);
buff.writeInt8(0, 0);
Expand All @@ -100,7 +101,7 @@ export class WebSocketHandler {
});

stdin.on('end', () => {
conn.close(ws.connection.CLOSE_REASON_NORMAL);
conn.close();
});
}
}