-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathcmd-cert.js
136 lines (125 loc) · 5.05 KB
/
cmd-cert.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
'use strict';
const commander = require('commander');
const cert = require('./lib/cert')();
const prompt = require('cli-prompt');
const writeConsoleLog = require('microgateway-core').Logging.writeConsoleLog;
const CONSOLE_LOG_TAG_COMP = 'microgateway cmd cert';
const setup = function setup() {
commander
.command('install')
.option('-o, --org <org>', 'the organization')
.option('-e, --env <env>', 'the environment')
.option('-t, --token <token>', 'OAuth token to use with management API')
.option('-u, --username <user>', 'username of the organization admin')
.option('-p, --password <password>', 'password of the organization admin')
.option('-f, --force', 'replace any existing keys')
.description('install a certificate for your organization')
.action((options) => {
options.error = optionError(options);
options.token = options.token || process.env.EDGEMICRO_SAML_TOKEN;
if (options.token) {
if (!options.org) { return options.error('org is required'); }
if (!options.env) { return options.error('env is required'); }
cert.installCert(options);
} 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'); }
promptForPassword(options,(options)=>{
if (!options.password) { return options.error('password is required'); }
cert.installCert(options)
});
}
});
commander
.command('delete')
.option('-o, --org <org>', 'the organization')
.option('-e, --env <env>', 'the environment')
.option('-t, --token <token>', 'OAuth token to use with management API')
.option('-u, --username <user>', 'username of the organization admin')
.option('-p, --password <password>', 'password of the organization admin')
.description('delete the certificate for your organization')
.action((options) => {
options.error = optionError(options);
options.token = options.token || process.env.EDGEMICRO_SAML_TOKEN;
if (options.token) {
if (!options.org) { return options.error('org is required'); }
if (!options.env) { return options.error('env is required'); }
cert.deleteCert(options);
} 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'); }
promptForPassword(options,(options)=>{
if (!options.password) { return options.error('password is required'); }
cert.deleteCert(options)
});
}
});
commander
.command('check')
.option('-o, --org <org>', 'the organization')
.option('-e, --env <env>', 'the environment')
.option('-t, --token <token>', 'OAuth token to use with management API')
.option('-u, --username <user>', 'username of the organization admin')
.option('-p, --password <password>', 'password of the organization admin')
.description('check that your organization has a certificate installed')
.action((options) => {
options.error = optionError(options);
options.token = options.token || process.env.EDGEMICRO_SAML_TOKEN;
if (options.token) {
if (!options.org) { return options.error('org is required'); }
if (!options.env) { return options.error('env is required'); }
cert.checkCert(options);
} 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'); }
promptForPassword(options,(options)=>{
if (!options.password) { return options.error('password is required'); }
cert.checkCert(options)
});
}
});
commander
.command('public-key')
.option('-o, --org <org>', 'the organization')
.option('-e, --env <env>', 'the environment')
.description('retrieve the public key')
.action((options) => {
options.error = optionError(options);
if (!options.org) { return options.error('org is required'); }
if (!options.env) { return options.error('env is required'); }
cert.retrievePublicKey(options)
});
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);
});
}
}
module.exports = setup;