首页 > 解决方案 > 如何从 Java 中的 AWS 证书管理器访问证书详细信息和私钥以构建 SSLContext?

问题描述

我正在从此代码中获取证书,但没有获取私钥

  AWSCertificateManager client = AWSCertificateManagerClientBuilder.standard()
              .withRegion(Regions.#####)
              .withCredentials(DefaultAWSCredentialsProviderChain.getInstance())
              .build();
  GetCertificateRequest req = new GetCertificateRequest();
  req.setCertificateArn("################################");

  // Retrieve the certificate and certificate chain. 
  // If you recently requested the certificate, loop until it has been created.
  GetCertificateResult result = null;
  long totalTimeout = 120000l;
  long timeSlept = 0l;
  long sleepInterval = 10000l;
  while (result == null && timeSlept < totalTimeout) {
     try {
        result = client.getCertificate(req);
        String certificate = result.getCertificate();
        String certificate_chain = result.getCertificateChain();
     }
     catch (RequestInProgressException ex) {
        Thread.sleep(sleepInterval);
     }
     catch (ResourceNotFoundException ex)
     {
        throw ex;
     }
     catch (InvalidArnException ex)
     {
        throw ex;
     }

     timeSlept += sleepInterval;
  }

标签: amazon-web-servicesssl-certificate

解决方案


AWS Certificate Manager 有两个主要选项,CertificatesPrivate CA.

用于 ACM 集成服务的免费公有证书 使用 AWS
Certificate Manager,预置用于 ACM 集成服务(例如 Elastic Load Balancing 和 API Gateway)的公有或私有 SSL/TLS 证书无需额外费用。您为运行应用程序而创建的 AWS 资源付费。对于私有证书,ACM Private CA 使您能够按月为您创建的服务和证书付费。当您创建更多私有证书时,您为每个证书支付更少的费用。

所以基本上Certificates由 AWS 管理,并与其他服务集成。在此选项中,您不能将此证书与其他非 AWS 资源一起使用,因此您无法访问证书私钥。

使用Private CAAWS 为您管理 CA。您可以创建证书,在这种情况下,您可以完全访问此证书,甚至是私钥。它不与其他 AWS 服务集成。


如果您想获取证书以在您的实例中使用,例如 Apache 或 Nginx。最好的方法是在您的实例前面放置一个 ALB。ALB 将使用来自 ACM 的公共证书。

在此之后,您可以决定是否要在 ALB 和您的实例之间进行加密。
如果您想要进行端到端加密,您可以使用自签名证书,因为 ALB 不会对其进行验证,或者您可以使用来自 ACM Private CA 的一个证书。


推荐阅读