Skip to content

Commit 3811829

Browse files
authored
Merge pull request #226 from brendandburns/cert-fix
Fix relative path loading.
2 parents 864ef9d + 5946eee commit 3811829

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

src/config.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ export class KubeConfig {
4545
*/
4646
public 'currentContext': string;
4747

48+
/**
49+
* Root directory for a config file driven config. Used for loading relative cert paths.
50+
*/
51+
public 'rootDirectory': string;
52+
4853
public getContexts() {
4954
return this.contexts;
5055
}
@@ -97,6 +102,7 @@ export class KubeConfig {
97102
}
98103

99104
public loadFromFile(file: string) {
105+
this.rootDirectory = path.dirname(file);
100106
this.loadFromString(fs.readFileSync(file, 'utf8'));
101107
}
102108

@@ -255,15 +261,18 @@ export class KubeConfig {
255261
if (cluster != null && cluster.skipTLSVerify) {
256262
opts.rejectUnauthorized = false;
257263
}
258-
const ca = cluster != null ? bufferFromFileOrString(cluster.caFile, cluster.caData) : null;
264+
const ca =
265+
cluster != null
266+
? bufferFromFileOrString(this.rootDirectory, cluster.caFile, cluster.caData)
267+
: null;
259268
if (ca) {
260269
opts.ca = ca;
261270
}
262-
const cert = bufferFromFileOrString(user.certFile, user.certData);
271+
const cert = bufferFromFileOrString(this.rootDirectory, user.certFile, user.certData);
263272
if (cert) {
264273
opts.cert = cert;
265274
}
266-
const key = bufferFromFileOrString(user.keyFile, user.keyData);
275+
const key = bufferFromFileOrString(this.rootDirectory, user.keyFile, user.keyData);
267276
if (key) {
268277
opts.key = key;
269278
}
@@ -355,8 +364,11 @@ export class Config {
355364
}
356365

357366
// This is public really only for testing.
358-
export function bufferFromFileOrString(file?: string, data?: string): Buffer | null {
367+
export function bufferFromFileOrString(root?: string, file?: string, data?: string): Buffer | null {
359368
if (file) {
369+
if (!path.isAbsolute(file) && root) {
370+
file = path.join(root, file);
371+
}
360372
return fs.readFileSync(file);
361373
}
362374
if (data) {

src/config_test.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { readFileSync } from 'fs';
22
import * as https from 'https';
3-
import { join } from 'path';
3+
import { dirname, join } from 'path';
44

55
import { expect } from 'chai';
66
import mockfs = require('mock-fs');
@@ -167,7 +167,7 @@ describe('KubeConfig', () => {
167167
it('should load the kubeconfig file properly', () => {
168168
const kc = new KubeConfig();
169169
kc.loadFromFile(kcFileName);
170-
170+
expect(kc.rootDirectory).to.equal(dirname(kcFileName));
171171
validateFileLoad(kc);
172172
});
173173
it('should fail to load a missing kubeconfig file', () => {
@@ -1012,13 +1012,28 @@ describe('KubeConfig', () => {
10121012
});
10131013

10141014
describe('BufferOrFile', () => {
1015+
it('should load from root if present', () => {
1016+
const data = 'some data for file';
1017+
const arg: any = {
1018+
configDir: {
1019+
config: data,
1020+
},
1021+
};
1022+
mockfs(arg);
1023+
const inputData = bufferFromFileOrString('configDir', 'config');
1024+
expect(inputData).to.not.equal(null);
1025+
if (inputData) {
1026+
expect(inputData.toString()).to.equal(data);
1027+
}
1028+
mockfs.restore();
1029+
});
10151030
it('should load from a file if present', () => {
10161031
const data = 'some data for file';
10171032
const arg: any = {
10181033
config: data,
10191034
};
10201035
mockfs(arg);
1021-
const inputData = bufferFromFileOrString('config');
1036+
const inputData = bufferFromFileOrString(undefined, 'config');
10221037
expect(inputData).to.not.equal(null);
10231038
if (inputData) {
10241039
expect(inputData.toString()).to.equal(data);

0 commit comments

Comments
 (0)