【问题标题】:upload .pfx certificate through azure devops pipeline通过 azure devops 管道上传 .pfx 证书
【发布时间】:2023-03-17 10:30:02
【问题描述】:

我想通过 azure devops 任务为我的应用服务上传 .pfx 证书。有人可以帮助我如何通过 ARM 模板上传证书

【问题讨论】:

    标签: azure-devops azure-pipelines devops


    【解决方案1】:

    您可以按照以下步骤使用 ARM 上传证书。

    1,转到管道、库下的安全文件并上传您的证书。

    2,添加download secure file task 以将您的证书下载到您的管道。您可以通过路径$(<mySecureFile>.secureFilePath) or $(Agent.TempDirectory) 引用它。有关预定义变量的更多信息,请查看here

    3,添加一个 powershell 任务以在脚本下方运行,以将您的证书转换为 base64 字符串。并将其存储到自定义环境变量certificateBase64Content。查看here 了解有关变量的更多信息

    $secName = “<certificateName>.pfx
    $tempDirectory = $env:AGENT_TEMPDIRECTORY
    
    $pfxFilePath = Join-Path $tempDirectory $secName
    
    $cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2
    $flag = [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::Exportable
    
    $cert.Import($pfxFilePath, "$(certificatePassword)", $flag)
    
    $bin = $cert.RawData
    $base64Value = [System.Convert]::ToBase64String($bin)
    
    Write-Host "##vso[task.setvariable variable=certificateBase64Content;]$base64Value"
    

    4、创建一个keyvault并授予Microsoft.Web资源提供者访问KeyVault的权限以获取证书,该证书将存储在keyvault中。

    请查看博客 "Create the KeyVault with the required settings" 部分以获取 ARM 模板示例。

    5,将证书存储在上一步创建的密钥库中。

    请查看博客 Store the certificate in KeyVault 部分以获取 ARM 模板示例。

    6、参考博客Deploy the certificate to your Web App的最后一步部署你的证书。

    提醒:

    在上述博客中,ARM 模板中定义的参数在 Azure 资源组部署任务中被覆盖。您可以在 azure 资源组部署任务 中的 Template 设置下进行配置

    加法:

    如果您不想使用 keyvault。上面的第4、5步可以省略,直接上传证书,在上面第3步中把你的证书转换并存入自定义变量后,把parameters('certificatePfxBase64')替换成你自己定义的变量certificateBase64Content

    "variables": {
        "certificateName": "[concat(parameters('certificatePrefixName'), uniqueString(resourceGroup().id))]"
      },
    "resources": [
        {
          "apiVersion": "2015-08-01",
          "name": "[variables('certificateName')]",
          "type": "Microsoft.Web/certificates",
          "location": "[resourceGroup().location]",
          "properties": {
            "pfxBlob": "[parameters('certificatePfxBase64')]",
            "password": "[parameters('certificatePfxPassword')]"
          },
          "tags": {
            "displayName": "Certificate"
          }
        }
      ]
    

    【讨论】:

    • 我们可以在不使用任何密钥库的情况下上传证书
    • 是的,您可以上传没有密钥保管库的证书。请参考此blog中的步骤
    • 无法打开链接。我需要在应用服务环境中的应用服务上安装ssl证书...
    • 您可以忽略第 4 步和第 5 步,上传 base 64 字符串格式的证书。我更新了上面的答案。
    • @GiedoDonkers 您可以定义一个管道secret variable 来存储密码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-13
    • 1970-01-01
    • 2021-10-09
    • 2022-06-22
    • 2020-01-18
    • 1970-01-01
    • 2021-07-23
    相关资源
    最近更新 更多