@@ -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 ) {
0 commit comments