Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ELBv2: Fix CF bug when passing certificates to ELB listener creation #8106

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion moto/elbv2/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,8 +361,12 @@ def create_from_cloudformation_json( # type: ignore[misc]

default_actions = elbv2_backend.convert_and_validate_properties(properties)
certificates = elbv2_backend.convert_and_validate_certificates(certificates)
if certificates:
certificate = certificates[0].get("certificate_arn")
else:
certificate = None
listener = elbv2_backend.create_listener(
load_balancer_arn, protocol, port, ssl_policy, certificates, default_actions
load_balancer_arn, protocol, port, ssl_policy, certificate, default_actions
)
return listener

Expand Down
93 changes: 93 additions & 0 deletions tests/test_elbv2/test_elbv2_cloudformation.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,3 +326,96 @@ def test_fixed_response_action_listener_rule_cloudformation():
},
}
]


@mock_aws
def test_https_listener_rule_with_certificate_cloudformation():
cnf_conn = boto3.client("cloudformation", region_name="us-east-1")
elbv2_client = boto3.client("elbv2", region_name="us-east-1")
acm_client = boto3.client("acm", region_name="us-east-1")

acm_request_response = acm_client.request_certificate(
DomainName="fake.domain.com",
DomainValidationOptions=[
{"DomainName": "fake.domain.com", "ValidationDomain": "domain.com"},
],
)
certificate_arn = acm_request_response["CertificateArn"]

template = {
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "ECS Cluster Test CloudFormation",
"Resources": {
"testVPC": {
"Type": "AWS::EC2::VPC",
"Properties": {"CidrBlock": "10.0.0.0/16"},
},
"subnet1": {
"Type": "AWS::EC2::Subnet",
"Properties": {
"CidrBlock": "10.0.0.0/24",
"VpcId": {"Ref": "testVPC"},
"AvalabilityZone": "us-east-1b",
},
},
"subnet2": {
"Type": "AWS::EC2::Subnet",
"Properties": {
"CidrBlock": "10.0.1.0/24",
"VpcId": {"Ref": "testVPC"},
"AvalabilityZone": "us-east-1b",
},
},
"testLb": {
"Type": "AWS::ElasticLoadBalancingV2::LoadBalancer",
"Properties": {
"Name": "my-lb",
"Subnets": [{"Ref": "subnet1"}, {"Ref": "subnet2"}],
"Type": "application",
"SecurityGroups": [],
},
},
"testListener": {
"Type": "AWS::ElasticLoadBalancingV2::Listener",
"Properties": {
"LoadBalancerArn": {"Ref": "testLb"},
"Certificates": [{"CertificateArn": certificate_arn}],
"Port": 443,
"Protocol": "HTTPS",
"DefaultActions": [
{
"Type": "fixed-response",
"FixedResponseConfig": {
"ContentType": "text/plain",
"MessageBody": "This page does not exist",
"StatusCode": "404",
},
}
],
},
},
},
}
template_json = json.dumps(template)
cnf_conn.create_stack(StackName="test-stack", TemplateBody=template_json)

resp = elbv2_client.describe_load_balancers(Names=["my-lb"])
assert len(resp["LoadBalancers"]) == 1
load_balancer_arn = resp["LoadBalancers"][0]["LoadBalancerArn"]

listeners = elbv2_client.describe_listeners(LoadBalancerArn=load_balancer_arn)

assert len(listeners["Listeners"]) == 1
assert listeners["Listeners"][0]["DefaultActions"] == [
{
"Type": "fixed-response",
"FixedResponseConfig": {
"ContentType": "text/plain",
"MessageBody": "This page does not exist",
"StatusCode": "404",
},
}
]
assert listeners["Listeners"][0]["Certificates"] == [
{"CertificateArn": certificate_arn}
]
Loading