Skip to content

Commit ea862fa

Browse files
committed
Address comments.
1 parent d796c4b commit ea862fa

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

src/config.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ import { CloudAuth } from './cloud_auth';
1313
import { Cluster, Context, newClusters, newContexts, newUsers, User } from './config_types';
1414
import { ExecAuth } from './exec_auth';
1515

16+
// fs.existsSync was removed in node 10
17+
function fileExists(filepath: string): boolean {
18+
try {
19+
fs.accessSync(filepath);
20+
return true;
21+
// tslint:disable-next-line:no-empty
22+
} catch (ignore) { }
23+
return false;
24+
}
25+
1626
export class KubeConfig {
1727
private static authenticators: Authenticator[] = [
1828
new CloudAuth(),
@@ -197,12 +207,10 @@ export class KubeConfig {
197207
const home = findHomeDir();
198208
if (home) {
199209
const config = path.join(home, '.kube', 'config');
200-
try {
201-
fs.accessSync(config);
210+
if (fileExists(config)) {
202211
this.loadFromFile(config);
203212
return;
204-
// tslint:disable-next-line:no-empty
205-
} catch (ignore) {}
213+
}
206214
}
207215
if (process.platform === 'win32' && shelljs.which('wsl.exe')) {
208216
// TODO: Handle if someome set $KUBECONFIG in wsl here...
@@ -213,12 +221,10 @@ export class KubeConfig {
213221
}
214222
}
215223

216-
try {
217-
fs.accessSync(Config.SERVICEACCOUNT_TOKEN_PATH);
224+
if (fileExists(Config.SERVICEACCOUNT_TOKEN_PATH)) {
218225
this.loadFromCluster();
219226
return;
220-
// tslint:disable-next-line:no-empty
221-
} catch (ignore) {}
227+
}
222228

223229
this.loadFromClusterAndUser(
224230
{ name: 'cluster', server: 'http://localhost:8080' } as Cluster,

src/exec_auth.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Authenticator } from './auth';
44
import { User } from './config_types';
55

66
export class ExecAuth implements Authenticator {
7-
private tokenCache: any = {};
7+
private readonly tokenCache: { [key: string]: any } = {};
88

99
public isAuthProvider(user: User) {
1010
return user.authProvider.name === 'exec' ||
@@ -15,11 +15,12 @@ export class ExecAuth implements Authenticator {
1515
// TODO: Handle client cert auth here, requires auth refactor.
1616
// See https://kubernetes.io/docs/reference/access-authn-authz/authentication/#input-and-output-formats
1717
// for details on this protocol.
18+
// TODO: Add a unit test for token caching.
1819
const cachedToken = this.tokenCache[user.name];
1920
if (cachedToken) {
2021
const date = Date.parse(cachedToken.status.expirationTimestamp);
2122
if (date < Date.now()) {
22-
return cachedToken.status.token;
23+
return `Bearer ${cachedToken.status.token}`;
2324
}
2425
this.tokenCache[user.name] = null;
2526
}
@@ -40,6 +41,7 @@ export class ExecAuth implements Authenticator {
4041
const result = shell.exec(cmd, opts);
4142
if (result.code === 0) {
4243
const obj = JSON.parse(result.stdout);
44+
this.tokenCache[user.name] = obj.status.token;
4345
return `Bearer ${obj.status.token}`;
4446
}
4547
throw new Error(result.stderr);

0 commit comments

Comments
 (0)