-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathall.js
More file actions
478 lines (429 loc) · 17.9 KB
/
all.js
File metadata and controls
478 lines (429 loc) · 17.9 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
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
var assert = require('assert');
var o365Utils = require('../o365Utils');
var PSCommandService = require('../psCommandService');
/**
* IMPORTANT!
* To run this test, you need to configure
* the following 4 variables!
*
* The credentials you are using to access o365 should
* be for a user that is setup as follows @:
* https://bitsofinfo.wordpress.com/2015/01/06/configuring-powershell-for-azure-ad-and-o365-exchange-management/
*
* @see https://github.com/bitsofinfo/powershell-credential-encryption-tools
*/
var PATH_TO_DECRYPT_UTIL_SCRIPT = 'C:\\pathto\\decryptUtil.ps1';
var PATH_TO_ENCRYPTED_CREDENTIALS = 'C:\\pathto\\encrypted.credentials';
var PATH_TO_SECRET_KEY = 'C:\\pathto\\secret.key';
var O365_TENANT_DOMAIN_NAME = "somedomain.com";
/**
* Following variables needed to test Certificate based connection to Exchange server
*
* @see https: //adamtheautomator.com/exchange-online-powershell-mfa/
* for setup instructions
*/
var PATH_TO_AUTH_CERTIFICATE = 'C:\\pathto\\certificate';
var CERTIFICATE_PASSWORD = 'xxxxxx';
var CERTIFICATE_THUMBPRINT = 'xxxxxxxxxx';
var APPLICATION_ID = '00000000-00000000-00000000-00000000';
var TENANT_ID = 'your.exhange.domain.name';
var testRun = function (done, initCommands, preDestroyCommands) {
var StatefulProcessCommandProxy = require("stateful-process-command-proxy");
// configure our proxy/pool of processes
var statefulProcessCommandProxy = new StatefulProcessCommandProxy({
name: "o365 RemotePSSession powershell pool",
max: 1,
min: 1,
idleTimeoutMS: 30000,
logFunction: function (severity, origin, msg) {
if (origin != 'Pool') {
console.log(severity.toUpperCase() + " " + origin + " " + msg);
}
},
processCommand: 'C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe',
processArgs: ['-Command', '-'],
processRetainMaxCmdHistory: 30,
processInvalidateOnRegex: {
'any': [{
'regex': '.*nomatch.*',
'flags': 'i'
}],
'stdout': [{
'regex': '.*nomatch.*'
}],
'stderr': [{
'regex': '.*nomatch.*'
}]
},
processCwd: null,
processEnvMap: null,
processUid: null,
processGid: null,
initCommands: initCommands,
validateFunction: function (processProxy) {
return processProxy.isValid();
},
preDestroyCommands: preDestroyCommands,
processCmdBlacklistRegex: o365Utils.getO365BlacklistedCommands(),
processCmdWhitelistRegex: o365Utils.getO365WhitelistedCommands(),
autoInvalidationConfig: o365Utils.getO365AutoInvalidationConfig(30000)
});
var myLogFunction = function (severity, origin, message) {
console.log(severity.toUpperCase() + ' ' + origin + ' ' + message);
};
// create our PSCommandService
var psCommandService = new PSCommandService(statefulProcessCommandProxy,
o365Utils.o365CommandRegistry,
myLogFunction);
// random seed for generated data
var random = "unitTest" + Math.abs(Math.floor(Math.random() * (1000 - 99999 + 1) + 1000));
var testUserName = "auser-" + random;
var testUserEmail = testUserName + "@" + O365_TENANT_DOMAIN_NAME;
var testUser2Name = "auser2-" + random;
var testUser2Email = testUser2Name + "@" + O365_TENANT_DOMAIN_NAME;
var testMailContactName = "amailContact-" + random;
var testMailContactEmail = testMailContactName + "@" + O365_TENANT_DOMAIN_NAME;
var testGroupName = "agroup-" + random;
var testGroupEmail = testGroupName + "@" + O365_TENANT_DOMAIN_NAME;
// total hack, needed due to deplays on ms side
var sleep = function (milliseconds) {
var start = new Date().getTime();
var c = 0;
for (var i = 0; i < 1e7; i++) {
if ((new Date().getTime() - start) > milliseconds) {
break;
} else {
console.log("SLEEP....");
}
}
};
var evalCmdResult = function (cmdResult, doWithCmdResult) {
if (cmdResult.stderr && cmdResult.stderr.length > 0) {
console.log("Stderr received: " + cmdResult.stderr);
assert(false);
// otherwise assume ok
} else {
return doWithCmdResult(cmdResult);
}
};
var evalCmdResults = function (cmdResults, doWithCmdResults) {
var hasErrors = false;
for (var i = 0; i < cmdResults.length; i++) {
var cmdResult = cmdResults[i];
if (cmdResult.stderr && cmdResult.stderr.length > 0) {
console.log("Stderr received: " + cmdResult.stderr);
hasErrors = true;
}
}
if (hasErrors) {
assert(false);
// otherwise assume ok
} else {
return doWithCmdResults(cmdResults);
}
};
var cleanupAndShutdown = function (done, error) {
psCommandService.execute('removeMsolUser', {
'UserPrincipalName': testUserEmail
});
psCommandService.execute('removeMsolUser', {
'UserPrincipalName': testUser2Email
});
psCommandService.execute('removeDistributionGroup', {
'Identity': testGroupEmail
});
psCommandService.execute('removeMailContact', {
'Identity': testMailContactEmail
});
// shut it all down
setTimeout(function () {
statefulProcessCommandProxy.shutdown();
}, 5000);
setTimeout(function () {
if (error) {
done(error);
} else {
done();
}
}, 10000);
// throw, it will stop the rest of the execution.
if (error) {
throw error;
}
};
// #1 create test users that we will use
var promise = psCommandService.executeAll(
[{
'commandName': 'newMsolUser',
'argMap': {
'DisplayName': testUserName,
'UserPrincipalName': testUserEmail
}
},
{
'commandName': 'newMsolUser',
'argMap': {
'DisplayName': testUser2Name,
'UserPrincipalName': testUser2Email
}
},
])
// handle newMsolUsers results... if ok getMsolUsers
.then(function (cmdResults) {
return evalCmdResults(cmdResults, function (cmdResults) {
try {
assert.equal(2, cmdResults.length);
} catch (e) {
cleanupAndShutdown(done, e);
}
console.log("msolUsers added OK: " + testUserEmail + " & " + testUser2Email);
return psCommandService.executeAll(
[{
'commandName': 'getMsolUser',
'argMap': {
'UserPrincipalName': testUserEmail
}
},
{
'commandName': 'getMsolUser',
'argMap': {
'UserPrincipalName': testUser2Email
}
}
]);
});
})
// handle getMsolUsers result... if ok create distributionGroup
.then(function (cmdResults) {
return evalCmdResults(cmdResults, function (cmdResults) {
try {
assert.equal(2, cmdResults.length);
} catch (e) {
cleanupAndShutdown(done, e);
}
for (var i = 0; i < cmdResults.length; i++) {
var cmdResult = cmdResults[i];
var msolUser = JSON.parse(cmdResult.stdout);
try {
// check that either of our expected ones are in here...
assert((testUserEmail == msolUser.UserPrincipalName) || (testUser2Email == msolUser.UserPrincipalName));
} catch (e) {
cleanupAndShutdown(done, e);
}
}
console.log("msolUsers fetched OK");
sleep(60000);
return psCommandService.execute('newDistributionGroup', {
'Name': testGroupName,
'DisplayName': testGroupName,
'PrimarySmtpAddress': testGroupEmail,
'ManagedBy': testUserEmail,
'Members': testUserEmail
});
});
})
// handle createDistributionResult ... if ok get distributionGroup
.then(function (cmdResult) {
return evalCmdResult(cmdResult, function (cmdResult) {
var distributionGroup = JSON.parse(cmdResult.stdout);
try {
assert.equal(testGroupEmail, distributionGroup.PrimarySmtpAddress);
} catch (e) {
cleanupAndShutdown(done, e);
}
console.log("distributionGroup created OK: " + distributionGroup.PrimarySmtpAddress);
return psCommandService.execute('getDistributionGroup', {
'Identity': testGroupEmail
});
});
})
// handle getDistributionGroup ... if ok get addDistributionGroupMember
// for user 1 and user 2
.then(function (cmdResult) {
return evalCmdResult(cmdResult, function (cmdResult) {
var distributionGroup = JSON.parse(cmdResult.stdout);
try {
assert.equal(testGroupEmail, distributionGroup.PrimarySmtpAddress);
} catch (e) {
cleanupAndShutdown(done, e);
}
console.log("distributionGroup fetched OK: " + distributionGroup.PrimarySmtpAddress);
return psCommandService.executeAll([{
'commandName': 'addDistributionGroupMember',
'argMap': {
'Identity': testGroupEmail,
'Member': testUserEmail,
'BypassSecurityGroupManagerCheck': null,
}
},
{
'commandName': 'addDistributionGroupMember',
'argMap': {
'Identity': testGroupEmail,
'Member': testUser2Email,
'BypassSecurityGroupManagerCheck': null,
}
}
]);
});
})
// handle addDistributionGroupMember ... if ok get getDistributionGroupMember
.then(function (cmdResults) {
return evalCmdResult(cmdResults, function (cmdResults) {
console.log("distributionGroupMembers added OK");
return psCommandService.execute('getDistributionGroupMember', {
'Identity': testGroupEmail
});
});
})
// handle getDistributionGrouMembers (should be 2) ...
// if ok get removeDistributionGroupMember (user2)
.then(function (cmdResult) {
return evalCmdResult(cmdResult, function (cmdResult) {
var members = JSON.parse(cmdResult.stdout);
try {
assert.equal(members.length, 2);
} catch (e) {
cleanupAndShutdown(done, e);
}
console.log("distributionGroup members fetched OK: " + members.length);
return psCommandService.execute('removeDistributionGroupMember', {
'Identity': testGroupEmail,
'Member': testUser2Email
});
});
})
// handle removeDistributionGroupMember ...
// if ok get getDistributionGroupMember
.then(function (cmdResult) {
return evalCmdResult(cmdResult, function (cmdResult) {
console.log("distributionGroupMember (user2) removed OK");
return psCommandService.execute('getDistributionGroupMember', {
'Identity': testGroupEmail
});
});
})
// handle getDistributionGrouMembers (should now be 1) ...
// if ok get newMailContact
.then(function (cmdResult) {
return evalCmdResult(cmdResult, function (cmdResult) {
var members = JSON.parse("[" + cmdResult.stdout + "]");
try {
assert.equal(members.length, 1);
assert.equal(members[0].WindowsLiveID, testUserEmail);
} catch (e) {
cleanupAndShutdown(done, e);
}
console.log("getDistributionGroupMember fetched OK: only user1 remains " + members.length);
return psCommandService.execute('newMailContact', {
'Name': testMailContactName,
'ExternalEmailAddress': testMailContactEmail,
});
});
})
// handle newMailContact add
// if ok get newMailContact
.then(function (cmdResult) {
return evalCmdResult(cmdResult, function (cmdResult) {
console.log("newMailContact added OK: " + testMailContactEmail);
return psCommandService.execute('getMailContact', {
'Identity': testMailContactEmail
});
});
})
// handle getMailContact
// if ok get addDistributionGroupMember
.then(function (cmdResult) {
return evalCmdResult(cmdResult, function (cmdResult) {
var contact = JSON.parse(cmdResult.stdout);
try {
assert.equal(testMailContactEmail, contact.PrimarySmtpAddress);
} catch (e) {
cleanupAndShutdown(done, e);
}
console.log("getMailContact fetched OK: " + testMailContactEmail);
return psCommandService.execute('addDistributionGroupMember', {
'Identity': testGroupEmail,
'Member': testMailContactEmail
});
});
})
// handle addDistributionGroupMember
// if ok get addDistributionGroupMember
.then(function (cmdResult) {
return evalCmdResult(cmdResult, function (cmdResult) {
console.log("addDistributionGroupMember mailContact added OK: " + testMailContactEmail);
return psCommandService.execute('getDistributionGroupMember', {
'Identity': testGroupEmail
});
});
})
// handle getDistributionGrouMembers (should now be 2) ...
// if ok get removeDistributionGroup
.then(function (cmdResult) {
return evalCmdResult(cmdResult, function (cmdResult) {
var members = JSON.parse(cmdResult.stdout);
try {
assert.equal(members.length, 2);
} catch (e) {
cleanupAndShutdown(done, e);
}
console.log("getDistributionGroupMember fetched OK: one mail contact and one user exist " + members.length);
return psCommandService.execute('removeDistributionGroup', {
'Identity': testGroupEmail
});
});
})
// handle removeDistributionGroup then remove msoluser...
.then(function (cmdResult) {
return evalCmdResult(cmdResult, function (cmdResult) {
console.log("distributionGroup removed OK: " + testGroupEmail);
return psCommandService.execute('removeMsolUser', {
'UserPrincipalName': testUserEmail
});
});
})
// handle removeMsolUser result... if ok shutdown...
.then(function (nothing) {
console.log("msolUser removed OK: " + testUserEmail);
cleanupAndShutdown(done, null);
})
.catch(function (error) {
console.log(error + "\n" + error.stack);
cleanupAndShutdown(done, error);
});
};
describe('test PSCommandService w/ o365CommandRegistry', function () {
it('Should test all group and mail contact commands then cleanup with Basic Auth for Exchange', function (done) {
this.timeout(120000);
testRun(done, o365Utils.getO365PSInitCommands(
PATH_TO_DECRYPT_UTIL_SCRIPT,
PATH_TO_ENCRYPTED_CREDENTIALS,
PATH_TO_SECRET_KEY,
10000, 30000, 60000), o365Utils.getO365PSDestroyCommands());
});
it('Should test all group and mail contact commands then cleanup with Certificate based auth', function (done) {
this.timeout(120000);
testRun(done, o365Utils.getO365PSKeyInitCommands(
PATH_TO_DECRYPT_UTIL_SCRIPT,
PATH_TO_ENCRYPTED_CREDENTIALS,
PATH_TO_SECRET_KEY,
PATH_TO_AUTH_CERTIFICATE,
CERTIFICATE_PASSWORD,
APPLICATION_ID,
TENANT_ID,
10000, 30000, 60000), o365Utils.getO365PSDestroyCommands());
});
// The CertificateThumbprint parameter is supported only in Microsoft Windows.
it('Should test all group and mail contact commands then cleanup with Certificate Thumb Print based auth', function (done) {
this.timeout(120000);
testRun(done, o365Utils.getO365PSInitCommands(
PATH_TO_DECRYPT_UTIL_SCRIPT,
PATH_TO_ENCRYPTED_CREDENTIALS,
PATH_TO_SECRET_KEY,
CERTIFICATE_THUMBPRINT,
APPLICATION_ID,
TENANT_ID,
10000, 30000, 60000), o365Utils.getO365PSDestroyCommands());
});
});