Run black on moto & test directories.
This commit is contained in:
parent
c820395dbf
commit
96e5b1993d
507 changed files with 52541 additions and 47814 deletions
|
|
@ -2,5 +2,5 @@ from __future__ import unicode_literals
|
|||
from .models import acm_backends
|
||||
from ..core.models import base_decorator
|
||||
|
||||
acm_backend = acm_backends['us-east-1']
|
||||
acm_backend = acm_backends["us-east-1"]
|
||||
mock_acm = base_decorator(acm_backends)
|
||||
|
|
|
|||
|
|
@ -57,20 +57,29 @@ class AWSError(Exception):
|
|||
self.message = message
|
||||
|
||||
def response(self):
|
||||
resp = {'__type': self.TYPE, 'message': self.message}
|
||||
resp = {"__type": self.TYPE, "message": self.message}
|
||||
return json.dumps(resp), dict(status=self.STATUS)
|
||||
|
||||
|
||||
class AWSValidationException(AWSError):
|
||||
TYPE = 'ValidationException'
|
||||
TYPE = "ValidationException"
|
||||
|
||||
|
||||
class AWSResourceNotFoundException(AWSError):
|
||||
TYPE = 'ResourceNotFoundException'
|
||||
TYPE = "ResourceNotFoundException"
|
||||
|
||||
|
||||
class CertBundle(BaseModel):
|
||||
def __init__(self, certificate, private_key, chain=None, region='us-east-1', arn=None, cert_type='IMPORTED', cert_status='ISSUED'):
|
||||
def __init__(
|
||||
self,
|
||||
certificate,
|
||||
private_key,
|
||||
chain=None,
|
||||
region="us-east-1",
|
||||
arn=None,
|
||||
cert_type="IMPORTED",
|
||||
cert_status="ISSUED",
|
||||
):
|
||||
self.created_at = datetime.datetime.now()
|
||||
self.cert = certificate
|
||||
self._cert = None
|
||||
|
|
@ -87,7 +96,7 @@ class CertBundle(BaseModel):
|
|||
if self.chain is None:
|
||||
self.chain = GOOGLE_ROOT_CA
|
||||
else:
|
||||
self.chain += b'\n' + GOOGLE_ROOT_CA
|
||||
self.chain += b"\n" + GOOGLE_ROOT_CA
|
||||
|
||||
# Takes care of PEM checking
|
||||
self.validate_pk()
|
||||
|
|
@ -114,149 +123,209 @@ class CertBundle(BaseModel):
|
|||
sans.add(domain_name)
|
||||
sans = [cryptography.x509.DNSName(item) for item in sans]
|
||||
|
||||
key = cryptography.hazmat.primitives.asymmetric.rsa.generate_private_key(public_exponent=65537, key_size=2048, backend=default_backend())
|
||||
subject = cryptography.x509.Name([
|
||||
cryptography.x509.NameAttribute(cryptography.x509.NameOID.COUNTRY_NAME, u"US"),
|
||||
cryptography.x509.NameAttribute(cryptography.x509.NameOID.STATE_OR_PROVINCE_NAME, u"CA"),
|
||||
cryptography.x509.NameAttribute(cryptography.x509.NameOID.LOCALITY_NAME, u"San Francisco"),
|
||||
cryptography.x509.NameAttribute(cryptography.x509.NameOID.ORGANIZATION_NAME, u"My Company"),
|
||||
cryptography.x509.NameAttribute(cryptography.x509.NameOID.COMMON_NAME, domain_name),
|
||||
])
|
||||
issuer = cryptography.x509.Name([ # C = US, O = Amazon, OU = Server CA 1B, CN = Amazon
|
||||
cryptography.x509.NameAttribute(cryptography.x509.NameOID.COUNTRY_NAME, u"US"),
|
||||
cryptography.x509.NameAttribute(cryptography.x509.NameOID.ORGANIZATION_NAME, u"Amazon"),
|
||||
cryptography.x509.NameAttribute(cryptography.x509.NameOID.ORGANIZATIONAL_UNIT_NAME, u"Server CA 1B"),
|
||||
cryptography.x509.NameAttribute(cryptography.x509.NameOID.COMMON_NAME, u"Amazon"),
|
||||
])
|
||||
cert = cryptography.x509.CertificateBuilder().subject_name(
|
||||
subject
|
||||
).issuer_name(
|
||||
issuer
|
||||
).public_key(
|
||||
key.public_key()
|
||||
).serial_number(
|
||||
cryptography.x509.random_serial_number()
|
||||
).not_valid_before(
|
||||
datetime.datetime.utcnow()
|
||||
).not_valid_after(
|
||||
datetime.datetime.utcnow() + datetime.timedelta(days=365)
|
||||
).add_extension(
|
||||
cryptography.x509.SubjectAlternativeName(sans),
|
||||
critical=False,
|
||||
).sign(key, hashes.SHA512(), default_backend())
|
||||
key = cryptography.hazmat.primitives.asymmetric.rsa.generate_private_key(
|
||||
public_exponent=65537, key_size=2048, backend=default_backend()
|
||||
)
|
||||
subject = cryptography.x509.Name(
|
||||
[
|
||||
cryptography.x509.NameAttribute(
|
||||
cryptography.x509.NameOID.COUNTRY_NAME, "US"
|
||||
),
|
||||
cryptography.x509.NameAttribute(
|
||||
cryptography.x509.NameOID.STATE_OR_PROVINCE_NAME, "CA"
|
||||
),
|
||||
cryptography.x509.NameAttribute(
|
||||
cryptography.x509.NameOID.LOCALITY_NAME, "San Francisco"
|
||||
),
|
||||
cryptography.x509.NameAttribute(
|
||||
cryptography.x509.NameOID.ORGANIZATION_NAME, "My Company"
|
||||
),
|
||||
cryptography.x509.NameAttribute(
|
||||
cryptography.x509.NameOID.COMMON_NAME, domain_name
|
||||
),
|
||||
]
|
||||
)
|
||||
issuer = cryptography.x509.Name(
|
||||
[ # C = US, O = Amazon, OU = Server CA 1B, CN = Amazon
|
||||
cryptography.x509.NameAttribute(
|
||||
cryptography.x509.NameOID.COUNTRY_NAME, "US"
|
||||
),
|
||||
cryptography.x509.NameAttribute(
|
||||
cryptography.x509.NameOID.ORGANIZATION_NAME, "Amazon"
|
||||
),
|
||||
cryptography.x509.NameAttribute(
|
||||
cryptography.x509.NameOID.ORGANIZATIONAL_UNIT_NAME, "Server CA 1B"
|
||||
),
|
||||
cryptography.x509.NameAttribute(
|
||||
cryptography.x509.NameOID.COMMON_NAME, "Amazon"
|
||||
),
|
||||
]
|
||||
)
|
||||
cert = (
|
||||
cryptography.x509.CertificateBuilder()
|
||||
.subject_name(subject)
|
||||
.issuer_name(issuer)
|
||||
.public_key(key.public_key())
|
||||
.serial_number(cryptography.x509.random_serial_number())
|
||||
.not_valid_before(datetime.datetime.utcnow())
|
||||
.not_valid_after(datetime.datetime.utcnow() + datetime.timedelta(days=365))
|
||||
.add_extension(
|
||||
cryptography.x509.SubjectAlternativeName(sans), critical=False
|
||||
)
|
||||
.sign(key, hashes.SHA512(), default_backend())
|
||||
)
|
||||
|
||||
cert_armored = cert.public_bytes(serialization.Encoding.PEM)
|
||||
private_key = key.private_bytes(
|
||||
encoding=serialization.Encoding.PEM,
|
||||
format=serialization.PrivateFormat.TraditionalOpenSSL,
|
||||
encryption_algorithm=serialization.NoEncryption()
|
||||
encryption_algorithm=serialization.NoEncryption(),
|
||||
)
|
||||
|
||||
return cls(cert_armored, private_key, cert_type='AMAZON_ISSUED', cert_status='PENDING_VALIDATION', region=region)
|
||||
return cls(
|
||||
cert_armored,
|
||||
private_key,
|
||||
cert_type="AMAZON_ISSUED",
|
||||
cert_status="PENDING_VALIDATION",
|
||||
region=region,
|
||||
)
|
||||
|
||||
def validate_pk(self):
|
||||
try:
|
||||
self._key = serialization.load_pem_private_key(self.key, password=None, backend=default_backend())
|
||||
self._key = serialization.load_pem_private_key(
|
||||
self.key, password=None, backend=default_backend()
|
||||
)
|
||||
|
||||
if self._key.key_size > 2048:
|
||||
AWSValidationException('The private key length is not supported. Only 1024-bit and 2048-bit are allowed.')
|
||||
AWSValidationException(
|
||||
"The private key length is not supported. Only 1024-bit and 2048-bit are allowed."
|
||||
)
|
||||
|
||||
except Exception as err:
|
||||
if isinstance(err, AWSValidationException):
|
||||
raise
|
||||
raise AWSValidationException('The private key is not PEM-encoded or is not valid.')
|
||||
raise AWSValidationException(
|
||||
"The private key is not PEM-encoded or is not valid."
|
||||
)
|
||||
|
||||
def validate_certificate(self):
|
||||
try:
|
||||
self._cert = cryptography.x509.load_pem_x509_certificate(self.cert, default_backend())
|
||||
self._cert = cryptography.x509.load_pem_x509_certificate(
|
||||
self.cert, default_backend()
|
||||
)
|
||||
|
||||
now = datetime.datetime.utcnow()
|
||||
if self._cert.not_valid_after < now:
|
||||
raise AWSValidationException('The certificate has expired, is not valid.')
|
||||
raise AWSValidationException(
|
||||
"The certificate has expired, is not valid."
|
||||
)
|
||||
|
||||
if self._cert.not_valid_before > now:
|
||||
raise AWSValidationException('The certificate is not in effect yet, is not valid.')
|
||||
raise AWSValidationException(
|
||||
"The certificate is not in effect yet, is not valid."
|
||||
)
|
||||
|
||||
# Extracting some common fields for ease of use
|
||||
# Have to search through cert.subject for OIDs
|
||||
self.common_name = self._cert.subject.get_attributes_for_oid(cryptography.x509.OID_COMMON_NAME)[0].value
|
||||
self.common_name = self._cert.subject.get_attributes_for_oid(
|
||||
cryptography.x509.OID_COMMON_NAME
|
||||
)[0].value
|
||||
|
||||
except Exception as err:
|
||||
if isinstance(err, AWSValidationException):
|
||||
raise
|
||||
raise AWSValidationException('The certificate is not PEM-encoded or is not valid.')
|
||||
raise AWSValidationException(
|
||||
"The certificate is not PEM-encoded or is not valid."
|
||||
)
|
||||
|
||||
def validate_chain(self):
|
||||
try:
|
||||
self._chain = []
|
||||
|
||||
for cert_armored in self.chain.split(b'-\n-'):
|
||||
for cert_armored in self.chain.split(b"-\n-"):
|
||||
# Would leave encoded but Py2 does not have raw binary strings
|
||||
cert_armored = cert_armored.decode()
|
||||
|
||||
# Fix missing -'s on split
|
||||
cert_armored = re.sub(r'^----B', '-----B', cert_armored)
|
||||
cert_armored = re.sub(r'E----$', 'E-----', cert_armored)
|
||||
cert = cryptography.x509.load_pem_x509_certificate(cert_armored.encode(), default_backend())
|
||||
cert_armored = re.sub(r"^----B", "-----B", cert_armored)
|
||||
cert_armored = re.sub(r"E----$", "E-----", cert_armored)
|
||||
cert = cryptography.x509.load_pem_x509_certificate(
|
||||
cert_armored.encode(), default_backend()
|
||||
)
|
||||
self._chain.append(cert)
|
||||
|
||||
now = datetime.datetime.now()
|
||||
if self._cert.not_valid_after < now:
|
||||
raise AWSValidationException('The certificate chain has expired, is not valid.')
|
||||
raise AWSValidationException(
|
||||
"The certificate chain has expired, is not valid."
|
||||
)
|
||||
|
||||
if self._cert.not_valid_before > now:
|
||||
raise AWSValidationException('The certificate chain is not in effect yet, is not valid.')
|
||||
raise AWSValidationException(
|
||||
"The certificate chain is not in effect yet, is not valid."
|
||||
)
|
||||
|
||||
except Exception as err:
|
||||
if isinstance(err, AWSValidationException):
|
||||
raise
|
||||
raise AWSValidationException('The certificate is not PEM-encoded or is not valid.')
|
||||
raise AWSValidationException(
|
||||
"The certificate is not PEM-encoded or is not valid."
|
||||
)
|
||||
|
||||
def check(self):
|
||||
# Basically, if the certificate is pending, and then checked again after 1 min
|
||||
# It will appear as if its been validated
|
||||
if self.type == 'AMAZON_ISSUED' and self.status == 'PENDING_VALIDATION' and \
|
||||
(datetime.datetime.now() - self.created_at).total_seconds() > 60: # 1min
|
||||
self.status = 'ISSUED'
|
||||
if (
|
||||
self.type == "AMAZON_ISSUED"
|
||||
and self.status == "PENDING_VALIDATION"
|
||||
and (datetime.datetime.now() - self.created_at).total_seconds() > 60
|
||||
): # 1min
|
||||
self.status = "ISSUED"
|
||||
|
||||
def describe(self):
|
||||
# 'RenewalSummary': {}, # Only when cert is amazon issued
|
||||
if self._key.key_size == 1024:
|
||||
key_algo = 'RSA_1024'
|
||||
key_algo = "RSA_1024"
|
||||
elif self._key.key_size == 2048:
|
||||
key_algo = 'RSA_2048'
|
||||
key_algo = "RSA_2048"
|
||||
else:
|
||||
key_algo = 'EC_prime256v1'
|
||||
key_algo = "EC_prime256v1"
|
||||
|
||||
# Look for SANs
|
||||
san_obj = self._cert.extensions.get_extension_for_oid(cryptography.x509.OID_SUBJECT_ALTERNATIVE_NAME)
|
||||
san_obj = self._cert.extensions.get_extension_for_oid(
|
||||
cryptography.x509.OID_SUBJECT_ALTERNATIVE_NAME
|
||||
)
|
||||
sans = []
|
||||
if san_obj is not None:
|
||||
sans = [item.value for item in san_obj.value]
|
||||
|
||||
result = {
|
||||
'Certificate': {
|
||||
'CertificateArn': self.arn,
|
||||
'DomainName': self.common_name,
|
||||
'InUseBy': [],
|
||||
'Issuer': self._cert.issuer.get_attributes_for_oid(cryptography.x509.OID_COMMON_NAME)[0].value,
|
||||
'KeyAlgorithm': key_algo,
|
||||
'NotAfter': datetime_to_epoch(self._cert.not_valid_after),
|
||||
'NotBefore': datetime_to_epoch(self._cert.not_valid_before),
|
||||
'Serial': self._cert.serial_number,
|
||||
'SignatureAlgorithm': self._cert.signature_algorithm_oid._name.upper().replace('ENCRYPTION', ''),
|
||||
'Status': self.status, # One of PENDING_VALIDATION, ISSUED, INACTIVE, EXPIRED, VALIDATION_TIMED_OUT, REVOKED, FAILED.
|
||||
'Subject': 'CN={0}'.format(self.common_name),
|
||||
'SubjectAlternativeNames': sans,
|
||||
'Type': self.type # One of IMPORTED, AMAZON_ISSUED
|
||||
"Certificate": {
|
||||
"CertificateArn": self.arn,
|
||||
"DomainName": self.common_name,
|
||||
"InUseBy": [],
|
||||
"Issuer": self._cert.issuer.get_attributes_for_oid(
|
||||
cryptography.x509.OID_COMMON_NAME
|
||||
)[0].value,
|
||||
"KeyAlgorithm": key_algo,
|
||||
"NotAfter": datetime_to_epoch(self._cert.not_valid_after),
|
||||
"NotBefore": datetime_to_epoch(self._cert.not_valid_before),
|
||||
"Serial": self._cert.serial_number,
|
||||
"SignatureAlgorithm": self._cert.signature_algorithm_oid._name.upper().replace(
|
||||
"ENCRYPTION", ""
|
||||
),
|
||||
"Status": self.status, # One of PENDING_VALIDATION, ISSUED, INACTIVE, EXPIRED, VALIDATION_TIMED_OUT, REVOKED, FAILED.
|
||||
"Subject": "CN={0}".format(self.common_name),
|
||||
"SubjectAlternativeNames": sans,
|
||||
"Type": self.type, # One of IMPORTED, AMAZON_ISSUED
|
||||
}
|
||||
}
|
||||
|
||||
if self.type == 'IMPORTED':
|
||||
result['Certificate']['ImportedAt'] = datetime_to_epoch(self.created_at)
|
||||
if self.type == "IMPORTED":
|
||||
result["Certificate"]["ImportedAt"] = datetime_to_epoch(self.created_at)
|
||||
else:
|
||||
result['Certificate']['CreatedAt'] = datetime_to_epoch(self.created_at)
|
||||
result['Certificate']['IssuedAt'] = datetime_to_epoch(self.created_at)
|
||||
result["Certificate"]["CreatedAt"] = datetime_to_epoch(self.created_at)
|
||||
result["Certificate"]["IssuedAt"] = datetime_to_epoch(self.created_at)
|
||||
|
||||
return result
|
||||
|
||||
|
|
@ -264,7 +333,7 @@ class CertBundle(BaseModel):
|
|||
return self.arn
|
||||
|
||||
def __repr__(self):
|
||||
return '<Certificate>'
|
||||
return "<Certificate>"
|
||||
|
||||
|
||||
class AWSCertificateManagerBackend(BaseBackend):
|
||||
|
|
@ -281,7 +350,9 @@ class AWSCertificateManagerBackend(BaseBackend):
|
|||
|
||||
@staticmethod
|
||||
def _arn_not_found(arn):
|
||||
msg = 'Certificate with arn {0} not found in account {1}'.format(arn, DEFAULT_ACCOUNT_ID)
|
||||
msg = "Certificate with arn {0} not found in account {1}".format(
|
||||
arn, DEFAULT_ACCOUNT_ID
|
||||
)
|
||||
return AWSResourceNotFoundException(msg)
|
||||
|
||||
def _get_arn_from_idempotency_token(self, token):
|
||||
|
|
@ -298,17 +369,20 @@ class AWSCertificateManagerBackend(BaseBackend):
|
|||
"""
|
||||
now = datetime.datetime.now()
|
||||
if token in self._idempotency_tokens:
|
||||
if self._idempotency_tokens[token]['expires'] < now:
|
||||
if self._idempotency_tokens[token]["expires"] < now:
|
||||
# Token has expired, new request
|
||||
del self._idempotency_tokens[token]
|
||||
return None
|
||||
else:
|
||||
return self._idempotency_tokens[token]['arn']
|
||||
return self._idempotency_tokens[token]["arn"]
|
||||
|
||||
return None
|
||||
|
||||
def _set_idempotency_token_arn(self, token, arn):
|
||||
self._idempotency_tokens[token] = {'arn': arn, 'expires': datetime.datetime.now() + datetime.timedelta(hours=1)}
|
||||
self._idempotency_tokens[token] = {
|
||||
"arn": arn,
|
||||
"expires": datetime.datetime.now() + datetime.timedelta(hours=1),
|
||||
}
|
||||
|
||||
def import_cert(self, certificate, private_key, chain=None, arn=None):
|
||||
if arn is not None:
|
||||
|
|
@ -316,7 +390,9 @@ class AWSCertificateManagerBackend(BaseBackend):
|
|||
raise self._arn_not_found(arn)
|
||||
else:
|
||||
# Will reuse provided ARN
|
||||
bundle = CertBundle(certificate, private_key, chain=chain, region=region, arn=arn)
|
||||
bundle = CertBundle(
|
||||
certificate, private_key, chain=chain, region=region, arn=arn
|
||||
)
|
||||
else:
|
||||
# Will generate a random ARN
|
||||
bundle = CertBundle(certificate, private_key, chain=chain, region=region)
|
||||
|
|
@ -351,13 +427,21 @@ class AWSCertificateManagerBackend(BaseBackend):
|
|||
|
||||
del self._certificates[arn]
|
||||
|
||||
def request_certificate(self, domain_name, domain_validation_options, idempotency_token, subject_alt_names):
|
||||
def request_certificate(
|
||||
self,
|
||||
domain_name,
|
||||
domain_validation_options,
|
||||
idempotency_token,
|
||||
subject_alt_names,
|
||||
):
|
||||
if idempotency_token is not None:
|
||||
arn = self._get_arn_from_idempotency_token(idempotency_token)
|
||||
if arn is not None:
|
||||
return arn
|
||||
|
||||
cert = CertBundle.generate_cert(domain_name, region=self.region, sans=subject_alt_names)
|
||||
cert = CertBundle.generate_cert(
|
||||
domain_name, region=self.region, sans=subject_alt_names
|
||||
)
|
||||
if idempotency_token is not None:
|
||||
self._set_idempotency_token_arn(idempotency_token, cert.arn)
|
||||
self._certificates[cert.arn] = cert
|
||||
|
|
@ -369,8 +453,8 @@ class AWSCertificateManagerBackend(BaseBackend):
|
|||
cert_bundle = self.get_certificate(arn)
|
||||
|
||||
for tag in tags:
|
||||
key = tag['Key']
|
||||
value = tag.get('Value', None)
|
||||
key = tag["Key"]
|
||||
value = tag.get("Value", None)
|
||||
cert_bundle.tags[key] = value
|
||||
|
||||
def remove_tags_from_certificate(self, arn, tags):
|
||||
|
|
@ -378,8 +462,8 @@ class AWSCertificateManagerBackend(BaseBackend):
|
|||
cert_bundle = self.get_certificate(arn)
|
||||
|
||||
for tag in tags:
|
||||
key = tag['Key']
|
||||
value = tag.get('Value', None)
|
||||
key = tag["Key"]
|
||||
value = tag.get("Value", None)
|
||||
|
||||
try:
|
||||
# If value isnt provided, just delete key
|
||||
|
|
|
|||
|
|
@ -7,7 +7,6 @@ from .models import acm_backends, AWSError, AWSValidationException
|
|||
|
||||
|
||||
class AWSCertificateManagerResponse(BaseResponse):
|
||||
|
||||
@property
|
||||
def acm_backend(self):
|
||||
"""
|
||||
|
|
@ -29,40 +28,49 @@ class AWSCertificateManagerResponse(BaseResponse):
|
|||
return self.request_params.get(param, default)
|
||||
|
||||
def add_tags_to_certificate(self):
|
||||
arn = self._get_param('CertificateArn')
|
||||
tags = self._get_param('Tags')
|
||||
arn = self._get_param("CertificateArn")
|
||||
tags = self._get_param("Tags")
|
||||
|
||||
if arn is None:
|
||||
msg = 'A required parameter for the specified action is not supplied.'
|
||||
return json.dumps({'__type': 'MissingParameter', 'message': msg}), dict(status=400)
|
||||
msg = "A required parameter for the specified action is not supplied."
|
||||
return (
|
||||
json.dumps({"__type": "MissingParameter", "message": msg}),
|
||||
dict(status=400),
|
||||
)
|
||||
|
||||
try:
|
||||
self.acm_backend.add_tags_to_certificate(arn, tags)
|
||||
except AWSError as err:
|
||||
return err.response()
|
||||
|
||||
return ''
|
||||
return ""
|
||||
|
||||
def delete_certificate(self):
|
||||
arn = self._get_param('CertificateArn')
|
||||
arn = self._get_param("CertificateArn")
|
||||
|
||||
if arn is None:
|
||||
msg = 'A required parameter for the specified action is not supplied.'
|
||||
return json.dumps({'__type': 'MissingParameter', 'message': msg}), dict(status=400)
|
||||
msg = "A required parameter for the specified action is not supplied."
|
||||
return (
|
||||
json.dumps({"__type": "MissingParameter", "message": msg}),
|
||||
dict(status=400),
|
||||
)
|
||||
|
||||
try:
|
||||
self.acm_backend.delete_certificate(arn)
|
||||
except AWSError as err:
|
||||
return err.response()
|
||||
|
||||
return ''
|
||||
return ""
|
||||
|
||||
def describe_certificate(self):
|
||||
arn = self._get_param('CertificateArn')
|
||||
arn = self._get_param("CertificateArn")
|
||||
|
||||
if arn is None:
|
||||
msg = 'A required parameter for the specified action is not supplied.'
|
||||
return json.dumps({'__type': 'MissingParameter', 'message': msg}), dict(status=400)
|
||||
msg = "A required parameter for the specified action is not supplied."
|
||||
return (
|
||||
json.dumps({"__type": "MissingParameter", "message": msg}),
|
||||
dict(status=400),
|
||||
)
|
||||
|
||||
try:
|
||||
cert_bundle = self.acm_backend.get_certificate(arn)
|
||||
|
|
@ -72,11 +80,14 @@ class AWSCertificateManagerResponse(BaseResponse):
|
|||
return json.dumps(cert_bundle.describe())
|
||||
|
||||
def get_certificate(self):
|
||||
arn = self._get_param('CertificateArn')
|
||||
arn = self._get_param("CertificateArn")
|
||||
|
||||
if arn is None:
|
||||
msg = 'A required parameter for the specified action is not supplied.'
|
||||
return json.dumps({'__type': 'MissingParameter', 'message': msg}), dict(status=400)
|
||||
msg = "A required parameter for the specified action is not supplied."
|
||||
return (
|
||||
json.dumps({"__type": "MissingParameter", "message": msg}),
|
||||
dict(status=400),
|
||||
)
|
||||
|
||||
try:
|
||||
cert_bundle = self.acm_backend.get_certificate(arn)
|
||||
|
|
@ -84,8 +95,8 @@ class AWSCertificateManagerResponse(BaseResponse):
|
|||
return err.response()
|
||||
|
||||
result = {
|
||||
'Certificate': cert_bundle.cert.decode(),
|
||||
'CertificateChain': cert_bundle.chain.decode()
|
||||
"Certificate": cert_bundle.cert.decode(),
|
||||
"CertificateChain": cert_bundle.chain.decode(),
|
||||
}
|
||||
return json.dumps(result)
|
||||
|
||||
|
|
@ -102,104 +113,129 @@ class AWSCertificateManagerResponse(BaseResponse):
|
|||
|
||||
:return: str(JSON) for response
|
||||
"""
|
||||
certificate = self._get_param('Certificate')
|
||||
private_key = self._get_param('PrivateKey')
|
||||
chain = self._get_param('CertificateChain') # Optional
|
||||
current_arn = self._get_param('CertificateArn') # Optional
|
||||
certificate = self._get_param("Certificate")
|
||||
private_key = self._get_param("PrivateKey")
|
||||
chain = self._get_param("CertificateChain") # Optional
|
||||
current_arn = self._get_param("CertificateArn") # Optional
|
||||
|
||||
# Simple parameter decoding. Rather do it here as its a data transport decision not part of the
|
||||
# actual data
|
||||
try:
|
||||
certificate = base64.standard_b64decode(certificate)
|
||||
except Exception:
|
||||
return AWSValidationException('The certificate is not PEM-encoded or is not valid.').response()
|
||||
return AWSValidationException(
|
||||
"The certificate is not PEM-encoded or is not valid."
|
||||
).response()
|
||||
try:
|
||||
private_key = base64.standard_b64decode(private_key)
|
||||
except Exception:
|
||||
return AWSValidationException('The private key is not PEM-encoded or is not valid.').response()
|
||||
return AWSValidationException(
|
||||
"The private key is not PEM-encoded or is not valid."
|
||||
).response()
|
||||
if chain is not None:
|
||||
try:
|
||||
chain = base64.standard_b64decode(chain)
|
||||
except Exception:
|
||||
return AWSValidationException('The certificate chain is not PEM-encoded or is not valid.').response()
|
||||
return AWSValidationException(
|
||||
"The certificate chain is not PEM-encoded or is not valid."
|
||||
).response()
|
||||
|
||||
try:
|
||||
arn = self.acm_backend.import_cert(certificate, private_key, chain=chain, arn=current_arn)
|
||||
arn = self.acm_backend.import_cert(
|
||||
certificate, private_key, chain=chain, arn=current_arn
|
||||
)
|
||||
except AWSError as err:
|
||||
return err.response()
|
||||
|
||||
return json.dumps({'CertificateArn': arn})
|
||||
return json.dumps({"CertificateArn": arn})
|
||||
|
||||
def list_certificates(self):
|
||||
certs = []
|
||||
statuses = self._get_param('CertificateStatuses')
|
||||
statuses = self._get_param("CertificateStatuses")
|
||||
for cert_bundle in self.acm_backend.get_certificates_list(statuses):
|
||||
certs.append({
|
||||
'CertificateArn': cert_bundle.arn,
|
||||
'DomainName': cert_bundle.common_name
|
||||
})
|
||||
certs.append(
|
||||
{
|
||||
"CertificateArn": cert_bundle.arn,
|
||||
"DomainName": cert_bundle.common_name,
|
||||
}
|
||||
)
|
||||
|
||||
result = {'CertificateSummaryList': certs}
|
||||
result = {"CertificateSummaryList": certs}
|
||||
return json.dumps(result)
|
||||
|
||||
def list_tags_for_certificate(self):
|
||||
arn = self._get_param('CertificateArn')
|
||||
arn = self._get_param("CertificateArn")
|
||||
|
||||
if arn is None:
|
||||
msg = 'A required parameter for the specified action is not supplied.'
|
||||
return {'__type': 'MissingParameter', 'message': msg}, dict(status=400)
|
||||
msg = "A required parameter for the specified action is not supplied."
|
||||
return {"__type": "MissingParameter", "message": msg}, dict(status=400)
|
||||
|
||||
try:
|
||||
cert_bundle = self.acm_backend.get_certificate(arn)
|
||||
except AWSError as err:
|
||||
return err.response()
|
||||
|
||||
result = {'Tags': []}
|
||||
result = {"Tags": []}
|
||||
# Tag "objects" can not contain the Value part
|
||||
for key, value in cert_bundle.tags.items():
|
||||
tag_dict = {'Key': key}
|
||||
tag_dict = {"Key": key}
|
||||
if value is not None:
|
||||
tag_dict['Value'] = value
|
||||
result['Tags'].append(tag_dict)
|
||||
tag_dict["Value"] = value
|
||||
result["Tags"].append(tag_dict)
|
||||
|
||||
return json.dumps(result)
|
||||
|
||||
def remove_tags_from_certificate(self):
|
||||
arn = self._get_param('CertificateArn')
|
||||
tags = self._get_param('Tags')
|
||||
arn = self._get_param("CertificateArn")
|
||||
tags = self._get_param("Tags")
|
||||
|
||||
if arn is None:
|
||||
msg = 'A required parameter for the specified action is not supplied.'
|
||||
return json.dumps({'__type': 'MissingParameter', 'message': msg}), dict(status=400)
|
||||
msg = "A required parameter for the specified action is not supplied."
|
||||
return (
|
||||
json.dumps({"__type": "MissingParameter", "message": msg}),
|
||||
dict(status=400),
|
||||
)
|
||||
|
||||
try:
|
||||
self.acm_backend.remove_tags_from_certificate(arn, tags)
|
||||
except AWSError as err:
|
||||
return err.response()
|
||||
|
||||
return ''
|
||||
return ""
|
||||
|
||||
def request_certificate(self):
|
||||
domain_name = self._get_param('DomainName')
|
||||
domain_validation_options = self._get_param('DomainValidationOptions') # is ignored atm
|
||||
idempotency_token = self._get_param('IdempotencyToken')
|
||||
subject_alt_names = self._get_param('SubjectAlternativeNames')
|
||||
domain_name = self._get_param("DomainName")
|
||||
domain_validation_options = self._get_param(
|
||||
"DomainValidationOptions"
|
||||
) # is ignored atm
|
||||
idempotency_token = self._get_param("IdempotencyToken")
|
||||
subject_alt_names = self._get_param("SubjectAlternativeNames")
|
||||
|
||||
if subject_alt_names is not None and len(subject_alt_names) > 10:
|
||||
# There is initial AWS limit of 10
|
||||
msg = 'An ACM limit has been exceeded. Need to request SAN limit to be raised'
|
||||
return json.dumps({'__type': 'LimitExceededException', 'message': msg}), dict(status=400)
|
||||
msg = (
|
||||
"An ACM limit has been exceeded. Need to request SAN limit to be raised"
|
||||
)
|
||||
return (
|
||||
json.dumps({"__type": "LimitExceededException", "message": msg}),
|
||||
dict(status=400),
|
||||
)
|
||||
|
||||
try:
|
||||
arn = self.acm_backend.request_certificate(domain_name, domain_validation_options, idempotency_token, subject_alt_names)
|
||||
arn = self.acm_backend.request_certificate(
|
||||
domain_name,
|
||||
domain_validation_options,
|
||||
idempotency_token,
|
||||
subject_alt_names,
|
||||
)
|
||||
except AWSError as err:
|
||||
return err.response()
|
||||
|
||||
return json.dumps({'CertificateArn': arn})
|
||||
return json.dumps({"CertificateArn": arn})
|
||||
|
||||
def resend_validation_email(self):
|
||||
arn = self._get_param('CertificateArn')
|
||||
domain = self._get_param('Domain')
|
||||
arn = self._get_param("CertificateArn")
|
||||
domain = self._get_param("Domain")
|
||||
# ValidationDomain not used yet.
|
||||
# Contains domain which is equal to or a subset of Domain
|
||||
# that AWS will send validation emails to
|
||||
|
|
@ -207,18 +243,21 @@ class AWSCertificateManagerResponse(BaseResponse):
|
|||
# validation_domain = self._get_param('ValidationDomain')
|
||||
|
||||
if arn is None:
|
||||
msg = 'A required parameter for the specified action is not supplied.'
|
||||
return json.dumps({'__type': 'MissingParameter', 'message': msg}), dict(status=400)
|
||||
msg = "A required parameter for the specified action is not supplied."
|
||||
return (
|
||||
json.dumps({"__type": "MissingParameter", "message": msg}),
|
||||
dict(status=400),
|
||||
)
|
||||
|
||||
try:
|
||||
cert_bundle = self.acm_backend.get_certificate(arn)
|
||||
|
||||
if cert_bundle.common_name != domain:
|
||||
msg = 'Parameter Domain does not match certificate domain'
|
||||
_type = 'InvalidDomainValidationOptionsException'
|
||||
return json.dumps({'__type': _type, 'message': msg}), dict(status=400)
|
||||
msg = "Parameter Domain does not match certificate domain"
|
||||
_type = "InvalidDomainValidationOptionsException"
|
||||
return json.dumps({"__type": _type, "message": msg}), dict(status=400)
|
||||
|
||||
except AWSError as err:
|
||||
return err.response()
|
||||
|
||||
return ''
|
||||
return ""
|
||||
|
|
|
|||
|
|
@ -1,10 +1,6 @@
|
|||
from __future__ import unicode_literals
|
||||
from .responses import AWSCertificateManagerResponse
|
||||
|
||||
url_bases = [
|
||||
"https?://acm.(.+).amazonaws.com",
|
||||
]
|
||||
url_bases = ["https?://acm.(.+).amazonaws.com"]
|
||||
|
||||
url_paths = {
|
||||
'{0}/$': AWSCertificateManagerResponse.dispatch,
|
||||
}
|
||||
url_paths = {"{0}/$": AWSCertificateManagerResponse.dispatch}
|
||||
|
|
|
|||
|
|
@ -4,4 +4,6 @@ import uuid
|
|||
def make_arn_for_certificate(account_id, region_name):
|
||||
# Example
|
||||
# arn:aws:acm:eu-west-2:764371465172:certificate/c4b738b8-56fe-4b3a-b841-1c047654780b
|
||||
return "arn:aws:acm:{0}:{1}:certificate/{2}".format(region_name, account_id, uuid.uuid4())
|
||||
return "arn:aws:acm:{0}:{1}:certificate/{2}".format(
|
||||
region_name, account_id, uuid.uuid4()
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue