-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathdeploy.ps1
More file actions
1031 lines (859 loc) · 37.4 KB
/
deploy.ps1
File metadata and controls
1031 lines (859 loc) · 37.4 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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
###############################################################################################################
##
## Azure IPAM Solution Deployment Script
##
###############################################################################################################
# Set minimum version requirements
#Requires -Version 7.2
#Requires -Modules @{ ModuleName="Az"; ModuleVersion="8.0.0"}
#Requires -Modules @{ ModuleName="Microsoft.Graph"; ModuleVersion="1.9.6"}
# Intake and set global parameters
[CmdletBinding(DefaultParameterSetName = 'Full')]
param(
[Parameter(ValueFromPipelineByPropertyName = $true,
Mandatory = $true,
ParameterSetName = 'Full')]
[Parameter(ValueFromPipelineByPropertyName = $true,
Mandatory = $true,
ParameterSetName = 'TemplateOnly')]
[Parameter(ValueFromPipelineByPropertyName = $true,
Mandatory = $true,
ParameterSetName = 'Function')]
[string]
$Location,
[Parameter(ValueFromPipelineByPropertyName = $true,
Mandatory = $false,
ParameterSetName = 'Full')]
[Parameter(ValueFromPipelineByPropertyName = $true,
Mandatory = $false,
ParameterSetName = 'AppsOnly')]
[string]
$UIAppName = 'ipam-ui-app',
[Parameter(ValueFromPipelineByPropertyName = $true,
Mandatory = $false,
ParameterSetName = 'Full')]
[Parameter(ValueFromPipelineByPropertyName = $true,
Mandatory = $false,
ParameterSetName = 'AppsOnly')]
[Parameter(ValueFromPipelineByPropertyName = $true,
Mandatory = $false,
ParameterSetName = 'Function')]
[Parameter(ValueFromPipelineByPropertyName = $true,
Mandatory = $false,
ParameterSetName = 'FuncAppsOnly')]
[string]
$EngineAppName = 'ipam-engine-app',
[Parameter(Mandatory = $false,
ParameterSetName = 'Full')]
[Parameter(Mandatory = $false,
ParameterSetName = 'TemplateOnly')]
[Parameter(Mandatory = $false,
ParameterSetName = 'Function')]
[ValidateLength(1,7)]
[string]
$NamePrefix,
[Parameter(Mandatory = $false,
ParameterSetName = 'Full')]
[Parameter(Mandatory = $false,
ParameterSetName = 'TemplateOnly')]
[Parameter(Mandatory = $false,
ParameterSetName = 'Function')]
[hashtable]
$Tags,
[Parameter(Mandatory = $true,
ParameterSetName = 'AppsOnly')]
[Parameter(Mandatory = $true,
ParameterSetName = 'FuncAppsOnly')]
[switch]
$AppsOnly,
[Parameter(Mandatory = $true,
ParameterSetName = 'Function')]
[Parameter(Mandatory = $true,
ParameterSetName = 'FuncAppsOnly')]
[switch]
$AsFunction,
[Parameter(Mandatory = $false,
ParameterSetName = 'Full')]
[Parameter(Mandatory = $false,
ParameterSetName = 'Function')]
[Parameter(Mandatory = $false,
ParameterSetName = 'TemplateOnly')]
[switch]
$PrivateACR,
[Parameter(Mandatory = $false,
ParameterSetName = 'Full')]
[Parameter(Mandatory = $false,
ParameterSetName = 'Function')]
[Parameter(Mandatory = $false,
ParameterSetName = 'TemplateOnly')]
[ValidateSet('Debian', 'RHEL')]
[string]
$ContainerType = 'Debian',
[Parameter(Mandatory = $true,
ParameterSetName = 'TemplateOnly')]
[ValidateScript({
if(-Not ($_ | Test-Path) ){
throw [System.ArgumentException]::New("Target file or does not exist.")
}
if(-Not ($_ | Test-Path -PathType Leaf) ){
throw [System.ArgumentException]::New("The 'ParameterFile' argument must be a file, folder paths are not allowed.")
}
if($_ -notmatch "(\.json)"){
throw [System.ArgumentException]::New("The file specified in the 'ParameterFile' argument must be of type json.")
}
return $true
})]
[System.IO.FileInfo]
$ParameterFile
)
DynamicParam {
$validators = @{
functionName = '^(?=^.{2,59}$)([^-][\w-]*[^-])$'
appServiceName = '^(?=^.{2,59}$)([^-][\w-]*[^-])$'
appServicePlanName = '^(?=^.{1,40}$)([\w-]*)$'
cosmosAccountName = '^(?=^.{3,44}$)([^-][a-z0-9-]*[^-])$'
cosmosContainerName = '^(?=^.{1,255}$)([^/\\#?]*)$'
cosmosDatabaseName = '^(?=^.{1,255}$)([^/\\#?]*)$'
keyVaultName = '^(?=^.{3,24}$)(?!.*--)([a-zA-Z][a-zA-Z0-9-]*[a-zA-Z0-9])$'
workspaceName = '^(?=^.{4,63}$)([a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9])$'
managedIdentityName = '^(?=^.{3,128}$)([a-zA-Z0-9][a-zA-Z0-9-_]*)$'
resourceGroupName = '^(?=^.{1,90}$)(?!.*\.$)([a-zA-Z0-9-_\.\p{L}\p{N}]*)$'
storageAccountName = '^(?=^.{3,24}$)([a-z0-9]*)$'
containerRegistryName = '^(?=^.{5,50}$)([a-zA-Z0-9]*)$'
}
if(-not $PrivateAcr) {
$validators.Remove('containerRegistryName')
}
if(-not $AsFunction) {
$validators.Remove('functionName')
$validators.Remove('storageAccountName')
}
if($AsFunction) {
$validators.Remove('appServiceName')
}
$attrFull = [System.Management.Automation.ParameterAttribute]::new()
$attrFull.ParameterSetName = 'ResourceNames'
$attrFull.ParameterSetName = "Full"
$attrFull.Mandatory = $false
$attrTemplateOnly = [System.Management.Automation.ParameterAttribute]::new()
$attrTemplateOnly.ParameterSetName = 'ResourceNames'
$attrTemplateOnly.ParameterSetName = "TemplateOnly"
$attrTemplateOnly.Mandatory = $false
$attrFunction = [System.Management.Automation.ParameterAttribute]::new()
$attrFunction.ParameterSetName = 'ResourceNames'
$attrFunction.ParameterSetName = "Function"
$attrFunction.Mandatory = $false
$attrValidation = [System.Management.Automation.ValidateScriptAttribute]::new({
$invalidFields = [System.Collections.ArrayList]@()
$missingFields = [System.Collections.ArrayList]@()
foreach ($validator in $validators.GetEnumerator()) {
if ($_.ContainsKey($validator.Name)) {
if (-not ($_[$validator.Name] -match $validator.Value)) {
$invalidFields.Add($validator.Name) | Out-Null
}
} else {
$missingFields.Add($validator.Name) | Out-Null
}
}
if ($invalidFields -or $missingFields) {
$deploymentType = $PrivateAcr ? "'$($PSCmdlet.ParameterSetName) w/ Private ACR'" : $PSCmdlet.ParameterSetName
Write-Host "ERROR: Missing or improperly formatted field(s) in 'ResourceNames' parameter for deploment type '$deploymentType'" -ForegroundColor Red
foreach ($field in $invalidFields) {
Write-Host "ERROR: Invalid Field ->" $field -ForegroundColor Red
}
foreach ($field in $missingFields) {
Write-Host "ERROR: Missing Field ->" $field -ForegroundColor Red
}
Write-Host "ERROR: Please refer to the 'Naming Rules and Restrictions for Azure Resources'" -ForegroundColor Red
Write-Host "ERROR: " -ForegroundColor Red -NoNewline
Write-Host "https://learn.microsoft.com/en-us/azure/azure-resource-manager/management/resource-name-rules" -ForegroundColor Yellow
Write-Host ""
throw [System.ArgumentException]::New("One of the required resource names is missing or invalid.")
}
return -not ($invalidFields -or $missingFields)
})
$attributeCollection = [System.Collections.ObjectModel.Collection[System.Attribute]]::new()
$attributeCollection.Add($attrFull)
$attributeCollection.Add($attrTemplateOnly)
$attributeCollection.Add($attrFunction)
$attributeCollection.Add($attrValidation)
$param = [System.Management.Automation.RuntimeDefinedParameter]::new('ResourceNames', [hashtable], $attributeCollection)
$paramDict = [System.Management.Automation.RuntimeDefinedParameterDictionary]::new()
$paramDict.Add('ResourceNames', $param)
return $paramDict
}
begin {
$ResourceNames = $PSBoundParameters['ResourceNames']
}
process {
$AZURE_ENV_MAP = @{
AzureCloud = "AZURE_PUBLIC"
AzureUSGovernment = "AZURE_US_GOV"
AzureGermanCloud = "AZURE_GERMANY"
AzureChinaCloud = "AZURE_CHINA"
}
$MIN_AZ_CLI_VER = [System.Version]'2.35.0'
$DEBUG_MODE = [bool]$PSCmdlet.MyInvocation.BoundParameters[“Debug”].IsPresent
# Set preference variables
$ErrorActionPreference = "Stop"
$DebugPreference = 'SilentlyContinue'
# Hide Azure PowerShell SDK Warnings
$Env:SuppressAzurePowerShellBreakingChangeWarnings = $true
# Set Log File Location
$logPath = [Io.Path]::Combine('..', 'logs')
New-Item -ItemType Directory -Force -Path $logpath | Out-Null
$debugLog = Join-Path -Path $logPath -ChildPath "debug_$(get-date -format `"yyyyMMddhhmmsstt`").log"
$errorLog = Join-Path -Path $logPath -ChildPath "error_$(get-date -format `"yyyyMMddhhmmsstt`").log"
$transcriptLog = Join-Path -Path $logPath -ChildPath "deploy_$(get-date -format `"yyyyMMddhhmmsstt`").log"
$debugSetting = $DEBUG_MODE ? 'Continue' : 'SilentlyContinue'
Start-Transcript -Path $transcriptLog | Out-Null
Function Test-Location {
Param(
[Parameter(Mandatory=$true)]
[string]$Location
)
$validLocations = Get-AzLocation | Select-Object -ExpandProperty Location
return $validLocations.Contains($Location)
}
# Create unique string to be used in naming for each deployment instance
Function New-UniqueIPAMDeployString {
$unique = (-join ((48..57) + (97..122) | Get-Random -Count 13 | % {[char]$_}))
$script:UIAppName = $UIAppName + "-" + $unique
$script:EngineAppName = $EngineAppName + "-" + $unique
return $unique
}
Function Deploy-IPAMApplications {
Param(
[Parameter(Mandatory=$false)]
[string]$EngineAppName = 'ipam-engine-app',
[Parameter(Mandatory=$false)]
[string]$UIAppName = 'ipam-ui-app',
[Parameter(Mandatory=$true)]
[string]$TenantId,
[Parameter(Mandatory=$true)]
[string]$AzureCloud,
[Parameter(Mandatory=$false)]
[bool]$AsFunction
)
$uiResourceAccess = [System.Collections.ArrayList]@(
@{
ResourceAppId = "00000003-0000-0000-c000-000000000000"; # Microsoft Graph
ResourceAccess = @(
@{
Id = "37f7f235-527c-4136-accd-4a02d197296e"; # openid
Type = "Scope"
},
@{
Id = "14dad69e-099b-42c9-810b-d002981feec1"; # profile
Type = "Scope"
},
@{
Id = "7427e0e9-2fba-42fe-b0c0-848c9e6a8182"; # offline_access
Type = "Scope"
},
@{
Id = "e1fe6dd8-ba31-4d61-89e7-88639da4683d"; # User.Read
Type = "Scope"
},
@{
Id = "06da0dbc-49e2-44d2-8312-53f166ab848a"; # Directory.Read.All
Type = "Scope"
}
)
}
)
$uiWebSettings = @{
ImplicitGrantSetting = @{
EnableAccessTokenIssuance = $true
EnableIdTokenIssuance = $true
}
}
# Create IPAM UI Application (If not deployed as a Function App)
if (-not $AsFunction) {
Write-Host "INFO: Creating Azure IPAM UI Application" -ForegroundColor Green
Write-Verbose -Message "Creating Azure IPAM UI Application"
$uiApp = New-AzADApplication `
-DisplayName $UiAppName `
-SPARedirectUri "https://replace-this-value.azurewebsites.net" `
-Web $uiWebSettings
}
$engineResourceMap = @{
"AZURE_PUBLIC" = @{
ResourceAppId = "797f4846-ba00-4fd7-ba43-dac1f8f63013"
ResourceAccessIds = @("41094075-9dad-400e-a0bd-54e686782033")
}
"AZURE_US_GOV" = @{
ResourceAppId = "40a69793-8fe6-4db1-9591-dbc5c57b17d8"
ResourceAccessIds = @("8eb49ffc-05ac-454c-9027-8648349217dd", "e59ee429-1fb1-4054-b99f-f542e8dc9b95")
}
"AZURE_GERMANY" = @{
ResourceAppId = "797f4846-ba00-4fd7-ba43-dac1f8f63013"
ResourceAccessIds = @("41094075-9dad-400e-a0bd-54e686782033")
}
"AZURE_CHINA" = @{
ResourceAppId = "797f4846-ba00-4fd7-ba43-dac1f8f63013"
ResourceAccessIds = @("41094075-9dad-400e-a0bd-54e686782033")
}
}
$engineResourceAppId = $engineResourceMap[$AzureCloud].ResourceAppId
$engineResourceAccess = [System.Collections.ArrayList]@()
foreach ($engineAccessId in $engineResourceMap[$AzureCloud].ResourceAccessIds) {
$access = @{
Id = $engineAccessId
Type = "Scope"
}
$engineResourceAccess.Add($access) | Out-Null
}
$engineResourceAccessList = [System.Collections.ArrayList]@(
@{
ResourceAppId = $engineResourceAppId
ResourceAccess = $engineResourceAccess
}
)
$engineApiGuid = New-Guid
$knownClientApplication = @(
$uiApp.AppId
)
$engineApiSettings = @{
Oauth2PermissionScope = @(
@{
AdminConsentDescription = "Allows the IPAM UI to access IPAM Engine API as the signed-in user."
AdminConsentDisplayName = "Access IPAM Engine API"
Id = $engineApiGuid
IsEnabled = $true
Type = "User"
UserConsentDescription = "Allow the IPAM UI to access IPAM Engine API on your behalf."
UserConsentDisplayName = "Access IPAM Engine API"
Value = "access_as_user"
}
)
PreAuthorizedApplication = @( # Allow Azure PowerShell/CLI to obtain access tokens
@{
AppId = "1950a258-227b-4e31-a9cf-717495945fc2" # Azure PowerShell
DelegatedPermissionId = @( $engineApiGuid )
},
@{
AppId = "04b07795-8ddb-461a-bbee-02f9e1bf7b46" # Azure CLI
DelegatedPermissionId = @( $engineApiGuid )
}
)
RequestedAccessTokenVersion = 2
}
# Add the UI App as a Known Client App (If not deployed as Function App)
if (-not $AsFunction) {
$engineApiSettings.Add("KnownClientApplication", $knownClientApplication)
}
# Create IPAM Engine Application
Write-Host "INFO: Creating Azure IPAM Engine Application" -ForegroundColor Green
Write-Verbose -Message "Creating Azure IPAM Engine Application"
$engineApp = New-AzADApplication `
-DisplayName $EngineAppName `
-Api $engineApiSettings `
-RequiredResourceAccess $engineResourceAccessList
# Update IPAM Engine API Endpoint (If not deployed as Function App)
if (-not $AsFunction) {
Write-Host "INFO: Updating Azure IPAM Engine API Endpoint" -ForegroundColor Green
Write-Verbose -Message "Updating Azure IPAM UI Engine API Endpoint"
Update-AzADApplication -ApplicationId $engineApp.AppId -IdentifierUri "api://$($engineApp.AppId)"
$uiEngineApiAccess =@{
ResourceAppId = $engineApp.AppId
ResourceAccess = @(
@{
Id = $engineApiGuid
Type = "Scope"
}
)
}
$uiResourceAccess.Add($uiEngineApiAccess) | Out-Null
}
# Update IPAM UI Application Resource Access (If not deployed as Function App)
if (-not $AsFunction) {
Write-Host "INFO: Updating Azure IPAM UI Application Resource Access" -ForegroundColor Green
Write-Verbose -Message "Updating Azure IPAM UI Application Resource Access"
Update-AzADApplication -ApplicationId $uiApp.AppId -RequiredResourceAccess $uiResourceAccess
$uiObject = Get-AzADApplication -ApplicationId $uiApp.AppId
}
$engineObject = Get-AzADApplication -ApplicationId $engineApp.AppId
# Create IPAM UI Service Principal (If not deployed as Function App)
if (-not $AsFunction) {
Write-Host "INFO: Creating Azure IPAM UI Service Principal" -ForegroundColor Green
Write-Verbose -Message "Creating Azure IPAM UI Service Principal"
New-AzADServicePrincipal -ApplicationObject $uiObject | Out-Null
}
$scope = "/providers/Microsoft.Management/managementGroups/$TenantId"
# Create IPAM Engine Service Principal
Write-Host "INFO: Creating Azure IPAM Engine Service Principal" -ForegroundColor Green
Write-Verbose -Message "Creating Azure IPAM Engine Service Principal"
New-AzADServicePrincipal -ApplicationObject $engineObject `
-Role "Reader" `
-Scope $scope `
| Out-Null
# Create IPAM Engine Secret
Write-Host "INFO: Creating Azure IPAM Engine Secret" -ForegroundColor Green
Write-Verbose -Message "Creating Azure IPAM Engine Secret"
$engineSecret = New-AzADAppCredential -ApplicationObject $engineObject -StartDate (Get-Date) -EndDate (Get-Date).AddYears(2)
Write-Host "INFO: Azure IPAM Engine & UI Applications/Service Principals created successfully" -ForegroundColor Green
Write-Verbose -Message "Azure IPAM Engine & UI Applications/Service Principals created successfully"
$appDetails = @{
EngineAppId = $engineApp.AppId
EngineSecret = $engineSecret.SecretText
}
# Add UI AppID to AppDetails (If not deployed as Function App)
if (-not $AsFunction) {
$appDetails.Add("UIAppId", $uiApp.AppId)
}
return $appDetails
}
Function Grant-AdminConsent {
Param(
[Parameter(Mandatory=$false)]
[string]$UIAppId,
[Parameter(Mandatory=$true)]
[string]$EngineAppId,
[Parameter(Mandatory=$false)]
[bool]$AsFunction,
[Parameter(Mandatory=$true)]
[string]$AzureCloud
)
$msGraphMap = @{
AZURE_PUBLIC = "graph.microsoft.com"
AZURE_US_GOV = "graph.microsoft.us"
AZURE_GERMANY = "graph.microsoft.de"
AZURE_CHINA = "microsoftgraph.chinacloudapi.cn"
};
$uiGraphScopes = [System.Collections.ArrayList]@(
@{
scopeId = "00000003-0000-0000-c000-000000000000" # Microsoft Graph
scopes = " openid profile offline_access User.Read Directory.Read.All"
}
)
$engineGraphScopes = [System.Collections.ArrayList]@(
@{
scopeId = "797f4846-ba00-4fd7-ba43-dac1f8f63013" # Azure Service Management
scopes = "user_impersonation"
}
)
# Get Microsoft Graph Access Token
$accesstoken = (Get-AzAccessToken -Resource "https://$($msGraphMap[$AzureCloud])/").Token
# Connect to Microsoft Graph
Write-Host "INFO: Logging in to Microsoft Graph" -ForegroundColor Green
Write-Verbose -Message "Logging in to Microsoft Graph"
Connect-MgGraph -AccessToken $accesstoken | Out-Null
# Fetch Azure IPAM UI Service Principal (If not deployed as Function App)
if (-not $AsFunction) {
$uiSpn = Get-AzADServicePrincipal `
-ApplicationId $UIAppId
}
# Fetch Azure IPAM Engine Service Principal
$engineSpn = Get-AzADServicePrincipal `
-ApplicationId $EngineAppId
# Grant admin consent for Microsoft Graph API permissions assigned to IPAM UI application (If not deployed as Function App)
if (-not $AsFunction) {
Write-Host "INFO: Granting admin consent for Microsoft Graph API permissions assigned to IPAM UI application" -ForegroundColor Green
Write-Verbose -Message "Granting admin consent for Microsoft Graph API permissions assigned to IPAM UI application"
foreach($scope in $uiGraphScopes) {
$msGraphId = Get-AzADServicePrincipal `
-ApplicationId $scope.scopeId
New-MgOauth2PermissionGrant `
-ResourceId $msGraphId.Id `
-Scope $scope.scopes `
-ClientId $uiSpn.Id `
-ConsentType AllPrincipals `
| Out-Null
}
Write-Host "INFO: Admin consent for Microsoft Graph API permissions granted successfully" -ForegroundColor Green
Write-Verbose -Message "Admin consent for Microsoft Graph API permissions granted successfully"
}
# Grant admin consent to the IPAM UI application for exposed API from the IPAM Engine application (If not deployed as a Function App)
if (-not $AsFunction) {
Write-Host "INFO: Granting admin consent to the IPAM UI application for exposed API from the IPAM Engine application" -ForegroundColor Green
Write-Verbose -Message "Granting admin consent to the IPAM UI application for exposed API from the IPAM Engine application"
New-MgOauth2PermissionGrant `
-ResourceId $engineSpn.Id `
-Scope "access_as_user" `
-ClientId $uiSpn.Id `
-ConsentType AllPrincipals `
| Out-Null
Write-Host "INFO: Admin consent for IPAM Engine exposed API granted successfully" -ForegroundColor Green
Write-Verbose -Message "Admin consent for IPAM Engine exposed API granted successfully"
}
# Grant admin consent for Azure Service Management API permissions assigned to IPAM Engine application
Write-Host "INFO: Granting admin consent for Azure Service Management API permissions assigned to IPAM Engine application" -ForegroundColor Green
Write-Verbose -Message "Granting admin consent for Azure Service Management API permissions assigned to IPAM Engine application"
foreach($scope in $engineGraphScopes) {
$msGraphId = Get-AzADServicePrincipal `
-ApplicationId $scope.scopeId
New-MgOauth2PermissionGrant `
-ResourceId $msGraphId.Id `
-Scope $scope.scopes `
-ClientId $engineSpn.Id `
-ConsentType AllPrincipals `
| Out-Null
}
Write-Host "INFO: Admin consent for Azure Service Management API permissions granted successfully" -ForegroundColor Green
Write-Verbose -Message "Admin consent for Azure Service Management API permissions granted successfully"
}
Function Save-Parameters {
Param(
[Parameter(Mandatory=$false)]
[string]$UIAppId = '00000000-0000-0000-000000000000',
[Parameter(Mandatory=$true)]
[string]$EngineAppId,
[Parameter(Mandatory=$true)]
[string]$EngineSecret,
[Parameter(Mandatory=$false)]
[bool]$AsFunction
)
Write-Host "INFO: Populating Bicep parameter file for infrastructure deployment" -ForegroundColor Green
Write-Verbose -Message "Populating Bicep parameter file for infrastructure deployment"
# Retrieve JSON object from sample parameter file
$parametersObject = Get-Content main.parameters.example.json | ConvertFrom-Json
# Update Parameter Values
$parametersObject.parameters.engineAppId.value = $EngineAppId
$parametersObject.parameters.engineAppSecret.value = $EngineSecret
$parametersObject.parameters.deployAsFunc.value = $AsFunction
if (-not $AsFunction) {
$parametersObject.parameters.uiAppId.value = $UIAppId
$parametersObject.parameters = $parametersObject.parameters | Select-Object * -ExcludeProperty namePrefix, tags
} else {
$parametersObject.parameters = $parametersObject.parameters | Select-Object * -ExcludeProperty uiAppId, namePrefix, tags
}
# Output updated parameter file for Bicep deployment
$parametersObject | ConvertTo-Json -Depth 4 | Out-File -FilePath main.parameters.json
Write-Host "INFO: Bicep parameter file populated successfully" -ForegroundColor Green
Write-Verbose -Message "Bicep parameter file populated successfully"
}
Function Import-Parameters {
Param(
[Parameter(Mandatory=$true)]
[System.IO.FileInfo]$ParameterFile
)
Write-Host "INFO: Importing values from Bicep parameters file" -ForegroundColor Green
Write-Verbose -Message "Importing values from Bicep parameters file"
# Retrieve JSON object from sample parameter file
$parametersObject = Get-Content $ParameterFile | ConvertFrom-Json
# Read Values from Parameters
$UIAppId = $parametersObject.parameters.uiAppId.value
$EngineAppId = $parametersObject.parameters.engineAppId.value
$EngineSecret = $parametersObject.parameters.engineAppSecret.value
$script:AsFunction = $parametersObject.parameters.deployAsFunc.value
$deployType = $script:AsFunction ? 'Function' : 'Full'
Write-Host "INFO: Successfully import Bicep parameter values for $deployType deployment" -ForegroundColor Green
Write-Verbose -Message "Successfully import Bicep parameter values for $deployType deployment"
$appDetails = @{
UIAppId = $UIAppId
EngineAppId = $EngineAppId
EngineSecret = $EngineSecret
}
return $appDetails
}
Function Deploy-Bicep {
Param(
[Parameter(Mandatory=$false)]
[string]$UIAppId = '00000000-0000-0000-0000-000000000000',
[Parameter(Mandatory=$true)]
[string]$EngineAppId,
[Parameter(Mandatory=$true)]
[string]$EngineSecret,
[Parameter(Mandatory=$true)]
[string]$UniqueSuffix,
[Parameter(Mandatory=$false)]
[string]$NamePrefix,
[Parameter(Mandatory=$false)]
[string]$AzureCloud,
[Parameter(Mandatory=$false)]
[bool]$AsFunction,
[Parameter(Mandatory=$false)]
[bool]$PrivateAcr,
[Parameter(Mandatory=$false)]
[hashtable]$Tags,
[Parameter(Mandatory=$false)]
[hashtable]$ResourceNames
)
Write-Host "INFO: Deploying IPAM bicep templates" -ForegroundColor Green
Write-Verbose -Message "Deploying bicep templates"
# Instantiate deployment parameter object
$deploymentParameters = @{
engineAppId = $EngineAppId
engineAppSecret = $EngineSecret
uiAppId = $UiAppId
}
if($UniqueSuffix) {
$deploymentParameters.Add('uniqueSuffix', $UniqueSuffix)
}
if($NamePrefix) {
$deploymentParameters.Add('namePrefix', $NamePrefix)
}
if($AzureCloud) {
$deploymentParameters.Add('azureCloud', $AzureCloud)
}
if($AsFunction) {
$deploymentParameters.Add('deployAsFunc', $AsFunction)
}
if($PrivateAcr) {
$deploymentParameters.Add('privateAcr', $PrivateAcr)
}
if($Tags) {
$deploymentParameters.Add('tags', $Tags)
}
if($ResourceNames) {
$deploymentParameters.Add('resourceNames', $ResourceNames)
}
$DebugPreference = $debugSetting
# Deploy IPAM bicep template
$deployment = &{
New-AzSubscriptionDeployment `
-Name "ipamInfraDeploy-$(Get-Date -Format `"yyyyMMddhhmmsstt`")" `
-Location $location `
-TemplateFile main.bicep `
-TemplateParameterObject $deploymentParameters `
5>$($DEBUG_MODE ? $debugLog : $null)
}
$DebugPreference = 'SilentlyContinue'
Write-Host "INFO: IPAM bicep templates deployed successfully" -ForegroundColor Green
Write-Verbose -Message "IPAM bicep template deployed successfully"
return $deployment
}
Function Update-UIApplication {
Param(
[Parameter(Mandatory=$true)]
[string]$UIAppId,
[Parameter(Mandatory=$true)]
[string]$Endpoint
)
Write-Host "INFO: Updating UI Application with SPA configuration" -ForegroundColor Green
Write-Verbose -Message "Updating UI Application with SPA configuration"
$appServiceEndpoint = "https://$Endpoint"
# Update UI Application with single-page application configuration
Update-AzADApplication -ApplicationId $UIAppId -SPARedirectUri $appServiceEndpoint
Write-Host "INFO: UI Application SPA configuration update complete" -ForegroundColor Green
Write-Verbose -Message "UI Application SPA configuration update complete"
}
# Main Deployment Script Section
Write-Host
Write-Host "NOTE: IPAM Deployment Type: $($PSCmdlet.ParameterSetName)" -ForegroundColor Magenta
if($DEBUG_MODE) {
Write-Host "DEBUG: Debug Mode Enabled" -ForegroundColor Gray
}
try {
if($PrivateAcr) {
# Verify Minimum Azure CLI Version
Write-Host "INFO: PrivateACR flag set, verifying minimum Azure CLI version" -ForegroundColor Green
Write-Verbose -Message "PrivateACR flag set, verifying minimum Azure CLI version"
$azureCliVer = [System.Version](az version | ConvertFrom-Json).'azure-cli'
if($azureCliVer -lt $MIN_AZ_CLI_VER) {
Write-Host "ERROR: Azure CLI must be version $MIN_AZ_CLI_VER or greater!" -ForegroundColor red
exit
}
# Verify Azure PowerShell and Azure CLI Contexts Match
Write-Host "INFO: PrivateACR flag set, verifying Azure PowerShell and Azure CLI contexts match" -ForegroundColor Green
Write-Verbose -Message "PrivateACR flag set, verifying Azure PowerShell and Azure CLI contexts match"
$azureCliContext = $(az account show | ConvertFrom-Json) 2>$null
if(-not $azureCliContext) {
Write-Host "ERROR: Azure CLI not logged in or no subscription has been selected!" -ForegroundColor red
exit
}
$azureCliSub = $azureCliContext.id
$azurePowerShellSub = (Get-AzContext).Subscription.Id
if($azurePowerShellSub -ne $azureCliSub) {
Write-Host "ERROR: Azure PowerShell and Azure CLI must be set to the same context!" -ForegroundColor red
exit
}
}
if ($PSCmdlet.ParameterSetName -in ('Full', 'AppsOnly', 'Function', 'FuncAppsOnly')) {
# Fetch Tenant ID
Write-Host "INFO: Fetching Tenant ID from Azure PowerShell SDK" -ForegroundColor Green
Write-Verbose -Message "Fetching Tenant ID from Azure PowerShell SDK"
$tenantId = (Get-AzContext).Tenant.Id
# Fetch Azure Cloud Type
Write-Host "INFO: Fetching Azure Cloud type from Azure PowerShell SDK" -ForegroundColor Green
Write-Verbose -Message "Fetching Azure Cloud type from Azure PowerShell SDK"
$azureCloud = $AZURE_ENV_MAP[(Get-AzContext).Environment.Name]
}
if ($PSCmdlet.ParameterSetName -in ('Full', 'TemplateOnly', 'Function', 'FuncTemplateOnly')) {
# Validate Azure Region
Write-Host "INFO: Validating Azure Region selected for deployment" -ForegroundColor Green
Write-Verbose -Message "Validating Azure Region selected for deployment"
if (Test-Location -Location $Location) {
Write-Host "INFO: Azure Region validated successfully" -ForegroundColor Green
Write-Verbose -Message "Azure Region validated successfully"
} else {
Write-Host "ERROR: Location provided is not a valid Azure Region!" -ForegroundColor red
exit
}
}
if ($PSCmdlet.ParameterSetName -in ('Full', 'AppsOnly', 'Function', 'FuncAppsOnly')) {
$unique = New-UniqueIPAMDeployString
$appDetails = Deploy-IPAMApplications `
-UIAppName $UIAppName `
-EngineAppName $EngineAppName `
-TenantId $tenantId `
-AzureCloud $azureCloud `
-AsFunction $AsFunction
$consentDetails = @{
EngineAppId = $appDetails.EngineAppId
AsFunction = $AsFunction
}
if ($PSCmdlet.ParameterSetName -in ('Full', 'AppsOnly')) {
$consentDetails.Add("UIAppId", $appDetails.UIAppId)
}
Grant-AdminConsent @consentDetails -AzureCloud $azureCloud
}
if ($PSCmdlet.ParameterSetName -in ('AppsOnly', 'FuncAppsOnly')) {
Save-Parameters @appDetails -AsFunction $AsFunction
}
if ($PSCmdlet.ParameterSetName -in ('TemplateOnly', 'FuncTemplateOnly')) {
$appDetails = Import-Parameters `
-ParameterFile $ParameterFile
}
if ($PSCmdlet.ParameterSetName -in ('Full', 'TemplateOnly', 'Function', 'FuncTemplateOnly')) {
$deployment = Deploy-Bicep @appDetails `
-UniqueSuffix $unique `
-NamePrefix $NamePrefix `
-AzureCloud $azureCloud `
-PrivateAcr $PrivateAcr `
-AsFunction $AsFunction `
-Tags $Tags `
-ResourceNames $ResourceNames
}
if ($PSCmdlet.ParameterSetName -eq 'Full') {
Update-UIApplication `
-UIAppId $appDetails.UIAppId `
-Endpoint $deployment.Outputs["appServiceHostName"].Value
}
if ($PSCmdlet.ParameterSetName -in ('Full', 'Function', 'TemplateOnly') -and $PrivateAcr) {
Write-Host "INFO: Building and pushing container images to Azure Container Registry" -ForegroundColor Green
Write-Verbose -Message "Building and pushing container images to Azure Container Registry"
$containerMap = @{
Debian = @{
Extension = "deb"
Port = 80
Images = @{
UI = 'node:16-slim'
Engine = 'python:3.9-slim'
LB = 'nginx:alpine'
}
}
RHEL = @{
Extension = "rhel"
Port = 8080
Images = @{
UI = 'registry.access.redhat.com/ubi8/nodejs-16'
Engine = 'registry.access.redhat.com/ubi8/python-39'
LB = 'registry.access.redhat.com/ubi8/nginx-120'
}
}
}
$enginePath = [Io.Path]::Combine('..', 'engine')
$engineDockerFile = Join-Path -Path $enginePath -ChildPath "Dockerfile.$($containerMap[$ContainerType].Extension)"
$functionDockerFile = Join-Path -Path $enginePath -ChildPath 'Dockerfile.func'
$uiPath = [Io.Path]::Combine('..', 'ui')
$uiDockerFile = Join-Path -Path $uiPath -ChildPath "Dockerfile.$($containerMap[$ContainerType].Extension)"
$lbPath = [Io.Path]::Combine('..', 'lb')
$lbDockerFile = Join-Path -Path $lbPath -ChildPath "Dockerfile"
if($AsFunction) {
# WRITE-HOST "INFO: Building Function container ($ContainerType)..." -ForegroundColor Green
# Write-Verbose -Message "INFO: Building Function container ($ContainerType)..."
# $funcBuildOutput = $(
# az acr build -r $deployment.Outputs["acrName"].Value `
# -t ipam-func:latest `
# -f $functionDockerFile $enginePath `
# --build-arg PORT=$($containerMap[$ContainerType].Port) `
# --build-arg BASE_IMAGE=$($containerMap[$ContainerType].Images.Engine)
# ) *>&1
WRITE-HOST "INFO: Building Function container..." -ForegroundColor Green
Write-Verbose -Message "INFO: Building Function container..."
$funcBuildOutput = $(
az acr build -r $deployment.Outputs["acrName"].Value `
-t ipam-func:latest `
-f $functionDockerFile $enginePath
) *>&1
if ($LASTEXITCODE -ne 0) {
throw $funcBuildOutput
} else {
WRITE-HOST "INFO: Function container image build and push completed successfully" -ForegroundColor Green
Write-Verbose -Message "Function container image build and push completed successfully"
}
Write-Host "INFO: Restarting Function App" -ForegroundColor Green
Write-Verbose -Message "Restarting Function App"
Restart-AzFunctionApp -Name $deployment.Outputs["appServiceName"].Value -ResourceGroupName $deployment.Outputs["resourceGroupName"].Value -Force | Out-Null
} else {
WRITE-HOST "INFO: Building Engine container ($ContainerType)..." -ForegroundColor Green
Write-Verbose -Message "INFO: Building Engine container ($ContainerType)..."
$engineBuildOutput = $(
az acr build -r $deployment.Outputs["acrName"].Value `
-t ipam-engine:latest `
-f $engineDockerFile $enginePath `
--build-arg PORT=$($containerMap[$ContainerType].Port) `
--build-arg BASE_IMAGE=$($containerMap[$ContainerType].Images.Engine)
) *>&1
if ($LASTEXITCODE -ne 0) {
throw $engineBuildOutput
} else {
WRITE-HOST "INFO: Engine container image build and push completed successfully" -ForegroundColor Green
Write-Verbose -Message "Engine container image build and push completed successfully"
}
WRITE-HOST "INFO: Building UI container ($ContainerType)..." -ForegroundColor Green
Write-Verbose -Message "INFO: Building UI container ($ContainerType)..."
$uiBuildOutput = $(
az acr build -r $deployment.Outputs["acrName"].Value `
-t ipam-ui:latest `
-f $uiDockerFile $uiPath `
--build-arg PORT=$($containerMap[$ContainerType].Port) `
--build-arg BASE_IMAGE=$($containerMap[$ContainerType].Images.UI)
) *>&1
if ($LASTEXITCODE -ne 0) {
throw $uiBuildOutput
} else {
WRITE-HOST "INFO: UI container image build and push completed successfully" -ForegroundColor Green
Write-Verbose -Message "UI container image build and push completed successfully"
}
WRITE-HOST "INFO: Building Load Balancer container ($ContainerType)..." -ForegroundColor Green
Write-Verbose -Message "INFO: Building Load Balancer container ($ContainerType)..."
$lbBuildOutput = $(
az acr build -r $deployment.Outputs["acrName"].Value `
-t ipam-lb:latest `
-f $lbDockerFile $lbPath `
--build-arg BASE_IMAGE=$($containerMap[$ContainerType].Images.LB)
) *>&1
if ($LASTEXITCODE -ne 0) {
throw $lbBuildOutput
} else {
WRITE-HOST "INFO: Load Balancer container image build and push completed successfully" -ForegroundColor Green
Write-Verbose -Message "Load Balancer container image build and push completed successfully"
}
Write-Host "INFO: Restarting App Service" -ForegroundColor Green
Write-Verbose -Message "Restarting App Service"
Restart-AzWebApp -Name $deployment.Outputs["appServiceName"].Value -ResourceGroupName $deployment.Outputs["resourceGroupName"].Value | Out-Null
}
}
Write-Host "INFO: Azure IPAM Solution deployed successfully" -ForegroundColor Green
Write-Verbose -Message "Azure IPAM Solution deployed successfully"