-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathupdate.ps1
More file actions
489 lines (435 loc) · 20.7 KB
/
Copy pathupdate.ps1
File metadata and controls
489 lines (435 loc) · 20.7 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
479
480
481
482
483
484
485
486
487
488
489
#################################################
# HelloID-Conn-Prov-Target-GoogleWorkSpace-Update
# PowerShell V2
#################################################
# Remove BuildingId from actionContext.Data if empty (as we do not want to update it with an empty value)
if ([string]::IsNullOrEmpty($actionContext.Data.BuildingId)) {
Write-Information "Skipping update of [BuildingId]. Reason: The provided value is empty. The current configuration does not allow setting this with an empty value."
$actionContext.Data.PsObject.Properties.Remove("BuildingId")
}
# Enable TLS1.2
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor [System.Net.SecurityProtocolType]::Tls12
#region functions
function Resolve-GoogleWSError {
[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)
if (-NOT([String]::IsNullOrEmpty(($errorDetailsObject.error | Select-Object -First 1).message))) {
$httpErrorObj.FriendlyMessage = $errorDetailsObject.error.message -join ', '
}
else {
$httpErrorObj.FriendlyMessage = $errorDetailsObject.error_description
}
}
catch {
$httpErrorObj.FriendlyMessage = $httpErrorObj.ErrorDetails
}
Write-Output $httpErrorObj
}
}
function Get-GoogleWSAccessToken {
[CmdletBinding()]
param (
[Parameter()]
[string]
$Issuer,
[Parameter()]
[string]
$Subject,
[Parameter()]
[string[]]$Scopes,
[Parameter()]
[string]
$P12CertificateBase64,
[Parameter()]
[string]
$P12CertificatePassword
)
try {
$now = [math]::Round(((Get-Date).ToUniversalTime() - ([datetime]"1970-01-01T00:00:00Z").ToUniversalTime()).TotalSeconds)
$jwtHeader = @{
alg = 'RS256'
typ = 'JWT'
} | ConvertTo-Json
$jwtBase64Header = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($jwtHeader))
$jwtPayload = [Ordered]@{
iss = $Issuer
sub = $Subject
scope = $($Scopes -join " ")
aud = "https://www.googleapis.com/oauth2/v4/token"
exp = $now + 3600
iat = $now
} | ConvertTo-Json
$jwtBase64Payload = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($jwtPayload))
$rawP12Certificate = [system.convert]::FromBase64String($P12CertificateBase64)
$p12Certificate = [System.Security.Cryptography.X509Certificates.X509Certificate2]::new($rawP12Certificate, $P12CertificatePassword, [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::Exportable)
$rsaPrivate = $P12Certificate.PrivateKey
$rsa = [System.Security.Cryptography.RSACryptoServiceProvider]::new()
$rsa.ImportParameters($rsaPrivate.ExportParameters($true))
$signatureInput = "$jwtBase64Header.$jwtBase64Payload"
$signature = $rsa.SignData([Text.Encoding]::UTF8.GetBytes($signatureInput), "SHA256")
$base64Signature = [System.Convert]::ToBase64String($signature)
$jwtToken = "$signatureInput.$base64Signature"
$splatParams = @{
Uri = 'https://www.googleapis.com/oauth2/v4/token'
Method = 'POST'
Body = @{
grant_type = 'urn:ietf:params:oauth:grant-type:jwt-bearer'
assertion = $jwtToken
}
ContentType = 'application/x-www-form-urlencoded'
}
$response = Invoke-RestMethod @splatParams
$response.access_token
}
catch {
$PSCmdlet.ThrowTerminatingError($_)
}
}
function ConvertTo-HelloIDAccountObject {
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline = $True)]
[object]
$GoogleAccountObject
)
process {
# Extract location information from the Google Workspace user object
# Locations represent physical spaces where the user works
# Reference: https://developers.google.com/workspace/admin/directory/reference/rest/v1/users#Location
# To extract additional location properties, add them alongside buildingId
# Note: Currently supporting single location (selecting the first one from the locations array)
# To support multiple locations, modify the retrieval logic to loop through all locations
$buildingId = $null
if ($null -ne $GoogleAccountObject.locations) {
$location = $GoogleAccountObject.locations | Select-Object -First 1
if (-not [string]::IsNullOrEmpty($location)) {
$buildingId = $location.buildingId
}
}
if ($null -ne $GoogleAccountObject.organizations) {
foreach ($organization in $GoogleAccountObject.organizations ) {
switch ($organization.type) {
'work' {
$department = $organization.department
$title = $organization.title
}
}
}
}
if ($null -ne $GoogleAccountObject.externalIds) {
foreach ($externalId in $GoogleAccountObject.externalIds ) {
switch ($externalId.type) {
'organization' {
$externalId = $externalId.value
}
}
}
}
if ($null -ne $GoogleAccountObject.relations) {
foreach ($relation in $GoogleAccountObject.relations) {
if ($relation.type -eq 'manager') {
$manager = $relation.value
break
}
}
}
if ($GoogleAccountObject.IncludeInGlobalAddressList) {
$includeInGlobalAddressList = 'true'
}
else {
$includeInGlobalAddressList = 'false'
}
$mobilePhone = $null
$workPhone = $null
foreach ($phone in $GoogleAccountObject.phones ) {
switch ($phone.type) {
'mobile' {
$mobilePhone = if ([string]::IsNullOrEmpty($phone.value)) { $null } else { $phone.value }
}
'work' {
$workPhone = if ([string]::IsNullOrEmpty($phone.value)) { $null } else { $phone.value }
}
}
}
$helloIdAccountObject = [PSCustomObject] @{
Container = "$($GoogleAccountObject.orgUnitPath)"
Department = "$department"
ExternalID = "$externalId"
FamilyName = "$($GoogleAccountObject.name.familyName)"
GivenName = "$($GoogleAccountObject.name.givenName)"
IncludeInGlobalAddressList = "$includeInGlobalAddressList"
Manager = "$Manager"
MobilePhone = $mobilePhone
PrimaryEmail = "$($GoogleAccountObject.PrimaryEmail)"
Title = "$title"
WorkPhone = $workPhone
BuildingId = $buildingId
}
Write-Output $helloIdAccountObject
}
}
function ConvertTo-GoogleAccountUpdateObject {
[CmdletBinding()]
param(
[Parameter()]
[object]
$HelloIDAccountObject,
[Parameter()]
[object]
$PropertiesToConvert,
[Parameter()]
[object]
$PreviousGoogleAccountObject
)
process {
$googleAccountUpdateObject = [PSCustomObject] @{}
# Build the user's location information
# Locations represent physical spaces where the user works (e.g., desk locations, buildings)
# Reference: https://developers.google.com/workspace/admin/directory/reference/rest/v1/users#Location
# Note: Currently supporting single location (selecting the first one during update)
if (('BuildingId' -in $PropertiesToConvert.Name)) {
[array]$locations = ($PreviousGoogleAccountObject.locations)
if ($null -ne $PreviousGoogleAccountObject.locations) {
$locations | Add-Member -MemberType 'NoteProperty' -Name 'buildingId' -Value "$($HelloIDAccountObject.BuildingId)" -Force
}
else {
$locations = @{
type = "desk"
area = "desk"
buildingId = "$($HelloIDAccountObject.BuildingId)"
}
}
$googleAccountUpdateObject | Add-Member -MemberType 'NoteProperty' -Name 'locations' -Value $locations -Force
}
if ('Container' -in $PropertiesToConvert.Name) {
$googleAccountUpdateObject | Add-Member -MemberType 'NoteProperty' -Name 'orgUnitPath' -Value $HelloIDAccountObject.Container
}
if ('ExternalID' -in $PropertiesToConvert.Name) {
$googleAccountUpdateObject | Add-Member -MemberType 'NoteProperty' -Name 'externalIds' -Value @(
@{
value = "$($actionContext.Data.ExternalId)"
type = 'organization'
}
)
}
if ('PrimaryEmail' -in $PropertiesToConvert.Name) {
$googleAccountUpdateObject | Add-Member -MemberType 'NoteProperty' -Name 'primaryEmail' -Value $HelloIDAccountObject.primaryEmail
}
if ('IncludeInGlobalAddressList' -in $PropertiesToConvert.Name) {
[bool]$includeInGlobalAddressList = [System.Convert]::ToBoolean($HelloIDAccountObject.includeInGlobalAddressList )
$googleAccountUpdateObject | Add-Member -MemberType 'NoteProperty' -Name 'includeInGlobalAddressList' -Value $includeInGlobalAddressList
}
if ('Manager' -in $PropertiesToConvert.Name) {
$googleAccountUpdateObject | Add-Member -MemberType 'NoteProperty' -Name 'relations' -Value @(
@{
type = 'manager'
value = "$($HelloIDAccountObject.Manager)"
}
)
}
# Update organization information (department and title) if changed
# Preserves existing organization data while updating the modified fields
if (('Department' -in $PropertiesToConvert.Name) -or ('Title' -in $PropertiesToConvert.Name)) {
[array]$organizations = ($PreviousGoogleAccountObject.organizations)
if ($null -ne $PreviousGoogleAccountObject.organizations) {
$organizations | Add-Member -MemberType 'NoteProperty' -Name 'title' -Value "$($HelloIDAccountObject.Title)" -Force
$organizations | Add-Member -MemberType 'NoteProperty' -Name 'department' -Value "$($HelloIDAccountObject.Department)" -Force
}
else {
$organizations = @{
title = "$($HelloIDAccountObject.Title)"
department = "$($HelloIDAccountObject.Department)"
type = 'work'
}
}
$googleAccountUpdateObject | Add-Member -MemberType 'NoteProperty' -Name 'organizations' -Value $organizations -Force
}
if (('FamilyName' -in $PropertiesToConvert.Name) -or ('GivenName' -in $PropertiesToConvert.Name)) {
$name = @{
givenName = "$($HelloIDAccountObject.givenName)"
familyName = "$($HelloIDAccountObject.FamilyName)"
}
$googleAccountUpdateObject | Add-Member -MemberType 'NoteProperty' -Name 'name' -Value $name
}
if (('MobilePhone' -in $PropertiesToConvert.Name) -or ('WorkPhone' -in $PropertiesToConvert.Name)) {
[array]$phones = ($PreviousGoogleAccountObject.phones)
$phoneTypes = @{
MobilePhone = 'mobile'
WorkPhone = 'work'
}
# Update the phone numbers list by updating existing numbers, adding new ones, and removing any empty entries.
foreach ($property in $phoneTypes.Keys) {
if ($property -in $PropertiesToConvert.Name) {
$objectToUpdate = $phones | Where-Object { $_.type -eq ($property -split 'Phone')[0] }
if ($null -ne $objectToUpdate) {
if ([System.String]::IsNullOrEmpty($HelloIDAccountObject.$property)) {
$phones = $phones | Where-Object { $_.type -ne ($property -split 'Phone')[0] }
}
else {
$objectToUpdate.value = "$($HelloIDAccountObject.$property)"
}
}
else {
[array]$phones += ([PSCustomObject]@{
type = $phoneTypes[$property]
value = "$($HelloIDAccountObject.$property)"
})
}
}
}
$googleAccountUpdateObject | Add-Member -MemberType 'NoteProperty' -Name 'phones' -Value $phones
}
write-output $googleAccountUpdateObject
}
}
#endregion
try {
# Verify if [aRef] has a value
if ([string]::IsNullOrEmpty($($actionContext.References.Account))) {
throw 'The account reference could not be found'
}
Write-Information 'Getting JWT token'
$splatGetGoogleWSTokenParams = @{
Issuer = $actionContext.Configuration.Issuer
Subject = $actionContext.Configuration.Subject
Scopes = @("https://www.googleapis.com/auth/admin.directory.user")
P12CertificateBase64 = $actionContext.Configuration.P12CertificateBase64
P12CertificatePassword = $actionContext.Configuration.P12CertificatePassword
}
$accessToken = Get-GoogleWSAccessToken @splatGetGoogleWSTokenParams
Write-Information 'Setting authentication headers'
$headers = [System.Collections.Generic.Dictionary[string, string]]::new()
$headers.Add('Authorization', "Bearer $($accessToken)")
Write-Information 'Verifying if a GoogleWS account exists'
try {
$splatGetUserParams = @{
Uri = "https://www.googleapis.com/admin/directory/v1/users/$($actionContext.References.Account)"
Method = 'GET'
Headers = $headers
}
$correlatedAccountGoogle = Invoke-RestMethod @splatGetUserParams
}
catch {
if ($_.Exception.Response.StatusCode -ne 404) {
throw $_
}
}
$correlatedAccount = ConvertTo-HelloIDAccountObject -GoogleAccountObject $correlatedAccountGoogle
$outputContext.PreviousData = $correlatedAccount
# Always compare the account against the current account in target system
if ($null -ne $correlatedAccount) {
$splatCompareProperties = @{
ReferenceObject = @($correlatedAccount.PSObject.Properties)
DifferenceObject = @($actionContext.Data.PSObject.Properties)
}
$propertiesChanged = Compare-Object @splatCompareProperties -PassThru | Where-Object { $_.SideIndicator -eq '=>' }
if ($actionContext.Configuration.MoveAccountOnUpdate -eq $false) {
[array]$propertiesChanged = $propertiesChanged | Where-Object { $_.Name -ne 'Container' }
$outputContext.Data.Container = $actionContext.PreviousData.Container
}
if ($propertiesChanged) {
$action = 'UpdateAccount'
}
else {
$action = 'NoChanges'
}
}
else {
$action = 'NotFound'
}
# Process
switch ($action) {
'UpdateAccount' {
Write-Information "Account property(s) required to update: $($propertiesChanged.Name -join ', ')"
$googleAccountUpdateObject = ConvertTo-GoogleAccountUpdateObject -HelloIDAccountObject $actionContext.Data -PropertiesToConvert $propertiesChanged -PreviousGoogleAccountObject $correlatedAccountGoogle
Write-Information "Updating GoogleWS account with accountReference: [$($actionContext.References.Account)]"
$splatUpdateParams = @{
Uri = "https://www.googleapis.com/admin/directory/v1/users/$($actionContext.References.Account)"
# Use PUT instead of PATCH as Google API supports patch semantics, meaning that you only need to include the fields you wish to update. Fields that are not present in the request will be preserved, and fields set to null will be cleared.
# Docs: https://developers.google.com/workspace/admin/directory/reference/rest/v1/users/update
Method = 'PUT'
Body = $googleAccountUpdateObject | ConvertTo-Json
Headers = $headers
ContentType = 'application/json;charset=utf-8'
}
if (-not($actionContext.DryRun -eq $true)) {
$updatedAccountGoogle = Invoke-RestMethod @splatUpdateParams
$outputContext.Data = $updatedAccountGoogle | ConvertTo-HelloIDAccountObject
if ($propertiesChanged.Name -contains 'primaryEmail') {
$outputContext.AccountReference = @{id = $correlatedAccountGoogle.id; primaryEmail = $updatedAccountGoogle.primaryEmail }
}
}
else {
Write-Information "[DryRun] Update GoogleWS account with accountReference: [$($actionContext.References.Account)], will be executed during enforcement"
}
$outputContext.Success = $true
$outputContext.AuditLogs.Add([PSCustomObject]@{
Message = "Update account was successful, Account property(s) updated: [$($propertiesChanged.name -join ',')]"
IsError = $false
})
break
}
'NoChanges' {
Write-Information "No changes to GoogleWS account with accountReference: [$($actionContext.References.Account)]"
$outputContext.Data = $correlatedAccount
$outputContext.Success = $true
$outputContext.AuditLogs.Add([PSCustomObject]@{
Message = 'No changes will be made to the account during enforcement'
IsError = $false
})
break
}
'NotFound' {
Write-Information "GoogleWS account: [$($actionContext.References.Account)] could not be found, possibly indicating that it may have been deleted"
$outputContext.Success = $false
$outputContext.AuditLogs.Add([PSCustomObject]@{
Message = "GoogleWS account with accountReference: [$($actionContext.References.Account)] could not be found, possibly indicating that it may have been deleted"
IsError = $true
})
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')) {
$errorObj = Resolve-GoogleWSError -ErrorObject $ex
$auditMessage = "Could not create or correlate GoogleWS account. Error: $($errorObj.FriendlyMessage)"
$warningMessage = "Error at Line '$($errorObj.ScriptLineNumber)': $($errorObj.Line). Error: $($errorObj.ErrorDetails)"
}
else {
$auditMessage = "Could not create or correlate GoogleWS account. Error: $($ex.Exception.Message)"
$warningMessage = "Error at Line '$($ex.InvocationInfo.ScriptLineNumber)': $($ex.InvocationInfo.Line). Error: $($ex.Exception.Message)"
}
Write-Warning $warningMessage
$outputContext.AuditLogs.Add([PSCustomObject]@{
Message = $auditMessage
IsError = $true
})
}