@@ -3,15 +3,15 @@ import https = require('https');
33import path = require( 'path' ) ;
44
55import base64 = require( 'base-64' ) ;
6- import execa = require( 'execa' ) ;
76import yaml = require( 'js-yaml' ) ;
8- // workaround for issue https://github.com/dchester/jsonpath/issues/96
9- import jsonpath = require( 'jsonpath/jsonpath.min' ) ;
107import request = require( 'request' ) ;
118import shelljs = require( 'shelljs' ) ;
129
13- import api = require( './api' ) ;
10+ import * as api from './api' ;
11+ import { Authenticator } from './auth' ;
12+ import { CloudAuth } from './cloud_auth' ;
1413import { Cluster , Context , newClusters , newContexts , newUsers , User } from './config_types' ;
14+ import { ExecAuth } from './exec_auth' ;
1515
1616export class KubeConfig {
1717 // Only public for testing.
@@ -49,6 +49,11 @@ export class KubeConfig {
4949 return null ;
5050 }
5151
52+ private static authenticators : Authenticator [ ] = [
53+ new CloudAuth ( ) ,
54+ new ExecAuth ( ) ,
55+ ] ;
56+
5257 /**
5358 * The list of all known clusters
5459 */
@@ -301,41 +306,12 @@ export class KubeConfig {
301306 let token : string | null = null ;
302307
303308 if ( user . authProvider && user . authProvider . config ) {
304- const config = user . authProvider . config ;
305- // This should probably be extracted as auth-provider specific plugins...
306- token = 'Bearer ' + config [ 'access-token' ] ;
307- const expiry = config . expiry ;
308-
309- if ( expiry ) {
310- const expiration = Date . parse ( expiry ) ;
311- if ( expiration < Date . now ( ) ) {
312- if ( config [ 'cmd-path' ] ) {
313- const args = config [ 'cmd-args' ] ? [ config [ 'cmd-args' ] ] : [ ] ;
314- // TODO: Cache to file?
315- // TODO: do this asynchronously
316- let result : execa . ExecaReturns ;
317-
318- try {
319- result = execa . sync ( config [ 'cmd-path' ] , args ) ;
320- } catch ( err ) {
321- throw new Error ( 'Failed to refresh token: ' + err . message ) ;
322- }
323-
324- const output = result . stdout . toString ( ) ;
325- const resultObj = JSON . parse ( output ) ;
326-
327- let pathKey = config [ 'token-key' ] ;
328- // Format in file is {<query>}, so slice it out and add '$'
329- pathKey = '$' + pathKey . slice ( 1 , - 1 ) ;
330-
331- config [ 'access-token' ] = jsonpath . query ( resultObj , pathKey ) ;
332- token = 'Bearer ' + config [ 'access-token' ] ;
333- } else {
334- throw new Error ( 'Token is expired!' ) ;
309+ KubeConfig . authenticators . forEach (
310+ ( authenticator : Authenticator ) => {
311+ if ( authenticator . isAuthProvider ( user ) ) {
312+ token = authenticator . getToken ( user ) ;
335313 }
336- }
337- }
338-
314+ } ) ;
339315 }
340316
341317 if ( user . token ) {
0 commit comments