2022-03-21 20:55:19 +00:00
|
|
|
import copy
|
2017-10-29 16:06:03 +00:00
|
|
|
import os
|
2023-11-30 15:55:51 +00:00
|
|
|
|
2017-07-20 22:00:30 +00:00
|
|
|
import boto3
|
2020-10-06 05:54:49 +00:00
|
|
|
import pytest
|
2023-11-30 15:55:51 +00:00
|
|
|
from botocore.exceptions import ClientError
|
2017-07-20 22:00:30 +00:00
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
from moto import mock_aws
|
2022-08-13 09:49:43 +00:00
|
|
|
from moto.core import DEFAULT_ACCOUNT_ID as ACCOUNT_ID
|
2023-11-30 15:55:51 +00:00
|
|
|
from moto.elbv2 import elbv2_backends
|
2021-01-29 11:31:56 +00:00
|
|
|
from tests import EXAMPLE_AMI_ID
|
2017-07-20 22:00:30 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2017-07-20 22:00:30 +00:00
|
|
|
def test_create_load_balancer():
|
2021-10-18 19:44:29 +00:00
|
|
|
response, _, security_group, subnet1, subnet2, conn = create_load_balancer()
|
2017-07-20 22:00:30 +00:00
|
|
|
|
2023-07-24 12:50:35 +00:00
|
|
|
lb = response["LoadBalancers"][0]
|
|
|
|
assert lb["DNSName"] == "my-lb-1.us-east-1.elb.amazonaws.com"
|
|
|
|
assert (
|
|
|
|
lb["LoadBalancerArn"]
|
|
|
|
== f"arn:aws:elasticloadbalancing:us-east-1:{ACCOUNT_ID}:loadbalancer/app/my-lb/50dc6c495c0c9188"
|
2017-10-02 19:36:47 +00:00
|
|
|
)
|
2023-07-24 12:50:35 +00:00
|
|
|
assert lb["SecurityGroups"] == [security_group.id]
|
|
|
|
assert lb["AvailabilityZones"] == [
|
|
|
|
{"SubnetId": subnet1.id, "ZoneName": "us-east-1a"},
|
|
|
|
{"SubnetId": subnet2.id, "ZoneName": "us-east-1b"},
|
|
|
|
]
|
|
|
|
assert lb["CreatedTime"].tzinfo is not None
|
|
|
|
assert lb["State"]["Code"] == "provisioning"
|
|
|
|
lb_arn = lb["LoadBalancerArn"]
|
2017-07-20 22:00:30 +00:00
|
|
|
|
|
|
|
# Ensure the tags persisted
|
2022-03-31 11:40:06 +00:00
|
|
|
tag_desc = conn.describe_tags(ResourceArns=[lb_arn])["TagDescriptions"][0]
|
2023-07-24 12:50:35 +00:00
|
|
|
assert tag_desc["ResourceArn"] == lb_arn
|
2022-03-31 11:40:06 +00:00
|
|
|
tags = {d["Key"]: d["Value"] for d in tag_desc["Tags"]}
|
2023-07-24 12:50:35 +00:00
|
|
|
assert tags == {"key_name": "a_value"}
|
2017-07-20 22:00:30 +00:00
|
|
|
|
|
|
|
|
2021-10-06 09:24:59 +00:00
|
|
|
def create_load_balancer():
|
2017-07-20 22:00:30 +00:00
|
|
|
conn = boto3.client("elbv2", region_name="us-east-1")
|
|
|
|
ec2 = boto3.resource("ec2", region_name="us-east-1")
|
|
|
|
|
2017-10-02 19:36:47 +00:00
|
|
|
security_group = ec2.create_security_group(
|
|
|
|
GroupName="a-security-group", Description="First One"
|
|
|
|
)
|
2017-07-20 22:00:30 +00:00
|
|
|
vpc = ec2.create_vpc(CidrBlock="172.28.7.0/24", InstanceTenancy="default")
|
2017-10-02 19:36:47 +00:00
|
|
|
subnet1 = ec2.create_subnet(
|
|
|
|
VpcId=vpc.id, CidrBlock="172.28.7.192/26", AvailabilityZone="us-east-1a"
|
|
|
|
)
|
|
|
|
subnet2 = ec2.create_subnet(
|
|
|
|
VpcId=vpc.id, CidrBlock="172.28.7.0/26", AvailabilityZone="us-east-1b"
|
|
|
|
)
|
2017-07-20 22:00:30 +00:00
|
|
|
|
2021-10-06 09:24:59 +00:00
|
|
|
response = conn.create_load_balancer(
|
2017-07-20 22:00:30 +00:00
|
|
|
Name="my-lb",
|
|
|
|
Subnets=[subnet1.id, subnet2.id],
|
|
|
|
SecurityGroups=[security_group.id],
|
|
|
|
Scheme="internal",
|
|
|
|
Tags=[{"Key": "key_name", "Value": "a_value"}],
|
|
|
|
)
|
2021-10-06 09:24:59 +00:00
|
|
|
return response, vpc, security_group, subnet1, subnet2, conn
|
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2022-03-21 20:55:19 +00:00
|
|
|
def test_create_elb_using_subnetmapping():
|
|
|
|
region = "us-west-1"
|
|
|
|
conn = boto3.client("elbv2", region_name=region)
|
|
|
|
ec2 = boto3.resource("ec2", region_name=region)
|
|
|
|
|
|
|
|
security_group = ec2.create_security_group(
|
|
|
|
GroupName="a-security-group", Description="First One"
|
|
|
|
)
|
|
|
|
vpc = ec2.create_vpc(CidrBlock="172.28.7.0/24", InstanceTenancy="default")
|
|
|
|
subnet1 = ec2.create_subnet(
|
2022-10-02 13:03:03 +00:00
|
|
|
VpcId=vpc.id, CidrBlock="172.28.7.0/26", AvailabilityZone=region + "a"
|
2022-03-21 20:55:19 +00:00
|
|
|
)
|
|
|
|
subnet2 = ec2.create_subnet(
|
2022-10-02 13:03:03 +00:00
|
|
|
VpcId=vpc.id, CidrBlock="172.28.7.192/26", AvailabilityZone=region + "b"
|
2022-03-21 20:55:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
conn.create_load_balancer(
|
|
|
|
Name="my-lb",
|
|
|
|
SubnetMappings=[{"SubnetId": subnet1.id}, {"SubnetId": subnet2.id}],
|
|
|
|
SecurityGroups=[security_group.id],
|
|
|
|
Scheme="internal",
|
|
|
|
Tags=[{"Key": "key_name", "Value": "a_value"}],
|
|
|
|
)
|
|
|
|
|
|
|
|
lb = conn.describe_load_balancers()["LoadBalancers"][0]
|
2023-07-24 12:50:35 +00:00
|
|
|
assert len(lb["AvailabilityZones"]) == 2
|
|
|
|
assert {"ZoneName": "us-west-1a", "SubnetId": subnet1.id} in lb["AvailabilityZones"]
|
|
|
|
assert {"ZoneName": "us-west-1b", "SubnetId": subnet2.id} in lb["AvailabilityZones"]
|
2022-03-21 20:55:19 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2021-10-06 09:24:59 +00:00
|
|
|
def test_describe_load_balancers():
|
2021-10-18 19:44:29 +00:00
|
|
|
response, _, _, _, _, conn = create_load_balancer()
|
2017-07-20 22:00:30 +00:00
|
|
|
|
|
|
|
response = conn.describe_load_balancers()
|
|
|
|
|
2023-07-24 12:50:35 +00:00
|
|
|
assert len(response["LoadBalancers"]) == 1
|
|
|
|
lb = response["LoadBalancers"][0]
|
|
|
|
assert lb["LoadBalancerName"] == "my-lb"
|
|
|
|
assert lb["State"]["Code"] == "active"
|
2017-07-20 22:00:30 +00:00
|
|
|
|
2023-07-24 12:50:35 +00:00
|
|
|
response = conn.describe_load_balancers(LoadBalancerArns=[lb["LoadBalancerArn"]])
|
|
|
|
assert response["LoadBalancers"][0]["LoadBalancerName"] == "my-lb"
|
2017-07-20 22:00:30 +00:00
|
|
|
|
|
|
|
response = conn.describe_load_balancers(Names=["my-lb"])
|
2023-07-24 12:50:35 +00:00
|
|
|
assert response["LoadBalancers"][0]["LoadBalancerName"] == "my-lb"
|
2017-07-20 22:00:30 +00:00
|
|
|
|
2020-10-06 05:54:49 +00:00
|
|
|
with pytest.raises(ClientError):
|
2017-07-20 22:00:30 +00:00
|
|
|
conn.describe_load_balancers(LoadBalancerArns=["not-a/real/arn"])
|
2020-10-06 05:54:49 +00:00
|
|
|
with pytest.raises(ClientError):
|
2017-07-20 22:00:30 +00:00
|
|
|
conn.describe_load_balancers(Names=["nope"])
|
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2022-03-15 12:34:52 +00:00
|
|
|
def test_describe_listeners():
|
|
|
|
conn = boto3.client("elbv2", region_name="us-east-1")
|
|
|
|
|
|
|
|
with pytest.raises(ClientError) as exc:
|
|
|
|
conn.describe_listeners()
|
|
|
|
err = exc.value.response["Error"]
|
2023-07-24 12:50:35 +00:00
|
|
|
assert err["Code"] == "ValidationError"
|
|
|
|
assert (
|
|
|
|
err["Message"] == "You must specify either listener ARNs or a load balancer ARN"
|
2022-03-15 12:34:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2017-07-20 22:00:30 +00:00
|
|
|
def test_add_remove_tags():
|
2021-10-18 19:44:29 +00:00
|
|
|
_, _, _, _, _, conn = create_load_balancer()
|
2017-07-20 22:00:30 +00:00
|
|
|
|
|
|
|
lbs = conn.describe_load_balancers()["LoadBalancers"]
|
2023-07-24 12:50:35 +00:00
|
|
|
assert len(lbs) == 1
|
2017-07-20 22:00:30 +00:00
|
|
|
lb = lbs[0]
|
|
|
|
|
2020-10-06 05:54:49 +00:00
|
|
|
with pytest.raises(ClientError):
|
2017-07-20 22:00:30 +00:00
|
|
|
conn.add_tags(ResourceArns=["missing-arn"], Tags=[{"Key": "a", "Value": "b"}])
|
2019-10-31 15:44:26 +00:00
|
|
|
|
2017-07-20 22:00:30 +00:00
|
|
|
conn.add_tags(
|
2023-07-24 12:50:35 +00:00
|
|
|
ResourceArns=[lb["LoadBalancerArn"]], Tags=[{"Key": "a", "Value": "b"}]
|
2017-07-20 22:00:30 +00:00
|
|
|
)
|
2019-10-31 15:44:26 +00:00
|
|
|
|
2017-07-20 22:00:30 +00:00
|
|
|
tags = {
|
|
|
|
d["Key"]: d["Value"]
|
2023-07-24 12:50:35 +00:00
|
|
|
for d in conn.describe_tags(ResourceArns=[lb["LoadBalancerArn"]])[
|
2017-07-20 22:00:30 +00:00
|
|
|
"TagDescriptions"
|
|
|
|
][0]["Tags"]
|
|
|
|
}
|
2023-07-24 12:50:35 +00:00
|
|
|
assert tags["a"] == "b"
|
2019-10-31 15:44:26 +00:00
|
|
|
|
2017-07-20 22:00:30 +00:00
|
|
|
conn.add_tags(
|
2023-07-24 12:50:35 +00:00
|
|
|
ResourceArns=[lb["LoadBalancerArn"]],
|
2017-07-20 22:00:30 +00:00
|
|
|
Tags=[
|
|
|
|
{"Key": "a", "Value": "b"},
|
|
|
|
{"Key": "b", "Value": "b"},
|
|
|
|
{"Key": "c", "Value": "b"},
|
|
|
|
{"Key": "d", "Value": "b"},
|
|
|
|
{"Key": "e", "Value": "b"},
|
|
|
|
{"Key": "f", "Value": "b"},
|
|
|
|
{"Key": "g", "Value": "b"},
|
|
|
|
{"Key": "h", "Value": "b"},
|
|
|
|
{"Key": "j", "Value": "b"},
|
|
|
|
],
|
2019-10-31 15:44:26 +00:00
|
|
|
)
|
|
|
|
|
2022-03-23 20:44:12 +00:00
|
|
|
with pytest.raises(ClientError) as exc:
|
|
|
|
conn.add_tags(
|
2023-07-24 12:50:35 +00:00
|
|
|
ResourceArns=[lb["LoadBalancerArn"]], Tags=[{"Key": "k", "Value": "b"}]
|
2022-03-23 20:44:12 +00:00
|
|
|
)
|
|
|
|
err = exc.value.response["Error"]
|
2023-07-24 12:50:35 +00:00
|
|
|
assert err["Code"] == "TooManyTagsError"
|
2019-10-31 15:44:26 +00:00
|
|
|
|
2017-07-20 22:00:30 +00:00
|
|
|
conn.add_tags(
|
2023-07-24 12:50:35 +00:00
|
|
|
ResourceArns=[lb["LoadBalancerArn"]], Tags=[{"Key": "j", "Value": "c"}]
|
2019-10-31 15:44:26 +00:00
|
|
|
)
|
|
|
|
|
2017-07-20 22:00:30 +00:00
|
|
|
tags = {
|
|
|
|
d["Key"]: d["Value"]
|
2023-07-24 12:50:35 +00:00
|
|
|
for d in conn.describe_tags(ResourceArns=[lb["LoadBalancerArn"]])[
|
2017-07-20 22:00:30 +00:00
|
|
|
"TagDescriptions"
|
|
|
|
][0]["Tags"]
|
|
|
|
}
|
2019-10-31 15:44:26 +00:00
|
|
|
|
2023-07-24 12:50:35 +00:00
|
|
|
assert tags["a"] == "b"
|
|
|
|
assert tags["b"] == "b"
|
|
|
|
assert tags["c"] == "b"
|
|
|
|
assert tags["d"] == "b"
|
|
|
|
assert tags["e"] == "b"
|
|
|
|
assert tags["f"] == "b"
|
|
|
|
assert tags["g"] == "b"
|
|
|
|
assert tags["h"] == "b"
|
|
|
|
assert tags["j"] == "c"
|
|
|
|
assert "k" not in tags
|
2019-10-31 15:44:26 +00:00
|
|
|
|
2023-07-24 12:50:35 +00:00
|
|
|
conn.remove_tags(ResourceArns=[lb["LoadBalancerArn"]], TagKeys=["a"])
|
2019-10-31 15:44:26 +00:00
|
|
|
|
2017-07-20 22:00:30 +00:00
|
|
|
tags = {
|
|
|
|
d["Key"]: d["Value"]
|
2023-07-24 12:50:35 +00:00
|
|
|
for d in conn.describe_tags(ResourceArns=[lb["LoadBalancerArn"]])[
|
2017-07-20 22:00:30 +00:00
|
|
|
"TagDescriptions"
|
|
|
|
][0]["Tags"]
|
|
|
|
}
|
2019-10-31 15:44:26 +00:00
|
|
|
|
2023-07-24 12:50:35 +00:00
|
|
|
assert "a" not in tags
|
|
|
|
assert tags["b"] == "b"
|
|
|
|
assert tags["c"] == "b"
|
|
|
|
assert tags["d"] == "b"
|
|
|
|
assert tags["e"] == "b"
|
|
|
|
assert tags["f"] == "b"
|
|
|
|
assert tags["g"] == "b"
|
|
|
|
assert tags["h"] == "b"
|
|
|
|
assert tags["j"] == "c"
|
2017-07-20 22:00:30 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2017-07-20 22:00:30 +00:00
|
|
|
def test_create_elb_in_multiple_region():
|
|
|
|
for region in ["us-west-1", "us-west-2"]:
|
|
|
|
conn = boto3.client("elbv2", region_name=region)
|
|
|
|
ec2 = boto3.resource("ec2", region_name=region)
|
|
|
|
|
2017-10-02 19:36:47 +00:00
|
|
|
security_group = ec2.create_security_group(
|
|
|
|
GroupName="a-security-group", Description="First One"
|
|
|
|
)
|
|
|
|
vpc = ec2.create_vpc(CidrBlock="172.28.7.0/24", InstanceTenancy="default")
|
|
|
|
subnet1 = ec2.create_subnet(
|
2022-10-02 13:03:03 +00:00
|
|
|
VpcId=vpc.id, CidrBlock="172.28.7.0/26", AvailabilityZone=region + "a"
|
2017-10-02 19:36:47 +00:00
|
|
|
)
|
|
|
|
subnet2 = ec2.create_subnet(
|
2022-10-02 13:03:03 +00:00
|
|
|
VpcId=vpc.id, CidrBlock="172.28.7.192/26", AvailabilityZone=region + "b"
|
2017-10-02 19:36:47 +00:00
|
|
|
)
|
2017-07-20 22:00:30 +00:00
|
|
|
|
|
|
|
conn.create_load_balancer(
|
|
|
|
Name="my-lb",
|
|
|
|
Subnets=[subnet1.id, subnet2.id],
|
|
|
|
SecurityGroups=[security_group.id],
|
|
|
|
Scheme="internal",
|
|
|
|
Tags=[{"Key": "key_name", "Value": "a_value"}],
|
|
|
|
)
|
|
|
|
|
2023-07-24 12:50:35 +00:00
|
|
|
west_1_lbs = boto3.client("elbv2", "us-west-1").describe_load_balancers()
|
|
|
|
assert len(west_1_lbs["LoadBalancers"]) == 1
|
|
|
|
west_2_lbs = boto3.client("elbv2", "us-west-2").describe_load_balancers()
|
|
|
|
assert len(west_2_lbs["LoadBalancers"]) == 1
|
2017-07-20 22:00:30 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2021-10-06 09:24:59 +00:00
|
|
|
def test_create_listeners_without_port():
|
2021-10-18 19:44:29 +00:00
|
|
|
response, vpc, _, _, _, conn = create_load_balancer()
|
2023-07-24 12:50:35 +00:00
|
|
|
load_balancer_arn = response["LoadBalancers"][0]["LoadBalancerArn"]
|
2021-10-06 09:24:59 +00:00
|
|
|
response = conn.create_target_group(
|
|
|
|
Name="a-target",
|
|
|
|
Protocol="HTTP",
|
|
|
|
Port=8080,
|
|
|
|
VpcId=vpc.id,
|
|
|
|
HealthCheckProtocol="HTTP",
|
|
|
|
HealthCheckPort="8080",
|
|
|
|
HealthCheckPath="/",
|
|
|
|
HealthCheckIntervalSeconds=5,
|
2023-09-14 12:52:14 +00:00
|
|
|
HealthCheckTimeoutSeconds=3,
|
2021-10-06 09:24:59 +00:00
|
|
|
HealthyThresholdCount=5,
|
|
|
|
UnhealthyThresholdCount=2,
|
|
|
|
Matcher={"HttpCode": "200"},
|
2017-10-02 19:36:47 +00:00
|
|
|
)
|
2023-07-24 12:50:35 +00:00
|
|
|
target_group = response["TargetGroups"][0]
|
2021-10-06 09:24:59 +00:00
|
|
|
target_group_arn = target_group["TargetGroupArn"]
|
|
|
|
response = conn.create_listener(
|
|
|
|
LoadBalancerArn=load_balancer_arn,
|
|
|
|
Protocol="HTTP",
|
|
|
|
DefaultActions=[{"Type": "forward", "TargetGroupArn": target_group_arn}],
|
2017-10-02 19:36:47 +00:00
|
|
|
)
|
2017-07-20 22:00:30 +00:00
|
|
|
|
2023-07-24 12:50:35 +00:00
|
|
|
listener = response["Listeners"][0]
|
|
|
|
assert listener.get("Port") is None
|
|
|
|
assert listener["Protocol"] == "HTTP"
|
|
|
|
assert listener["DefaultActions"] == [
|
|
|
|
{"TargetGroupArn": target_group_arn, "Type": "forward"}
|
|
|
|
]
|
2017-07-20 22:00:30 +00:00
|
|
|
|
2021-10-06 09:24:59 +00:00
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2021-10-11 21:12:38 +00:00
|
|
|
def test_create_rule_forward_config_as_second_arg():
|
2023-01-07 11:35:14 +00:00
|
|
|
# https://github.com/getmoto/moto/issues/4123
|
2021-10-11 21:12:38 +00:00
|
|
|
# Necessary because there was some convoluted way of parsing arguments
|
|
|
|
# Actions with type=forward had to be the first action specified
|
2021-10-18 19:44:29 +00:00
|
|
|
response, vpc, _, _, _, elbv2 = create_load_balancer()
|
2021-10-11 21:12:38 +00:00
|
|
|
|
2023-07-24 12:50:35 +00:00
|
|
|
load_balancer_arn = response["LoadBalancers"][0]["LoadBalancerArn"]
|
2021-10-11 21:12:38 +00:00
|
|
|
|
|
|
|
response = elbv2.create_listener(
|
|
|
|
LoadBalancerArn=load_balancer_arn, Protocol="HTTP", Port=80, DefaultActions=[]
|
|
|
|
)
|
2023-07-24 12:50:35 +00:00
|
|
|
http_listener_arn = response["Listeners"][0]["ListenerArn"]
|
2021-10-11 21:12:38 +00:00
|
|
|
|
|
|
|
priority = 100
|
|
|
|
|
|
|
|
response = elbv2.create_target_group(
|
|
|
|
Name="a-target",
|
|
|
|
Protocol="HTTP",
|
|
|
|
Port=8080,
|
|
|
|
VpcId=vpc.id,
|
|
|
|
HealthCheckProtocol="HTTP",
|
|
|
|
HealthCheckPort="8080",
|
|
|
|
HealthCheckPath="/",
|
|
|
|
Matcher={"HttpCode": "200"},
|
|
|
|
)
|
2023-07-24 12:50:35 +00:00
|
|
|
target_group = response["TargetGroups"][0]
|
2021-10-11 21:12:38 +00:00
|
|
|
|
|
|
|
# No targets registered yet
|
2023-07-24 12:50:35 +00:00
|
|
|
target_group_arn = target_group["TargetGroupArn"]
|
2021-10-11 21:12:38 +00:00
|
|
|
elbv2.create_rule(
|
|
|
|
ListenerArn=http_listener_arn,
|
|
|
|
Conditions=[
|
2022-03-23 20:44:12 +00:00
|
|
|
{"Field": "path-pattern", "PathPatternConfig": {"Values": ["/sth*"]}}
|
2021-10-11 21:12:38 +00:00
|
|
|
],
|
|
|
|
Priority=priority,
|
|
|
|
Actions=[
|
|
|
|
{
|
|
|
|
"Type": "authenticate-cognito",
|
|
|
|
"Order": 1,
|
|
|
|
"AuthenticateCognitoConfig": {
|
|
|
|
"UserPoolArn": "?1",
|
|
|
|
"UserPoolClientId": "?2",
|
|
|
|
"UserPoolDomain": "?2",
|
|
|
|
"SessionCookieName": "AWSELBAuthSessionCookie",
|
|
|
|
"Scope": "openid",
|
|
|
|
"SessionTimeout": 604800,
|
|
|
|
"OnUnauthenticatedRequest": "authenticate",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Type": "forward",
|
|
|
|
"Order": 2,
|
|
|
|
"ForwardConfig": {
|
|
|
|
"TargetGroups": [
|
|
|
|
{"TargetGroupArn": target_group_arn, "Weight": 1},
|
|
|
|
],
|
|
|
|
"TargetGroupStickinessConfig": {"Enabled": False},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
)
|
|
|
|
all_rules = elbv2.describe_rules(ListenerArn=http_listener_arn)["Rules"]
|
|
|
|
our_rule = all_rules[0]
|
|
|
|
actions = our_rule["Actions"]
|
|
|
|
forward_action = [a for a in actions if "ForwardConfig" in a.keys()][0]
|
2023-07-24 12:50:35 +00:00
|
|
|
assert forward_action == {
|
|
|
|
"ForwardConfig": {
|
|
|
|
"TargetGroups": [{"TargetGroupArn": target_group_arn, "Weight": 1}],
|
|
|
|
"TargetGroupStickinessConfig": {"Enabled": False},
|
|
|
|
},
|
|
|
|
"Type": "forward",
|
|
|
|
"Order": 2,
|
|
|
|
}
|
2021-10-11 21:12:38 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2017-07-20 22:00:30 +00:00
|
|
|
def test_describe_paginated_balancers():
|
|
|
|
conn = boto3.client("elbv2", region_name="us-east-1")
|
|
|
|
ec2 = boto3.resource("ec2", region_name="us-east-1")
|
|
|
|
|
2017-10-02 19:36:47 +00:00
|
|
|
security_group = ec2.create_security_group(
|
|
|
|
GroupName="a-security-group", Description="First One"
|
|
|
|
)
|
2017-07-20 22:00:30 +00:00
|
|
|
vpc = ec2.create_vpc(CidrBlock="172.28.7.0/24", InstanceTenancy="default")
|
2017-10-02 19:36:47 +00:00
|
|
|
subnet1 = ec2.create_subnet(
|
|
|
|
VpcId=vpc.id, CidrBlock="172.28.7.192/26", AvailabilityZone="us-east-1a"
|
|
|
|
)
|
|
|
|
subnet2 = ec2.create_subnet(
|
|
|
|
VpcId=vpc.id, CidrBlock="172.28.7.0/26", AvailabilityZone="us-east-1b"
|
|
|
|
)
|
2017-07-20 22:00:30 +00:00
|
|
|
|
|
|
|
for i in range(51):
|
|
|
|
conn.create_load_balancer(
|
2022-11-17 22:41:08 +00:00
|
|
|
Name=f"my-lb{i}",
|
2017-07-20 22:00:30 +00:00
|
|
|
Subnets=[subnet1.id, subnet2.id],
|
|
|
|
SecurityGroups=[security_group.id],
|
|
|
|
Scheme="internal",
|
|
|
|
Tags=[{"Key": "key_name", "Value": "a_value"}],
|
|
|
|
)
|
|
|
|
|
|
|
|
resp = conn.describe_load_balancers()
|
2023-07-24 12:50:35 +00:00
|
|
|
assert len(resp["LoadBalancers"]) == 50
|
|
|
|
assert resp["NextMarker"] == resp["LoadBalancers"][-1]["LoadBalancerName"]
|
2017-07-20 22:00:30 +00:00
|
|
|
resp2 = conn.describe_load_balancers(Marker=resp["NextMarker"])
|
2023-07-24 12:50:35 +00:00
|
|
|
assert len(resp2["LoadBalancers"]) == 1
|
2017-07-20 22:00:30 +00:00
|
|
|
assert "NextToken" not in resp2.keys()
|
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2017-07-20 22:00:30 +00:00
|
|
|
def test_delete_load_balancer():
|
2021-10-18 19:44:29 +00:00
|
|
|
response, _, _, _, _, conn = create_load_balancer()
|
2017-07-20 22:00:30 +00:00
|
|
|
|
2023-07-24 12:50:35 +00:00
|
|
|
assert len(response["LoadBalancers"]) == 1
|
|
|
|
lb = response["LoadBalancers"][0]
|
2017-07-20 22:00:30 +00:00
|
|
|
|
2023-07-24 12:50:35 +00:00
|
|
|
conn.delete_load_balancer(LoadBalancerArn=lb["LoadBalancerArn"])
|
|
|
|
balancers = conn.describe_load_balancers()["LoadBalancers"]
|
|
|
|
assert len(balancers) == 0
|
2017-07-20 22:00:30 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2017-07-20 22:00:30 +00:00
|
|
|
def test_register_targets():
|
|
|
|
conn = boto3.client("elbv2", region_name="us-east-1")
|
|
|
|
ec2 = boto3.resource("ec2", region_name="us-east-1")
|
|
|
|
|
2017-10-02 19:36:47 +00:00
|
|
|
security_group = ec2.create_security_group(
|
|
|
|
GroupName="a-security-group", Description="First One"
|
|
|
|
)
|
2017-07-20 22:00:30 +00:00
|
|
|
vpc = ec2.create_vpc(CidrBlock="172.28.7.0/24", InstanceTenancy="default")
|
2017-10-02 19:36:47 +00:00
|
|
|
subnet1 = ec2.create_subnet(
|
|
|
|
VpcId=vpc.id, CidrBlock="172.28.7.192/26", AvailabilityZone="us-east-1a"
|
|
|
|
)
|
|
|
|
subnet2 = ec2.create_subnet(
|
|
|
|
VpcId=vpc.id, CidrBlock="172.28.7.0/26", AvailabilityZone="us-east-1b"
|
|
|
|
)
|
2017-07-20 22:00:30 +00:00
|
|
|
|
|
|
|
conn.create_load_balancer(
|
|
|
|
Name="my-lb",
|
|
|
|
Subnets=[subnet1.id, subnet2.id],
|
|
|
|
SecurityGroups=[security_group.id],
|
|
|
|
Scheme="internal",
|
|
|
|
Tags=[{"Key": "key_name", "Value": "a_value"}],
|
|
|
|
)
|
|
|
|
|
|
|
|
response = conn.create_target_group(
|
|
|
|
Name="a-target",
|
|
|
|
Protocol="HTTP",
|
|
|
|
Port=8080,
|
|
|
|
VpcId=vpc.id,
|
|
|
|
HealthCheckProtocol="HTTP",
|
|
|
|
HealthCheckPort="8080",
|
|
|
|
HealthCheckPath="/",
|
|
|
|
HealthCheckIntervalSeconds=5,
|
2023-09-14 12:52:14 +00:00
|
|
|
HealthCheckTimeoutSeconds=3,
|
2017-07-20 22:00:30 +00:00
|
|
|
HealthyThresholdCount=5,
|
|
|
|
UnhealthyThresholdCount=2,
|
|
|
|
Matcher={"HttpCode": "200"},
|
|
|
|
)
|
2023-07-24 12:50:35 +00:00
|
|
|
target_group = response["TargetGroups"][0]
|
2017-07-20 22:00:30 +00:00
|
|
|
|
|
|
|
# No targets registered yet
|
2017-10-02 19:36:47 +00:00
|
|
|
response = conn.describe_target_health(
|
2023-07-24 12:50:35 +00:00
|
|
|
TargetGroupArn=target_group["TargetGroupArn"]
|
2019-10-31 15:44:26 +00:00
|
|
|
)
|
2023-07-24 12:50:35 +00:00
|
|
|
assert len(response["TargetHealthDescriptions"]) == 0
|
2017-07-20 22:00:30 +00:00
|
|
|
|
2021-01-29 11:31:56 +00:00
|
|
|
response = ec2.create_instances(ImageId=EXAMPLE_AMI_ID, MinCount=2, MaxCount=2)
|
2017-07-20 22:00:30 +00:00
|
|
|
instance_id1 = response[0].id
|
|
|
|
instance_id2 = response[1].id
|
|
|
|
|
|
|
|
response = conn.register_targets(
|
2023-07-24 12:50:35 +00:00
|
|
|
TargetGroupArn=target_group["TargetGroupArn"],
|
2017-07-20 22:00:30 +00:00
|
|
|
Targets=[
|
|
|
|
{"Id": instance_id1, "Port": 5060},
|
|
|
|
{"Id": instance_id2, "Port": 4030},
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
2017-10-02 19:36:47 +00:00
|
|
|
response = conn.describe_target_health(
|
2023-07-24 12:50:35 +00:00
|
|
|
TargetGroupArn=target_group["TargetGroupArn"]
|
2019-10-31 15:44:26 +00:00
|
|
|
)
|
2023-07-24 12:50:35 +00:00
|
|
|
assert len(response["TargetHealthDescriptions"]) == 2
|
2017-07-20 22:00:30 +00:00
|
|
|
|
|
|
|
response = conn.deregister_targets(
|
2023-07-24 12:50:35 +00:00
|
|
|
TargetGroupArn=target_group["TargetGroupArn"],
|
2017-07-20 22:00:30 +00:00
|
|
|
Targets=[{"Id": instance_id2}],
|
|
|
|
)
|
|
|
|
|
2017-10-02 19:36:47 +00:00
|
|
|
response = conn.describe_target_health(
|
2023-07-24 12:50:35 +00:00
|
|
|
TargetGroupArn=target_group["TargetGroupArn"]
|
2019-10-31 15:44:26 +00:00
|
|
|
)
|
2023-07-24 12:50:35 +00:00
|
|
|
assert len(response["TargetHealthDescriptions"]) == 1
|
2017-08-02 22:57:15 +00:00
|
|
|
|
2023-04-17 16:43:14 +00:00
|
|
|
def get_target_by_instance_id(instance_id):
|
2023-07-24 12:50:35 +00:00
|
|
|
for target in response["TargetHealthDescriptions"]:
|
|
|
|
if target["Target"]["Id"] == instance_id:
|
2023-04-17 16:43:14 +00:00
|
|
|
return target
|
|
|
|
return None
|
|
|
|
|
|
|
|
def assert_target_not_registered(target):
|
2023-07-24 12:50:35 +00:00
|
|
|
assert target["TargetHealth"]["State"] == "unavailable"
|
|
|
|
assert target["TargetHealth"]["Reason"] == "Target.NotRegistered"
|
2023-04-17 16:43:14 +00:00
|
|
|
|
|
|
|
response = conn.describe_target_health(
|
2023-07-24 12:50:35 +00:00
|
|
|
TargetGroupArn=target_group["TargetGroupArn"],
|
2023-04-17 16:43:14 +00:00
|
|
|
Targets=[{"Id": instance_id2}],
|
|
|
|
)
|
2023-07-24 12:50:35 +00:00
|
|
|
assert len(response["TargetHealthDescriptions"]) == 1
|
2023-04-17 16:43:14 +00:00
|
|
|
target_default_port = get_target_by_instance_id(instance_id2)
|
|
|
|
assert target_default_port is not None
|
2023-07-24 12:50:35 +00:00
|
|
|
assert target_default_port["Target"]["Port"] == 8080
|
2023-04-17 16:43:14 +00:00
|
|
|
assert_target_not_registered(target_default_port)
|
|
|
|
|
|
|
|
response = conn.describe_target_health(
|
2023-07-24 12:50:35 +00:00
|
|
|
TargetGroupArn=target_group["TargetGroupArn"],
|
2023-04-17 16:43:14 +00:00
|
|
|
Targets=[{"Id": instance_id2, "Port": 4030}],
|
|
|
|
)
|
2023-07-24 12:50:35 +00:00
|
|
|
assert len(response["TargetHealthDescriptions"]) == 1
|
2023-04-17 16:43:14 +00:00
|
|
|
target_custom_port = get_target_by_instance_id(instance_id2)
|
|
|
|
assert target_custom_port is not None
|
2023-07-24 12:50:35 +00:00
|
|
|
assert target_custom_port["Target"]["Port"] == 4030
|
2023-04-17 16:43:14 +00:00
|
|
|
assert_target_not_registered(target_custom_port)
|
|
|
|
|
2017-08-02 22:57:15 +00:00
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2019-07-18 14:25:41 +00:00
|
|
|
def test_stopped_instance_target():
|
|
|
|
target_group_port = 8080
|
|
|
|
|
|
|
|
conn = boto3.client("elbv2", region_name="us-east-1")
|
|
|
|
ec2 = boto3.resource("ec2", region_name="us-east-1")
|
|
|
|
|
|
|
|
security_group = ec2.create_security_group(
|
|
|
|
GroupName="a-security-group", Description="First One"
|
|
|
|
)
|
|
|
|
vpc = ec2.create_vpc(CidrBlock="172.28.7.0/24", InstanceTenancy="default")
|
|
|
|
subnet1 = ec2.create_subnet(
|
|
|
|
VpcId=vpc.id, CidrBlock="172.28.7.192/26", AvailabilityZone="us-east-1a"
|
|
|
|
)
|
|
|
|
subnet2 = ec2.create_subnet(
|
|
|
|
VpcId=vpc.id, CidrBlock="172.28.7.0/26", AvailabilityZone="us-east-1b"
|
|
|
|
)
|
|
|
|
|
|
|
|
conn.create_load_balancer(
|
|
|
|
Name="my-lb",
|
|
|
|
Subnets=[subnet1.id, subnet2.id],
|
|
|
|
SecurityGroups=[security_group.id],
|
|
|
|
Scheme="internal",
|
|
|
|
Tags=[{"Key": "key_name", "Value": "a_value"}],
|
|
|
|
)
|
|
|
|
|
|
|
|
response = conn.create_target_group(
|
|
|
|
Name="a-target",
|
|
|
|
Protocol="HTTP",
|
|
|
|
Port=target_group_port,
|
|
|
|
VpcId=vpc.id,
|
|
|
|
HealthCheckProtocol="HTTP",
|
|
|
|
HealthCheckPath="/",
|
|
|
|
HealthCheckIntervalSeconds=5,
|
2023-09-14 12:52:14 +00:00
|
|
|
HealthCheckTimeoutSeconds=3,
|
2019-07-18 14:25:41 +00:00
|
|
|
HealthyThresholdCount=5,
|
|
|
|
UnhealthyThresholdCount=2,
|
|
|
|
Matcher={"HttpCode": "200"},
|
|
|
|
)
|
2023-07-24 12:50:35 +00:00
|
|
|
target_group = response["TargetGroups"][0]
|
2019-07-18 14:25:41 +00:00
|
|
|
|
|
|
|
# No targets registered yet
|
|
|
|
response = conn.describe_target_health(
|
2023-07-24 12:50:35 +00:00
|
|
|
TargetGroupArn=target_group["TargetGroupArn"]
|
2019-10-31 15:44:26 +00:00
|
|
|
)
|
2023-07-24 12:50:35 +00:00
|
|
|
assert len(response["TargetHealthDescriptions"]) == 0
|
2019-07-18 14:25:41 +00:00
|
|
|
|
2021-01-29 11:31:56 +00:00
|
|
|
response = ec2.create_instances(ImageId=EXAMPLE_AMI_ID, MinCount=1, MaxCount=1)
|
2019-07-18 14:25:41 +00:00
|
|
|
instance = response[0]
|
|
|
|
|
|
|
|
target_dict = {"Id": instance.id, "Port": 500}
|
|
|
|
|
|
|
|
response = conn.register_targets(
|
2023-07-24 12:50:35 +00:00
|
|
|
TargetGroupArn=target_group["TargetGroupArn"], Targets=[target_dict]
|
2019-07-18 14:25:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
response = conn.describe_target_health(
|
2023-07-24 12:50:35 +00:00
|
|
|
TargetGroupArn=target_group["TargetGroupArn"]
|
2019-10-31 15:44:26 +00:00
|
|
|
)
|
2023-07-24 12:50:35 +00:00
|
|
|
assert len(response["TargetHealthDescriptions"]) == 1
|
|
|
|
target_health_description = response["TargetHealthDescriptions"][0]
|
2019-07-18 14:25:41 +00:00
|
|
|
|
2023-07-24 12:50:35 +00:00
|
|
|
assert target_health_description["Target"] == target_dict
|
2023-09-14 12:52:14 +00:00
|
|
|
assert target_health_description["HealthCheckPort"] == "traffic-port"
|
2023-07-24 12:50:35 +00:00
|
|
|
assert target_health_description["TargetHealth"] == {"State": "healthy"}
|
2019-07-18 14:25:41 +00:00
|
|
|
|
|
|
|
instance.stop()
|
|
|
|
|
|
|
|
response = conn.describe_target_health(
|
2023-07-24 12:50:35 +00:00
|
|
|
TargetGroupArn=target_group["TargetGroupArn"]
|
|
|
|
)
|
|
|
|
assert len(response["TargetHealthDescriptions"]) == 1
|
|
|
|
target_health_description = response["TargetHealthDescriptions"][0]
|
|
|
|
assert target_health_description["Target"] == target_dict
|
2023-09-14 12:52:14 +00:00
|
|
|
assert target_health_description["HealthCheckPort"] == "traffic-port"
|
2023-07-24 12:50:35 +00:00
|
|
|
assert target_health_description["TargetHealth"] == {
|
|
|
|
"State": "unused",
|
|
|
|
"Reason": "Target.InvalidState",
|
|
|
|
"Description": "Target is in the stopped state",
|
|
|
|
}
|
2019-07-18 14:25:41 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2019-08-30 16:21:11 +00:00
|
|
|
def test_terminated_instance_target():
|
|
|
|
target_group_port = 8080
|
|
|
|
|
|
|
|
conn = boto3.client("elbv2", region_name="us-east-1")
|
|
|
|
ec2 = boto3.resource("ec2", region_name="us-east-1")
|
|
|
|
|
|
|
|
security_group = ec2.create_security_group(
|
|
|
|
GroupName="a-security-group", Description="First One"
|
|
|
|
)
|
|
|
|
vpc = ec2.create_vpc(CidrBlock="172.28.7.0/24", InstanceTenancy="default")
|
|
|
|
subnet1 = ec2.create_subnet(
|
|
|
|
VpcId=vpc.id, CidrBlock="172.28.7.192/26", AvailabilityZone="us-east-1a"
|
|
|
|
)
|
|
|
|
subnet2 = ec2.create_subnet(
|
|
|
|
VpcId=vpc.id, CidrBlock="172.28.7.0/26", AvailabilityZone="us-east-1b"
|
|
|
|
)
|
|
|
|
|
|
|
|
conn.create_load_balancer(
|
|
|
|
Name="my-lb",
|
|
|
|
Subnets=[subnet1.id, subnet2.id],
|
|
|
|
SecurityGroups=[security_group.id],
|
|
|
|
Scheme="internal",
|
|
|
|
Tags=[{"Key": "key_name", "Value": "a_value"}],
|
|
|
|
)
|
|
|
|
|
|
|
|
response = conn.create_target_group(
|
|
|
|
Name="a-target",
|
|
|
|
Protocol="HTTP",
|
|
|
|
Port=target_group_port,
|
|
|
|
VpcId=vpc.id,
|
|
|
|
HealthCheckProtocol="HTTP",
|
|
|
|
HealthCheckPath="/",
|
|
|
|
HealthCheckIntervalSeconds=5,
|
2023-09-14 12:52:14 +00:00
|
|
|
HealthCheckTimeoutSeconds=3,
|
2019-08-30 16:21:11 +00:00
|
|
|
HealthyThresholdCount=5,
|
|
|
|
UnhealthyThresholdCount=2,
|
|
|
|
Matcher={"HttpCode": "200"},
|
|
|
|
)
|
2023-07-24 12:50:35 +00:00
|
|
|
target_group = response["TargetGroups"][0]
|
2019-08-30 16:21:11 +00:00
|
|
|
|
|
|
|
# No targets registered yet
|
|
|
|
response = conn.describe_target_health(
|
2023-07-24 12:50:35 +00:00
|
|
|
TargetGroupArn=target_group["TargetGroupArn"]
|
2019-10-31 15:44:26 +00:00
|
|
|
)
|
2023-07-24 12:50:35 +00:00
|
|
|
assert len(response["TargetHealthDescriptions"]) == 0
|
2019-08-30 16:21:11 +00:00
|
|
|
|
2021-01-29 11:31:56 +00:00
|
|
|
response = ec2.create_instances(ImageId=EXAMPLE_AMI_ID, MinCount=1, MaxCount=1)
|
2019-08-30 16:21:11 +00:00
|
|
|
instance = response[0]
|
|
|
|
|
|
|
|
target_dict = {"Id": instance.id, "Port": 500}
|
|
|
|
|
|
|
|
response = conn.register_targets(
|
2023-07-24 12:50:35 +00:00
|
|
|
TargetGroupArn=target_group["TargetGroupArn"], Targets=[target_dict]
|
2019-08-30 16:21:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
response = conn.describe_target_health(
|
2023-07-24 12:50:35 +00:00
|
|
|
TargetGroupArn=target_group["TargetGroupArn"]
|
2019-10-31 15:44:26 +00:00
|
|
|
)
|
2023-07-24 12:50:35 +00:00
|
|
|
assert len(response["TargetHealthDescriptions"]) == 1
|
|
|
|
target_health_description = response["TargetHealthDescriptions"][0]
|
2019-08-30 16:21:11 +00:00
|
|
|
|
2023-07-24 12:50:35 +00:00
|
|
|
assert target_health_description["Target"] == target_dict
|
2023-09-14 12:52:14 +00:00
|
|
|
assert target_health_description["HealthCheckPort"] == "traffic-port"
|
2023-07-24 12:50:35 +00:00
|
|
|
assert target_health_description["TargetHealth"] == {"State": "healthy"}
|
2019-08-30 16:21:11 +00:00
|
|
|
|
|
|
|
instance.terminate()
|
|
|
|
|
|
|
|
response = conn.describe_target_health(
|
2023-07-24 12:50:35 +00:00
|
|
|
TargetGroupArn=target_group["TargetGroupArn"]
|
2019-10-31 15:44:26 +00:00
|
|
|
)
|
2023-07-24 12:50:35 +00:00
|
|
|
assert len(response["TargetHealthDescriptions"]) == 0
|
2019-08-30 16:21:11 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2021-06-09 15:34:53 +00:00
|
|
|
def test_create_rule_priority_in_use():
|
2021-10-18 19:44:29 +00:00
|
|
|
response, _, _, _, _, elbv2 = create_load_balancer()
|
2021-06-09 15:34:53 +00:00
|
|
|
|
2023-07-24 12:50:35 +00:00
|
|
|
load_balancer_arn = response["LoadBalancers"][0]["LoadBalancerArn"]
|
2021-06-09 15:34:53 +00:00
|
|
|
|
|
|
|
response = elbv2.create_listener(
|
|
|
|
LoadBalancerArn=load_balancer_arn, Protocol="HTTP", Port=80, DefaultActions=[]
|
|
|
|
)
|
2023-07-24 12:50:35 +00:00
|
|
|
http_listener_arn = response["Listeners"][0]["ListenerArn"]
|
2021-06-09 15:34:53 +00:00
|
|
|
|
|
|
|
priority = 100
|
|
|
|
elbv2.create_rule(
|
|
|
|
ListenerArn=http_listener_arn, Priority=priority, Conditions=[], Actions=[]
|
|
|
|
)
|
|
|
|
|
|
|
|
# test for PriorityInUse
|
|
|
|
with pytest.raises(ClientError) as ex:
|
|
|
|
elbv2.create_rule(
|
|
|
|
ListenerArn=http_listener_arn, Priority=priority, Conditions=[], Actions=[]
|
|
|
|
)
|
|
|
|
err = ex.value.response["Error"]
|
2023-07-24 12:50:35 +00:00
|
|
|
assert err["Code"] == "PriorityInUse"
|
|
|
|
assert err["Message"] == "The specified priority is in use."
|
2021-06-09 15:34:53 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2021-12-12 21:10:15 +00:00
|
|
|
def test_modify_rule_conditions():
|
|
|
|
response, _, _, _, _, elbv2 = create_load_balancer()
|
2023-07-24 12:50:35 +00:00
|
|
|
load_balancer_arn = response["LoadBalancers"][0]["LoadBalancerArn"]
|
2021-12-12 21:10:15 +00:00
|
|
|
|
|
|
|
action = {
|
|
|
|
"Type": "redirect",
|
|
|
|
"RedirectConfig": {
|
|
|
|
"Protocol": "HTTPS",
|
|
|
|
"Port": "443",
|
|
|
|
"StatusCode": "HTTP_301",
|
|
|
|
},
|
|
|
|
}
|
2022-03-23 20:44:12 +00:00
|
|
|
condition = {"Field": "path-pattern", "PathPatternConfig": {"Values": ["/sth*"]}}
|
2021-12-12 21:10:15 +00:00
|
|
|
|
|
|
|
response = elbv2.create_listener(
|
|
|
|
LoadBalancerArn=load_balancer_arn,
|
|
|
|
Protocol="HTTP",
|
|
|
|
Port=80,
|
|
|
|
DefaultActions=[action],
|
|
|
|
)
|
2023-07-24 12:50:35 +00:00
|
|
|
http_listener_arn = response["Listeners"][0]["ListenerArn"]
|
2021-12-12 21:10:15 +00:00
|
|
|
|
|
|
|
response = elbv2.create_rule(
|
|
|
|
ListenerArn=http_listener_arn, Priority=100, Conditions=[], Actions=[]
|
|
|
|
)
|
|
|
|
rule = response["Rules"][0]
|
|
|
|
|
|
|
|
assert len(rule["Actions"]) == 0
|
|
|
|
assert len(rule["Conditions"]) == 0
|
|
|
|
|
|
|
|
response = elbv2.modify_rule(RuleArn=rule["RuleArn"], Actions=[action])
|
|
|
|
rule = response["Rules"][0]
|
|
|
|
|
|
|
|
assert len(rule["Actions"]) == 1
|
|
|
|
assert len(rule["Conditions"]) == 0
|
|
|
|
|
|
|
|
response = elbv2.modify_rule(RuleArn=rule["RuleArn"], Conditions=[condition])
|
|
|
|
rule = response["Rules"][0]
|
|
|
|
|
|
|
|
assert len(rule["Actions"]) == 1
|
|
|
|
assert len(rule["Conditions"]) == 1
|
|
|
|
|
|
|
|
response = elbv2.modify_rule(
|
|
|
|
RuleArn=rule["RuleArn"],
|
|
|
|
Conditions=[condition, condition],
|
|
|
|
Actions=[action, action],
|
|
|
|
)
|
|
|
|
rule = response["Rules"][0]
|
|
|
|
|
|
|
|
assert len(rule["Actions"]) == 2
|
|
|
|
assert len(rule["Conditions"]) == 2
|
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2017-08-16 19:28:32 +00:00
|
|
|
def test_handle_listener_rules():
|
2021-10-18 19:44:29 +00:00
|
|
|
response, vpc, _, _, _, conn = create_load_balancer()
|
2017-08-16 12:09:14 +00:00
|
|
|
|
2023-07-24 12:50:35 +00:00
|
|
|
load_balancer_arn = response["LoadBalancers"][0]["LoadBalancerArn"]
|
2017-08-16 12:09:14 +00:00
|
|
|
|
|
|
|
response = conn.create_target_group(
|
|
|
|
Name="a-target",
|
|
|
|
Protocol="HTTP",
|
|
|
|
Port=8080,
|
|
|
|
VpcId=vpc.id,
|
|
|
|
HealthCheckProtocol="HTTP",
|
|
|
|
HealthCheckPort="8080",
|
|
|
|
HealthCheckPath="/",
|
|
|
|
HealthCheckIntervalSeconds=5,
|
2023-09-14 12:52:14 +00:00
|
|
|
HealthCheckTimeoutSeconds=3,
|
2017-08-16 12:09:14 +00:00
|
|
|
HealthyThresholdCount=5,
|
|
|
|
UnhealthyThresholdCount=2,
|
|
|
|
Matcher={"HttpCode": "200"},
|
|
|
|
)
|
2023-07-24 12:50:35 +00:00
|
|
|
target_group = response["TargetGroups"][0]
|
2017-08-16 12:09:14 +00:00
|
|
|
|
|
|
|
# Plain HTTP listener
|
|
|
|
response = conn.create_listener(
|
|
|
|
LoadBalancerArn=load_balancer_arn,
|
|
|
|
Protocol="HTTP",
|
|
|
|
Port=80,
|
|
|
|
DefaultActions=[
|
2023-07-24 12:50:35 +00:00
|
|
|
{"Type": "forward", "TargetGroupArn": target_group["TargetGroupArn"]}
|
2019-10-31 15:44:26 +00:00
|
|
|
],
|
2017-08-16 12:09:14 +00:00
|
|
|
)
|
2023-07-24 12:50:35 +00:00
|
|
|
listener = response["Listeners"][0]
|
|
|
|
assert listener["Port"] == 80
|
|
|
|
assert listener["Protocol"] == "HTTP"
|
|
|
|
assert listener["DefaultActions"] == [
|
|
|
|
{"TargetGroupArn": target_group["TargetGroupArn"], "Type": "forward"}
|
|
|
|
]
|
|
|
|
http_listener_arn = listener["ListenerArn"]
|
2017-08-16 12:09:14 +00:00
|
|
|
|
|
|
|
# create first rule
|
|
|
|
priority = 100
|
|
|
|
host = "xxx.example.com"
|
|
|
|
path_pattern = "foobar"
|
2020-06-27 08:42:32 +00:00
|
|
|
pathpatternconfig_pattern = "foobar2"
|
2017-08-16 17:29:49 +00:00
|
|
|
created_rule = conn.create_rule(
|
2017-08-16 12:09:14 +00:00
|
|
|
ListenerArn=http_listener_arn,
|
|
|
|
Priority=priority,
|
|
|
|
Conditions=[
|
|
|
|
{"Field": "host-header", "Values": [host]},
|
2017-10-02 19:36:47 +00:00
|
|
|
{"Field": "path-pattern", "Values": [path_pattern]},
|
2020-06-27 08:42:32 +00:00
|
|
|
{
|
|
|
|
"Field": "path-pattern",
|
|
|
|
"PathPatternConfig": {"Values": [pathpatternconfig_pattern]},
|
|
|
|
},
|
2017-08-16 12:09:14 +00:00
|
|
|
],
|
2023-07-24 12:50:35 +00:00
|
|
|
Actions=[{"TargetGroupArn": target_group["TargetGroupArn"], "Type": "forward"}],
|
2021-06-05 09:04:04 +00:00
|
|
|
)
|
2023-07-24 12:50:35 +00:00
|
|
|
rule = created_rule["Rules"][0]
|
|
|
|
assert rule["Priority"] == "100"
|
2017-08-16 12:09:14 +00:00
|
|
|
|
|
|
|
# check if rules is sorted by priority
|
2021-06-05 09:04:04 +00:00
|
|
|
priority = 500
|
2017-08-16 12:09:14 +00:00
|
|
|
host = "yyy.example.com"
|
|
|
|
path_pattern = "foobar"
|
|
|
|
rules = conn.create_rule(
|
|
|
|
ListenerArn=http_listener_arn,
|
|
|
|
Priority=priority,
|
|
|
|
Conditions=[
|
|
|
|
{"Field": "host-header", "Values": [host]},
|
2017-10-02 19:36:47 +00:00
|
|
|
{"Field": "path-pattern", "Values": [path_pattern]},
|
2020-06-27 08:42:32 +00:00
|
|
|
{
|
|
|
|
"Field": "path-pattern",
|
|
|
|
"PathPatternConfig": {"Values": [pathpatternconfig_pattern]},
|
|
|
|
},
|
2017-08-16 12:09:14 +00:00
|
|
|
],
|
2023-07-24 12:50:35 +00:00
|
|
|
Actions=[{"TargetGroupArn": target_group["TargetGroupArn"], "Type": "forward"}],
|
2017-08-16 12:09:14 +00:00
|
|
|
)
|
|
|
|
|
2021-06-09 17:41:18 +00:00
|
|
|
# add rule that uses forward_config
|
|
|
|
priority = 550
|
|
|
|
host = "aaa.example.com"
|
|
|
|
path_pattern = "barfoo"
|
|
|
|
rules = conn.create_rule(
|
|
|
|
ListenerArn=http_listener_arn,
|
|
|
|
Priority=priority,
|
|
|
|
Conditions=[
|
|
|
|
{"Field": "host-header", "Values": [host]},
|
|
|
|
{"Field": "path-pattern", "Values": [path_pattern]},
|
|
|
|
{
|
|
|
|
"Field": "path-pattern",
|
|
|
|
"PathPatternConfig": {"Values": [pathpatternconfig_pattern]},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
Actions=[
|
|
|
|
{
|
|
|
|
"Type": "forward",
|
|
|
|
"ForwardConfig": {
|
|
|
|
"TargetGroups": [
|
|
|
|
{
|
2023-07-24 12:50:35 +00:00
|
|
|
"TargetGroupArn": target_group["TargetGroupArn"],
|
2021-06-09 17:41:18 +00:00
|
|
|
"Weight": 1,
|
|
|
|
},
|
|
|
|
{
|
2023-07-24 12:50:35 +00:00
|
|
|
"TargetGroupArn": target_group["TargetGroupArn"],
|
2021-06-09 17:41:18 +00:00
|
|
|
"Weight": 2,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
# test for PriorityInUse
|
|
|
|
with pytest.raises(ClientError):
|
|
|
|
conn.create_rule(
|
|
|
|
ListenerArn=http_listener_arn,
|
|
|
|
Priority=priority,
|
|
|
|
Conditions=[
|
|
|
|
{"Field": "host-header", "Values": [host]},
|
|
|
|
{"Field": "path-pattern", "Values": [path_pattern]},
|
|
|
|
{
|
|
|
|
"Field": "path-pattern",
|
|
|
|
"PathPatternConfig": {"Values": [pathpatternconfig_pattern]},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
Actions=[
|
|
|
|
{
|
2023-07-24 12:50:35 +00:00
|
|
|
"TargetGroupArn": target_group["TargetGroupArn"],
|
2021-06-09 17:41:18 +00:00
|
|
|
"Type": "forward",
|
|
|
|
}
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
2017-08-16 16:57:02 +00:00
|
|
|
# test for describe listeners
|
|
|
|
obtained_rules = conn.describe_rules(ListenerArn=http_listener_arn)
|
2023-07-24 12:50:35 +00:00
|
|
|
assert len(obtained_rules["Rules"]) == 4
|
2017-08-16 17:29:49 +00:00
|
|
|
priorities = [rule["Priority"] for rule in obtained_rules["Rules"]]
|
2023-07-24 12:50:35 +00:00
|
|
|
assert priorities == ["100", "500", "550", "default"]
|
2017-08-16 16:57:02 +00:00
|
|
|
|
2017-08-16 17:25:39 +00:00
|
|
|
first_rule = obtained_rules["Rules"][0]
|
2017-08-16 18:10:26 +00:00
|
|
|
second_rule = obtained_rules["Rules"][1]
|
2021-06-09 17:41:18 +00:00
|
|
|
third_rule = obtained_rules["Rules"][2]
|
2021-08-21 04:50:10 +00:00
|
|
|
default_rule = obtained_rules["Rules"][3]
|
2023-07-24 12:50:35 +00:00
|
|
|
assert first_rule["IsDefault"] is False
|
|
|
|
assert default_rule["IsDefault"] is True
|
2017-08-16 16:57:02 +00:00
|
|
|
obtained_rules = conn.describe_rules(RuleArns=[first_rule["RuleArn"]])
|
2023-07-24 12:50:35 +00:00
|
|
|
assert obtained_rules["Rules"] == [first_rule]
|
2017-08-16 16:57:02 +00:00
|
|
|
|
|
|
|
# test for pagination
|
2017-10-02 19:36:47 +00:00
|
|
|
obtained_rules = conn.describe_rules(ListenerArn=http_listener_arn, PageSize=1)
|
2023-07-24 12:50:35 +00:00
|
|
|
assert len(obtained_rules["Rules"]) == 1
|
2017-08-16 19:37:42 +00:00
|
|
|
next_marker = obtained_rules["NextMarker"]
|
|
|
|
|
2017-10-02 19:36:47 +00:00
|
|
|
following_rules = conn.describe_rules(
|
|
|
|
ListenerArn=http_listener_arn, PageSize=1, Marker=next_marker
|
|
|
|
)
|
2023-07-24 12:50:35 +00:00
|
|
|
assert len(following_rules["Rules"]) == 1
|
|
|
|
assert (
|
|
|
|
following_rules["Rules"][0]["RuleArn"] != obtained_rules["Rules"][0]["RuleArn"]
|
2017-10-02 19:36:47 +00:00
|
|
|
)
|
2017-08-16 16:57:02 +00:00
|
|
|
|
|
|
|
# test for invalid describe rule request
|
2020-10-06 05:54:49 +00:00
|
|
|
with pytest.raises(ClientError):
|
2017-08-16 16:57:02 +00:00
|
|
|
conn.describe_rules()
|
2020-10-06 05:54:49 +00:00
|
|
|
with pytest.raises(ClientError):
|
2017-08-16 16:57:02 +00:00
|
|
|
conn.describe_rules(RuleArns=[])
|
2020-10-06 05:54:49 +00:00
|
|
|
with pytest.raises(ClientError):
|
2017-08-16 16:57:02 +00:00
|
|
|
conn.describe_rules(
|
|
|
|
ListenerArn=http_listener_arn, RuleArns=[first_rule["RuleArn"]]
|
|
|
|
)
|
|
|
|
|
2017-08-21 19:28:58 +00:00
|
|
|
# modify rule partially
|
2017-08-16 17:25:39 +00:00
|
|
|
new_host = "new.example.com"
|
|
|
|
new_path_pattern = "new_path"
|
2020-06-27 08:42:32 +00:00
|
|
|
new_pathpatternconfig_pattern = "new_path2"
|
2021-10-18 19:44:29 +00:00
|
|
|
conn.modify_rule(
|
2017-08-16 17:25:39 +00:00
|
|
|
RuleArn=first_rule["RuleArn"],
|
|
|
|
Conditions=[
|
2017-10-02 19:36:47 +00:00
|
|
|
{"Field": "host-header", "Values": [new_host]},
|
|
|
|
{"Field": "path-pattern", "Values": [new_path_pattern]},
|
2020-06-27 08:42:32 +00:00
|
|
|
{
|
|
|
|
"Field": "path-pattern",
|
|
|
|
"PathPatternConfig": {"Values": [new_pathpatternconfig_pattern]},
|
|
|
|
},
|
2019-10-31 15:44:26 +00:00
|
|
|
],
|
2021-06-05 09:04:04 +00:00
|
|
|
)
|
2017-08-21 19:28:58 +00:00
|
|
|
|
2017-08-16 17:25:39 +00:00
|
|
|
rules = conn.describe_rules(ListenerArn=http_listener_arn)
|
2017-08-21 19:28:58 +00:00
|
|
|
obtained_rule = rules["Rules"][0]
|
2023-07-24 12:50:35 +00:00
|
|
|
assert obtained_rule["Conditions"][0]["Values"][0] == new_host
|
|
|
|
assert obtained_rule["Conditions"][1]["Values"][0] == new_path_pattern
|
|
|
|
assert (
|
|
|
|
obtained_rule["Conditions"][2]["PathPatternConfig"]["Values"][0]
|
|
|
|
== new_pathpatternconfig_pattern
|
2020-06-27 08:42:32 +00:00
|
|
|
)
|
2023-07-24 12:50:35 +00:00
|
|
|
assert (
|
|
|
|
obtained_rule["Actions"][0]["TargetGroupArn"] == target_group["TargetGroupArn"]
|
2019-10-31 15:44:26 +00:00
|
|
|
)
|
2017-08-16 17:25:39 +00:00
|
|
|
|
2017-08-16 18:10:26 +00:00
|
|
|
# modify priority
|
2022-04-16 20:57:02 +00:00
|
|
|
new_priority = int(first_rule["Priority"]) - 1
|
|
|
|
updated_rule = conn.set_rule_priorities(
|
2017-08-16 18:10:26 +00:00
|
|
|
RulePriorities=[
|
2017-10-02 19:36:47 +00:00
|
|
|
{
|
|
|
|
"RuleArn": first_rule["RuleArn"],
|
2022-04-16 20:57:02 +00:00
|
|
|
"Priority": new_priority,
|
2017-10-02 19:36:47 +00:00
|
|
|
}
|
2017-08-16 18:10:26 +00:00
|
|
|
]
|
|
|
|
)
|
2021-06-09 17:41:18 +00:00
|
|
|
|
2022-04-16 20:57:02 +00:00
|
|
|
# assert response of SetRulePriorities operation
|
2023-07-24 12:50:35 +00:00
|
|
|
assert len(updated_rule["Rules"]) == 1
|
|
|
|
assert updated_rule["Rules"][0]["RuleArn"] == first_rule["RuleArn"]
|
|
|
|
assert updated_rule["Rules"][0]["Priority"] == str(new_priority)
|
|
|
|
assert len(updated_rule["Rules"][0]["Conditions"]) == 3
|
|
|
|
assert len(updated_rule["Rules"][0]["Actions"]) == 1
|
2022-04-16 20:57:02 +00:00
|
|
|
|
2021-06-09 17:41:18 +00:00
|
|
|
# modify forward_config rule partially rule
|
|
|
|
new_host_2 = "new.examplewebsite.com"
|
|
|
|
new_path_pattern_2 = "new_path_2"
|
|
|
|
new_pathpatternconfig_pattern_2 = "new_path_2"
|
2021-10-18 19:44:29 +00:00
|
|
|
conn.modify_rule(
|
2021-06-09 17:41:18 +00:00
|
|
|
RuleArn=third_rule["RuleArn"],
|
|
|
|
Conditions=[
|
|
|
|
{"Field": "host-header", "Values": [new_host_2]},
|
|
|
|
{"Field": "path-pattern", "Values": [new_path_pattern_2]},
|
|
|
|
{
|
|
|
|
"Field": "path-pattern",
|
|
|
|
"PathPatternConfig": {"Values": [new_pathpatternconfig_pattern_2]},
|
|
|
|
},
|
|
|
|
],
|
2023-07-24 12:50:35 +00:00
|
|
|
Actions=[{"TargetGroupArn": target_group["TargetGroupArn"], "Type": "forward"}],
|
2021-06-09 17:41:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
rules = conn.describe_rules(ListenerArn=http_listener_arn)
|
|
|
|
obtained_rule = rules["Rules"][2]
|
2023-07-24 12:50:35 +00:00
|
|
|
assert obtained_rule["Conditions"][0]["Values"][0] == new_host_2
|
|
|
|
assert obtained_rule["Conditions"][1]["Values"][0] == new_path_pattern_2
|
|
|
|
assert (
|
|
|
|
obtained_rule["Conditions"][2]["PathPatternConfig"]["Values"][0]
|
|
|
|
== new_pathpatternconfig_pattern_2
|
2021-06-09 17:41:18 +00:00
|
|
|
)
|
2023-07-24 12:50:35 +00:00
|
|
|
assert (
|
|
|
|
obtained_rule["Actions"][0]["TargetGroupArn"] == target_group["TargetGroupArn"]
|
2021-06-09 17:41:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# modify priority
|
|
|
|
conn.set_rule_priorities(
|
|
|
|
RulePriorities=[
|
|
|
|
{
|
|
|
|
"RuleArn": third_rule["RuleArn"],
|
|
|
|
"Priority": int(third_rule["Priority"]) - 1,
|
|
|
|
}
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2020-10-06 05:54:49 +00:00
|
|
|
with pytest.raises(ClientError):
|
2017-08-16 18:10:26 +00:00
|
|
|
conn.set_rule_priorities(
|
|
|
|
RulePriorities=[
|
|
|
|
{"RuleArn": first_rule["RuleArn"], "Priority": 999},
|
|
|
|
{"RuleArn": second_rule["RuleArn"], "Priority": 999},
|
2021-06-09 17:41:18 +00:00
|
|
|
{"RuleArn": third_rule["RuleArn"], "Priority": 999},
|
2017-08-16 18:10:26 +00:00
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2017-08-16 16:57:02 +00:00
|
|
|
# delete
|
|
|
|
arn = first_rule["RuleArn"]
|
2017-08-16 15:35:45 +00:00
|
|
|
conn.delete_rule(RuleArn=arn)
|
2017-08-21 19:30:03 +00:00
|
|
|
rules = conn.describe_rules(ListenerArn=http_listener_arn)["Rules"]
|
2023-07-24 12:50:35 +00:00
|
|
|
assert len(rules) == 3
|
2017-08-16 15:35:45 +00:00
|
|
|
|
2017-08-16 12:09:14 +00:00
|
|
|
# test for invalid action type
|
|
|
|
safe_priority = 2
|
2020-10-06 05:54:49 +00:00
|
|
|
with pytest.raises(ClientError):
|
2017-10-02 19:38:36 +00:00
|
|
|
conn.create_rule(
|
2017-08-16 12:09:14 +00:00
|
|
|
ListenerArn=http_listener_arn,
|
|
|
|
Priority=safe_priority,
|
|
|
|
Conditions=[
|
|
|
|
{"Field": "host-header", "Values": [host]},
|
2017-10-02 19:36:47 +00:00
|
|
|
{"Field": "path-pattern", "Values": [path_pattern]},
|
2017-08-16 12:09:14 +00:00
|
|
|
],
|
2019-10-31 15:44:26 +00:00
|
|
|
Actions=[
|
2017-10-02 19:36:47 +00:00
|
|
|
{
|
2023-07-24 12:50:35 +00:00
|
|
|
"TargetGroupArn": target_group["TargetGroupArn"],
|
2017-08-16 12:09:14 +00:00
|
|
|
"Type": "forward2",
|
|
|
|
}
|
2019-10-31 15:44:26 +00:00
|
|
|
],
|
2017-08-16 12:09:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# test for invalid action type
|
|
|
|
safe_priority = 2
|
2023-07-24 12:50:35 +00:00
|
|
|
invalid_target_group_arn = target_group["TargetGroupArn"] + "x"
|
2020-10-06 05:54:49 +00:00
|
|
|
with pytest.raises(ClientError):
|
2017-10-02 19:38:36 +00:00
|
|
|
conn.create_rule(
|
2017-08-16 12:09:14 +00:00
|
|
|
ListenerArn=http_listener_arn,
|
|
|
|
Priority=safe_priority,
|
|
|
|
Conditions=[
|
|
|
|
{"Field": "host-header", "Values": [host]},
|
2017-10-02 19:36:47 +00:00
|
|
|
{"Field": "path-pattern", "Values": [path_pattern]},
|
2017-08-16 12:09:14 +00:00
|
|
|
],
|
|
|
|
Actions=[{"TargetGroupArn": invalid_target_group_arn, "Type": "forward"}],
|
|
|
|
)
|
|
|
|
|
|
|
|
# test for invalid condition field_name
|
|
|
|
safe_priority = 2
|
2020-10-06 05:54:49 +00:00
|
|
|
with pytest.raises(ClientError):
|
2017-10-02 19:38:36 +00:00
|
|
|
conn.create_rule(
|
2017-08-16 12:09:14 +00:00
|
|
|
ListenerArn=http_listener_arn,
|
|
|
|
Priority=safe_priority,
|
|
|
|
Conditions=[{"Field": "xxxxxxx", "Values": [host]}],
|
|
|
|
Actions=[
|
|
|
|
{
|
2023-07-24 12:50:35 +00:00
|
|
|
"TargetGroupArn": target_group["TargetGroupArn"],
|
2017-08-16 12:09:14 +00:00
|
|
|
"Type": "forward",
|
|
|
|
}
|
2019-10-31 15:44:26 +00:00
|
|
|
],
|
2017-08-16 12:09:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# test for emptry condition value
|
|
|
|
safe_priority = 2
|
2020-10-06 05:54:49 +00:00
|
|
|
with pytest.raises(ClientError):
|
2017-10-02 19:38:36 +00:00
|
|
|
conn.create_rule(
|
2017-08-16 12:09:14 +00:00
|
|
|
ListenerArn=http_listener_arn,
|
|
|
|
Priority=safe_priority,
|
|
|
|
Conditions=[{"Field": "host-header", "Values": []}],
|
|
|
|
Actions=[
|
|
|
|
{
|
2023-07-24 12:50:35 +00:00
|
|
|
"TargetGroupArn": target_group["TargetGroupArn"],
|
2017-08-16 12:09:14 +00:00
|
|
|
"Type": "forward",
|
|
|
|
}
|
2019-10-31 15:44:26 +00:00
|
|
|
],
|
2017-08-16 12:09:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# test for multiple condition value
|
|
|
|
safe_priority = 2
|
2020-10-06 05:54:49 +00:00
|
|
|
with pytest.raises(ClientError):
|
2017-10-02 19:38:36 +00:00
|
|
|
conn.create_rule(
|
2017-08-16 12:09:14 +00:00
|
|
|
ListenerArn=http_listener_arn,
|
|
|
|
Priority=safe_priority,
|
|
|
|
Conditions=[{"Field": "host-header", "Values": [host, host]}],
|
|
|
|
Actions=[
|
|
|
|
{
|
2023-07-24 12:50:35 +00:00
|
|
|
"TargetGroupArn": target_group["TargetGroupArn"],
|
2017-08-16 12:09:14 +00:00
|
|
|
"Type": "forward",
|
|
|
|
}
|
2019-10-31 15:44:26 +00:00
|
|
|
],
|
2017-08-16 12:09:14 +00:00
|
|
|
)
|
2017-09-07 18:25:59 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2017-10-29 14:14:17 +00:00
|
|
|
def test_describe_account_limits():
|
|
|
|
client = boto3.client("elbv2", region_name="eu-central-1")
|
|
|
|
|
|
|
|
resp = client.describe_account_limits()
|
2023-07-24 12:50:35 +00:00
|
|
|
assert "Name" in resp["Limits"][0]
|
|
|
|
assert "Max" in resp["Limits"][0]
|
2017-10-29 14:14:17 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2017-10-29 14:14:17 +00:00
|
|
|
def test_describe_ssl_policies():
|
|
|
|
client = boto3.client("elbv2", region_name="eu-central-1")
|
|
|
|
|
|
|
|
resp = client.describe_ssl_policies()
|
2023-11-10 14:54:25 +00:00
|
|
|
assert len(resp["SslPolicies"]) > 0
|
2017-10-29 14:14:17 +00:00
|
|
|
|
|
|
|
resp = client.describe_ssl_policies(
|
2021-10-07 18:18:02 +00:00
|
|
|
Names=["ELBSecurityPolicy-TLS-1-2-2017-01", "ELBSecurityPolicy-2016-08"]
|
|
|
|
)
|
2023-07-24 12:50:35 +00:00
|
|
|
assert len(resp["SslPolicies"]) == 2
|
2021-10-07 18:18:02 +00:00
|
|
|
|
|
|
|
resp = client.describe_ssl_policies(
|
|
|
|
Names=[
|
|
|
|
"ELBSecurityPolicy-TLS-1-2-2017-01",
|
|
|
|
"ELBSecurityPolicy-2016-08",
|
|
|
|
"ELBSecurityPolicy-2016-08",
|
|
|
|
]
|
2017-10-29 14:14:17 +00:00
|
|
|
)
|
2023-07-24 12:50:35 +00:00
|
|
|
assert len(resp["SslPolicies"]) == 2
|
2017-10-29 14:14:17 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2017-10-29 14:14:17 +00:00
|
|
|
def test_set_ip_address_type():
|
2021-10-18 19:44:29 +00:00
|
|
|
response, _, security_group, subnet1, subnet2, client = create_load_balancer()
|
2017-10-29 14:14:17 +00:00
|
|
|
arn = response["LoadBalancers"][0]["LoadBalancerArn"]
|
|
|
|
|
|
|
|
# Internal LBs cant be dualstack yet
|
2020-10-06 05:54:49 +00:00
|
|
|
with pytest.raises(ClientError):
|
2017-10-29 14:14:17 +00:00
|
|
|
client.set_ip_address_type(LoadBalancerArn=arn, IpAddressType="dualstack")
|
|
|
|
|
|
|
|
# Create internet facing one
|
|
|
|
response = client.create_load_balancer(
|
|
|
|
Name="my-lb2",
|
|
|
|
Subnets=[subnet1.id, subnet2.id],
|
|
|
|
SecurityGroups=[security_group.id],
|
|
|
|
Scheme="internet-facing",
|
|
|
|
Tags=[{"Key": "key_name", "Value": "a_value"}],
|
|
|
|
)
|
|
|
|
arn = response["LoadBalancers"][0]["LoadBalancerArn"]
|
2019-10-31 15:44:26 +00:00
|
|
|
|
2019-05-25 10:18:39 +00:00
|
|
|
client.set_ip_address_type(LoadBalancerArn=arn, IpAddressType="dualstack")
|
2017-10-29 14:14:17 +00:00
|
|
|
|
2023-09-15 19:57:11 +00:00
|
|
|
with pytest.raises(ClientError) as ex:
|
|
|
|
client.set_ip_address_type(LoadBalancerArn=arn, IpAddressType="internal")
|
|
|
|
err = ex.value.response["Error"]
|
|
|
|
assert err["Code"] == "ValidationError"
|
|
|
|
|
2017-10-29 14:14:17 +00:00
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2017-10-29 14:14:17 +00:00
|
|
|
def test_set_security_groups():
|
|
|
|
client = boto3.client("elbv2", region_name="us-east-1")
|
|
|
|
ec2 = boto3.resource("ec2", region_name="us-east-1")
|
|
|
|
|
|
|
|
security_group = ec2.create_security_group(
|
|
|
|
GroupName="a-security-group", Description="First One"
|
|
|
|
)
|
|
|
|
security_group2 = ec2.create_security_group(
|
|
|
|
GroupName="b-security-group", Description="Second One"
|
|
|
|
)
|
|
|
|
vpc = ec2.create_vpc(CidrBlock="172.28.7.0/24", InstanceTenancy="default")
|
|
|
|
subnet1 = ec2.create_subnet(
|
|
|
|
VpcId=vpc.id, CidrBlock="172.28.7.192/26", AvailabilityZone="us-east-1a"
|
|
|
|
)
|
|
|
|
subnet2 = ec2.create_subnet(
|
|
|
|
VpcId=vpc.id, CidrBlock="172.28.7.0/26", AvailabilityZone="us-east-1b"
|
|
|
|
)
|
|
|
|
|
|
|
|
response = client.create_load_balancer(
|
|
|
|
Name="my-lb",
|
|
|
|
Subnets=[subnet1.id, subnet2.id],
|
|
|
|
SecurityGroups=[security_group.id],
|
|
|
|
Scheme="internal",
|
|
|
|
Tags=[{"Key": "key_name", "Value": "a_value"}],
|
|
|
|
)
|
|
|
|
arn = response["LoadBalancers"][0]["LoadBalancerArn"]
|
|
|
|
|
|
|
|
client.set_security_groups(
|
|
|
|
LoadBalancerArn=arn, SecurityGroups=[security_group.id, security_group2.id]
|
|
|
|
)
|
|
|
|
|
|
|
|
resp = client.describe_load_balancers(LoadBalancerArns=[arn])
|
2023-07-24 12:50:35 +00:00
|
|
|
assert len(resp["LoadBalancers"][0]["SecurityGroups"]) == 2
|
2017-10-29 14:14:17 +00:00
|
|
|
|
2020-10-06 05:54:49 +00:00
|
|
|
with pytest.raises(ClientError):
|
2020-01-20 23:21:11 +00:00
|
|
|
client.set_security_groups(LoadBalancerArn=arn, SecurityGroups=["non_existent"])
|
2017-10-29 14:14:17 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2021-08-16 14:13:50 +00:00
|
|
|
def test_modify_load_balancer_attributes_idle_timeout():
|
2021-10-18 19:44:29 +00:00
|
|
|
response, _, _, _, _, client = create_load_balancer()
|
2017-10-29 14:14:17 +00:00
|
|
|
arn = response["LoadBalancers"][0]["LoadBalancerArn"]
|
|
|
|
|
|
|
|
client.modify_load_balancer_attributes(
|
|
|
|
LoadBalancerArn=arn,
|
|
|
|
Attributes=[{"Key": "idle_timeout.timeout_seconds", "Value": "600"}],
|
|
|
|
)
|
|
|
|
|
|
|
|
# Check its 600 not 60
|
|
|
|
response = client.describe_load_balancer_attributes(LoadBalancerArn=arn)
|
|
|
|
idle_timeout = list(
|
|
|
|
filter(
|
|
|
|
lambda item: item["Key"] == "idle_timeout.timeout_seconds",
|
|
|
|
response["Attributes"],
|
2019-10-31 15:44:26 +00:00
|
|
|
)
|
2017-10-29 14:14:17 +00:00
|
|
|
)[0]
|
2023-07-24 12:50:35 +00:00
|
|
|
assert idle_timeout["Value"] == "600"
|
2017-10-29 14:14:17 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2021-11-06 12:37:19 +00:00
|
|
|
def test_modify_load_balancer_attributes_routing_http2_enabled():
|
|
|
|
response, _, _, _, _, client = create_load_balancer()
|
|
|
|
arn = response["LoadBalancers"][0]["LoadBalancerArn"]
|
|
|
|
|
|
|
|
client.modify_load_balancer_attributes(
|
|
|
|
LoadBalancerArn=arn,
|
|
|
|
Attributes=[{"Key": "routing.http2.enabled", "Value": "false"}],
|
|
|
|
)
|
|
|
|
|
|
|
|
response = client.describe_load_balancer_attributes(LoadBalancerArn=arn)
|
|
|
|
routing_http2_enabled = list(
|
|
|
|
filter(
|
|
|
|
lambda item: item["Key"] == "routing.http2.enabled", response["Attributes"]
|
|
|
|
)
|
|
|
|
)[0]
|
2023-07-24 12:50:35 +00:00
|
|
|
assert routing_http2_enabled["Value"] == "false"
|
2021-11-06 12:37:19 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2022-01-30 23:00:26 +00:00
|
|
|
def test_modify_load_balancer_attributes_crosszone_enabled():
|
|
|
|
response, _, _, _, _, client = create_load_balancer()
|
|
|
|
arn = response["LoadBalancers"][0]["LoadBalancerArn"]
|
|
|
|
|
|
|
|
client.modify_load_balancer_attributes(
|
|
|
|
LoadBalancerArn=arn,
|
|
|
|
Attributes=[
|
|
|
|
{"Key": "load_balancing.cross_zone.enabled", "Value": "false"},
|
|
|
|
{"Key": "deletion_protection.enabled", "Value": "false"},
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
attrs = client.describe_load_balancer_attributes(LoadBalancerArn=arn)["Attributes"]
|
2023-07-24 12:50:35 +00:00
|
|
|
assert {"Key": "deletion_protection.enabled", "Value": "false"} in attrs
|
|
|
|
assert {"Key": "load_balancing.cross_zone.enabled", "Value": "false"} in attrs
|
2022-01-30 23:00:26 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2021-11-06 12:37:19 +00:00
|
|
|
def test_modify_load_balancer_attributes_routing_http_drop_invalid_header_fields_enabled():
|
|
|
|
response, _, _, _, _, client = create_load_balancer()
|
|
|
|
arn = response["LoadBalancers"][0]["LoadBalancerArn"]
|
|
|
|
|
|
|
|
client.modify_load_balancer_attributes(
|
|
|
|
LoadBalancerArn=arn,
|
|
|
|
Attributes=[
|
|
|
|
{"Key": "routing.http.drop_invalid_header_fields.enabled", "Value": "false"}
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
response = client.describe_load_balancer_attributes(LoadBalancerArn=arn)
|
|
|
|
routing_http_drop_invalid_header_fields_enabled = list(
|
|
|
|
filter(
|
|
|
|
lambda item: item["Key"]
|
|
|
|
== "routing.http.drop_invalid_header_fields.enabled",
|
|
|
|
response["Attributes"],
|
|
|
|
)
|
|
|
|
)[0]
|
2023-07-24 12:50:35 +00:00
|
|
|
assert routing_http_drop_invalid_header_fields_enabled["Value"] == "false"
|
2021-11-06 12:37:19 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2024-01-23 14:40:44 +00:00
|
|
|
def test_modify_load_balancer_attributes_connection_logs_s3():
|
|
|
|
response, _, _, _, _, client = create_load_balancer()
|
|
|
|
arn = response["LoadBalancers"][0]["LoadBalancerArn"]
|
|
|
|
|
|
|
|
client.modify_load_balancer_attributes(
|
|
|
|
LoadBalancerArn=arn,
|
|
|
|
Attributes=[
|
|
|
|
{"Key": "connection_logs.s3.enabled", "Value": "true"},
|
|
|
|
{"Key": "connection_logs.s3.bucket", "Value": "s3bucket"},
|
|
|
|
{"Key": "connection_logs.s3.prefix", "Value": "test_prefix"},
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
response = client.describe_load_balancer_attributes(LoadBalancerArn=arn)
|
|
|
|
attrs = client.describe_load_balancer_attributes(LoadBalancerArn=arn)["Attributes"]
|
|
|
|
assert {"Key": "connection_logs.s3.enabled", "Value": "true"} in attrs
|
|
|
|
assert {"Key": "connection_logs.s3.bucket", "Value": "s3bucket"} in attrs
|
|
|
|
assert {"Key": "connection_logs.s3.prefix", "Value": "test_prefix"} in attrs
|
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2017-10-29 14:14:17 +00:00
|
|
|
def test_modify_listener_http_to_https():
|
|
|
|
client = boto3.client("elbv2", region_name="eu-central-1")
|
|
|
|
acm = boto3.client("acm", region_name="eu-central-1")
|
|
|
|
ec2 = boto3.resource("ec2", region_name="eu-central-1")
|
|
|
|
|
|
|
|
security_group = ec2.create_security_group(
|
|
|
|
GroupName="a-security-group", Description="First One"
|
|
|
|
)
|
|
|
|
vpc = ec2.create_vpc(CidrBlock="172.28.7.0/24", InstanceTenancy="default")
|
|
|
|
subnet1 = ec2.create_subnet(
|
|
|
|
VpcId=vpc.id, CidrBlock="172.28.7.192/26", AvailabilityZone="eu-central-1a"
|
|
|
|
)
|
|
|
|
subnet2 = ec2.create_subnet(
|
|
|
|
VpcId=vpc.id, CidrBlock="172.28.7.0/26", AvailabilityZone="eu-central-1b"
|
|
|
|
)
|
|
|
|
|
|
|
|
response = client.create_load_balancer(
|
|
|
|
Name="my-lb",
|
|
|
|
Subnets=[subnet1.id, subnet2.id],
|
|
|
|
SecurityGroups=[security_group.id],
|
|
|
|
Scheme="internal",
|
|
|
|
Tags=[{"Key": "key_name", "Value": "a_value"}],
|
|
|
|
)
|
|
|
|
|
2023-07-24 12:50:35 +00:00
|
|
|
load_balancer_arn = response["LoadBalancers"][0]["LoadBalancerArn"]
|
2017-10-29 14:14:17 +00:00
|
|
|
|
|
|
|
response = client.create_target_group(
|
|
|
|
Name="a-target",
|
|
|
|
Protocol="HTTP",
|
|
|
|
Port=8080,
|
|
|
|
VpcId=vpc.id,
|
|
|
|
HealthCheckProtocol="HTTP",
|
|
|
|
HealthCheckPort="8080",
|
|
|
|
HealthCheckPath="/",
|
|
|
|
HealthCheckIntervalSeconds=5,
|
2023-09-14 12:52:14 +00:00
|
|
|
HealthCheckTimeoutSeconds=3,
|
2017-10-29 14:14:17 +00:00
|
|
|
HealthyThresholdCount=5,
|
|
|
|
UnhealthyThresholdCount=2,
|
|
|
|
Matcher={"HttpCode": "200"},
|
|
|
|
)
|
2023-07-24 12:50:35 +00:00
|
|
|
target_group = response["TargetGroups"][0]
|
2017-10-29 14:14:17 +00:00
|
|
|
target_group_arn = target_group["TargetGroupArn"]
|
|
|
|
|
|
|
|
# Plain HTTP listener
|
|
|
|
response = client.create_listener(
|
|
|
|
LoadBalancerArn=load_balancer_arn,
|
|
|
|
Protocol="HTTP",
|
|
|
|
Port=80,
|
|
|
|
DefaultActions=[{"Type": "forward", "TargetGroupArn": target_group_arn}],
|
|
|
|
)
|
|
|
|
listener_arn = response["Listeners"][0]["ListenerArn"]
|
|
|
|
|
2022-03-21 20:55:19 +00:00
|
|
|
# No default cert
|
|
|
|
with pytest.raises(ClientError) as ex:
|
|
|
|
client.modify_listener(
|
|
|
|
ListenerArn=listener_arn,
|
|
|
|
Port=443,
|
|
|
|
Protocol="HTTPS",
|
|
|
|
SslPolicy="ELBSecurityPolicy-TLS-1-2-2017-01",
|
|
|
|
Certificates=[],
|
|
|
|
DefaultActions=[{"Type": "forward", "TargetGroupArn": target_group_arn}],
|
|
|
|
)
|
|
|
|
err = ex.value.response["Error"]
|
2023-07-24 12:50:35 +00:00
|
|
|
assert err["Code"] == "CertificateWereNotPassed"
|
|
|
|
assert (
|
|
|
|
err["Message"]
|
|
|
|
== "You must provide a list containing exactly one certificate if the listener protocol is HTTPS."
|
2022-03-21 20:55:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
acm.request_certificate(
|
2017-10-29 14:14:17 +00:00
|
|
|
DomainName="google.com",
|
|
|
|
SubjectAlternativeNames=["google.com", "www.google.com", "mail.google.com"],
|
|
|
|
)
|
|
|
|
response = acm.request_certificate(
|
|
|
|
DomainName="yahoo.com",
|
|
|
|
SubjectAlternativeNames=["yahoo.com", "www.yahoo.com", "mail.yahoo.com"],
|
|
|
|
)
|
|
|
|
yahoo_arn = response["CertificateArn"]
|
|
|
|
|
|
|
|
response = client.modify_listener(
|
|
|
|
ListenerArn=listener_arn,
|
|
|
|
Port=443,
|
|
|
|
Protocol="HTTPS",
|
|
|
|
SslPolicy="ELBSecurityPolicy-TLS-1-2-2017-01",
|
2021-07-01 15:25:40 +00:00
|
|
|
Certificates=[{"CertificateArn": yahoo_arn}],
|
2017-10-29 14:14:17 +00:00
|
|
|
DefaultActions=[{"Type": "forward", "TargetGroupArn": target_group_arn}],
|
2019-10-31 15:44:26 +00:00
|
|
|
)
|
2023-07-24 12:50:35 +00:00
|
|
|
assert response["Listeners"][0]["Port"] == 443
|
|
|
|
assert response["Listeners"][0]["Protocol"] == "HTTPS"
|
|
|
|
assert response["Listeners"][0]["SslPolicy"] == "ELBSecurityPolicy-TLS-1-2-2017-01"
|
|
|
|
assert len(response["Listeners"][0]["Certificates"]) == 1
|
2017-10-29 14:14:17 +00:00
|
|
|
|
2017-10-29 16:06:03 +00:00
|
|
|
# Check default cert, can't do this in server mode
|
|
|
|
if os.environ.get("TEST_SERVER_MODE", "false").lower() == "false":
|
|
|
|
listener = (
|
2022-08-13 09:49:43 +00:00
|
|
|
elbv2_backends[ACCOUNT_ID]["eu-central-1"]
|
2017-10-29 16:06:03 +00:00
|
|
|
.load_balancers[load_balancer_arn]
|
|
|
|
.listeners[listener_arn]
|
2019-10-31 15:44:26 +00:00
|
|
|
)
|
2023-07-24 12:50:35 +00:00
|
|
|
assert listener.certificate == yahoo_arn
|
2017-10-29 14:14:17 +00:00
|
|
|
|
|
|
|
# Bad cert
|
2022-01-29 12:04:14 +00:00
|
|
|
with pytest.raises(ClientError) as exc:
|
2017-10-29 14:14:17 +00:00
|
|
|
client.modify_listener(
|
|
|
|
ListenerArn=listener_arn,
|
|
|
|
Port=443,
|
|
|
|
Protocol="HTTPS",
|
|
|
|
SslPolicy="ELBSecurityPolicy-TLS-1-2-2017-01",
|
|
|
|
Certificates=[{"CertificateArn": "lalala", "IsDefault": True}],
|
|
|
|
DefaultActions=[{"Type": "forward", "TargetGroupArn": target_group_arn}],
|
|
|
|
)
|
2022-01-29 12:04:14 +00:00
|
|
|
err = exc.value.response["Error"]
|
2023-07-24 12:50:35 +00:00
|
|
|
assert err["Message"] == "Certificate lalala not found"
|
2022-01-29 12:04:14 +00:00
|
|
|
|
|
|
|
# Unknown protocol
|
|
|
|
with pytest.raises(ClientError) as exc:
|
|
|
|
client.modify_listener(
|
|
|
|
ListenerArn=listener_arn,
|
|
|
|
Port=443,
|
|
|
|
Protocol="HTP",
|
|
|
|
SslPolicy="ELBSecurityPolicy-TLS-1-2-2017-01",
|
|
|
|
Certificates=[{"CertificateArn": yahoo_arn, "IsDefault": True}],
|
|
|
|
DefaultActions=[{"Type": "forward", "TargetGroupArn": target_group_arn}],
|
|
|
|
)
|
|
|
|
err = exc.value.response["Error"]
|
2023-07-24 12:50:35 +00:00
|
|
|
assert err["Message"] == "Protocol HTP is not supported"
|
2022-01-29 12:04:14 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2022-01-29 12:04:14 +00:00
|
|
|
def test_modify_listener_of_https_target_group():
|
|
|
|
# Verify we can add a listener for a TargetGroup that is already HTTPS
|
|
|
|
client = boto3.client("elbv2", region_name="eu-central-1")
|
|
|
|
acm = boto3.client("acm", region_name="eu-central-1")
|
|
|
|
ec2 = boto3.resource("ec2", region_name="eu-central-1")
|
|
|
|
|
|
|
|
security_group = ec2.create_security_group(
|
|
|
|
GroupName="a-security-group", Description="First One"
|
|
|
|
)
|
|
|
|
vpc = ec2.create_vpc(CidrBlock="172.28.7.0/24", InstanceTenancy="default")
|
|
|
|
subnet1 = ec2.create_subnet(
|
|
|
|
VpcId=vpc.id, CidrBlock="172.28.7.192/26", AvailabilityZone="eu-central-1a"
|
|
|
|
)
|
|
|
|
|
|
|
|
response = client.create_load_balancer(
|
|
|
|
Name="my-lb",
|
|
|
|
Subnets=[subnet1.id],
|
|
|
|
SecurityGroups=[security_group.id],
|
|
|
|
Scheme="internal",
|
|
|
|
Tags=[{"Key": "key_name", "Value": "a_value"}],
|
|
|
|
)
|
|
|
|
|
2023-07-24 12:50:35 +00:00
|
|
|
load_balancer_arn = response["LoadBalancers"][0]["LoadBalancerArn"]
|
2022-01-29 12:04:14 +00:00
|
|
|
|
|
|
|
response = client.create_target_group(
|
|
|
|
Name="a-target", Protocol="HTTPS", Port=8443, VpcId=vpc.id
|
|
|
|
)
|
2023-07-24 12:50:35 +00:00
|
|
|
target_group = response["TargetGroups"][0]
|
2022-01-29 12:04:14 +00:00
|
|
|
target_group_arn = target_group["TargetGroupArn"]
|
|
|
|
|
|
|
|
# HTTPS listener
|
|
|
|
response = acm.request_certificate(
|
|
|
|
DomainName="google.com", SubjectAlternativeNames=["google.com"]
|
|
|
|
)
|
|
|
|
google_arn = response["CertificateArn"]
|
|
|
|
response = client.create_listener(
|
|
|
|
LoadBalancerArn=load_balancer_arn,
|
|
|
|
Protocol="HTTPS",
|
|
|
|
Port=443,
|
|
|
|
Certificates=[{"CertificateArn": google_arn}],
|
|
|
|
DefaultActions=[{"Type": "forward", "TargetGroupArn": target_group_arn}],
|
|
|
|
)
|
|
|
|
listener_arn = response["Listeners"][0]["ListenerArn"]
|
|
|
|
|
|
|
|
# Now modify the HTTPS listener with a different certificate
|
|
|
|
response = acm.request_certificate(
|
|
|
|
DomainName="yahoo.com", SubjectAlternativeNames=["yahoo.com"]
|
|
|
|
)
|
|
|
|
yahoo_arn = response["CertificateArn"]
|
|
|
|
|
|
|
|
listener = client.modify_listener(
|
|
|
|
ListenerArn=listener_arn,
|
|
|
|
Certificates=[{"CertificateArn": yahoo_arn}],
|
|
|
|
DefaultActions=[{"Type": "forward", "TargetGroupArn": target_group_arn}],
|
|
|
|
)["Listeners"][0]
|
2023-07-24 12:50:35 +00:00
|
|
|
assert listener["Certificates"] == [{"CertificateArn": yahoo_arn}]
|
2022-01-29 12:04:14 +00:00
|
|
|
|
|
|
|
listener = client.describe_listeners(ListenerArns=[listener_arn])["Listeners"][0]
|
2023-07-24 12:50:35 +00:00
|
|
|
assert listener["Certificates"] == [{"CertificateArn": yahoo_arn}]
|
2018-04-11 22:16:56 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2022-03-21 20:55:19 +00:00
|
|
|
def test_add_unknown_listener_certificate():
|
|
|
|
client = boto3.client("elbv2", region_name="eu-central-1")
|
|
|
|
with pytest.raises(ClientError) as exc:
|
|
|
|
client.add_listener_certificates(
|
|
|
|
ListenerArn="unknown", Certificates=[{"CertificateArn": "google_arn"}]
|
|
|
|
)
|
|
|
|
err = exc.value.response["Error"]
|
2023-07-24 12:50:35 +00:00
|
|
|
assert err["Code"] == "ListenerNotFound"
|
2022-03-21 20:55:19 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2022-03-21 20:55:19 +00:00
|
|
|
def test_describe_unknown_listener_certificate():
|
|
|
|
client = boto3.client("elbv2", region_name="eu-central-1")
|
|
|
|
with pytest.raises(ClientError) as exc:
|
|
|
|
client.describe_listener_certificates(ListenerArn="unknown")
|
|
|
|
err = exc.value.response["Error"]
|
2023-07-24 12:50:35 +00:00
|
|
|
assert err["Code"] == "ListenerNotFound"
|
2022-03-21 20:55:19 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2022-03-21 20:55:19 +00:00
|
|
|
def test_add_listener_certificate():
|
|
|
|
# Verify we can add a listener for a TargetGroup that is already HTTPS
|
|
|
|
client = boto3.client("elbv2", region_name="eu-central-1")
|
|
|
|
acm = boto3.client("acm", region_name="eu-central-1")
|
|
|
|
ec2 = boto3.resource("ec2", region_name="eu-central-1")
|
|
|
|
|
|
|
|
vpc = ec2.create_vpc(CidrBlock="172.28.7.0/24", InstanceTenancy="default")
|
|
|
|
subnet1 = ec2.create_subnet(
|
|
|
|
VpcId=vpc.id, CidrBlock="172.28.7.192/26", AvailabilityZone="eu-central-1a"
|
|
|
|
)
|
|
|
|
|
|
|
|
response = client.create_load_balancer(
|
|
|
|
Name="my-lb",
|
|
|
|
Subnets=[subnet1.id],
|
|
|
|
Scheme="internal",
|
|
|
|
Tags=[{"Key": "key_name", "Value": "a_value"}],
|
|
|
|
)
|
|
|
|
|
2023-07-24 12:50:35 +00:00
|
|
|
load_balancer_arn = response["LoadBalancers"][0]["LoadBalancerArn"]
|
2022-03-21 20:55:19 +00:00
|
|
|
|
2023-09-14 12:52:14 +00:00
|
|
|
response = client.create_target_group(
|
|
|
|
Name="a-target", Protocol="HTTPS", Port=8443, VpcId=vpc.id
|
|
|
|
)
|
2023-07-24 12:50:35 +00:00
|
|
|
target_group_arn = response["TargetGroups"][0]["TargetGroupArn"]
|
2022-03-21 20:55:19 +00:00
|
|
|
|
|
|
|
# HTTPS listener
|
|
|
|
response = acm.request_certificate(
|
|
|
|
DomainName="google.com", SubjectAlternativeNames=["google.com"]
|
|
|
|
)
|
|
|
|
google_arn = response["CertificateArn"]
|
|
|
|
response = client.create_listener(
|
|
|
|
LoadBalancerArn=load_balancer_arn,
|
|
|
|
Protocol="HTTPS",
|
|
|
|
Port=443,
|
|
|
|
DefaultActions=[{"Type": "forward", "TargetGroupArn": target_group_arn}],
|
|
|
|
)
|
|
|
|
listener_arn = response["Listeners"][0]["ListenerArn"]
|
|
|
|
|
|
|
|
certs = client.add_listener_certificates(
|
|
|
|
ListenerArn=listener_arn, Certificates=[{"CertificateArn": google_arn}]
|
|
|
|
)["Certificates"]
|
2023-07-24 12:50:35 +00:00
|
|
|
assert len(certs) == 1
|
|
|
|
assert certs[0]["CertificateArn"] == google_arn
|
2022-03-21 20:55:19 +00:00
|
|
|
|
|
|
|
certs = client.describe_listener_certificates(ListenerArn=listener_arn)[
|
|
|
|
"Certificates"
|
|
|
|
]
|
2023-07-24 12:50:35 +00:00
|
|
|
assert len(certs) == 1
|
|
|
|
assert certs[0]["CertificateArn"] == google_arn
|
2022-03-21 20:55:19 +00:00
|
|
|
|
|
|
|
client.remove_listener_certificates(
|
|
|
|
ListenerArn=listener_arn, Certificates=[{"CertificateArn": google_arn}]
|
|
|
|
)
|
|
|
|
|
|
|
|
certs = client.describe_listener_certificates(ListenerArn=listener_arn)[
|
|
|
|
"Certificates"
|
|
|
|
]
|
2023-07-24 12:50:35 +00:00
|
|
|
assert len(certs) == 0
|
2022-03-21 20:55:19 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2022-03-21 20:55:19 +00:00
|
|
|
def test_forward_config_action():
|
2023-09-14 12:52:14 +00:00
|
|
|
response, vpc, _, _, _, conn = create_load_balancer()
|
2023-07-24 12:50:35 +00:00
|
|
|
load_balancer_arn = response["LoadBalancers"][0]["LoadBalancerArn"]
|
2022-03-21 20:55:19 +00:00
|
|
|
|
2023-09-14 12:52:14 +00:00
|
|
|
response = conn.create_target_group(
|
|
|
|
Name="a-target", Protocol="HTTPS", Port=8443, VpcId=vpc.id
|
|
|
|
)
|
2023-07-24 12:50:35 +00:00
|
|
|
target_group_arn = response["TargetGroups"][0]["TargetGroupArn"]
|
2022-03-21 20:55:19 +00:00
|
|
|
|
|
|
|
action = {
|
|
|
|
"Type": "forward",
|
|
|
|
"ForwardConfig": {
|
|
|
|
"TargetGroups": [{"TargetGroupArn": target_group_arn, "Weight": 1}],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
expected_action = copy.deepcopy(action)
|
|
|
|
expected_action["ForwardConfig"]["TargetGroupStickinessConfig"] = {"Enabled": False}
|
|
|
|
|
|
|
|
response = conn.create_listener(
|
|
|
|
LoadBalancerArn=load_balancer_arn,
|
|
|
|
Protocol="HTTP",
|
|
|
|
Port=80,
|
|
|
|
DefaultActions=[action],
|
|
|
|
)
|
|
|
|
|
2023-07-24 12:50:35 +00:00
|
|
|
listener = response["Listeners"][0]
|
|
|
|
assert listener["DefaultActions"] == [expected_action]
|
|
|
|
listener_arn = listener["ListenerArn"]
|
2022-03-21 20:55:19 +00:00
|
|
|
|
|
|
|
describe_listener_response = conn.describe_listeners(ListenerArns=[listener_arn])
|
|
|
|
describe_listener_actions = describe_listener_response["Listeners"][0][
|
|
|
|
"DefaultActions"
|
|
|
|
]
|
2023-07-24 12:50:35 +00:00
|
|
|
assert describe_listener_actions == [expected_action]
|
2022-03-21 20:55:19 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2022-03-21 20:55:19 +00:00
|
|
|
def test_forward_config_action__with_stickiness():
|
2023-09-14 12:52:14 +00:00
|
|
|
response, vpc, _, _, _, conn = create_load_balancer()
|
2023-07-24 12:50:35 +00:00
|
|
|
load_balancer_arn = response["LoadBalancers"][0]["LoadBalancerArn"]
|
2022-03-21 20:55:19 +00:00
|
|
|
|
2023-09-14 12:52:14 +00:00
|
|
|
response = conn.create_target_group(
|
|
|
|
Name="a-target", Protocol="HTTPS", Port=8443, VpcId=vpc.id
|
|
|
|
)
|
2023-07-24 12:50:35 +00:00
|
|
|
target_group_arn = response["TargetGroups"][0]["TargetGroupArn"]
|
2022-03-21 20:55:19 +00:00
|
|
|
|
|
|
|
action = {
|
|
|
|
"Type": "forward",
|
|
|
|
"ForwardConfig": {
|
|
|
|
"TargetGroups": [{"TargetGroupArn": target_group_arn, "Weight": 1}],
|
|
|
|
"TargetGroupStickinessConfig": {"Enabled": True},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
response = conn.create_listener(
|
|
|
|
LoadBalancerArn=load_balancer_arn,
|
|
|
|
Protocol="HTTP",
|
|
|
|
Port=80,
|
|
|
|
DefaultActions=[action],
|
|
|
|
)
|
|
|
|
|
2023-07-24 12:50:35 +00:00
|
|
|
listener = response["Listeners"][0]
|
|
|
|
assert listener["DefaultActions"] == [action]
|
|
|
|
listener_arn = listener["ListenerArn"]
|
2022-03-21 20:55:19 +00:00
|
|
|
|
|
|
|
describe_listener_response = conn.describe_listeners(ListenerArns=[listener_arn])
|
|
|
|
describe_listener_actions = describe_listener_response["Listeners"][0][
|
|
|
|
"DefaultActions"
|
|
|
|
]
|
2023-07-24 12:50:35 +00:00
|
|
|
assert describe_listener_actions == [action]
|
2022-03-21 20:55:19 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2019-05-25 10:18:39 +00:00
|
|
|
def test_redirect_action_listener_rule():
|
2021-10-18 19:44:29 +00:00
|
|
|
response, _, _, _, _, conn = create_load_balancer()
|
2023-07-24 12:50:35 +00:00
|
|
|
load_balancer_arn = response["LoadBalancers"][0]["LoadBalancerArn"]
|
2019-10-31 15:44:26 +00:00
|
|
|
|
2021-06-05 09:04:04 +00:00
|
|
|
action = {
|
|
|
|
"Type": "redirect",
|
|
|
|
"RedirectConfig": {
|
|
|
|
"Protocol": "HTTPS",
|
|
|
|
"Port": "443",
|
|
|
|
"StatusCode": "HTTP_301",
|
2022-03-21 20:55:19 +00:00
|
|
|
"Host": "h",
|
|
|
|
"Path": "p",
|
|
|
|
"Query": "q",
|
2021-06-05 09:04:04 +00:00
|
|
|
},
|
2022-03-21 20:55:19 +00:00
|
|
|
"Order": 1,
|
2021-06-05 09:04:04 +00:00
|
|
|
}
|
|
|
|
|
2019-05-25 10:18:39 +00:00
|
|
|
response = conn.create_listener(
|
|
|
|
LoadBalancerArn=load_balancer_arn,
|
|
|
|
Protocol="HTTP",
|
|
|
|
Port=80,
|
2021-06-05 09:04:04 +00:00
|
|
|
DefaultActions=[action],
|
2019-05-25 10:18:39 +00:00
|
|
|
)
|
2019-10-31 15:44:26 +00:00
|
|
|
|
2023-07-24 12:50:35 +00:00
|
|
|
listener = response["Listeners"][0]
|
|
|
|
assert listener["DefaultActions"] == [action]
|
|
|
|
listener_arn = listener["ListenerArn"]
|
2019-05-25 10:18:39 +00:00
|
|
|
|
2021-10-18 19:44:29 +00:00
|
|
|
conn.create_rule(
|
2021-06-05 09:04:04 +00:00
|
|
|
ListenerArn=listener_arn,
|
|
|
|
Conditions=[{"Field": "path-pattern", "Values": ["/*"]}],
|
|
|
|
Priority=3,
|
|
|
|
Actions=[action],
|
|
|
|
)
|
2019-05-25 10:18:39 +00:00
|
|
|
describe_rules_response = conn.describe_rules(ListenerArn=listener_arn)
|
2023-07-24 12:50:35 +00:00
|
|
|
assert describe_rules_response["Rules"][0]["Actions"] == [action]
|
2019-05-25 10:18:39 +00:00
|
|
|
|
|
|
|
describe_listener_response = conn.describe_listeners(ListenerArns=[listener_arn])
|
|
|
|
describe_listener_actions = describe_listener_response["Listeners"][0][
|
|
|
|
"DefaultActions"
|
|
|
|
]
|
2023-07-24 12:50:35 +00:00
|
|
|
assert describe_listener_actions == [action]
|
2019-05-25 10:18:39 +00:00
|
|
|
|
|
|
|
modify_listener_response = conn.modify_listener(ListenerArn=listener_arn, Port=81)
|
|
|
|
modify_listener_actions = modify_listener_response["Listeners"][0]["DefaultActions"]
|
2023-07-24 12:50:35 +00:00
|
|
|
assert modify_listener_actions == [action]
|
2019-05-25 10:18:39 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2019-08-10 04:34:52 +00:00
|
|
|
def test_cognito_action_listener_rule():
|
2021-10-18 19:44:29 +00:00
|
|
|
response, _, _, _, _, conn = create_load_balancer()
|
2023-07-24 12:50:35 +00:00
|
|
|
load_balancer_arn = response["LoadBalancers"][0]["LoadBalancerArn"]
|
2019-08-10 04:34:52 +00:00
|
|
|
|
|
|
|
action = {
|
|
|
|
"Type": "authenticate-cognito",
|
|
|
|
"AuthenticateCognitoConfig": {
|
2022-11-17 22:41:08 +00:00
|
|
|
"UserPoolArn": f"arn:aws:cognito-idp:us-east-1:{ACCOUNT_ID}:userpool/us-east-1_ABCD1234",
|
2019-08-10 04:34:52 +00:00
|
|
|
"UserPoolClientId": "abcd1234abcd",
|
|
|
|
"UserPoolDomain": "testpool",
|
2022-03-21 20:55:19 +00:00
|
|
|
"AuthenticationRequestExtraParams": {"param": "test"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
response = conn.create_listener(
|
|
|
|
LoadBalancerArn=load_balancer_arn,
|
|
|
|
Protocol="HTTP",
|
|
|
|
Port=80,
|
|
|
|
DefaultActions=[action],
|
|
|
|
)
|
|
|
|
|
2023-07-24 12:50:35 +00:00
|
|
|
listener = response["Listeners"][0]
|
|
|
|
assert listener["DefaultActions"][0] == action
|
|
|
|
listener_arn = listener["ListenerArn"]
|
2022-03-21 20:55:19 +00:00
|
|
|
|
|
|
|
conn.create_rule(
|
|
|
|
ListenerArn=listener_arn,
|
|
|
|
Conditions=[{"Field": "path-pattern", "Values": ["/*"]}],
|
|
|
|
Priority=3,
|
|
|
|
Actions=[action],
|
|
|
|
)
|
|
|
|
describe_rules_response = conn.describe_rules(ListenerArn=listener_arn)
|
2023-07-24 12:50:35 +00:00
|
|
|
assert describe_rules_response["Rules"][0]["Actions"][0] == action
|
2022-03-21 20:55:19 +00:00
|
|
|
|
|
|
|
describe_listener_response = conn.describe_listeners(ListenerArns=[listener_arn])
|
|
|
|
describe_listener_actions = describe_listener_response["Listeners"][0][
|
|
|
|
"DefaultActions"
|
|
|
|
][0]
|
2023-07-24 12:50:35 +00:00
|
|
|
assert describe_listener_actions == action
|
2022-03-21 20:55:19 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2022-03-21 20:55:19 +00:00
|
|
|
def test_oidc_action_listener__simple():
|
|
|
|
response, _, _, _, _, conn = create_load_balancer()
|
2023-07-24 12:50:35 +00:00
|
|
|
load_balancer_arn = response["LoadBalancers"][0]["LoadBalancerArn"]
|
2022-03-21 20:55:19 +00:00
|
|
|
|
|
|
|
action = {
|
|
|
|
"Type": "authenticate-oidc",
|
|
|
|
"AuthenticateOidcConfig": {
|
|
|
|
"AuthorizationEndpoint": "ae",
|
|
|
|
"ClientId": "ci",
|
|
|
|
"TokenEndpoint": "te",
|
|
|
|
"UserInfoEndpoint": "uie",
|
|
|
|
"Issuer": "is",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
response = conn.create_listener(
|
|
|
|
LoadBalancerArn=load_balancer_arn,
|
|
|
|
Protocol="HTTP",
|
|
|
|
Port=80,
|
|
|
|
DefaultActions=[action],
|
|
|
|
)
|
|
|
|
|
2023-07-24 12:50:35 +00:00
|
|
|
listener = response["Listeners"][0]
|
|
|
|
assert listener["DefaultActions"][0] == action
|
|
|
|
listener_arn = listener["ListenerArn"]
|
2022-03-21 20:55:19 +00:00
|
|
|
|
|
|
|
conn.create_rule(
|
|
|
|
ListenerArn=listener_arn,
|
|
|
|
Conditions=[{"Field": "path-pattern", "Values": ["/*"]}],
|
|
|
|
Priority=3,
|
|
|
|
Actions=[action],
|
|
|
|
)
|
|
|
|
describe_rules_response = conn.describe_rules(ListenerArn=listener_arn)
|
2023-07-24 12:50:35 +00:00
|
|
|
assert describe_rules_response["Rules"][0]["Actions"][0] == action
|
2022-03-21 20:55:19 +00:00
|
|
|
|
|
|
|
describe_listener_response = conn.describe_listeners(ListenerArns=[listener_arn])
|
|
|
|
describe_listener_actions = describe_listener_response["Listeners"][0][
|
|
|
|
"DefaultActions"
|
|
|
|
][0]
|
2023-07-24 12:50:35 +00:00
|
|
|
assert describe_listener_actions == action
|
2022-03-21 20:55:19 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2022-03-21 20:55:19 +00:00
|
|
|
@pytest.mark.parametrize("use_secret", [True, False])
|
|
|
|
def test_oidc_action_listener(use_secret):
|
|
|
|
response, _, _, _, _, conn = create_load_balancer()
|
2023-07-24 12:50:35 +00:00
|
|
|
load_balancer_arn = response["LoadBalancers"][0]["LoadBalancerArn"]
|
2022-03-21 20:55:19 +00:00
|
|
|
|
|
|
|
action = {
|
|
|
|
"Type": "authenticate-oidc",
|
|
|
|
"AuthenticateOidcConfig": {
|
|
|
|
"Issuer": "is",
|
|
|
|
"AuthorizationEndpoint": "ae",
|
|
|
|
"TokenEndpoint": "te",
|
|
|
|
"UserInfoEndpoint": "uie",
|
|
|
|
"ClientId": "ci",
|
|
|
|
"ClientSecret": "cs",
|
|
|
|
"SessionCookieName": "scn",
|
|
|
|
"Scope": "s",
|
|
|
|
"SessionTimeout": 42,
|
|
|
|
"AuthenticationRequestExtraParams": {"param": "test"},
|
|
|
|
"OnUnauthenticatedRequest": "our",
|
|
|
|
"UseExistingClientSecret": use_secret,
|
2019-08-10 04:34:52 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
response = conn.create_listener(
|
|
|
|
LoadBalancerArn=load_balancer_arn,
|
|
|
|
Protocol="HTTP",
|
|
|
|
Port=80,
|
|
|
|
DefaultActions=[action],
|
|
|
|
)
|
|
|
|
|
2023-07-24 12:50:35 +00:00
|
|
|
listener = response["Listeners"][0]
|
|
|
|
assert listener["DefaultActions"][0] == action
|
|
|
|
listener_arn = listener["ListenerArn"]
|
2019-08-10 04:34:52 +00:00
|
|
|
|
2021-10-18 19:44:29 +00:00
|
|
|
conn.create_rule(
|
2021-06-05 09:04:04 +00:00
|
|
|
ListenerArn=listener_arn,
|
|
|
|
Conditions=[{"Field": "path-pattern", "Values": ["/*"]}],
|
|
|
|
Priority=3,
|
|
|
|
Actions=[action],
|
|
|
|
)
|
2019-08-10 04:34:52 +00:00
|
|
|
describe_rules_response = conn.describe_rules(ListenerArn=listener_arn)
|
2023-07-24 12:50:35 +00:00
|
|
|
assert describe_rules_response["Rules"][0]["Actions"][0] == action
|
2019-08-10 04:34:52 +00:00
|
|
|
|
|
|
|
describe_listener_response = conn.describe_listeners(ListenerArns=[listener_arn])
|
|
|
|
describe_listener_actions = describe_listener_response["Listeners"][0][
|
|
|
|
"DefaultActions"
|
|
|
|
][0]
|
2023-07-24 12:50:35 +00:00
|
|
|
assert describe_listener_actions == action
|
2019-08-10 04:34:52 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2019-09-12 15:24:47 +00:00
|
|
|
def test_fixed_response_action_listener_rule():
|
2021-10-18 19:44:29 +00:00
|
|
|
response, _, _, _, _, conn = create_load_balancer()
|
2023-07-24 12:50:35 +00:00
|
|
|
load_balancer_arn = response["LoadBalancers"][0]["LoadBalancerArn"]
|
2019-09-12 15:24:47 +00:00
|
|
|
|
|
|
|
action = {
|
|
|
|
"Type": "fixed-response",
|
|
|
|
"FixedResponseConfig": {
|
|
|
|
"ContentType": "text/plain",
|
|
|
|
"MessageBody": "This page does not exist",
|
|
|
|
"StatusCode": "404",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
response = conn.create_listener(
|
|
|
|
LoadBalancerArn=load_balancer_arn,
|
|
|
|
Protocol="HTTP",
|
|
|
|
Port=80,
|
|
|
|
DefaultActions=[action],
|
|
|
|
)
|
|
|
|
|
2023-07-24 12:50:35 +00:00
|
|
|
listener = response["Listeners"][0]
|
|
|
|
assert listener["DefaultActions"][0] == action
|
|
|
|
listener_arn = listener["ListenerArn"]
|
2019-09-12 15:24:47 +00:00
|
|
|
|
2021-10-18 19:44:29 +00:00
|
|
|
conn.create_rule(
|
2021-06-05 09:04:04 +00:00
|
|
|
ListenerArn=listener_arn,
|
|
|
|
Conditions=[{"Field": "path-pattern", "Values": ["/*"]}],
|
|
|
|
Priority=3,
|
|
|
|
Actions=[action],
|
|
|
|
)
|
2019-09-12 15:24:47 +00:00
|
|
|
describe_rules_response = conn.describe_rules(ListenerArn=listener_arn)
|
2023-07-24 12:50:35 +00:00
|
|
|
assert describe_rules_response["Rules"][0]["Actions"][0] == action
|
2019-09-12 15:24:47 +00:00
|
|
|
|
|
|
|
describe_listener_response = conn.describe_listeners(ListenerArns=[listener_arn])
|
|
|
|
describe_listener_actions = describe_listener_response["Listeners"][0][
|
|
|
|
"DefaultActions"
|
|
|
|
][0]
|
2023-07-24 12:50:35 +00:00
|
|
|
assert describe_listener_actions == action
|
2019-09-12 15:24:47 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2019-09-12 16:29:03 +00:00
|
|
|
def test_fixed_response_action_listener_rule_validates_status_code():
|
2021-10-18 19:44:29 +00:00
|
|
|
response, _, _, _, _, conn = create_load_balancer()
|
2023-07-24 12:50:35 +00:00
|
|
|
load_balancer_arn = response["LoadBalancers"][0]["LoadBalancerArn"]
|
2019-09-12 16:29:03 +00:00
|
|
|
|
2021-01-31 12:21:24 +00:00
|
|
|
invalid_status_code_action = {
|
2019-09-12 16:29:03 +00:00
|
|
|
"Type": "fixed-response",
|
|
|
|
"FixedResponseConfig": {
|
|
|
|
"ContentType": "text/plain",
|
|
|
|
"MessageBody": "This page does not exist",
|
2021-01-31 12:21:24 +00:00
|
|
|
"StatusCode": "100",
|
2019-09-12 16:29:03 +00:00
|
|
|
},
|
|
|
|
}
|
2021-01-31 12:21:24 +00:00
|
|
|
|
2023-07-24 12:50:35 +00:00
|
|
|
with pytest.raises(ClientError) as exc:
|
2019-09-12 16:29:03 +00:00
|
|
|
conn.create_listener(
|
|
|
|
LoadBalancerArn=load_balancer_arn,
|
|
|
|
Protocol="HTTP",
|
|
|
|
Port=80,
|
2021-01-31 12:21:24 +00:00
|
|
|
DefaultActions=[invalid_status_code_action],
|
2019-09-12 16:29:03 +00:00
|
|
|
)
|
|
|
|
|
2023-07-24 12:50:35 +00:00
|
|
|
assert exc.value.response["Error"]["Code"] == "ValidationError"
|
2019-09-12 16:29:03 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2019-09-12 16:29:03 +00:00
|
|
|
def test_fixed_response_action_listener_rule_validates_content_type():
|
2021-10-18 19:44:29 +00:00
|
|
|
response, _, _, _, _, conn = create_load_balancer()
|
2023-07-24 12:50:35 +00:00
|
|
|
load_balancer_arn = response["LoadBalancers"][0]["LoadBalancerArn"]
|
2019-09-12 16:29:03 +00:00
|
|
|
|
|
|
|
invalid_content_type_action = {
|
|
|
|
"Type": "fixed-response",
|
|
|
|
"FixedResponseConfig": {
|
|
|
|
"ContentType": "Fake content type",
|
|
|
|
"MessageBody": "This page does not exist",
|
|
|
|
"StatusCode": "200",
|
|
|
|
},
|
|
|
|
}
|
2023-07-24 12:50:35 +00:00
|
|
|
with pytest.raises(ClientError) as exc:
|
2019-09-12 16:29:03 +00:00
|
|
|
conn.create_listener(
|
|
|
|
LoadBalancerArn=load_balancer_arn,
|
|
|
|
Protocol="HTTP",
|
|
|
|
Port=80,
|
|
|
|
DefaultActions=[invalid_content_type_action],
|
|
|
|
)
|
2023-07-24 12:50:35 +00:00
|
|
|
assert exc.value.response["Error"]["Code"] == "InvalidLoadBalancerAction"
|
2022-03-21 20:55:19 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2022-03-21 20:55:19 +00:00
|
|
|
def test_create_listener_with_alpn_policy():
|
|
|
|
response, _, _, _, _, conn = create_load_balancer()
|
2023-07-24 12:50:35 +00:00
|
|
|
load_balancer_arn = response["LoadBalancers"][0]["LoadBalancerArn"]
|
2022-03-21 20:55:19 +00:00
|
|
|
|
|
|
|
response = conn.create_listener(
|
|
|
|
LoadBalancerArn=load_balancer_arn,
|
|
|
|
Protocol="HTTP",
|
|
|
|
Port=80,
|
|
|
|
DefaultActions=[],
|
|
|
|
AlpnPolicy=["pol1", "pol2"],
|
|
|
|
)
|
|
|
|
|
2023-07-24 12:50:35 +00:00
|
|
|
listener = response["Listeners"][0]
|
2022-03-21 20:55:19 +00:00
|
|
|
listener_arn = listener["ListenerArn"]
|
2023-07-24 12:50:35 +00:00
|
|
|
assert listener["AlpnPolicy"] == ["pol1", "pol2"]
|
2022-03-21 20:55:19 +00:00
|
|
|
|
|
|
|
describe = conn.describe_listeners(ListenerArns=[listener_arn])["Listeners"][0]
|
2023-07-24 12:50:35 +00:00
|
|
|
assert describe["AlpnPolicy"] == ["pol1", "pol2"]
|