Skip to content

Commit

Permalink
Fix relative path loading.
Browse files Browse the repository at this point in the history
  • Loading branch information
brendandburns committed Mar 22, 2019
1 parent 864ef9d commit 5946eee
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
20 changes: 16 additions & 4 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ export class KubeConfig {
*/
public 'currentContext': string;

/**
* Root directory for a config file driven config. Used for loading relative cert paths.
*/
public 'rootDirectory': string;

public getContexts() {
return this.contexts;
}
Expand Down Expand Up @@ -97,6 +102,7 @@ export class KubeConfig {
}

public loadFromFile(file: string) {
this.rootDirectory = path.dirname(file);
this.loadFromString(fs.readFileSync(file, 'utf8'));
}

Expand Down Expand Up @@ -255,15 +261,18 @@ export class KubeConfig {
if (cluster != null && cluster.skipTLSVerify) {
opts.rejectUnauthorized = false;
}
const ca = cluster != null ? bufferFromFileOrString(cluster.caFile, cluster.caData) : null;
const ca =
cluster != null
? bufferFromFileOrString(this.rootDirectory, cluster.caFile, cluster.caData)
: null;
if (ca) {
opts.ca = ca;
}
const cert = bufferFromFileOrString(user.certFile, user.certData);
const cert = bufferFromFileOrString(this.rootDirectory, user.certFile, user.certData);
if (cert) {
opts.cert = cert;
}
const key = bufferFromFileOrString(user.keyFile, user.keyData);
const key = bufferFromFileOrString(this.rootDirectory, user.keyFile, user.keyData);
if (key) {
opts.key = key;
}
Expand Down Expand Up @@ -355,8 +364,11 @@ export class Config {
}

// This is public really only for testing.
export function bufferFromFileOrString(file?: string, data?: string): Buffer | null {
export function bufferFromFileOrString(root?: string, file?: string, data?: string): Buffer | null {
if (file) {
if (!path.isAbsolute(file) && root) {
file = path.join(root, file);
}
return fs.readFileSync(file);
}
if (data) {
Expand Down
21 changes: 18 additions & 3 deletions src/config_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { readFileSync } from 'fs';
import * as https from 'https';
import { join } from 'path';
import { dirname, join } from 'path';

import { expect } from 'chai';
import mockfs = require('mock-fs');
Expand Down Expand Up @@ -167,7 +167,7 @@ describe('KubeConfig', () => {
it('should load the kubeconfig file properly', () => {
const kc = new KubeConfig();
kc.loadFromFile(kcFileName);

expect(kc.rootDirectory).to.equal(dirname(kcFileName));
validateFileLoad(kc);
});
it('should fail to load a missing kubeconfig file', () => {
Expand Down Expand Up @@ -1012,13 +1012,28 @@ describe('KubeConfig', () => {
});

describe('BufferOrFile', () => {
it('should load from root if present', () => {
const data = 'some data for file';
const arg: any = {
configDir: {
config: data,
},
};
mockfs(arg);
const inputData = bufferFromFileOrString('configDir', 'config');
expect(inputData).to.not.equal(null);
if (inputData) {
expect(inputData.toString()).to.equal(data);
}
mockfs.restore();
});
it('should load from a file if present', () => {
const data = 'some data for file';
const arg: any = {
config: data,
};
mockfs(arg);
const inputData = bufferFromFileOrString('config');
const inputData = bufferFromFileOrString(undefined, 'config');
expect(inputData).to.not.equal(null);
if (inputData) {
expect(inputData.toString()).to.equal(data);
Expand Down

0 comments on commit 5946eee

Please sign in to comment.