为了给一些背景知识,我们有一个使用客户端证书身份验证保护的WebAPI项目。
我们的构建发布步骤之一是使用Powershell调用我们自己的API之一,但我们需要为该调用添加证书才能工作。
我们正在使用以下任务来检索Azure托管密码(直接在密钥保管库上生成的实际自签名证书)。
https://docs.microsoft.com/en-us/vsts/build-release/tasks/deploy/azure-key-vault?view=vsts
任务成功返回证书并将其放在变量上,但是我们无法将该变量转换为实际的X509Certificate实例(Invoke-WebRequest),该变量在上面的页面中表示它是字符串表示形式。预计。
我们从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
此时,我甚至不确定构建从任务中获得什么类型的值。它是一个字符串,一个受保护的字符串,还是什么?以及如何将其转换为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证书的单个实例?
非常感谢任何帮助,谢谢。
答案 0 :(得分:1)
您提供的文档链接(https://docs.microsoft.com/en-us/vsts/build-release/tasks/deploy/azure-key-vault?view=vsts#arguments)似乎回答了您的问题(证书编码为base64字符串):
如果从Vault中获取的值是证书(例如,PFX文件),则任务变量将以字符串格式包含PFX的内容。您可以使用以下PowerShell代码从任务变量中检索PFX文件:
$kvSecretBytes = [System.Convert]::FromBase64String($(PfxSecret)) $certCollection = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2Collection $certCollection.Import($kvSecretBytes,$null,[System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::Exportable)