-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathidm.js
272 lines (233 loc) · 7.9 KB
/
idm.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
/*
* webMethods.io CLI
* Copyright 2022 Software AG
* Apache-2.0
*/
const { user } = require('./experimental.js');
const rest = require('./rest-fetch.js');
const sync_rest = require('./sync-rest-fetch.js');
var domainName, username, password, timeout, prettyprint;
var url;
var bearerToken;
function debug(message) {
logger.debug("<IDM> " + message);
}
function help() {
return `
\x1b[4mwebMethods Cloud IDM\x1b[0m
\x1b[32mProvides an IDM authtoken for subsequent calls\x1b[0m
$ node wmiocli.js
-d tenant.int-aws-us.webmethods.io
-u user
-p password
idm-authtoken
\x1b[32mGets an IDM users details\x1b[0m
$ node wmiocli.js
-d tenant.int-aws-us.webmethods.io
-u user
-p password
idm-user <username>
\x1b[32mFinds users that match the search criteria\x1b[0m
$ node wmiocli.js
-d tenant.int-aws-us.webmethods.io
-u user
-p password
idm-user-search <query> <include roles - true|false> <product>
where product is one of: apigateway, apiportal, webmethodsioint, b2b,
cumulocity, cloudcontainer, devportal, mft, metering, wmapps
e.g.
idm-user-search Dave true webmethodsioint
\x1b[32mCounts users that match the search criteria\x1b[0m
$ node wmiocli.js
-d tenant.int-aws-us.webmethods.io
-u user
-p password
idm-user-count <query>
where query can be a partial username, e.g.
idm-user-count dave
\x1b[32mLists environment available roles\x1b[0m
$ node wmiocli.js
-d tenant.int-aws-us.webmethods.io
-u user
-p password
idm-roles
\x1b[32mGets user role mappings for the given userid\x1b[0m
$ node wmiocli.js
-d tenant.int-aws-us.webmethods.io
-u user
-p password
idm-user-role-mappings <userid>
where userid is the unique ID returned from the idm-user call
\x1b[32mCreates a user\x1b[0m
$ node wmiocli.js
-d tenant.int-aws-us.webmethods.io
-u user
-p password
idm-user-create <first-name> <last-name> <email> <username>
\x1b[32mDeletes a user\x1b[0m
$ node wmiocli.js
-d tenant.int-aws-us.webmethods.io
-u user
-p password
idm-user-delete <userid>
where userid is the unique ID returned from the idm-user call
\x1b[32mUnlocks a user\x1b[0m
$ node wmiocli.js
-d tenant.int-aws-us.webmethods.io
-u user
-p password
idm-user-unlock <userid>
where userid is the unique ID returned from the idm-user call
`;
}
async function init(inDomainName, inUsername, inPassword, inTimeout, inPrettyPrint) {
domainName = inDomainName;
username = inUsername;
password = inPassword;
timeout = inTimeout;
prettyprint = inPrettyPrint;
debug("Started Init");
url = getApiUrl() + "/authtoken";
debug("Username [" + username + "]");
debug("URL [" + url + "]");
debug("Timeout [" + timeout + "]");
try {
debug("Going to call the sync_rest/get call");
const result = await sync_rest.get(url, inUsername, inPassword, inTimeout, "text/plain");
//debug("Setting bearer Token: " + result.data.substr(0, 10));
bearerToken = result;
if (result.error) {
console.error("Error:", result.error);
}
} catch (error) {
console.error("Unhandled error:", error);
}
debug("done");
}
function getApiUrl()
{
var idmBaseUrl;
if(domainName.indexOf('int-aws-de')>0)idmBaseUrl="https://idmas-eu-central-1.softwareag.cloud";
if(domainName.indexOf('int-aws-us')>0)idmBaseUrl="https://idmas-us-west-2.softwareag.cloud";
if(domainName.indexOf('int-aw-au')>0)idmBaseUrl="https://idmas-aw-au.softwareag.cloud";
if(domainName.indexOf('int-az-au')>0)idmBaseUrl="https://idmas-az-au.softwareag.cloud";
if(domainName.indexOf('int-az-us')>0)idmBaseUrl="https://idmas-az-us.softwareag.cloud";
if(domainName.indexOf('int-az-eu')>0)idmBaseUrl="https://idmas-az-eu.softwareag.cloud";
idmBaseUrl+="/idm/environments/";
"https://idmas-eu-central-1.softwareag.cloud/idm/environments/"
var env = domainName.split(".")[0];
return idmBaseUrl + env;
}
function setHeaders()
{
var headers = [
{"name":"Authorization","value":"Bearer " + bearerToken},
];
return headers;
}
async function authToken(){
debug("Getting authtoken");
url=getApiUrl() + "/authtoken";
await sync_rest.get(url, username, password, timeout, "text/plain");
}
function searchUserInfo(query,includeRoles,products)
{
headers = setHeaders();
url=getApiUrl()+"/users/search?query=" + query + "&includeRoles=" + includeRoles + "&products=" + products;
rest.get(url, username, password, timeout, processResponse);
}
function getUserInfo(inUsername)
{
url=getApiUrl()+"/users?username=" + inUsername;
rest.get(url, username, password, timeout, processResponse);
}
function countUsers(query)
{
var params="";
if(query!==undefined)params="?query=" + query;
url=getApiUrl()+"/users/count" + params;
rest.get(url, username, password, timeout, processTextResponse,"text/plain");
}
function roleMappings(userId)
{
url=getApiUrl()+"/users/" + userId + "/role-mappings";
rest.get(url, username, password, timeout, processResponse);
}
function allRoles()
{
url=getApiUrl()+"/roles/all";
rest.get(url, username, password, timeout, processResponse);
}
async function resetPassword(userId,newPassword)
{
url=getApiUrl()+"/users/" + userId + "/resetPassword";
debug("URL: " + url);
const base64Pw = Buffer.from(`${newPassword}`).toString('base64');
var data={};
data.password = base64Pw;
debug("Post body: " + JSON.stringify(data));
headers = setHeaders();
response = await sync_rest.custom(url, undefined, undefined, timeout,data,undefined,"PUT", headers,true,false,undefined,undefined);
debug("Response\n" + JSON.stringify(response));
console.log(JSON.stringify({"response":response}));
}
async function createUser(firstName,lastName,email,username)
{
debug("Started Create User");
url=getApiUrl()+"/users";
var data={};
data.firstName = firstName;
data.lastName = lastName;
data.email = email;
data.username = username;
headers = setHeaders();
response = await sync_rest.custom(url, undefined, undefined, timeout,data,undefined,"POST", headers,true,false,undefined,undefined);
console.log(JSON.stringify({"response":response}));
}
async function deleteUser(userId)
{
url=getApiUrl()+"/users/" + userId;
headers = setHeaders();
response = await sync_rest.custom(url, undefined, undefined, timeout,undefined,undefined,"DELETE", headers,true,false,undefined,undefined);
console.log(JSON.stringify({"response":"deleted"}));
}
async function unlockUser(userId)
{
url=getApiUrl()+"/users/" + userId + "/unlock-user";
headers = setHeaders();
response = await sync_rest.custom(url, undefined, undefined, timeout,undefined,undefined,"DELETE", headers,true,false,undefined,undefined);
console.log(JSON.stringify({"response":"unlocked"}));
}
/**
* Call back function to process REST response
* @param {return data from REST request} data
* @param {status} status
*/
function processTextResponse(restEndPointUrl, err, data, response) {
let status = response.status;
//console.log(status);
//console.log(response.text);
console.log(JSON.stringify({"response":data}));
if (status != 200) {
console.log("Error: " + status);
process.exit(status);
}
}
/**
* Call back function to process REST response
* @param {return data from REST request} data
* @param {status} status
*/
function processResponse(restEndPointUrl, err, data, response) {
let status = response.status;
if (prettyprint == true) {
console.log(JSON.stringify(data, null, 4));
}
else {
console.log(JSON.stringify(data));
}
if (status != 200) {
process.exit(status);
}
}
module.exports = { help, init, authToken, getUserInfo, deleteUser, unlockUser, resetPassword, searchUserInfo,countUsers,roleMappings,allRoles,createUser };