Skip to content

Commit 108cb30

Browse files
authored
Update MSI logic during migration (#25712)
1 parent 6c5282e commit 108cb30

File tree

1 file changed

+34
-24
lines changed

1 file changed

+34
-24
lines changed

src/Cdn/Cdn.Autorest/custom/Start-AzFrontDoorCdnProfilePrepareMigration.ps1

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -238,12 +238,16 @@ function Start-AzFrontDoorCdnProfilePrepareMigration {
238238
throw "MigrationWebApplicationFirewallMapping parameter instance should be equal to the number of WAF policy instance in the profile."
239239
}
240240

241-
if (($PSBoundParameters.ContainsKey('IdentityType')) -ne ($allPoliciesWithVault.count -gt 0)) {
242-
throw "MSIIdentity should be associated if the front door has Customer Certificates. If not, remove MSIIdentity parameter."
241+
# We should raise a complaint if the customer did not enable managed identity when they have BYOC enabled.
242+
# However, if the customer does not have BYOC but has specified a managed identity, we could ignore the validation for BYOC, no need to keep consisence with Portal behavior.
243+
if (($allPoliciesWithVault.count -gt 0) -and !($PSBoundParameters.ContainsKey('IdentityType')))
244+
{
245+
throw "IdentityType parameter should be provided when the front door has Customer Certificates."
243246
}
244-
Write-Host("The parameters have been successfully validated.")
245247

246-
# Deal with Waf policy
248+
Write-Host("The parameters have been validated successfully.")
249+
250+
# Step1: Deal with Waf policy
247251
if ($PSBoundParameters.ContainsKey('MigrationWebApplicationFirewallMapping')) {
248252
Write-Host("Starting to configure WAF policy upgrades.")
249253

@@ -287,7 +291,7 @@ function Start-AzFrontDoorCdnProfilePrepareMigration {
287291
Write-Host("WAF policy upgrades have been configured successfully.")
288292
}
289293

290-
# Create AFDx Profile
294+
# Step2: Create AFDx Profile
291295
# If create AfdX profile firstly, then an error ("Invalid migrated to waf reference.") will be thrown if the migrated-To-WAF is supposed to created. (not exists in current subscription)
292296
Write-Host("Your new Front Door profile is being created. Please wait until the process has finished completely. This may take several minutes.")
293297
$null = $PSBoundParameters.Remove('IdentityType')
@@ -302,9 +306,8 @@ function Start-AzFrontDoorCdnProfilePrepareMigration {
302306

303307
Write-Host("Your new Front Door profile with the configuration has been successfully created.")
304308

305-
# Deal with MSI parameter
306-
# if ($PSBoundParameters.ContainsKey('IdentityType')) {
307-
if ($allPoliciesWithVault.count -gt 0) {
309+
# Step 3: Deal with MSI parameter
310+
if (${IdentityType}) {
308311
Write-Host("Starting to enable managed identity.")
309312

310313
# Waiting for results of profile created return
@@ -318,7 +321,7 @@ function Start-AzFrontDoorCdnProfilePrepareMigration {
318321

319322
$enableMSISuccessMessage = 'Enabling managed identity succeeded.'
320323
$enableMSIRetryMessage = 'Retrying to enable managed identity...'
321-
$enableMSIErrorMessage = "Enableing managed identity failed."
324+
$enableMSIErrorMessage = "Enabling managed identity failed."
322325
$profileIdentity = RetryCommand -Command 'Update-AzFrontDoorCdnProfile' -CommandArgs $commandArgs -RetryTimes 6 -SecondsDelay 20 -SuccessMessage $enableMSISuccessMessage -RetryMessage $enableMSIRetryMessage -ErrorMessage $enableMSIErrorMessage
323326
$identity = [System.Collections.ArrayList]@()
324327
foreach ($id in $profileIdentity.IdentityUserAssignedIdentity.Values.PrincipalId) {
@@ -331,24 +334,31 @@ function Start-AzFrontDoorCdnProfilePrepareMigration {
331334
$identity.Add($profileIdentity.IdentityPrincipalId) | Out-Null
332335
}
333336

334-
# Waiting for MSI granted access...
337+
# Waiting for Enabling managed identity...
335338
Start-Sleep(20)
336-
Write-Host("Starting to grant managed identity to key vault.")
337-
foreach ($vault in $allPoliciesWithVault) {
338-
foreach ($principal in $identity) {
339-
$grantAccessSuccessMessage = 'Granting managed identity to key vault succeeded.'
340-
$grantAccessRetryMessage = 'Retrying to grant managed identity to key vault...'
341-
$grantAccessErrorMessage = 'Granting managed identity to key vault failed.'
342-
343-
$commandInfo = @{ VaultName = $vault; ObjectId = $principal; PermissionsToSecrets = 'Get'; PermissionsToCertificates = 'Get'; ErrorAction = 'Stop'; BypassObjectIdValidation = $true}
344-
345-
# Set-AzKeyVaultAccessPolicy -VaultName $vault -ObjectId $principal -PermissionsToSecrets Get -PermissionsToCertificates Get
346-
# Adding the parameter `-BypassObjectIdValidation` to bypass the validation when using pipeline to do migration, the type of `-BypassObjectIdValidation` is 'SwitchParameter'.
347-
RetryCommand -Command 'Set-AzKeyVaultAccessPolicy' -CommandArgs $commandInfo -RetryTimes 6 -SecondsDelay 20 -SuccessMessage $grantAccessSuccessMessage -RetryMessage $grantAccessRetryMessage -ErrorMessage $grantAccessErrorMessage
339+
340+
# When the classic front door has BYOC, need to grant managed identity to the key vault.
341+
if ($allPoliciesWithVault.count -gt 0)
342+
{
343+
Write-Host("Starting to grant managed identity to key vault.")
344+
foreach ($vault in $allPoliciesWithVault) {
345+
foreach ($principal in $identity) {
346+
$grantAccessSuccessMessage = 'Granting managed identity to key vault succeeded.'
347+
$grantAccessRetryMessage = 'Retrying to grant managed identity to key vault...'
348+
$grantAccessErrorMessage = 'Granting managed identity to key vault failed.'
349+
350+
$commandInfo = @{ VaultName = $vault; ObjectId = $principal; PermissionsToSecrets = 'Get'; PermissionsToCertificates = 'Get'; ErrorAction = 'Stop'; BypassObjectIdValidation = $true}
351+
352+
# Set-AzKeyVaultAccessPolicy -VaultName $vault -ObjectId $principal -PermissionsToSecrets Get -PermissionsToCertificates Get
353+
# Adding the parameter `-BypassObjectIdValidation` to bypass the validation when using pipeline to do migration, the type of `-BypassObjectIdValidation` is 'SwitchParameter'.
354+
RetryCommand -Command 'Set-AzKeyVaultAccessPolicy' -CommandArgs $commandInfo -RetryTimes 6 -SecondsDelay 20 -SuccessMessage $grantAccessSuccessMessage -RetryMessage $grantAccessRetryMessage -ErrorMessage $grantAccessErrorMessage
355+
}
348356
}
349-
}
350357

351-
Write-Host("Your have successfully granted managed identity to key vault.")
358+
Write-Host("Your have successfully granted managed identity to key vault.")
359+
}
360+
} else {
361+
Write-Debug("IdentityType paramter not provided and no BYOC for the current front door, skip Managed Identity step.")
352362
}
353363

354364
Write-Host("The change need to be committed after this.")

0 commit comments

Comments
 (0)