【问题标题】:Import certificate in AWS ACM using python使用 python 在 AWS ACM 中导入证书
【发布时间】:2020-07-19 02:17:55
【问题描述】:

我正在使用 python 将 openssl 证书导入 AWS ACM。我总是出错:

Response:
{
      "errorMessage": "An error occurred (ValidationException) when calling the ImportCertificate operation: The certificate field contains more than one certificate. You can specify only one certificate in this field.",
      "errorType": "ClientError",
      "stackTrace": [
        "  File \"/var/task/lambda_function.py\", line 7, in lambda_handler\n    response = client.import_certificate(\n",
        "  File \"/var/runtime/botocore/client.py\", line 316, in _api_call\n    return self._make_api_call(operation_name, kwargs)\n",
        "  File \"/var/runtime/botocore/client.py\", line 626, in _make_api_call\n    raise error_class(parsed_response, operation_name)\n"
      ]
}

这是我的代码:

import boto3

client = boto3.client('acm')

def lambda_handler(event, context):
    response = client.import_certificate(
        Certificate='sample.vpn.crt',
        PrivateKey='sample.vpn.key',
        CertificateChain='ca.crt'
    )

任何帮助将不胜感激。

【问题讨论】:

    标签: python amazon-web-services aws-acm


    【解决方案1】:

    如boto3 docs中所说,三个参数的类型不应该是字符串,而是字节。对我来说,诀窍是从包中读取证书文件,如下所示:

    import boto3
    
    client = boto3.client('acm')
    
    def lambda_handler(event, context):
        certificate=open('sample.vpn.crt', 'rb').read()
        privatekey=open('sample.vpn.key', 'rb').read()
        chain=open('ca.crt', 'rb').read()
    
        response = client.import_certificate(
            Certificate=certificate,
            PrivateKey=privatekey,
            CertificateChain=chain
        )
    

    不幸的是,在这种情况下,错误消息有点误导。如果您仍然收到相同的错误消息,请确保您的证书文件具有 ACM 要求的格式。您可以通过尝试使用 ACM 控制台导入证书来进行测试。如果您收到同样的错误,请按照 AWS 在此 troubleshooting page 上提供的步骤操作。

    【讨论】:

      【解决方案2】:

      发生错误是因为您应该传递证书的值,而不是文件名:

          CertificateArn='string',
          Certificate=b'bytes',
          PrivateKey=b'bytes',
      

      因此,您可以尝试以下方法:

      with open('sample.vpn.pem','r') as f:
          crt = f.read()
      
      with open('sample.vpn.pem','rb') as f:
          key = f.read()
      
      with open('ca.crt','rb') as f:
          chain = f.read()
      
      response = client.import_certificate(
          Certificate=crt,
          PrivateKey=key,
          CertificateChain=chain)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-12-19
        • 1970-01-01
        • 2020-03-02
        • 2017-08-28
        • 2020-07-04
        • 2018-02-05
        • 1970-01-01
        相关资源
        最近更新 更多