-
Notifications
You must be signed in to change notification settings - Fork 492
/
config.ts
271 lines (219 loc) · 7.9 KB
/
config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import fs = require('fs');
import os = require('os');
import path = require('path');
import https = require('https');
import base64 = require('base-64');
import jsonpath = require('jsonpath');
import request = require('request');
import shelljs = require('shelljs');
import yaml = require('js-yaml');
import api = require('./api');
import { Cluster, newClusters, User, newUsers, Context, newContexts } from './config_types';
import client = require('./auth-wrapper');
export class KubeConfig {
/**
* The list of all known clusters
*/
'clusters': Cluster[];
/**
* The list of all known users
*/
'users': User[];
/**
* The list of all known contexts
*/
'contexts': Context[];
/**
* The name of the current context
*/
'currentContext': string;
constructor() { }
public getContexts() {
return this.contexts;
}
public getClusters() {
return this.clusters;
}
public getUsers() {
return this.users;
}
public getCurrentContext() {
return this.currentContext;
}
public setCurrentContext(context: string) {
this.currentContext = context;
}
// Only really public for testing...
public static findObject(list: Object[], name: string, key: string) {
for (let obj of list) {
if (obj['name'] == name) {
if (obj[key]) {
return obj[key];
}
return obj;
}
}
return null;
}
private getCurrentContextObject() {
return this.getContextObject(this.currentContext);
}
public getContextObject(name: string) {
return KubeConfig.findObject(this.contexts, name, 'context');
}
public getCurrentCluster() {
return this.getCluster(this.getCurrentContextObject()['cluster']);
}
public getCluster(name: string): Cluster {
return KubeConfig.findObject(this.clusters, name, 'cluster');
}
public getCurrentUser() {
return this.getUser(this.getCurrentContextObject()['user']);
}
public getUser(name: string): User {
return KubeConfig.findObject(this.users, name, 'user');
}
public loadFromFile(file: string) {
this.loadFromString(fs.readFileSync(file, 'utf8'));
}
private bufferFromFileOrString(file: string, data: string) {
if (file) {
return fs.readFileSync(file);
}
if (data) {
return new Buffer(base64.decode(data), 'utf-8');
}
return null;
}
private applyHTTPSOptions(opts: request.Options | https.RequestOptions) {
const cluster = this.getCurrentCluster();
const user = this.getCurrentUser();
opts.ca = this.bufferFromFileOrString(cluster.caFile, cluster.caData);
opts.cert = this.bufferFromFileOrString(user.certFile, user.certData);
opts.key = this.bufferFromFileOrString(user.keyFile, user.keyData);
}
private applyAuthorizationHeader(opts: request.Options | https.RequestOptions) {
const user = this.getCurrentUser();
let token = null;
if (user.authProvider && user.authProvider.config) {
const config = user.authProvider.config;
// This should probably be extracted as auth-provider specific plugins...
token = 'Bearer ' + config['access-token'];
const expiry = config['expiry'];
if (expiry) {
let expiration = Date.parse(expiry);
if (expiration < Date.now()) {
if (config['cmd-path']) {
let cmd = '"' + config['cmd-path'] + '"';
if (config['cmd-args']) {
cmd = cmd + ' ' + config['cmd-args'];
}
// TODO: Cache to file?
const result = shelljs.exec(cmd, { silent: true });
if (result['code'] != 0) {
throw new Error('Failed to refresh token: ' + result.stderr);
}
let resultObj = JSON.parse(result.stdout.toString());
let path = config['token-key'];
// Format in file is {<query>}, so slice it out and add '$'
path = '$' + path.slice(1, -1);
config['access-token'] = jsonpath.query(resultObj, path);
token = 'Bearer ' + config['access-token'];
} else {
throw new Error('Token is expired!');
}
}
}
}
if (user.token) {
token = 'Bearer ' + user.token;
}
if (token) {
if (!opts.headers) {
opts.headers = [];
}
opts.headers['Authorization'] = token;
}
}
private applyOptions(opts: request.Options | https.RequestOptions) {
this.applyHTTPSOptions(opts);
this.applyAuthorizationHeader(opts);
}
public applytoHTTPSOptions(opts: https.RequestOptions) {
const user = this.getCurrentUser();
this.applyOptions(opts);
if (user.username) {
opts.auth = `${user.username}:${user.password}`;
}
}
public applyToRequest(opts: request.Options) {
const cluster = this.getCurrentCluster();
const user = this.getCurrentUser();
this.applyOptions(opts);
if (cluster.skipTLSVerify) {
opts.strictSSL = false
}
if (user.username) {
opts.auth = {
username: user.username,
password: user.password
}
}
}
public loadFromString(config: string) {
var obj = yaml.safeLoad(config);
if (obj['apiVersion'] != 'v1') {
throw new TypeError('unknown version: ' + obj['apiVersion']);
}
this.clusters = newClusters(obj['clusters']);
this.contexts = newContexts(obj['contexts']);
this.users = newUsers(obj['users']);
this.currentContext = obj['current-context'];
}
}
export class Config {
public static SERVICEACCOUNT_ROOT =
'/var/run/secrets/kubernetes.io/serviceaccount';
public static SERVICEACCOUNT_CA_PATH =
Config.SERVICEACCOUNT_ROOT + '/ca.crt';
public static SERVICEACCOUNT_TOKEN_PATH =
Config.SERVICEACCOUNT_ROOT + '/token';
public static fromFile(filename: string): api.Core_v1Api {
let kc = new KubeConfig();
kc.loadFromFile(filename);
let k8sApi = new client.Core_v1Api(kc.getCurrentCluster()['server']);
k8sApi.setDefaultAuthentication(kc);
return k8sApi;
}
public static fromCluster(): api.Core_v1Api {
let host = process.env.KUBERNETES_SERVICE_HOST
let port = process.env.KUBERNETES_SERVICE_PORT
// TODO: better error checking here.
let caCert = fs.readFileSync(Config.SERVICEACCOUNT_CA_PATH);
let token = fs.readFileSync(Config.SERVICEACCOUNT_TOKEN_PATH);
let k8sApi = new client.Core_v1Api('https://' + host + ':' + port);
k8sApi.setDefaultAuthentication({
'applyToRequest': (opts) => {
opts.ca = caCert;
if (!opts.headers) {
opts.headers = [];
}
opts.headers['Authorization'] = 'Bearer ' + token;
}
});
return k8sApi;
}
public static defaultClient(): api.Core_v1Api {
if (process.env.KUBECONFIG) {
return Config.fromFile(process.env.KUBECONFIG);
}
let config = path.join(process.env.HOME, ".kube", "config");
if (fs.existsSync(config)) {
return Config.fromFile(config);
}
if (fs.existsSync(Config.SERVICEACCOUNT_TOKEN_PATH)) {
return Config.fromCluster();
}
return new client.Core_v1Api('http://localhost:8080');
}
}