-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathenv.js
More file actions
257 lines (219 loc) · 9.09 KB
/
env.js
File metadata and controls
257 lines (219 loc) · 9.09 KB
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
/*
Copyright NetFoundry Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
var path = require('path');
var fs = require('fs');
var nconf = require('nconf');
const isEqual = require('lodash.isequal');
var nval = require('nconf-validator')(nconf);
const Ajv = require("ajv");
const ajv = new Ajv({ validateFormats: true });
const addFormats = require("ajv-formats");
addFormats(ajv); // Adds support for formats like "uri", "email", "date", etc.
/**
* Reach into teh ZBR dependency, and obtain the "hashed" name of the ZBR CSS file.
* This will be the default used
*/
function getZBRCSSname() {
try {
let pathToZitiBrowzerRuntimeModule = require.resolve('@openziti/ziti-browzer-runtime');
pathToZitiBrowzerRuntimeModule = pathToZitiBrowzerRuntimeModule.substring(0, pathToZitiBrowzerRuntimeModule.lastIndexOf('/'));
let zbrCSSName;
fs.readdirSync(pathToZitiBrowzerRuntimeModule).forEach(file => {
if (file.startsWith('ziti-browzer-css-')) {
zbrCSSName = file;
}
});
return zbrCSSName;
}
catch (e) {
console.error(e);
}
}
/** -----------------------------------------------------------------------------------------------
* Config value Order-of-precedence is:
* 1) cmd line args
* 2) env vars
* 3) config.json
* 4) defaults
* -----------------------------------------------------------------------------------------------*/
nconf
.argv()
.env()
.file(
{
file: path.join(__dirname, '../config.json')
}
)
.defaults(
{
ZITI_BROWZER_BOOTSTRAPPER_LOGLEVEL: 'error',
ZITI_BROWZER_BOOTSTRAPPER_SCHEME: 'http',
ZITI_BROWZER_BOOTSTRAPPER_WILDCARD_VHOSTS: false,
ZITI_BROWZER_BOOTSTRAPPER_LISTEN_PORT: 80,
ZITI_BROWZER_LOAD_BALANCER_PORT: 443,
ZITI_CONTROLLER_PORT: 443,
ZITI_BROWZER_RUNTIME_LOGLEVEL: 'error',
ZITI_BROWZER_RUNTIME_HOTKEY: 'alt+f12',
NODE_EXTRA_CA_CERTS: 'node_modules/node_extra_ca_certs_mozilla_bundle/ca_bundle/ca_intermediate_root_bundle.pem',
ZITI_BROWZER_WHITELABEL: JSON.stringify({
"enable": {
"browZerButton": true,
"browZerToastMessages": true,
"browZerSplashScreen": true,
"browZerThroughputChart": true
},
"branding": {
"browZerName": "OpenZiti BrowZer",
"browZerSplashMessage": "Stand by while OpenZiti BrowZer Bootstraps your private web app",
"browZerButtonIconSvgUrl": `https://${nconf.get('ZITI_BROWZER_BOOTSTRAPPER_HOST')}/ziti-browzer-logo.svg`,
"browZerCSS": `https://${nconf.get('ZITI_BROWZER_BOOTSTRAPPER_HOST')}/${getZBRCSSname()}`
}
}),
}
)
.required(
[
'ZITI_BROWZER_BOOTSTRAPPER_TARGETS',
'ZITI_BROWZER_BOOTSTRAPPER_HOST',
'ZITI_CONTROLLER_HOST',
]
);
const whitelabelSchema = {
type: "object",
properties: {
enable: {
type: "object",
properties: {
browZerButton: { type: "boolean" },
browZerToastMessages: { type: "boolean" },
browZerSplashScreen: { type: "boolean" },
browZerThroughputChart: { type: "boolean" }
},
required: [
"browZerButton",
"browZerToastMessages",
"browZerSplashScreen",
"browZerThroughputChart"
],
additionalProperties: false // Prevent misspelled keys
},
branding: {
type: "object",
properties: {
browZerName: { type: "string" },
browZerSplashMessage: { type: "string" },
browZerButtonIconSvgUrl: { type: "string", format: "uri"},
browZerCSS: { type: "string", format: "uri"},
},
required: [
"browZerName",
"browZerSplashMessage",
"browZerButtonIconSvgUrl",
"browZerCSS"
],
additionalProperties: false // Prevent misspelled keys
}
},
additionalProperties: false // Prevent misspelled keys
};
/** -----------------------------------------------------------------------------------------------
* config validation rules
* -----------------------------------------------------------------------------------------------*/
nval.addRule('ZITI_BROWZER_BOOTSTRAPPER_TARGETS', 'json');
if (nconf.get('ZITI_BROWZER_BOOTSTRAPPER_LOG_TAGS')) {
nval.addRule('ZITI_BROWZER_BOOTSTRAPPER_LOG_TAGS', 'json');
}
nval.addRule('ZITI_BROWZER_WHITELABEL', 'json');
nval.addRule('ZITI_BROWZER_BOOTSTRAPPER_WILDCARD_VHOSTS', Boolean);
nval.addRule('ZITI_BROWZER_BOOTSTRAPPER_SCHEME', ['http', 'https']);
if (nconf.get('ZITI_BROWZER_LOAD_BALANCER_HOST')) {
nval.addRule('ZITI_BROWZER_LOAD_BALANCER_HOST', 'domain')
}
// M2M OIDC-related config
if (nconf.get('ZITI_BROWZER_BOOTSTRAPPER_IDP_BASE_URL')) {
nval.addRule('ZITI_BROWZER_BOOTSTRAPPER_IDP_BASE_URL', 'url')
nconf.required([
'ZITI_BROWZER_BOOTSTRAPPER_IDP_CLIENT_ID',
'ZITI_BROWZER_BOOTSTRAPPER_IDP_CLIENT_SECRET',
'ZITI_BROWZER_BOOTSTRAPPER_IDP_CLIENT_AUDIENCE'
]);
nval.addRule('ZITI_BROWZER_BOOTSTRAPPER_IDP_CLIENT_ID', String)
nval.addRule('ZITI_BROWZER_BOOTSTRAPPER_IDP_CLIENT_SECRET', String)
nval.addRule('ZITI_BROWZER_BOOTSTRAPPER_IDP_CLIENT_AUDIENCE', String)
}
if (nconf.get('ZITI_BROWZER_LOAD_BALANCER_PORT')) {
nval.addRule('ZITI_BROWZER_LOAD_BALANCER_PORT', 'port')
}
if (nconf.get('ZITI_BROWZER_BOOTSTRAPPER_SKIP_CONTROLLER_CERT_CHECK')) {
nval.addRule('ZITI_BROWZER_BOOTSTRAPPER_SKIP_CONTROLLER_CERT_CHECK', Boolean)
}
if (nconf.get('ZITI_BROWZER_BOOTSTRAPPER_SKIP_DEPRECATION_WARNINGS')) {
nval.addRule('ZITI_BROWZER_BOOTSTRAPPER_SKIP_DEPRECATION_WARNINGS', Boolean)
}
if (nconf.get('ZITI_BROWZER_BOOTSTRAPPER_CERTIFICATE_PATH')) {
nval.addRule('ZITI_BROWZER_BOOTSTRAPPER_CERTIFICATE_PATH', String);
}
if (nconf.get('ZITI_BROWZER_BOOTSTRAPPER_KEY_PATH')) {
nval.addRule('ZITI_BROWZER_BOOTSTRAPPER_KEY_PATH', String);
}
nval.addRule('ZITI_BROWZER_BOOTSTRAPPER_HOST', 'domain')
nval.addRule('ZITI_CONTROLLER_HOST', 'domain')
nval.addRule('ZITI_CONTROLLER_PORT', 'port')
if (nconf.get('ZITI_BROWZER_BOOTSTRAPPER_LISTEN_PORT')) {
nval.addRule('ZITI_BROWZER_BOOTSTRAPPER_LISTEN_PORT', 'port');
}
nval.addRule('ZITI_BROWZER_BOOTSTRAPPER_LOGLEVEL', function(x) {
if (! (typeof x === 'string')) { return false;}
x = x.toLowerCase();
if (['error', 'warn', 'info', 'verbose', 'debug', 'silly'].includes(x)) {
return true;
}
return false;
})
if (nconf.get('ZITI_BROWZER_BOOTSTRAPPER_GITHUB_API_TOKEN')) {
nval.addRule('ZITI_BROWZER_BOOTSTRAPPER_GITHUB_API_TOKEN', String);
}
/**
* Now validate the config
*/
nval.validate();
const validateWhitelabel = ajv.compile(whitelabelSchema);
const whitelabelConfigData = JSON.parse(nconf.get('ZITI_BROWZER_WHITELABEL'));
const valid = validateWhitelabel(whitelabelConfigData);
if (!valid) {
console.error("Whitelabel Configuration validation failed:", validateWhitelabel.errors);
throw new Error("Whitelabel Configuration validation failed")
}
function asBool (value) {
const val = value.toLowerCase()
const allowedValues = [
'false',
'0',
'true',
'1'
]
if (allowedValues.indexOf(val) === -1) {
throw new Error('should be either "true", "false", "TRUE", "FALSE", 1, or 0')
}
return !(((val === '0') || (val === 'false')))
}
module.exports = function(key) {
let val = nconf.get(key);
if (val && (isEqual(key, 'ZITI_BROWZER_BOOTSTRAPPER_WILDCARD_VHOSTS')
|| isEqual(key, 'ZITI_BROWZER_BOOTSTRAPPER_SKIP_CONTROLLER_CERT_CHECK')
|| isEqual(key, 'ZITI_BROWZER_BOOTSTRAPPER_SKIP_DEPRECATION_WARNINGS'))
) {
val = asBool(val);
}
return val;
};