-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelete.ps1
284 lines (241 loc) · 11.3 KB
/
delete.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
##################################################
# HelloID-Conn-Prov-Target-Raet-Beaufort-IAM-API-Identity-Delete
# PowerShell V2
##################################################
# Enable TLS1.2
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor [System.Net.SecurityProtocolType]::Tls12
# Set debug logging
switch ($($actionContext.Configuration.isDebug)) {
$true { $VerbosePreference = 'Continue' }
$false { $VerbosePreference = 'SilentlyContinue' }
}
# Used to connect to RAET IAM API endpoints
$Script:AuthenticationUrl = "https://connect.visma.com/connect/token"
$Script:BaseUrl = "https://api.youforce.com"
#region functions
function Resolve-HTTPError {
[CmdletBinding()]
param (
[Parameter(Mandatory,
ValueFromPipeline
)]
[object]$ErrorObject
)
process {
$httpErrorObj = [PSCustomObject]@{
FullyQualifiedErrorId = $ErrorObject.FullyQualifiedErrorId
MyCommand = $ErrorObject.InvocationInfo.MyCommand
RequestUri = $ErrorObject.TargetObject.RequestUri
ScriptStackTrace = $ErrorObject.ScriptStackTrace
ErrorMessage = ''
}
if ($ErrorObject.Exception.GetType().FullName -eq 'Microsoft.PowerShell.Commands.HttpResponseException') {
$httpErrorObj.ErrorMessage = $ErrorObject.ErrorDetails.Message
}
elseif ($ErrorObject.Exception.GetType().FullName -eq 'System.Net.WebException') {
$httpErrorObj.ErrorMessage = [System.IO.StreamReader]::new($ErrorObject.Exception.Response.GetResponseStream()).ReadToEnd()
}
Write-Output $httpErrorObj
}
}
function New-RaetSession {
[CmdletBinding()]
param (
[Alias("Param1")]
[parameter(Mandatory = $true)]
[string]
$ClientId,
[Alias("Param2")]
[parameter(Mandatory = $true)]
[string]
$ClientSecret,
[Alias("Param3")]
[parameter(Mandatory = $false)]
[string]
$TenantId
)
#Check if the current token is still valid
$accessTokenValid = Confirm-AccessTokenIsValid
if ($true -eq $accessTokenValid) {
return
}
try {
# Set TLS to accept TLS, TLS 1.1 and TLS 1.2
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls -bor [Net.SecurityProtocolType]::Tls11 -bor [Net.SecurityProtocolType]::Tls12
$authorisationBody = @{
'grant_type' = "client_credentials"
'client_id' = $ClientId
'client_secret' = $ClientSecret
'tenant_id' = $TenantId
}
$splatAccessTokenParams = @{
Uri = $Script:AuthenticationUrl
Headers = @{'Cache-Control' = "no-cache" }
Method = 'POST'
ContentType = "application/x-www-form-urlencoded"
Body = $authorisationBody
UseBasicParsing = $true
}
Write-Verbose "Creating Access Token at uri '$($splatAccessTokenParams.Uri)'"
$result = Invoke-RestMethod @splatAccessTokenParams
if ($null -eq $result.access_token) {
throw $result
}
$Script:expirationTimeAccessToken = (Get-Date).AddSeconds($result.expires_in)
$Script:AuthenticationHeaders = @{
'Authorization' = "Bearer $($result.access_token)"
'Accept' = "application/json"
}
Write-Verbose "Successfully created Access Token at uri '$($splatAccessTokenParams.Uri)'"
}
catch {
# Clear verboseErrorMessage and auditErrorMessage to make sure it isn't filled with a previouw error message
$verboseErrorMessage = $null
$auditErrorMessage = $null
$ex = $PSItem
if ( $($ex.Exception.GetType().FullName -eq 'Microsoft.PowerShell.Commands.HttpResponseException') -or $($ex.Exception.GetType().FullName -eq 'System.Net.WebException')) {
$errorObject = Resolve-HTTPError -Error $ex
$verboseErrorMessage = $errorObject.ErrorMessage
$auditErrorMessage = $errorObject.ErrorMessage
}
# If error message empty, fall back on $ex.Exception.Message
if ([String]::IsNullOrEmpty($verboseErrorMessage)) {
$verboseErrorMessage = $ex.Exception.Message
}
if ([String]::IsNullOrEmpty($auditErrorMessage)) {
$auditErrorMessage = $ex.Exception.Message
}
Write-Verbose "Error at Line '$($ex.InvocationInfo.ScriptLineNumber)': $($ex.InvocationInfo.Line). Error: $($verboseErrorMessage)"
throw "Error creating Access Token at uri ''$($splatAccessTokenParams.Uri)'. Please check credentials. Error Message: $auditErrorMessage"
}
}
function Confirm-AccessTokenIsValid {
if ($null -ne $Script:expirationTimeAccessToken) {
if ((Get-Date) -le $Script:expirationTimeAccessToken) {
return $true
}
}
return $false
}
#endregion functions
try {
# Verify if [aRef] has a value
if ([string]::IsNullOrEmpty($($actionContext.References.Account))) {
throw 'The account reference could not be found'
}
$account = $actionContext.Data
$accessTokenValid = Confirm-AccessTokenIsValid
if ($true -ne $accessTokenValid) {
$splatRaetSession = @{
ClientId = $actionContext.Configuration.clientId
ClientSecret = $actionContext.Configuration.clientSecret
TenantId = $actionContext.Configuration.tenantId
}
New-RaetSession @splatRaetSession
}
Write-Verbose "Verifying if a Raet Beaufort user account for [$($personContext.Person.DisplayName)] exists"
$splatWebRequest = @{
Uri = "$($Script:BaseUrl)/iam/v1.0/users(employeeId=$($actionContext.References.Account))"
Headers = $Script:AuthenticationHeaders
Method = 'GET'
ContentType = "application/json"
UseBasicParsing = $true
}
$correlatedAccount = Invoke-RestMethod @splatWebRequest -Verbose:$false
$outputContext.PreviousData.id = $correlatedAccount.id
$outputContext.PreviousData.identity = $correlatedAccount.identityId
# Patch to API has no response so we give back the mapping value to HelloID
$outputContext.Data.id = $correlatedAccount.id
$outputContext.Data.identity = $account.identity
# Always compare the account against the current account in target system
if ($null -ne $correlatedAccount.id) {
if ([string]$correlatedAccount.identityId -ne $account.identity -and $null -ne $account.identity) {
$propertiesChanged += @('Identity')
}
if ($propertiesChanged) {
$updateAction = 'Update'
$dryRunMessage = "Would update RAET user with employeeId '$($actionContext.References.Account)'. Current identity: $($correlatedAccount.identityId). New identity: $($account.identity)"
}
else {
$updateAction = 'NoChanges'
$dryRunMessage = 'No changes will be made to the account during enforcement'
}
$action = 'DeleteAccount'
}
else {
$updateAction = 'NotFound'
$dryRunMessage = "Raet Beaufort user account: [$($actionContext.References.Account)] for person: [$($personContext.Person.DisplayName)] could not be found, possibly indicating that it could be deleted, or the account is not correlated"
}
# Add a message and the result of each of the validations showing what will happen during enforcement
if ($actionContext.DryRun -eq $true) {
Write-Verbose "[DryRun] $dryRunMessage" -Verbose
}
# Process
if (-not($actionContext.DryRun -eq $true)) {
switch ($updateAction) {
'Update' {
Write-Verbose "Updating RAET user with employeeId '$($account.externalID)'. Current identity: $($correlatedAccount.identityId). New identity: $($account.identity)"
# Some what confusing that the GET gives back a 'id' (aRef) and you have to PATCH the UPN on 'id' a well. This needs to be hardcoded
$updateAccount = [PSCustomObject]@{
id = $account.identity
}
$body = ($updateAccount | ConvertTo-Json -Depth 10)
$splatWebRequest = @{
Uri = "$($Script:BaseUrl)/iam/v1.0/users(employeeId=$($actionContext.References.Account))/identity"
Headers = $Script:AuthenticationHeaders
Method = 'PATCH'
Body = ([System.Text.Encoding]::UTF8.GetBytes($body))
ContentType = "application/json;charset=utf-8"
UseBasicParsing = $true
}
$updatedAccount = Invoke-RestMethod @splatWebRequest -Verbose:$false
$outputContext.Success = $true
$outputContext.AuditLogs.Add([PSCustomObject]@{
Action = $action
Message = "Update account was successful, current identity: [$($correlatedAccount.identityId)]. New identity: [$($account.identity)]"
IsError = $false
})
break
}
'NoChanges' {
Write-Verbose "No changes to Raet Beaufort employee account with accountReference: [$($actionContext.References.Account)]"
$outputContext.Success = $true
# Remove this AuditLog?
$outputContext.AuditLogs.Add([PSCustomObject]@{
Message = 'No changes will be made to the account during enforcement'
IsError = $false
})
break
}
'NotFound' {
$outputContext.Success = $true
$outputContext.AuditLogs.Add([PSCustomObject]@{
Message = "Raet Beaufort user account [$($actionContext.References.Account)] for: [$($personContext.Person.DisplayName)] could not be found, possibly indicating that it could be deleted, or the account is not correlated"
IsError = $false
})
break
}
}
}
}
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')) {
$errorObject = Resolve-HTTPError -Error $ex
$verboseErrorMessage = $errorObject.ErrorMessage
$auditErrorMessage = $errorObject.ErrorMessage
}
# If error message empty, fall back on $ex.Exception.Message
if ([String]::IsNullOrEmpty($verboseErrorMessage)) {
$verboseErrorMessage = $ex.Exception.Message
}
if ([String]::IsNullOrEmpty($auditErrorMessage)) {
$auditErrorMessage = $ex.Exception.Message
}
Write-Verbose "Error at Line '$($ex.InvocationInfo.ScriptLineNumber)': $($ex.InvocationInfo.Line). Error: $($verboseErrorMessage)"
$outputContext.AuditLogs.Add([PSCustomObject]@{
Message = "Could not update Raet Beaufort user account. Error Message: $($auditErrorMessage)"
IsError = $true
})
}