azure - VSTS - 如何将 Azure Key Vault 检索到的客户端证书转换为 Powershell X509Certificate 实例

标签 azure azure-devops devops azure-powershell azure-keyvault

为了提供一些背景知识,我们有一个通过客户端证书身份验证保护安全的 WebAPI 项目。

我们的构建发布步骤之一是使用 Powershell 调用我们自己的 API 之一,但我们需要添加证书才能使该调用正常工作。

我们使用下面的任务来检索 Azure 托管 key (直接在 Key Vault 上生成的实际自签名证书)。

https://learn.microsoft.com/en-us/vsts/build-release/tasks/deploy/azure-key-vault?view=vsts

该任务成功返回证书并将其放入变量中,但我们无法将该变量(上面的页面显示它是一个字符串表示形式)转换为 Invoke-WebRequest 的 X509Certificate 的实际实例预计。

我们从 VSTS 发布过程中得到的错误如下:

2018-05-16T19:33:12.2384270Z ##[error]Cannot bind parameter 'Certificate'. Cannot convert value "***" to type "System.Security.Cryptography.X509Certificates.X509Certificate". Error: "The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters."

2018-05-16T19:33:12.2398107Z ##[debug]Processed: ##vso[task.logissue type=error]Cannot bind parameter 'Certificate'. Cannot convert value "***" to type "System.Security.Cryptography.X509Certificates.X509Certificate". Error: "The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters."

2018-05-16T19:33:12.2399696Z ##[debug]Processed: ##vso[task.complete result=Failed]

2018-05-16T19:33:12.2611215Z ##[section]Finishing: Azure PowerShell script: API Validation

此时,我什至不确定构建从任务中返回什么类型的值。它是一个字符串、一个 protected 字符串还是什么?如何将其转换为 X509Certificate?

Invoke-WebRequest documentation显示 -Certificate 开关需要 X509 证书类型。

[-Certificate <X509Certificate>]

我已经在网上看到一些代码,显示如何转换从 Key Vault 检索证书,但这是直接使用 Azure CmdLets 检索证书并获取返回的字节值,而不是像 VSTS 任务那样的字符串表示形式。

$PFXPath = 'mycert.pfx'
$PFXPassword = ''
$PFX = New-Object -TypeName 'System.Security.Cryptography.X509Certificates.X509Certificate2Collection'
$PFX.Import($PFXPath,$PFXPassword,[System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::PersistKeySet)
$PFX

话虽如此,有谁知道如何将使用 VSTS 任务(返回字符串表示形式)检索到的证书转换为我可以传递给 Powershell Invoke-WebRequest 调用的 X509 证书的单个实例?

非常感谢任何帮助,谢谢。

最佳答案

您提供的文档链接( https://learn.microsoft.com/en-us/vsts/build-release/tasks/deploy/azure-key-vault?view=vsts#arguments )似乎回答了您的问题(证书被编码为 base64 字符串):

If the value fetched from the vault is a certificate (for example, a PFX file), the task variable will contain the contents of the PFX in string format. You can use the following PowerShell code to retrieve the PFX file from the task variable:

$kvSecretBytes = [System.Convert]::FromBase64String($(PfxSecret))
$certCollection = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2Collection
$certCollection.Import($kvSecretBytes,$null,[System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::Exportable)

关于azure - VSTS - 如何将 Azure Key Vault 检索到的客户端证书转换为 Powershell X509Certificate 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50493896/

相关文章:

Sql Server导入bacpac文件时收到 "Archive file cannot be size 0."

c# - VS中Azure函数本地开发找不到函数

azure - 配置 Postman 以测试从 Azure AD B2C 获取新的访问 token

powershell - 获取代理池的内部版本/版本列表?

node.js - 使用 NodeJs 和 Express 部署 Web 应用程序

go - 未检测到 Vault Token Helper?

azure - 如何在 IIS 而不是 IIS Express 上部署 Web 角色?

Azure 应用程序注册通过 powershell Azure CLI 添加授权的客户端应用程序

node.js - Azure DevOps 管道打开的文件太多

jenkins - groovy.lang.MissingPropertyException : No such property: any for class: WorkflowScript