【问题标题】:Unable to access ssl certificate from octopus library as X509Certificate2 object using powershell无法使用 powershell 从章鱼库作为 X509Certificate2 对象访问 ssl 证书
【发布时间】:2019-03-29 17:47:55
【问题描述】:

我正在尝试使用存储在 Octopus 库中的 pfx 创建 jwt 令牌。为此,我必须创建一个 X509Certificate2 对象,它将证书路径和密码作为输入。有人可以建议一种使用 powershell 的方法吗?

$cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($certpath,'password')

我已经阅读了一些关于如何在章鱼中访问证书变量的文档,但是我如何使用它们来创建 X509Certificate2 的对象。 https://octopus.com/docs/deployment-process/variables/certificate-variables

【问题讨论】:

    标签: powershell ssl ssl-certificate octopus-deploy octopus


    【解决方案1】:

    在浏览了 Microsoft 和 Octopus 文档后,我设法让它工作了。 Octopus 将证书作为 base64 编码字符串存储在名为 Cert.Pfx 的变量中,X509Certificate2 的构造函数将字节数组作为第一个参数。所以第一步我只需要将base64编码的字符串转换为字节数组。

    $certbytearray=[System.Convert]::FromBase64String($OctopusParameters["Cert.Pfx"])
    $CertPassKey="password"
    $cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($certbytearray,$CertPassKey)
    

    【讨论】:

      【解决方案2】:

      @Biplab 感谢您发布您的解决方案;它为我节省了很多头痛!我在没有密码的情况下遇到了稍微不同的情况,我发现当我尝试在没有密码的情况下调用时,X509Certificate2 构造函数将字节数组解释为文件名,即使documentation 表明它应该只接受一个字节数组。

      我通过导入来让它在没有密码的情况下工作。

      $certbytearray=[System.Convert]::FromBase64String($OctopusParameters["mycert.Pfx"])
      $mycert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2
      $mycert.import($certbytearray)
      write-host $mycert.ThumbPrint
      if ($mycert.HasPrivateKey)
      { write-host "has private key"}
      

      【讨论】:

        猜你喜欢
        • 2019-10-31
        • 2022-07-20
        • 1970-01-01
        • 2014-06-13
        • 2020-05-18
        • 2020-12-02
        • 2010-11-20
        • 2018-09-13
        • 1970-01-01
        相关资源
        最近更新 更多