Skip to content

Commit 52bb59d

Browse files
authored
Merge pull request #10 from bitsofinfo/nf/key-based-auth
Added two init/destroy methods to use key based Exchange authentication
2 parents 8c96529 + 3dfa3ba commit 52bb59d

File tree

6 files changed

+971
-518
lines changed

6 files changed

+971
-518
lines changed

README.md

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,20 @@ This provides the PSCommandService class which is a wrapper around [StatefulProc
2626

2727
This script simply exports a few useful pre-defined parameter sets (that one would pass to the constructor of StatefulProcessComamndProxy) for the initialization, destruction and auto-invalidation of "powershell" processes who connect to o365 and establish a remote PSSession that will be long lived. (and validate that the session is still legit)
2828

29+
#### Exchange authentication
30+
31+
`o365Utils.js` init command `getO365PSInitCommands` is using a deprecated authentication [method](https://techcommunity.microsoft.com/t5/exchange-team-blog/modern-auth-and-unattended-scripts-in-exchange-online-powershell/ba-p/1497387)
32+
33+
Mictosoft has added [Exchange Online PowerShell V2](https://techcommunity.microsoft.com/t5/exchange-team-blog/announcing-general-availability-of-the-exchange-online/ba-p/1436623) that supports cerificate based authentication.
34+
35+
Full setup is descibed [here](https://adamtheautomator.com/exchange-online-powershell-mfa/)
36+
37+
Three sets of init commands are availiable as of version `1.1.0`:
38+
39+
* `getO365PSInitCommands` - backward compatible old basic authentication
40+
* `getO365PSKeyInitCommands` - new Exchange authentication with private key and password
41+
* `getO365PSThumbprintInitCommands` - new Exchange authentication with the thumb print for the certificate
42+
2943
### <a name="usage"></a>Usage
3044

3145
1) Configure your o365 tenant with a user with the appropriate permissions to manage o365 via Powershell. [See this article to get going](https://bitsofinfo.wordpress.com/2015/01/06/configuring-powershell-for-azure-ad-and-o365-exchange-management/)
@@ -34,15 +48,18 @@ This script simply exports a few useful pre-defined parameter sets (that one wou
3448

3549
3) From within this project install the necessary npm dependencies for this module, including [stateful-process-command-proxy](https://github.com/bitsofinfo/stateful-process-command-proxy). You can checkout the latter manually and do a ```npm install stateful-process-command-proxy```
3650

37-
4) Configure ```example.js``` appropriately, in particular the ```initCommands``` for the StatefulProcessCommandProxy; the paths to the items you created via the second step above
51+
4) Configure ```example.js```/```example_key_auth.js```/```examplekey_thumb_auth.js``` appropriately, in particular the ```initCommands``` for the StatefulProcessCommandProxy; the paths to the items you created via the second step above
3852

39-
5) Tweak the group that is fetched at the bottom of ```example.js```
53+
5) Tweak the group that is fetched at the bottom of ```example.js```/```example_key_auth.js```/```examplekey_thumb_auth.js```
4054

41-
7) There is also a unit-test (```test\all.js```) for the command registry in ```o365Utils.js``` which gives an example of usage.
55+
7) There is also a unit-test (```test\all.js```) for the command registry in ```o365Utils.js``` which gives an example of usage for all thre possible Exchange connect variations.
4256

4357
### <a id="history"></a>History
4458

4559
```
60+
v1.1.0 - 2020-12-03
61+
- Added option for key and thumbprint based Exchange authentication
62+
4663
v1.0.0 - 2016-06-08
4764
- Get-DistributionGroupMember - added "-ResultSize Unlimited"
4865

example.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
var Promise = require('promise');
21
var StatefulProcessCommandProxy = require("stateful-process-command-proxy");
32
var PSCommandService = require('./psCommandService');
43
var o365Utils = require('./o365Utils');

example_key_auth.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
var StatefulProcessCommandProxy = require("stateful-process-command-proxy");
2+
var PSCommandService = require('./psCommandService');
3+
var o365Utils = require('./o365Utils');
4+
5+
6+
7+
8+
var statefulProcessCommandProxy = new StatefulProcessCommandProxy({
9+
name: "StatefulProcessCommandProxy",
10+
max: 1,
11+
min: 1,
12+
idleTimeoutMS:120000,
13+
log: function(severity,origin,msg) {
14+
console.log(severity.toUpperCase() + " " +origin+" "+ msg);
15+
},
16+
17+
processCommand: 'C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe',
18+
processArgs: ['-Command','-'],
19+
20+
21+
processRetainMaxCmdHistory : 20,
22+
processInvalidateOnRegex : {
23+
'any':[],
24+
'stdout':[],
25+
'stderr':[{'regex':'.*error.*'}]
26+
},
27+
processCwd : null,
28+
processEnvMap : null,
29+
processUid : null,
30+
processGid : null,
31+
32+
initCommands: o365Utils.getO365PSKeyInitCommands(
33+
'C:\\pathto\\decryptUtil.ps1',
34+
'C:\\pathto\\encrypted.credentials',
35+
'C:\\pathto\\secret.key',
36+
'C:\\pathto\\certificate',
37+
'certificatePassword',
38+
'00000000-00000000-00000000-00000000',
39+
'your.exhange.domain.name',
40+
10000,30000,60000),
41+
42+
43+
validateFunction: function(processProxy) {
44+
var isValid = processProxy.isValid();
45+
if(!isValid) {
46+
console.log("ProcessProxy.isValid() returns FALSE!");
47+
}
48+
return isValid;
49+
},
50+
51+
52+
preDestroyCommands: o365Utils.getO365PSKeyDestroyCommands(),
53+
54+
processCmdWhitelistRegex: o365Utils.getO365WhitelistedCommands(),
55+
56+
processCmdBlacklistRegex: o365Utils.getO365BlacklistedCommands(),
57+
58+
autoInvalidationConfig: o365Utils.getO365AutoInvalidationConfig(30000)
59+
60+
});
61+
62+
var myLogFunction = function(severity,origin,message) {
63+
console.log(severity.toUpperCase() + ' ' + origin + ' ' + message);
64+
}
65+
66+
67+
/**
68+
* Fetch a group!
69+
*/
70+
var psCommandService = new PSCommandService(statefulProcessCommandProxy,
71+
o365Utils.o365CommandRegistry,
72+
myLogFunction);
73+
74+
psCommandService.execute('getDistributionGroup',{'Identity':"someGroupName"})
75+
.then(function(groupJson) {
76+
console.log(groupJson);
77+
}).catch(function(error) {
78+
console.log(error);
79+
});
80+
81+
setTimeout(function(){statefulProcessCommandProxy.shutdown()},80000);

example_key_thumb_auth.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
var StatefulProcessCommandProxy = require("stateful-process-command-proxy");
2+
var PSCommandService = require('./psCommandService');
3+
var o365Utils = require('./o365Utils');
4+
5+
6+
7+
8+
var statefulProcessCommandProxy = new StatefulProcessCommandProxy({
9+
name: "StatefulProcessCommandProxy",
10+
max: 1,
11+
min: 1,
12+
idleTimeoutMS:120000,
13+
log: function(severity,origin,msg) {
14+
console.log(severity.toUpperCase() + " " +origin+" "+ msg);
15+
},
16+
17+
processCommand: 'C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe',
18+
processArgs: ['-Command','-'],
19+
20+
21+
processRetainMaxCmdHistory : 20,
22+
processInvalidateOnRegex : {
23+
'any':[],
24+
'stdout':[],
25+
'stderr':[{'regex':'.*error.*'}]
26+
},
27+
processCwd : null,
28+
processEnvMap : null,
29+
processUid : null,
30+
processGid : null,
31+
32+
initCommands: o365Utils.getO365PSThumbprintInitCommands(
33+
'C:\\pathto\\decryptUtil.ps1',
34+
'C:\\pathto\\encrypted.credentials',
35+
'C:\\pathto\\secret.key',
36+
'certificatethumbprint',
37+
'00000000-00000000-00000000-00000000',
38+
'your.exhange.domain.name',
39+
10000,30000,60000),
40+
41+
42+
validateFunction: function(processProxy) {
43+
var isValid = processProxy.isValid();
44+
if(!isValid) {
45+
console.log("ProcessProxy.isValid() returns FALSE!");
46+
}
47+
return isValid;
48+
},
49+
50+
51+
preDestroyCommands: o365Utils.getO365PSThumbprintDestroyCommands(),
52+
53+
processCmdWhitelistRegex: o365Utils.getO365WhitelistedCommands(),
54+
55+
processCmdBlacklistRegex: o365Utils.getO365BlacklistedCommands(),
56+
57+
autoInvalidationConfig: o365Utils.getO365AutoInvalidationConfig(30000)
58+
59+
});
60+
61+
var myLogFunction = function(severity,origin,message) {
62+
console.log(severity.toUpperCase() + ' ' + origin + ' ' + message);
63+
}
64+
65+
66+
/**
67+
* Fetch a group!
68+
*/
69+
var psCommandService = new PSCommandService(statefulProcessCommandProxy,
70+
o365Utils.o365CommandRegistry,
71+
myLogFunction);
72+
73+
psCommandService.execute('getDistributionGroup',{'Identity':"someGroupName"})
74+
.then(function(groupJson) {
75+
console.log(groupJson);
76+
}).catch(function(error) {
77+
console.log(error);
78+
});
79+
80+
setTimeout(function(){statefulProcessCommandProxy.shutdown()},80000);

0 commit comments

Comments
 (0)