Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix relative path loading. #226

Merged
merged 1 commit into from
Mar 24, 2019
Merged
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
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