-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathcmd-private.js
233 lines (211 loc) · 9.19 KB
/
cmd-private.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
'use strict';
var app = require('commander');
var privateOperations = require('./lib/private')();
//const debug = require('debug')('configure');
const upgradekvm = require('./lib/upgrade-kvm')();
const upgradeauth = require('./lib/upgrade-edgeauth')();
const rotatekey = require('./lib/rotate-key')();
const writeConsoleLog = require('microgateway-core').Logging.writeConsoleLog;
const CONSOLE_LOG_TAG_COMP = 'microgateway cmd private';
var prompt = require('cli-prompt');
module.exports = function() {
app
.command('configure')
.description('Automated, one-time setup of edgemicro with Apigee Private Cloud')
.option('-o, --org <org>', 'the organization')
.option('-r, --runtime-url <runtimeUrl>', 'the URL of the runtime server')
.option('-m, --mgmt-url <mgmtUrl>', 'the URL of the management server')
.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('-v, --virtual-hosts <virtualHosts>', 'comma separated virtual hosts to deploy with')
.option('-c, --configDir <configDir>', 'Set the directory where configs are read from.')
.option('-t, --token <token>', 'OAuth token to use with management API')
.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')
.option('-d, --debug', 'execute with debug output')
.action((options) => {
options.error = optionError(options);
options.token = options.token || process.env.EDGEMICRO_SAML_TOKEN;
options.configDir = options.configDir || process.env.EDGEMICRO_CONFIG_DIR;
if (!options.org) {
return options.error('org is required');
}
if (!options.env) {
return options.error('env is required');
}
//if token is not passed, username is mandatory
if (!options.token) {
//If there is no token then we can go through the password process
if (!options.username) {
return options.error('username is required');
}
if (!options.password) {
promptForPassword(options, (options) => {
if (!options.password) {
return options.error('password is required');
}
});
}
}
if (options.key || options.cert) {
if (!options.key || !options.cert) {
return options.error('key and cert must be passed together');
}
}
if (!options.runtimeUrl) {
return options.error('runtimeUrl is required');
}
if (!options.mgmtUrl) {
return options.error('mgmtUrl is required');
}
if (!options.runtimeUrl.includes('http')) {
return options.error('runtimeUrl requires a prototcol http or https');
}
if (!options.mgmtUrl.includes('http')) {
return options.error('runtimeUrl requires a prototcol http or https');
}
privateOperations.configureEdgemicro(options);
});
app
.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.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.proxyuri) {
return options.error('proxyuri is required')
}
if (options.proxyuri && !options.proxyuri.includes('http')) {
return options.error('proxyuri requires a prototcol http or https')
}
upgradekvm.upgradekvm(options);
});
app
.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('-v, --virtualhost <virtualhost>', 'virtual host of the proxy')
.option('-m, --mgmt-url <mgmtUrl>', 'the URL of the management server')
.option('-t, --token <token>', 'OAuth token to use with management API')
.description('upgrade edgemicro-auth proxy')
.action((options) => {
options.error = optionError(options);
options.token = options.token || process.env.EDGEMICRO_SAML_TOKEN;
options.noncpsOrg = true;
if (!options.token) {
if (!options.username) {
return options.error('username is required');
}
promptForPassword(options, (options) => {
if (!options.password) {
return options.error('password is required');
}
});
}
if (!options.org) {
return options.error('org is required');
}
if (!options.env) {
return options.error('env is required');
}
if (!options.mgmtUrl) {
return options.error('mgmtUrl is required');
}
if (!options.mgmtUrl.includes('http')) {
return options.error('runtimeUrl requires a prototcol http or https')
}
upgradeauth.upgradeauth(options, () => {});
});
app
.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.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.rotatekeyuri) {
return options.error('rotatekeyuri 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);
});
app.parse(process.argv);
var running = false;
app.commands.forEach(function(command) {
if (command._name === app.rawArgs[2]) {
running = true;
}
});
if (!running) {
app.help();
}
}
// 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);
});
}
}
function optionError(caller) {
return(((obj) => {
return((message) => {
writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},message);
obj.help();
});
})(caller))
}