gpt4 book ai didi

azure - 使用 Azure devops 中的 Azure powershell 任务向 Azure Keyvault 添加/获取 key

转载 作者:行者123 更新时间:2023-12-03 00:59:41 25 4
gpt4 key购买 nike

我正在尝试使用 Azure DevOps 中的 Azure Powershell 任务在我的 Azure Keyvault 内设置 secret 。我使用以下代码:

Set-AzureKeyVaultSecret -VaultName $KeyvaultName -Name $SecretName -SecretValue $secretvalue

将名称和值全部设置在变量内,并尝试在没有变量的情况下使用它。使用以下代码将该值保存为安全字符串。ConvertTo-SecureString

但是当我在 Azure DevOps Release 管道中运行此 powershell 代码时,我不断收到以下错误消息:

Cannot retrieve access token for resource 'AzureKeyVaultServiceEndpointResourceId'.  Please ensure that you have provided the appropriate access tokens when using access token login.

因此,我通过将服务主体和构建服务器都添加到具有获取、列表、设置 secret 权限的访问策略中,确保服务主体和构建服务器对 keyvault 具有正确的访问权限。

我还添加了以下代码行以确保配置文件正确加载

########################################################################################
$azureRmProfile = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile
$profileClient = New-Object -TypeName Microsoft.Azure.Commands.ResourceManager.Common.RMProfileClient -ArgumentList ($azureRmProfile)
$context = Get-AzureRmContext
$AzureToken = $profileClient.AcquireAccessToken($context.Tenant.Id)
Add-AzureRmAccount -AccessToken $AzureToken.AccessToken -AccountId $AzureToken.UserId
########################################################################################

通过在内联脚本的开头添加此代码,并使用带有 commando 的配置文件作为 -DefaultProfile 的变量。

我还启用了允许脚本访问 Oauth token 的选项。 enter image description here

是否有人也尝试从 Azure DevOps 中的 powershell 任务设置 secret 。或者知道为什么 powershell 脚本无法访问 keyvault。azurermContext 突击队为我提供了正确的输出,甚至尝试了 Get-AzureRmKeyvault 命令来确定与环境的连接是否已正确设置。这也没有带来任何问题。

最佳答案

下面肯定可以工作(经常使用)

Set-AzContext -SubscriptionId $SubscriptionId
## $SubscriptionId is a subscription ID where is the target KV

$Secretvalue = ConvertTo-SecureString $SecretValuePlainText -AsPlainText -Force
## $SecretValuePlainText is the secret to store

Set-AzKeyVaultSecret -VaultName $KeyVaultName -Name $SecretName -SecretValue $Secretvalue -ErrorVariable setSecretError -Expires $ExpirationDate -NotBefore $ActivationDate
## $SecretName, $ExpirationDate, $ActivationDate - obvious :)

当然,如果您引用的变量不是来自脚本或内联,而是来自发布,则使用 $(variable_name)

我们为此使用的服务主体/服务连接是目标订阅(或 key 保管库,由您决定)的临时所有者。

关于azure - 使用 Azure devops 中的 Azure powershell 任务向 Azure Keyvault 添加/获取 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58235066/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com