如何在Azure DevOps经典发布管道中自动化Azure应用程序配置角色分配?

z5btuh9x  于 2023-10-22  发布在  其他
关注(0)|答案(1)|浏览(127)

我们为我们的Web服务提供了一个经典的Azure Release管道。最近,我们将Azure应用程序配置添加到服务中。作为管道的一部分,我们运行PowerShell脚本,自动将Azure KeyVault角色分配给应用的托管身份:

param (
   [string][Parameter(Mandatory=$true)]$resourceGroupName,
   [string]$keyVaultName
)

$lastDeployment = Get-AzureRmResourceGroupDeployment -ResourceGroupName $resourceGroupName | Sort Timestamp -Descending | Select -First 1 

if(!$lastDeployment) {
    throw "Deployment could not be found for Resource Group '$resourceGroupName'."
}

if(!$lastDeployment.Outputs) {
    throw "No output parameters could be found for the last deployment of Resource Group '$resourceGroupName'."
}

$servicePrincipalName = $lastDeployment.Outputs.Item("appname").Value
Write-host $servicePrincipalName
$servicePrincipalId = $(Get-AzureRmADServicePrincipal -DisplayName $servicePrincipalName).Id
Write-host $servicePrincipalId
Set-AzureRmKeyVaultAccessPolicy -VaultName $keyVaultName -ObjectId $servicePrincipalId -PermissionsToSecrets List,Get -BypassObjectIdValidation

我们如何对应用程序配置执行相同的操作,以将数据读取器角色给予给管道中最后部署的应用程序?

q1qsirdb

q1qsirdb1#

根据您当前的PowerShell脚本示例,您正在设置服务主体的权限。
我们如何对应用程序配置执行相同的操作,以将数据读取器角色给予给管道中最后部署的应用程序?
为了满足您的需求,您可以使用以下PowerShell脚本将应用配置数据读取器角色授予服务主体。

New-AzRoleAssignment  -ApplicationId appid -RoleDefinitionName "App Configuration Data Reader" -ResourceName  /subscriptions/{SubscriptionID}/resourcegroups/{ResourceGroupName}/providers/Microsoft.AppConfiguration/configurationStores/{yourappconfigurationname}

PowerShell脚本示例:

param (
   [string][Parameter(Mandatory=$true)]$resourceGroupName,
   [string]$appconfigname
)

$lastDeployment = Get-AzureRmResourceGroupDeployment -ResourceGroupName $resourceGroupName | Sort Timestamp -Descending | Select -First 1 

if(!$lastDeployment) {
    throw "Deployment could not be found for Resource Group '$resourceGroupName'."
}

if(!$lastDeployment.Outputs) {
    throw "No output parameters could be found for the last deployment of Resource Group '$resourceGroupName'."
}

$servicePrincipalName = $lastDeployment.Outputs.Item("appname").Value
Write-host $servicePrincipalName
$servicePrincipalAppId = $(Get-AzureRmADServicePrincipal -DisplayName $servicePrincipalName).AppId
Write-host $servicePrincipalAppId
New-AzRoleAssignment  -ApplicationId $servicePrincipalAppId -RoleDefinitionName "App Configuration Data Reader" -ResourceName  /subscriptions/{SubscriptionID}/resourcegroups/{ResourceGroupName}/providers/Microsoft.AppConfiguration/configurationStores/{yourappconfigurationname}

有关更多详细信息,您可以参考this ticket和文档:New-AzRoleAssignment

相关问题