-
Notifications
You must be signed in to change notification settings - Fork 0
/
create.ps1
286 lines (254 loc) · 10.8 KB
/
create.ps1
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
#################################################
# HelloID-Conn-Prov-Target-Ecare-Create
# PowerShell V2
#################################################
# Enable TLS1.2
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor [System.Net.SecurityProtocolType]::Tls12
#region functions
function Get-GenericScimOAuthToken {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string]
$ClientID,
[Parameter(Mandatory = $true)]
[string]
$ClientSecret,
[Parameter(Mandatory = $true)]
[string]
$TokenUrl
)
try {
$headers = @{
'content-type' = 'application/x-www-form-urlencoded'
}
$body = @{
client_id = $ClientID
client_secret = $ClientSecret
grant_type = 'client_credentials'
scope = "Ecare.Service.SCIM"
}
$splatParams = @{
Uri = "$($TokenUrl)/connect/token"
Method = 'POST'
Headers = $headers
Body = $body
}
$Response = Invoke-RestMethod @splatParams
Write-Output $Response.access_token
} catch {
$PSCmdlet.ThrowTerminatingError($PSItem)
}
}
function Invoke-EcareRestMethod {
[CmdletBinding()]
param (
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]
$Method,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]
$Uri,
[object]
$Body,
[string]
$ContentType = 'application/json',
[Parameter(Mandatory = $false)]
[System.Collections.IDictionary]
$Headers = @{}
)
process {
try {
$splatParams = @{
Uri = $Uri
Headers = $headers
Method = $Method
ContentType = $ContentType
}
if ($Body) {
$splatParams['Body'] = $Body
}
Invoke-RestMethod @splatParams -Verbose:$false
} catch {
$PSCmdlet.ThrowTerminatingError($_)
}
}
}
function Resolve-EcareError {
[CmdletBinding()]
param (
[Parameter(Mandatory)]
[object]
$ErrorObject
)
process {
$httpErrorObj = [PSCustomObject]@{
ScriptLineNumber = $ErrorObject.InvocationInfo.ScriptLineNumber
Line = $ErrorObject.InvocationInfo.Line
ErrorDetails = $ErrorObject.Exception.Message
FriendlyMessage = $ErrorObject.Exception.Message
}
if (-not [string]::IsNullOrEmpty($ErrorObject.ErrorDetails.Message)) {
$httpErrorObj.ErrorDetails = $ErrorObject.ErrorDetails.Message
} elseif ($ErrorObject.Exception.GetType().FullName -eq 'System.Net.WebException') {
if ($null -ne $ErrorObject.Exception.Response) {
$streamReaderResponse = [System.IO.StreamReader]::new($ErrorObject.Exception.Response.GetResponseStream()).ReadToEnd()
if (-not [string]::IsNullOrEmpty($streamReaderResponse)) {
$httpErrorObj.ErrorDetails = $streamReaderResponse
}
}
}
try {
$errorDetailsObject = ($httpErrorObj.ErrorDetails | ConvertFrom-Json)
# Make sure to inspect the error result object and add only the error message as a FriendlyMessage.
# $httpErrorObj.FriendlyMessage = $errorDetailsObject.message
$httpErrorObj.FriendlyMessage = $httpErrorObj.ErrorDetails # Temporarily assignment
} catch {
$httpErrorObj.FriendlyMessage = $httpErrorObj.ErrorDetails
}
Write-Output $httpErrorObj
}
}
#endregion
try {
# Initial Assignments
$outputContext.AccountReference = 'Currently not available'
$accessToken = Get-GenericScimOAuthToken -ClientID $actionContext.Configuration.ClientId -ClientSecret $actionContext.Configuration.ClientSecret -TokenUrl $actionContext.Configuration.tokenUrl
$headers = @{
Authorization = "Bearer $accessToken"
}
# Validate correlation configuration
if ($actionContext.CorrelationConfiguration.Enabled) {
$correlationField = $actionContext.CorrelationConfiguration.accountField
$correlationValue = $actionContext.CorrelationConfiguration.accountFieldValue
if ([string]::IsNullOrEmpty($($correlationField))) {
throw 'Correlation is enabled but not configured correctly'
}
if ([string]::IsNullOrEmpty($($correlationValue))) {
throw 'Correlation is enabled but [accountFieldValue] is empty. Please make sure it is correctly mapped'
}
# Verify if a user must be either [created ] or just [correlated]
$splatParams = @{
Uri = "$($actionContext.Configuration.BaseUrl)/scim/Users?filter=urn:ietf:params:scim:schemas:extension:enterprise:2.0:User.employeeNumber eq $correlationValue"
Method = 'Get'
Headers = $headers
}
$webResponse = Invoke-EcareRestMethod @splatParams
if ($webResponse.Resources.count -eq 1) {
$correlatedAccount = $webResponse.Resources | Select-Object -First 1
} elseif ($webResponse.Resources.count -gt 1) {
throw "Multiple accounts are found for [$($actionContext.References.Account)]"
}
}
if ($null -ne $correlatedAccount) {
$action = 'CorrelateAccount'
} else {
$action = 'CreateAccount'
}
# Add a message and the result of each of the validations showing what will happen during enforcement
if ($actionContext.DryRun -eq $true) {
Write-Information "[DryRun] $action Ecare account for: [$($personContext.Person.DisplayName)], will be executed during enforcement"
}
# Process
if (-not($actionContext.DryRun -eq $true)) {
switch ($action) {
'CreateAccount' {
Write-Information 'Creating and correlating Ecare account'
$body = [ordered]@{
schemas = @(
'urn:ietf:params:scim:schemas:core:2.0:User',
'urn:ietf:params:scim:schemas:extension:enterprise:2.0:User'
)
userName = $actionContext.Data.userName
externalId = $actionContext.Data.externalId
emails = @(
[ordered]@{
type = 'work'
primary = $true
value = $actionContext.Data.WorkEmail
}
)
roles = @()
'urn:ietf:params:scim:schemas:extension:enterprise:2.0:User' = @{
employeeNumber = $actionContext.Data.employeeNumber
}
}
$splatCreate = @{
Uri = "$($actionContext.Configuration.BaseUrl)/scim/Users"
Method = 'POST'
Headers = $headers
Body = ($body | ConvertTo-Json)
ContentType = 'application/json'
}
try {
$createdAccount = Invoke-RestMethod @splatCreate
} catch {
if ($_.Exception.StatusCode -eq 'NotFound') {
throw "Employee [$($actionContext.Data.employeeNumber)] not found, Employee objects are not managed in this connector"
} else {
throw
}
}
if ($actionContext.Data.active -eq $false) {
Write-Information 'Disable the account for the newly created user.'
$bodyDisable = @{
Schemas = @(
'urn:ietf:params:scim:api:messages:2.0:PatchOp'
)
Operations = @(
@{
op = 'replace'
path = 'active'
value = 'false'
}
)
}
$splatDisable = @{
Uri = "$($actionContext.Configuration.BaseUrl)/scim/Users/$($createdAccount.id)"
Method = 'Patch'
Headers = $headers
Body = ($bodyDisable | ConvertTo-Json)
ContentType = "application/json"
}
$createdAccount = Invoke-RestMethod @splatDisable
}
$outputContext.Data = $createdAccount
$outputContext.AccountReference = $createdAccount.id
$auditLogMessage = "Create account was successful. AccountReference is: [$($outputContext.AccountReference)]"
break
}
'CorrelateAccount' {
Write-Information 'Correlating Ecare account'
$outputContext.Data = $correlatedAccount
$outputContext.AccountReference = $correlatedAccount.id
$outputContext.AccountCorrelated = $true
$auditLogMessage = "Correlated account: [$($correlatedAccount.id)] on field: [$($correlationField)] with value: [$($correlationValue)]"
break
}
}
$outputContext.success = $true
$outputContext.AuditLogs.Add([PSCustomObject]@{
Action = $action
Message = $auditLogMessage
IsError = $false
})
}
} catch {
$outputContext.success = $false
$ex = $PSItem
if ($($ex.Exception.GetType().FullName -eq 'Microsoft.PowerShell.Commands.HttpResponseException') -or
$($ex.Exception.GetType().FullName -eq 'System.Net.WebException')) {
$errorObj = Resolve-EcareError -ErrorObject $ex
$auditMessage = "Could not create or correlate Ecare account. Error: $($errorObj.FriendlyMessage)"
Write-Warning "Error at Line '$($errorObj.ScriptLineNumber)': $($errorObj.Line). Error: $($errorObj.ErrorDetails)"
} else {
$auditMessage = "Could not create or correlate Ecare account. Error: $($ex.Exception.Message)"
Write-Warning "Error at Line '$($ex.InvocationInfo.ScriptLineNumber)': $($ex.InvocationInfo.Line). Error: $($ex.Exception.Message)"
}
$outputContext.AuditLogs.Add([PSCustomObject]@{
Message = $auditMessage
IsError = $true
})
}