【问题标题】:Is it possible in a .NET Core application to retrieve a certificate from AWS Certificate Manager and use it in a HttpClient post?.NET Core 应用程序是否可以从 AWS Certificate Manager 检索证书并在 HttpClient 帖子中使用它?
【发布时间】:2019-02-14 16:11:20
【问题描述】:

我的 .Net 核心应用程序使用 HttpClient 向外部 Web 服务发出 post 请求。外部 Web 服务需要证书来进行验证。

证书安装在 AWS 中,我有一个指向证书的 ARN。

是否可以从 AWS Certificate Manager 以编程方式获取证书并在我的 HtppClient 中使用它,例如这是我通常用来添加证书但我需要从 AWS 获取它的代码。

   private HttpClientHandler HttpClientHandler()
   {
        var handler = new HttpClientHandler
        {
            ClientCertificateOptions = ClientCertificateOption.Manual,
            SslProtocols = SslProtocols.Tls12
        };
        handler.ClientCertificates.Add(new X509Certificate2("cert.crt")); //TODO: fetch from AWS.
        return handler;
    }

【问题讨论】:

    标签: .net-core aws-sdk x509certificate2 aws-certificate-manager


    【解决方案1】:

    正如 Zack 所说,接受的答案不起作用。它确实从 ACM 中检索证书,但它不能用作 HttpClient 的客户端证书,因为它没有私钥。

    据我所知,没有办法从 ACM 中获取私钥,所以我最终将它放在 SecretsManager 中并执行以下操作:

    var certManagerClient = new AmazonCertificateManagerClient();
    var awsCert = certManagerClient.GetCertificateAsync(arn).Result;
    byte[] awsBytes = Encoding.ASCII.GetBytes(awsCert.Certificate);
    var cert = new X509Certificate2(awsBytes);
    
    var secretsManagerClient = new AmazonSecretsManagerClient();
    var privateKey = secretsManagerClient.GetSecretValueAsync(new GetSecretValueRequest { SecretId = secretId }).Result.SecretString;
    byte[] privateKeyBytes = Convert.FromBase64String(privateKey);
    var privateKey = RSA.Create();
    privateKey.ImportRSAPrivateKey(new ReadOnlySpan<byte>(privateKeyBytes), out _);
    var certWithPrivateKey = cert.CopyWithPrivateKey(privateKey);
    

    然后在我的 HttpClientHandler 中使用 certWithPrivateKey:

    var handler = new HttpClientHandler { ClientCertificateOptions = ClientCertificateOption.Manual };
    handler.ClientCertificates.Add(certWithPrivateKey);
    

    【讨论】:

    • 我正在尝试复制您在这里所做的事情。尝试读取密钥时,我不断收到 ASN1 损坏的数据。您是如何将密钥设置/导入机密管理器的?
    • 我刚刚将 base-64 字符串从我的 pem 文件中复制到 Secrets Manager 中,格式与 AWS ACM 要求的格式相同。
    • 有趣。这对我不起作用。 ACM 控制台让您包含 ——BEGIN RSA — — … 我怀疑 SDK 客户端会在您下载证书时处理该问题。将 PK 加载到机密管理器中,我必须去掉页眉和页脚,以便将其转换为字节数组。如果包含页眉和页脚,Convert.FromBase64String() 会抛出异常。
    • @JDBennett 你只需要对秘密管理器部分执行此操作,我在证书管理器中添加了带有 ——BEGIN RSA ——的密钥,但在将其放入秘密管理器时省略了它,这有效,没有任何问题。您在执行此操作时遇到了什么问题?
    【解决方案2】:

    所以,这是可能的。

    我从 NuGet 安装了 AWSSDK.Core 和 AWSSDK.CertificateManager。

    然后,我为 AWS 创建了一个凭证文件,请参阅 Amazon 的说明 https://docs.aws.amazon.com/cli/latest/userguide/cli-config-files.html

    接下来,我使用了 AmazonCertificateManagerClient 来获取证书。

    AmazonCertificateManagerClient client = new AmazonCertificateManagerClient();
    var certificates = client.GetCertificateAsync(arn).Result;
    

    然后我将证书从字符串转换为字节,然后添加到处理程序中。

    var handler = new HttpClientHandler{
      ClientCertificateOptions = ClientCertificateOption.Manual,
      SslProtocols = SslProtocols.Tls12
    };
    
    byte[] toBytes = Encoding.ASCII.GetBytes(certificates.Certificate);
    var cert = new X509Certificate2(toBytes);
    
    handler.ClientCertificates.Add(cert); 
    var httpClient = new HttpClient(handler);
    

    显然,不适合生产的代码,希望对您有所帮助。

    【讨论】:

    • 这对我不起作用。我注意到来自 ACM 的证书不包含私钥。
    【解决方案3】:

    如果您使用 AWS 开发工具包,您可以使用 AmazonCertificateManagerClient 获取证书。有关详细信息,请参阅the AWS SDK documentation。 (选择Amazon.CertificateManager > AmazonCertificateManagerClient

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-14
      • 1970-01-01
      • 2017-07-15
      • 1970-01-01
      • 2016-05-12
      • 2016-12-14
      • 2017-08-24
      • 2017-10-01
      相关资源
      最近更新 更多