-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathcmd.js
621 lines (584 loc) · 27.2 KB
/
cmd.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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
'use strict';
const commander = require('commander');
const url = require('url');
const fs = require('fs');
const os = require('os');
const debug = require('debug')('start');
const request = require('postman-request');
const configure = require('./lib/configure')();
const upgradekvm = require('./lib/upgrade-kvm')();
const upgradeauth = require('./lib/upgrade-edgeauth')();
const rotatekey = require('./lib/rotate-key')();
const verify = require('./lib/verify')();
const run = require('./lib/gateway')();
const keyGenerator = require('./lib/key-gen')();
const prompt = require('cli-prompt');
const init = require('./lib/init');
var portastic = require('portastic');
const writeConsoleLog = require('microgateway-core').Logging.writeConsoleLog;
const CONSOLE_LOG_TAG_COMP = 'microgateway cmd';
const setup = function setup() {
commander
.version(require('../package.json').version);
commander
.command('token [action]', 'JWT token commands, see: "edgemicro token -h"')
.command('cert [action]', 'ssh cert commands to store on Apigee Vault, see: "edgemicro cert -h"')
.command('private [action]', 'Automated, one-time configuration with Edge On-Premises, see: "edgemicro private -h"');
commander
.command('configure')
.description('Automated, one-time configuration with Edge Cloud')
.option('-o, --org <org>', 'the organization')
.option('-e, --env <env>', 'the environment')
.option('-v, --virtualHosts <virtualHosts>', 'override virtualHosts (default: "default,secure")')
.option('-u, --username <user>', 'username of the organization admin')
.option('-p, --password <password>', 'password of the organization admin')
.option('-t, --token <token>', 'OAuth token to use with management API')
.option('-r, --url <url>', 'organization\'s custom API URL (https://api.example.com)')
.option('-d, --debug', 'execute with debug output')
.option('-c, --configDir <configDir>', 'Set the directory where configs are written.')
.option('-x, --proxyName <proxyName>', 'Set the custom proxy name for edgemicro-auth')
.option('-k --key <key>', 'Path to private key to be used by Apigee Edge')
.option('-s --cert <cert>', 'Path to certificate to be used by Apigee Edge')
.action((options) => {
options.error = optionError(options);
options.token = options.token || process.env.EDGEMICRO_SAML_TOKEN;
if (options.token) {
//If there is a token lets configure with standard opts.
if (!options.org) {
return options.error('org is required');
}
if (!options.env) {
return options.error('env is required');
}
options.configDir = options.configDir || process.env.EDGEMICRO_CONFIG_DIR;
configure.configure(options, () => { });
} else {
//If there is no token then we can go through the password process
if (!options.username) {
return options.error('username is required');
}
if (!options.org) {
return options.error('org is required');
}
if (!options.env) {
return options.error('env is required');
}
if (options.key || options.cert) {
if (!options.key || !options.cert) {
return options.error('key and cert must be passed together');
}
}
options.configDir = options.configDir || process.env.EDGEMICRO_CONFIG_DIR;
promptForPassword(options, (options) => {
if (!options.password) {
return options.error('password is required');
}
configure.configure(options, () => { });
})
}
});
commander
.command('init')
.description('initialize default.yaml into home dir')
.option('-c, --configDir <configDir>', 'Set the directory where configs are written.')
.action((options) => {
options.configDir = options.configDir || process.env.EDGEMICRO_CONFIG_DIR;
init(options, (err, location) => {
writeConsoleLog('log', { component: CONSOLE_LOG_TAG_COMP }, "config initialized to %s", location)
})
});
commander
.command('verify')
.description('verify Edge Micro configuration by testing config endpoints')
.option('-o, --org <org>', 'the organization')
.option('-e, --env <env>', 'the environment')
.option('-k, --key <key>', 'key for authenticating with Edge')
.option('-s, --secret <secret>', 'secret for authenticating with Edge')
.action((options) => {
options.error = optionError(options);
if (!options.org) {
return options.error('org is required');
}
if (!options.env) {
return options.error('env is required');
}
if (!options.key) {
return options.error('key is required');
}
if (!options.secret) {
return options.error('secret is required');
}
verify.verify(options);
});
commander
.command('start')
.option('-o, --org <org>', 'the organization')
.option('-e, --env <env>', 'the environment')
.option('-k, --key <key>', 'key for authenticating with Edge')
.option('-s, --secret <secret>', 'secret for authenticating with Edge')
.option('-p, --processes <processes>', 'number of processes to start, defaults to # of cores')
.option('-d, --pluginDir <pluginDir>', 'absolute path to plugin directory')
.option('-r, --port <portNumber>', 'override port in the config.yaml file')
.option('-c, --configDir <configDir>', 'Set the directory where configs are read from.')
.option('-u, --configUrl <configUrl>', 'Provide the endpoint to download the edgemicro config file')
.option('-a, --apiProxyName <apiProxyName>', 'the api proxy name; must be used with env var EDGEMICRO_LOCAL')
.option('-v, --revision <revision>', 'api proxy revision; required if apiProxyName is set')
.option('-b, --basepath <basepath>', 'api proxy basePath; required if apiProxyName is set')
.option('-t, --target <target>', 'target endpoint for proxy; required if apiProxyName is set')
.option('-m, --metrics', 'enable metrics plugin and start admin server')
.description('start the gateway based on configuration')
.action((options) => {
options.error = optionError(options);
options.secret = options.secret;
options.key = options.key;
options.org = options.org || process.env.EDGEMICRO_ORG;
options.env = options.env || process.env.EDGEMICRO_ENV;
options.processes = options.processes || process.env.EDGEMICRO_PROCESSES;
options.pluginDir = options.pluginDir || process.env.EDGEMICRO_PLUGIN_DIR;
options.configDir = options.configDir || process.env.EDGEMICRO_CONFIG_DIR;
options.configUrl = options.configUrl || process.env.EDGEMICRO_CONFIG_URL;
options.apiProxyName = options.apiProxyName || process.env.EDGEMICRO_API_PROXYNAME;
options.revision = options.revision || process.env.EDGEMICRO_API_REVISION;
options.basepath = options.basepath || process.env.EDGEMICRO_API_BASEPATH;
options.target = options.target || process.env.EDGEMICRO_API_TARGET;
debug("EDGEMICRO_LOCAL: " + process.env.EDGEMICRO_LOCAL)
debug("EDGEMICRO_LOCAL_PROXY: " + process.env.EDGEMICRO_LOCAL_PROXY)
if (options.port) {
portastic.test(options.port)
.then(function (isAvailable) {
if (!isAvailable) {
options.error('port is not available.');
process.exit(1);
}
});
}
if (!options.key && !process.env.EDGEMICRO_LOCAL && !process.env.EDGEMICRO_KEY) {
return options.error('key is required');
}
if (!options.secret && !process.env.EDGEMICRO_LOCAL && !process.env.EDGEMICRO_SECRET) {
return options.error('secret is required');
}
if (!options.org) {
return options.error('org is required');
}
if (!options.env) {
return options.error('env is required');
}
if (options.apiProxyName || options.target || options.revision || options.basepath || process.env.EDGEMICRO_LOCAL || process.env.EDGEMICRO_LOCAL_PROXY) {
//if any of these are set, look for environment variable
if (!process.env.EDGEMICRO_LOCAL && !process.env.EDGEMICRO_LOCAL_PROXY) {
return options.error('set the EDGEMICRO_LOCAL or EDGEMICRO_LOCAL_PROXY variable for apiProxyName parameter');
//process.exit(1);
} else if (process.env.EDGEMICRO_LOCAL && process.env.EDGEMICRO_LOCAL_PROXY) {
return options.error('set the EDGEMICRO_LOCAL or EDGEMICRO_LOCAL_PROXY; not both');
//process.exit(1);
} else {
if (options.apiProxyName && options.target && options.revision && options.basepath) {
if (!validateUrl(options.target)) {
return options.error('target endpoint not a valid url');
//process.exit(1);
}
if (process.env.EDGEMICRO_LOCAL) {
//create fake credentials - not used anywhere
options.key = 'dummy';
options.secret = 'dummy';
}
//start gateway
run.start(options);
return;
} else {
return options.error('apiProxyName, target, revision and basepath are all mandatory parms when EDGEMICRO_LOCAL or EDGEMICRO_LOCAL_PROXY is set');
//process.exit(1);
}
}
}
if (options.configUrl) {
options.configDir = options.configDir || os.homedir() + "/" + ".edgemicro";
if (!fs.existsSync(options.configDir)) fs.mkdirSync(options.configDir);
var fileName = options.org + "-" + options.env + "-config.yaml";
debug(fileName);
var filePath = options.configDir + "/" + fileName;
debug(filePath);
var parsedUrl = url.parse(options.configUrl, true);
debug(options.configUrl);
if (parsedUrl.protocol === "http:" || parsedUrl.protocol === "https:") {
debug("downloading file...");
request.get(options.configUrl, function (error, response, body) {
if (error) {
writeConsoleLog('error', { component: CONSOLE_LOG_TAG_COMP }, "config file did not download: " + error);
process.exit(1);
}
try {
debug(body);
fs.writeFileSync(filePath, body, 'utf8');
run.start(options);
} catch (err) {
writeConsoleLog('error', { component: CONSOLE_LOG_TAG_COMP }, "config file could not be written: " + err);
process.exit(1);
}
});
} else {
writeConsoleLog('error', { component: CONSOLE_LOG_TAG_COMP }, "url protocol not supported: " + parsedUrl.protocol);
process.exit(1);
}
} else {
run.start(options);
}
});
commander
.command('reload')
.option('-o, --org <org>', 'the organization')
.option('-e, --env <env>', 'the environment')
.option('-k, --key <key>', 'key for authenticating with Edge')
.option('-s, --secret <secret>', 'secret for authenticating with Edge')
.option('-c, --configDir <configDir>', 'Set the directory where configs are written.')
.option('-u, --configUrl <configUrl>', 'Provide the endpoint to download the edgemicro config file')
.description('reload the edgemicro cluster by pulling new configuration')
.action((options) => {
options.error = optionError(options);
options.secret = options.secret || process.env.EDGEMICRO_SECRET;
options.key = options.key || process.env.EDGEMICRO_KEY;
options.org = options.org || process.env.EDGEMICRO_ORG;
options.env = options.env || process.env.EDGEMICRO_ENV;
options.configDir = options.configDir || process.env.EDGEMICRO_CONFIG_DIR;
options.configUrl = options.configUrl || process.env.EDGEMICRO_CONFIG_URL;
if (!options.key) {
return options.error('key is required');
}
if (!options.secret) {
return options.error('secret is required');
}
if (!options.org) {
return options.error('org is required');
}
if (!options.env) {
return options.error('env is required');
}
if (options.configUrl) {
options.configDir = options.configDir || os.homedir() + "/" + ".edgemicro";
if (!fs.existsSync(options.configDir)) fs.mkdirSync(options.configDir);
var fileName = options.org + "-" + options.env + "-config.yaml";
debug(fileName);
var filePath = options.configDir + "/" + fileName;
debug(filePath);
var parsedUrl = url.parse(options.configUrl, true);
debug(options.configUrl);
if (parsedUrl.protocol === "http:" || parsedUrl.protocol === "https:") {
debug("downloading file...");
request.get(options.configUrl, function (error, response, body) {
if (error) {
writeConsoleLog('error', { component: CONSOLE_LOG_TAG_COMP }, "config file did not download: " + error);
process.exit(1);
}
try {
debug(body);
fs.writeFileSync(filePath, body, 'utf8');
run.reload(options);
} catch (err) {
writeConsoleLog('error', { component: CONSOLE_LOG_TAG_COMP }, "config file could not be written: " + err);
process.exit(1);
}
});
}
} else run.reload(options);
});
commander
.command('stop')
.description('stop the edgemicro cluster')
.action((options) => {
run.stop(options);
});
commander
.command('status')
.description('Status of the edgemicro cluster')
.action((options) => {
run.status(options);
});
commander
.command('genkeys')
.option('-o, --org <org>', 'the organization')
.option('-e, --env <env>', 'the environment')
.option('-u, --username <user>', 'username of the organization admin')
.option('-p, --password <password>', 'password of the organization admin')
.option('-t, --token <token>', 'OAuth token to use with management API')
.description('generate authentication keys for runtime auth between Microgateway and Edge')
.action((options) => {
options.error = optionError(options);
options.token = options.token || process.env.EDGEMICRO_SAML_TOKEN;
if (options.token) {
//If there is a token lets configure with standard opts.
if (!options.org) {
return options.error('org is required');
}
if (!options.env) {
return options.error('env is required');
}
options.configDir = options.configDir || process.env.EDGEMICRO_CONFIG_DIR;
keyGenerator.generate(options, (err) => {
if (err) {
process.exit(1)
} else {
process.exit(0)
}
});
} else {
//If there is no token then we can go through the password process
if (!options.username) {
return options.error('username is required');
}
if (!options.org) {
return options.error('org is required');
}
if (!options.env) {
return options.error('env is required');
}
promptForPassword(options, (options) => {
if (!options.password) {
return options.error('password is required');
}
keyGenerator.generate(options, (err) => {
if (err) {
process.exit(1)
} else {
process.exit(0)
}
});
})
}
});
commander
.command('revokekeys')
.option('-o, --org <org>', 'the organization')
.option('-e, --env <env>', 'the environment')
.option('-u, --username <user>', 'username of the organization admin')
.option('-p, --password <password>', 'password of the organization admin')
.option('-k, --key <key>', 'Microgateway Key to be revoked')
.option('-s, --secret <secret>', 'Microgateway secret to be revoked')
.option('-t, --token <token>', 'Microgateway login token')
.description('revoke authentication keys for runtime auth between Microgateway and Edge')
.action((options) => {
options.error = optionError(options);
options.token = options.token || process.env.EDGEMICRO_SAML_TOKEN;
if (options.token) {
//If there is a token lets configure with standard opts.
if (!options.org) {
return options.error('org is required');
}
if (!options.env) {
return options.error('env is required');
}
if (!options.key) {
return options.error('key is required');
}
if (!options.secret) {
return options.error('secret is required');
}
options.configDir = options.configDir || process.env.EDGEMICRO_CONFIG_DIR;
keyGenerator.revoke(options, (err) => {
if (err) {
process.exit(1)
} else {
process.exit(0)
}
});
} else {
if (!options.username) {
return options.error('username is required');
}
if (!options.org) {
return options.error('org is required');
}
if (!options.env) {
return options.error('env is required');
}
if (!options.key) {
return options.error('key is required');
}
if (!options.secret) {
return options.error('secret is required');
}
promptForPassword(options, (options) => {
if (!options.password) {
return options.error('password is required');
}
keyGenerator.revoke(options, (err) => {
if (err) {
process.exit(1)
} else {
process.exit(0)
}
});
});
}
});
commander
.command('upgradekvm')
.option('-o, --org <org>', 'the organization')
.option('-e, --env <env>', 'the environment')
.option('-k, --key <key>', 'key for authenticating with Edge')
.option('-s, --secret <secret>', 'secret for authenticating with Edge')
.option('-p, --proxyuri <proxyuri>', 'proxyuri for edgeauth proxy')
.description('upgrade kvm to support JWT Key rotation')
.action((options) => {
options.error = optionError(options);
if (!options.org) {
return options.error('org is required');
}
if (!options.env) {
return options.error('env is required');
}
if (!options.key) {
return options.error('key is required');
}
if (!options.secret) {
return options.error('secret is required');
}
if (options.proxyuri && !options.proxyuri.includes('http')) {
return options.error('proxyuri requires a prototcol http or https')
}
upgradekvm.upgradekvm(options);
});
commander
.command('upgradeauth')
.option('-o, --org <org>', 'the organization')
.option('-e, --env <env>', 'the environment')
.option('-u, --username <user>', 'username of the organization admin')
.option('-p, --password <password>', 'password of the organization admin')
.option('-t, --token <token>', 'OAuth token to use with management API')
.option('-v, --virtualhost <virtualhost>', 'virtual host of the proxy')
.option('-b, --baseuri <baseuri>', 'baseuri for management apis')
.description('upgrade edgemicro-auth proxy')
.action((options) => {
options.error = optionError(options);
options.token = options.token || process.env.EDGEMICRO_SAML_TOKEN;
options.noncpsOrg = false;
if (!options.org) {
return options.error('org is required');
}
if (!options.env) {
return options.error('env is required');
}
if (options.token) {
upgradeauth.upgradeauth(options, () => { });
} else {
if (!options.username) {
return options.error('username is required');
}
promptForPassword(options, (options) => {
if (!options.password) {
return options.error('password is required');
}
upgradeauth.upgradeauth(options, () => { });
});
}
});
commander
.command('rotatekey')
.option('-o, --org <org>', 'the organization')
.option('-e, --env <env>', 'the environment')
.option('-k, --key <key>', 'key for authenticating with Edge')
.option('-s, --secret <secret>', 'secret for authenticating with Edge')
.option('-i, --kid <kid>', 'new key identifier')
.option('-r, --rotatekeyuri <rotatekeyuri>', 'rotate key url')
.option('-n, --nbf <nbf>', 'not before time in minutes')
.option('-p, --privatekey <privatekey>', 'Path to private key to be used by Apigee Edge')
.option('-c, --cert <cert>', 'Path to certificate to be used by Apigee Edge')
.description('Rotate JWT Keys')
.action((options) => {
options.error = optionError(options);
if (!options.org) {
return options.error('org is required');
}
if (!options.env) {
return options.error('env is required');
}
if (!options.key) {
return options.error('key is required');
}
if (!options.secret) {
return options.error('secret is required');
}
if (options.rotatekeyuri && !options.rotatekeyuri.includes('http')) {
return options.error('rotatekeyuri requires a prototcol http or https')
}
if (options.nbf && options.nbf !== 'undefined' && isNaN(options.nbf)) {
return options.error('nbf value should be numeric');
} else if (options.nbf && options.nbf !== 'undefined' && options.nbf - Math.floor(options.nbf) !== 0) {
return options.error('nbf value should be numeric and whole number');
}
if (options.privatekey || options.cert) {
if (!options.privatekey || !options.cert) {
return options.error('privatekey and cert must be passed together');
}
}
rotatekey.rotatekey(options);
});
commander
.command('clean')
.option('-o, --org <org>', 'the organization')
.option('-e, --env <env>', 'the environment')
.option('-u, --username <user>', 'username of the organization admin')
.option('-p, --password <password>', 'password of the organization admin')
.description('clean up microgateway artifacts from the org')
.action((options) => {
options.error = optionError(options);
if (!options.username) {
return options.error('username is required');
}
if (!options.org) {
return options.error('org is required');
}
if (!options.env) {
return options.error('env is required');
}
if (!options.kid) {
return options.error('kid is required');
}
promptForPassword(options, (options) => {
if (!options.password) {
return options.error('password is required');
}
//TODO
})
});
commander.parse(process.argv);
var running = false;
commander.commands.forEach(function (command) {
if (command._name === commander.rawArgs[2]) {
running = true;
}
});
if (!running) {
commander.help();
}
};
function optionError(caller) {
return (((obj) => {
return ((message) => {
writeConsoleLog('error', { component: CONSOLE_LOG_TAG_COMP }, message);
obj.help();
});
})(caller))
}
// prompt for a password if it is not specified
function promptForPassword(options, cb) {
if (options.password) {
cb(options);
} else {
prompt.password("password:", function (pw) {
options.password = pw;
cb(options);
});
}
}
//check url format
function validateUrl(target) {
try {
url.parse(target, true);
return true;
} catch (err) {
writeConsoleLog('error', { component: CONSOLE_LOG_TAG_COMP }, "Malformed URL: " + err);
return false;
}
}
module.exports = setup;