Skip to content

Commit

Permalink
EC2: create_route() should allow overlapping CIDR's (#6106)
Browse files Browse the repository at this point in the history
  • Loading branch information
bblommers committed Mar 22, 2023
1 parent 681db43 commit 53603b0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
4 changes: 2 additions & 2 deletions moto/ec2/models/route_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ def __validate_destination_cidr_block(
for route in route_table.routes.values():
if not route.destination_cidr_block:
continue
if not route.local and ip_v4_network.overlaps(
ipaddress.IPv4Network(str(route.destination_cidr_block))
if not route.local and ip_v4_network == ipaddress.IPv4Network(
str(route.destination_cidr_block)
):
raise RouteAlreadyExistsError(destination_cidr_block)
22 changes: 12 additions & 10 deletions tests/test_ec2/test_route_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -664,16 +664,18 @@ def test_routes_already_exist():
ex.value.response["ResponseMetadata"].should.have.key("RequestId")
ex.value.response["Error"]["Code"].should.equal("RouteAlreadyExists")

with pytest.raises(ClientError) as ex:
client.create_route(
RouteTableId=main_route_table.id,
DestinationCidrBlock=ROUTE_SUB_CIDR,
GatewayId=igw.id,
)

ex.value.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400)
ex.value.response["ResponseMetadata"].should.have.key("RequestId")
ex.value.response["Error"]["Code"].should.equal("RouteAlreadyExists")
# We can create a sub cidr
client.create_route(
RouteTableId=main_route_table.id,
DestinationCidrBlock=ROUTE_SUB_CIDR,
GatewayId=igw.id,
)
# Or even a catch-all
client.create_route(
RouteTableId=main_route_table.id,
DestinationCidrBlock="0.0.0.0/0",
GatewayId=igw.id,
)


@mock_ec2
Expand Down

0 comments on commit 53603b0

Please sign in to comment.