-
Notifications
You must be signed in to change notification settings - Fork 142
/
Copy pathcontext.js
114 lines (106 loc) · 3.07 KB
/
context.js
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
'use strict';
const fs = require('fs'),
URI = require('urijs'),
Cluster = require('./cluster'),
Namespace = require('./namespace'),
User = require('./user');
/**
* contexts:
* - context:
* cluster: horse-cluster
* namespace: chisel-ns
* user: green-user
* name: federal-context
* - context:
* cluster: pig-cluster
* namespace: saw-ns
* user: black-user
* name: queen-anne-context
*/
class Context {
constructor({ cluster, namespace, user, name }) {
if (typeof name === 'undefined') {
if (typeof namespace.name === 'undefined') {
this.name = cluster.name + '/' + user.username;
} else {
this.name = namespace.name + '/' + cluster.name + '/' + user.username;
}
} else {
this.name = name;
}
this.cluster = cluster;
this.namespace = namespace;
this.user = user;
}
getMasterApi() {
if (typeof this.cluster.server === 'undefined') {
return undefined;
}
const api = Context.getBaseMasterApi(this.cluster.server);
// TODO: handle browser support for loading file certs
if (this.user.certificatePath) {
api.cert = fs.readFileSync(this.user.certificatePath);
}
if (this.user.certificateBase64) {
api.cert = Buffer.from(this.user.certificateBase64, 'base64');
}
if (this.user.keyPath) {
api.key = fs.readFileSync(this.user.keyPath);
}
if (this.user.keyBase64) {
api.key = Buffer.from(this.user.keyBase64, 'base64');
}
if (this.user.token) {
api.headers['Authorization'] = `Bearer ${this.user.token}`;
}
if (this.cluster.rejectUnauthorized) {
api.rejectUnauthorized = false;
}
if (this.cluster.ca) {
api.ca = fs.readFileSync(this.cluster.ca);
}
if (this.cluster.certData) {
api.ca = Buffer.from(this.cluster.certData, 'base64');
}
return api;
}
static getBaseMasterApi(url) {
const api = {
headers : {
'Accept' : 'application/json, text/plain, */*',
},
get url() {
// Do not report default ports as it can cause non matching redirection URLs
// during OAuth authentication
const skipPort = !this.port || this.protocol === 'http:' && this.port === '80' || this.protocol === 'https:' && this.port === '443';
let url = `${this.protocol}//${this.hostname}`;
if (!skipPort) url += `:${this.port}`;
if (this.path) url += this.path;
return url;
},
set url(url) {
const uri = URI.parse(url);
let parts = {};
if (uri.protocol) {
parts = uri;
} else {
URI.parseHost(url, parts);
}
const { protocol = 'https', hostname, port, path } = parts;
this.protocol = protocol + ':';
this.hostname = hostname;
this.port = port;
this.path = path;
}
}
api.url = url;
return api;
}
}
Context.default = new Context({
cluster : Cluster.default,
namespace : Namespace.default,
user : User.default,
name : '',
});
module.exports = Context;