当从 Azure Pipeline 重复调用 Import-AzKeyVaultCertificate 时,如何避免状态码冲突?

问题描述 投票:0回答:1

我正试图建立一个管道,这将。

  • 通过使用AzureResourceManagerTemplateDeployment@3任务部署一个KeyVault "my-keyvault"。
  • 运行Powershell脚本(如下所示)在keyvault中创建自签证书
  • 最后使用AzureResourceManagerTemplateDeployment@3任务部署一个SF集群 "my-cluster",并为节点到节点和客户端到节点的通信提供上述证书(以后我计划引入2种不同的证书)。

我的PowerShell脚本用于生成自签名的证书,当第一次调用时,效果良好。

param(
    [string] [Parameter(Mandatory=$true)] $Password,
    [string] [Parameter(Mandatory=$true)] $CertDnsName,
    [string] [Parameter(Mandatory=$true)] $KeyVaultName,
    [string] [Parameter(Mandatory=$true)] $CertName
)

$SecurePassword = ConvertTo-SecureString -String $Password -AsPlainText -Force
$CertFileFullPath = $(Join-Path (Split-Path -Parent $MyInvocation.MyCommand.Definition) "\$CertDnsName.pfx")

$NewCert = New-SelfSignedCertificate -CertStoreLocation Cert:\CurrentUser\My -DnsName $CertDnsName 
Export-PfxCertificate -FilePath $CertFileFullPath -Password $SecurePassword -Cert $NewCert

Import-AzKeyVaultCertificate -VaultName $KeyVaultName -Name $CertName -FilePath $CertFileFullPath -Password $SecurePassword

但是当我用----------------------------------------------------重复调用时

New-ServiceFabricClusterCertificate.ps1 -Password "blah" -CertDnsName "my-hostname" -KeyVaultName "my-keyvault" -CertName "my-cluster-cert"

然后我得到了错误。

Import-AzKeyVaultCertificate : Operation returned an invalid status code 'Conflict'

+ ... NewSecret = Import-AzKeyVaultCertificate -VaultName $KeyVaultName -Na ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [Import-AzKeyVaultCertificate], KeyVaultErrorException
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.KeyVault.ImportAzureKeyVaultCertificate

冲突错误代码描述在 证书创建方法 作为。

当首次创建KV证书时,还将创建一个与证书名称相同的可寻址密钥和秘密。如果该名称已经被使用,那么操作将失败,http状态码为409(冲突)。可寻址密钥和秘密的属性来自于KV证书的属性。以这种方式创建的可寻址密钥和秘密被标记为托管密钥和秘密,其寿命由Key Vault管理。管理密钥和秘密是只读的。注意:如果KV证书过期或被禁用,相应的密钥和秘密将无法使用。

如果这是第一次创建KV证书的操作,那么需要一个策略。也可以在连续的创建操作中提供一个策略来替换策略资源。如果没有提供策略,那么服务上的策略资源将被用来创建下一个版本的KV证书。需要注意的是,当创建下一个版本的请求正在进行时,当前的KV证书以及相应的可寻址密钥和秘密保持不变。

我的问题是,我不明白它的意思,也不知道该怎么做才能使我的PowerShell脚本重复调用。

我试着从KV中手动删除证书,并在同一个KV中搜索同名的密钥和秘密(因为文档中说 "一个可寻址的密钥和秘密也被创建了")--但这没有帮助。

现在,每当我运行这个脚本时,我都会得到冲突的错误。

azure azure-service-fabric azure-resource-manager azure-keyvault arm-template
1个回答
0
投票

以下管道任务已经为我解决了这个问题--。

  • 首先,我删除了KeyVault中的自签证书(失败也没关系)
  • 然后,我清除KeyVault中的自签证书(失败了也没关系)。
  • 最后我再次导入自签证书

并注意到清除命令使用的是 deletedcertificates 同上

# Note: to generate a self-signed cert, follow the following steps:
# $SecurePassword = ConvertTo-SecureString -String 'password123' -AsPlainText -Force
# $NewCert = New-SelfSignedCertificate -CertStoreLocation Cert:\LocalMachine\My -DnsName 'myhost.westeurope.cloudapp.azure.com'
# Export-PfxCertificate -FilePath C:\Temp\my-self-signed-cert.pfx -Password $SecurePassword -Cert $NewCert
# $Bytes = [System.IO.File]::ReadAllBytes('C:\temp\my-self-signed-cert.pfx')
# $Base64 = [System.Convert]::ToBase64String($Bytes)
# After that please copy-paste the Base64 content into the task below

# purge the self-signed cert from the Keyvault to avoid conflict; ignore failures
- task: AzureCLI@2
  inputs:
    azureSubscription: '${{ parameters.mysub }}'
    scriptType: 'pscore'
    scriptLocation: 'inlineScript'
    powerShellErrorActionPreference: 'silentlyContinue'
    inlineScript: |
      az keyvault certificate delete --vault-name mykeyvault --id 'https://mykeyvault.vault.azure.net/certificates/my-self-signed-cert'
      az keyvault certificate purge --vault-name mykeyvault --id 'https://mykeyvault.vault.azure.net/deletedcertificates/my-self-signed-cert'

# import the self-signed certificate into the Keyvault
- task: AzurePowerShell@5
  inputs:
    azureSubscription: '${{ parameters.mysub }}'
    ScriptType: 'InlineScript'
    azurePowerShellVersion: '3.1.0'
    Inline: |
      $Pwd = ConvertTo-SecureString -String 'password123' -Force -AsPlainText
      $Base64 = 'MIIK....3000_chars_here_base64_encoded_...U1ICAgfQ=='
      $Cert = Import-AzKeyVaultCertificate -VaultName mykeyvault -Name my-self-signed-cert -CertificateString $Base64 -Password $Pwd
      echo "##vso[task.setvariable variable=Thumbprint;isOutput=true]$Cert.Thumbprint"
© www.soinside.com 2019 - 2024. All rights reserved.