Skip to content

Commit

Permalink
Merge pull request #284 from ndeloof/electron
Browse files Browse the repository at this point in the history
fix Electron support
  • Loading branch information
k8s-ci-robot authored Jun 21, 2019
2 parents c5f9419 + 0055a50 commit 951dd13
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ examples/package-lock.json
.nyc_output
kubernetes-client-node-*.tgz
**/*.swp

.idea/
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"@types/request": "^2.47.1",
"@types/underscore": "^1.8.9",
"@types/ws": "^6.0.1",
"execa": "1.0.0",
"isomorphic-ws": "^4.0.1",
"js-yaml": "^3.13.1",
"json-stream": "^1.0.0",
Expand Down
20 changes: 8 additions & 12 deletions src/cloud_auth.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as proc from 'child_process';
import * as jsonpath from 'jsonpath-plus';
import * as shelljs from 'shelljs';

import { Authenticator } from './auth';
import { User } from './config_types';
Expand Down Expand Up @@ -50,27 +50,23 @@ export class CloudAuth implements Authenticator {
}

private updateAccessToken(config: Config) {
if (!config['cmd-path']) {
let cmd = config['cmd-path'];
if (!cmd) {
throw new Error('Token is expired!');
}
const args = config['cmd-args'];
if (args) {
cmd = cmd + ' ' + args;
}
// TODO: Cache to file?
// TODO: do this asynchronously
let result: any;
let output: any;
try {
let cmd = config['cmd-path'];
if (args) {
cmd = `"${cmd}" ${args}`;
}
result = shelljs.exec(cmd, { silent: true });
if (result.code !== 0) {
throw new Error(result.stderr);
}
output = proc.execSync(cmd);
} catch (err) {
throw new Error('Failed to refresh token: ' + err.message);
}

const output = result.stdout.toString();
const resultObj = JSON.parse(output);

const tokenPathKeyInConfig = config['token-key'];
Expand Down
15 changes: 9 additions & 6 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import execa = require('execa');
import fs = require('fs');
import https = require('https');
import path = require('path');
Expand Down Expand Up @@ -258,12 +259,14 @@ export class KubeConfig {
}
if (process.platform === 'win32' && shelljs.which('wsl.exe')) {
// TODO: Handle if someome set $KUBECONFIG in wsl here...
const result = shelljs.exec('wsl.exe cat $HOME/.kube/config', {
silent: true,
});
if (result.code === 0) {
this.loadFromString(result.stdout);
return;
try {
const result = execa.sync('wsl.exe', ['cat', shelljs.homedir() + '/.kube/config']);
if (result.code === 0) {
this.loadFromString(result.stdout);
return;
}
} catch (err) {
// Falling back to alternative auth
}
}

Expand Down
16 changes: 8 additions & 8 deletions src/config_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ describe('KubeConfig', () => {
it('should exec with expired token', () => {
const config = new KubeConfig();
const token = 'token';
const responseStr = `{ "token": { "accessToken": "${token}" } }`;
const responseStr = `{"token":{"accessToken":"${token}"}}`;
config.loadFromClusterAndUser(
{ skipTLSVerify: false } as Cluster,
{
Expand All @@ -737,7 +737,7 @@ describe('KubeConfig', () => {
it('should exec without access-token', () => {
const config = new KubeConfig();
const token = 'token';
const responseStr = `{ "token": { "accessToken": "${token}" } }`;
const responseStr = `{"token":{"accessToken":"${token}"}}`;
config.loadFromClusterAndUser(
{ skipTLSVerify: false } as Cluster,
{
Expand All @@ -762,7 +762,7 @@ describe('KubeConfig', () => {
it('should exec without access-token', () => {
const config = new KubeConfig();
const token = 'token';
const responseStr = `{ "token": { "accessToken": "${token}" } }`;
const responseStr = `{"token":{"accessToken":"${token}"}}`;
config.loadFromClusterAndUser(
{ skipTLSVerify: false } as Cluster,
{
Expand All @@ -787,7 +787,7 @@ describe('KubeConfig', () => {
it('should exec with exec auth and env vars', () => {
const config = new KubeConfig();
const token = 'token';
const responseStr = `'{"status": { "token": "${token}" }}'`;
const responseStr = `{"status": { "token": "${token}" }}`;
config.loadFromClusterAndUser(
{ skipTLSVerify: false } as Cluster,
{
Expand Down Expand Up @@ -819,13 +819,13 @@ describe('KubeConfig', () => {
it('should exec with exec auth', () => {
const config = new KubeConfig();
const token = 'token';
const responseStr = `'{
const responseStr = `{
"apiVersion": "client.authentication.k8s.io/v1beta1",
"kind": "ExecCredential",
"status": {
"token": "${token}"
}
}'`;
}`;
config.loadFromClusterAndUser(
{ skipTLSVerify: false } as Cluster,
{
Expand All @@ -851,13 +851,13 @@ describe('KubeConfig', () => {
it('should exec with exec auth (other location)', () => {
const config = new KubeConfig();
const token = 'token';
const responseStr = `'{
const responseStr = `{
"apiVersion": "client.authentication.k8s.io/v1beta1",
"kind": "ExecCredential",
"status": {
"token": "${token}"
}
}'`;
}`;
config.loadFromClusterAndUser(
{ skipTLSVerify: false } as Cluster,
{
Expand Down
13 changes: 5 additions & 8 deletions src/exec_auth.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import * as shell from 'shelljs';
import execa = require('execa');

import { Authenticator } from './auth';
import { User } from './config_types';

export class ExecAuth implements Authenticator {
private readonly tokenCache: { [key: string]: any } = {};
private execFn: (cmd: string, opts: shell.ExecOpts) => shell.ShellReturnValue = shell.exec;
private execFn: (cmd: string, args: string[], opts: execa.SyncOptions) => execa.ExecaSyncReturnValue =
execa.sync;

public isAuthProvider(user: User) {
if (!user) {
Expand Down Expand Up @@ -48,17 +49,13 @@ export class ExecAuth implements Authenticator {
if (!exec.command) {
throw new Error('No command was specified for exec authProvider!');
}
let cmd = exec.command;
if (exec.args) {
cmd = `${cmd} ${exec.args.join(' ')}`;
}
let opts: shell.ExecOpts = { silent: true };
let opts = {};
if (exec.env) {
const env = process.env;
exec.env.forEach((elt) => (env[elt.name] = elt.value));
opts = { ...opts, env };
}
const result = this.execFn(cmd, opts);
const result = this.execFn(exec.command, exec.args, opts);
if (result.code === 0) {
const obj = JSON.parse(result.stdout);
this.tokenCache[user.name] = obj;
Expand Down
17 changes: 13 additions & 4 deletions src/exec_auth_test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import { expect } from 'chai';
import * as shell from 'shelljs';

import execa = require('execa');
import { ExecAuth } from './exec_auth';

describe('ExecAuth', () => {
it('should correctly exec', async () => {
const auth = new ExecAuth();
(auth as any).execFn = (command: string, opts: shell.ExecOpts): shell.ShellReturnValue => {
(auth as any).execFn = (
command: string,
args: string[],
opts: execa.SyncOptions,
): execa.ExecaSyncReturnValue => {
return {
code: 0,
stdout: JSON.stringify({ status: { token: 'foo' } }),
} as shell.ShellReturnValue;
} as execa.ExecaSyncReturnValue;
};

const token = auth.getToken({
Expand All @@ -29,12 +34,16 @@ describe('ExecAuth', () => {
it('should exec with env vars', async () => {
const auth = new ExecAuth();
let optsOut: shell.ExecOpts = {};
(auth as any).execFn = (command: string, opts: shell.ExecOpts): shell.ShellReturnValue => {
(auth as any).execFn = (
command: string,
args: string[],
opts: execa.SyncOptions,
): execa.ExecaSyncReturnValue => {
optsOut = opts;
return {
code: 0,
stdout: JSON.stringify({ status: { token: 'foo' } }),
} as shell.ShellReturnValue;
} as execa.ExecaSyncReturnValue;
};
process.env.BLABBLE = 'flubble';
const token = auth.getToken({
Expand Down

0 comments on commit 951dd13

Please sign in to comment.