mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(ec2): report NAT gateways as zonal and honor PrivateIpAddress (#7059)
* fix(ec2): report NAT gateways as zonal and honor PrivateIpAddress The Terraform AWS provider reads network_interface_id, private_ip and public_ip from a NAT gateway only when it is reported with availabilityMode zonal. Floci never sent availabilityMode, so all three stayed null. Every gateway Floci creates sits in one subnet, so the responses now say zonal. Floci has no regional NAT gateways. A regional request that named only its VPC failed on the missing subnet, and one that also named a subnet got a zonal gateway, through both CreateNatGateway and AWS::EC2::NatGateway. Both now refuse AvailabilityMode regional with UnsupportedOperation. Any other value except zonal, such as Regional, fails with InvalidParameterValue instead of creating a zonal gateway. Once Terraform reads private_ip back, the gateway has to hold the address it was asked for. private_ip forces a new gateway, and CreateNatGateway ignored PrivateIpAddress, so a config that set it would plan a replacement on every run. CreateNatGateway and AWS::EC2::NatGateway now give the gateway's interface the requested address, checked by the same rules as RunInstances: inside the subnet, not reserved and not in use. A changed PrivateIpAddress replaces the CloudFormation resource, as it is createOnly. A gateway has its primary address only, so SecondaryAllocationIds, SecondaryPrivateIpAddresses and SecondaryPrivateIpAddressCount are refused with UnsupportedOperation instead of being dropped. AWS::EC2::NatGateway also sets EniId, the gateway's primary interface, which Fn::GetAtt resolved to the literal "Nat.EniId" although the gateway has had a real interface since #5622. Closes #6767 * fix(ec2): take no VPC network lease for a NAT gateway's requested address DeleteNatGateway never returns a lease, so the one CreateNatGateway took for its PrivateIpAddress kept the address from automatic allocation after the gateway was gone. Check that the address is free the way CreateNetworkInterface does, without the lease; automatic allocation already steps around addresses a stored NAT gateway holds. Ec2NatGatewayAddressIntegrationTest now sends its requests through the shared Ec2Requests.ec2 instead of its own copy.
This commit is contained in:
1 parent
2ad35bba1d
commit
dcf8ec7430
13 files changed
+519
-244
No files matched your search
+11
-2
@@ -380,7 +380,8 @@ class CloudFormationEc2NetworkingIntegrationTest {
|
||||
"AssocId": {"Value": {"Ref": "PublicAssoc"}},
|
||||
"EipRef": {"Value": {"Ref": "NatEip"}},
|
||||
"EipAllocationId": {"Value": {"Fn::GetAtt": ["NatEip", "AllocationId"]}},
|
||||
"NatId": {"Value": {"Ref": "Nat"}}
|
||||
"NatId": {"Value": {"Ref": "Nat"}},
|
||||
"NatEniId": {"Value": {"Fn::GetAtt": ["Nat", "EniId"]}}
|
||||
}
|
||||
}
|
||||
""".formatted(vpcId, publicSubnetCidr, extraSubnetTag);
|
||||
@@ -411,6 +412,7 @@ class CloudFormationEc2NetworkingIntegrationTest {
|
||||
assertTrue(out.get("EipAllocationId").startsWith("eipalloc-"), out.get("EipAllocationId"));
|
||||
assertFalse(out.get("EipRef").startsWith("eipalloc-"), "Ref of an EIP is its public IP: " + out.get("EipRef"));
|
||||
assertTrue(out.get("NatId").startsWith("nat-"), out.get("NatId"));
|
||||
assertTrue(out.get("NatEniId").startsWith("eni-"), out.get("NatEniId"));
|
||||
|
||||
String subnets = ec2("DescribeSubnets");
|
||||
assertTrue(subnets.contains(out.get("SubnetId")));
|
||||
@@ -419,7 +421,10 @@ class CloudFormationEc2NetworkingIntegrationTest {
|
||||
assertTrue(routeTables.contains("<value>public-rt</value>"), "route table tags must reach EC2");
|
||||
assertTrue(routeTables.contains(out.get("PublicRtId")) && routeTables.contains(out.get("AssocId")));
|
||||
assertTrue(routeTables.contains(out.get("NatId")), "the private route must point at the NAT gateway");
|
||||
assertTrue(ec2("DescribeNatGateways").contains(out.get("NatId")));
|
||||
String natGateways = ec2("DescribeNatGateways");
|
||||
assertTrue(natGateways.contains(out.get("NatId")));
|
||||
assertTrue(natGateways.contains("<networkInterfaceId>" + out.get("NatEniId") + "</networkInterfaceId>"),
|
||||
"EniId must be the gateway's own interface");
|
||||
assertTrue(ec2("DescribeAddresses").contains(out.get("EipAllocationId")));
|
||||
assertTrue(ec2("DescribeInternetGateways").contains(out.get("IgwId")));
|
||||
|
||||
@@ -452,6 +457,10 @@ class CloudFormationEc2NetworkingIntegrationTest {
|
||||
Map<String, String> replaced = XmlParser.extractPairs(describeStacks("UPDATE_COMPLETE"), "Outputs", "OutputKey", "OutputValue");
|
||||
assertFalse(out.get("SubnetId").equals(replaced.get("SubnetId")), "a changed CIDR must replace the subnet");
|
||||
assertFalse(out.get("NatId").equals(replaced.get("NatId")), "the NAT gateway follows its subnet");
|
||||
assertFalse(out.get("NatEniId").equals(replaced.get("NatEniId")), "EniId must not be the displaced gateway's");
|
||||
assertTrue(ec2("DescribeNatGateways", Map.of("NatGatewayId.1", replaced.get("NatId")))
|
||||
.contains("<networkInterfaceId>" + replaced.get("NatEniId") + "</networkInterfaceId>"),
|
||||
"EniId must be the replacement gateway's own interface");
|
||||
assertFalse(out.get("AssocId").equals(replaced.get("AssocId")), "the association follows its subnet");
|
||||
assertEquals(out.get("IgwId"), replaced.get("IgwId"));
|
||||
assertFalse(out.get("EipAllocationId").equals(replaced.get("EipAllocationId")), "the gateway takes the new EIP");
|
||||
|
||||
+117
-29
@@ -12,6 +12,7 @@ import io.github.hectorvent.floci.services.ec2.model.Address;
|
||||
import io.github.hectorvent.floci.services.ec2.model.InternetGateway;
|
||||
import io.github.hectorvent.floci.services.ec2.model.ManagedPrefixList;
|
||||
import io.github.hectorvent.floci.services.ec2.model.NatGateway;
|
||||
import io.github.hectorvent.floci.services.ec2.model.NatGatewayAddress;
|
||||
import io.github.hectorvent.floci.services.ec2.model.PrefixListEntry;
|
||||
import io.github.hectorvent.floci.services.ec2.model.Route;
|
||||
import io.github.hectorvent.floci.services.ec2.model.RouteTable;
|
||||
@@ -99,6 +100,22 @@ class Ec2NetworkCfnProvisionerTest {
|
||||
return r;
|
||||
}
|
||||
|
||||
private static NatGateway natGateway(String natGatewayId) {
|
||||
NatGateway nat = new NatGateway();
|
||||
nat.setNatGatewayId(natGatewayId);
|
||||
return nat;
|
||||
}
|
||||
|
||||
/** A gateway with the one primary address CreateNatGateway gives it. */
|
||||
private static NatGateway natGateway(String natGatewayId, String eniId, String privateIp) {
|
||||
NatGateway nat = natGateway(natGatewayId);
|
||||
NatGatewayAddress primary = new NatGatewayAddress();
|
||||
primary.setNetworkInterfaceId(eniId);
|
||||
primary.setPrivateIp(privateIp);
|
||||
nat.getNatGatewayAddresses().add(primary);
|
||||
return nat;
|
||||
}
|
||||
|
||||
private static Subnet subnet() {
|
||||
Subnet s = new Subnet();
|
||||
s.setSubnetId(SUBNET_ID);
|
||||
@@ -201,16 +218,16 @@ class Ec2NetworkCfnProvisionerTest {
|
||||
|
||||
@Test
|
||||
void natGatewayIsCreatedPublicWithNoTags() {
|
||||
NatGateway nat = new NatGateway();
|
||||
nat.setNatGatewayId(NAT_ID);
|
||||
when(ec2.createNatGateway(REGION, SUBNET_ID, ALLOC_ID, "public", List.of())).thenReturn(nat);
|
||||
NatGateway nat = natGateway(NAT_ID, "eni-0123456789abcdef0", "10.0.1.10");
|
||||
when(ec2.createNatGateway(REGION, SUBNET_ID, ALLOC_ID, "public", null, List.of())).thenReturn(nat);
|
||||
StackResource r = resource("AWS::EC2::NatGateway", "Nat");
|
||||
|
||||
provisioner.provision(r, mapper.createObjectNode().put("SubnetId", SUBNET_ID).put("AllocationId", ALLOC_ID), ctx());
|
||||
|
||||
assertEquals(NAT_ID, r.getPhysicalId());
|
||||
assertEquals(Set.of("NatGatewayId"), r.getAttributes().keySet().stream()
|
||||
assertEquals(Set.of("NatGatewayId", "EniId"), r.getAttributes().keySet().stream()
|
||||
.filter(key -> !key.startsWith("__Floci")).collect(Collectors.toSet()));
|
||||
assertEquals("eni-0123456789abcdef0", r.getAttributes().get("EniId"));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -219,10 +236,9 @@ class Ec2NetworkCfnProvisionerTest {
|
||||
*/
|
||||
@Test
|
||||
void natGatewayRecordsTheSubnetAndElasticIpItUsesUnderHiddenKeys() {
|
||||
NatGateway nat = new NatGateway();
|
||||
nat.setNatGatewayId(NAT_ID);
|
||||
NatGateway nat = natGateway(NAT_ID);
|
||||
nat.setSubnetId(SUBNET_ID);
|
||||
when(ec2.createNatGateway(REGION, SUBNET_ID, ALLOC_ID, "public", List.of())).thenReturn(nat);
|
||||
when(ec2.createNatGateway(REGION, SUBNET_ID, ALLOC_ID, "public", null, List.of())).thenReturn(nat);
|
||||
Address addr = new Address();
|
||||
addr.setAllocationId(ALLOC_ID);
|
||||
addr.setPublicIp(PUBLIC_IP);
|
||||
@@ -462,19 +478,17 @@ class Ec2NetworkCfnProvisionerTest {
|
||||
|
||||
@Test
|
||||
void aNatGatewayWithTheSameSubnetAndAllocationIsKeptAndAChangedSubnetReplacesIt() {
|
||||
NatGateway nat = new NatGateway();
|
||||
nat.setNatGatewayId(NAT_ID);
|
||||
NatGateway nat = natGateway(NAT_ID);
|
||||
nat.setSubnetId(SUBNET_ID);
|
||||
nat.setAllocationId(ALLOC_ID);
|
||||
when(ec2.describeNatGateways(REGION, List.of(NAT_ID), Map.of())).thenReturn(List.of(nat));
|
||||
StackResource same = prior("AWS::EC2::NatGateway", "Nat", NAT_ID, Map.of("NatGatewayId", NAT_ID));
|
||||
provisioner.provision(same, mapper.createObjectNode().put("SubnetId", SUBNET_ID).put("AllocationId", ALLOC_ID), ctx(NAT_ID));
|
||||
verify(ec2, never()).createNatGateway(any(), any(), any(), any(), any());
|
||||
verify(ec2, never()).createNatGateway(any(), any(), any(), any(), any(), any());
|
||||
assertEquals(NAT_ID, same.getPhysicalId());
|
||||
|
||||
NatGateway replacement = new NatGateway();
|
||||
replacement.setNatGatewayId("nat-replacement");
|
||||
when(ec2.createNatGateway(REGION, "subnet-other", ALLOC_ID, "public", List.of())).thenReturn(replacement);
|
||||
NatGateway replacement = natGateway("nat-replacement");
|
||||
when(ec2.createNatGateway(REGION, "subnet-other", ALLOC_ID, "public", null, List.of())).thenReturn(replacement);
|
||||
StackResource moved = prior("AWS::EC2::NatGateway", "Nat", NAT_ID, Map.of("NatGatewayId", NAT_ID));
|
||||
provisioner.provision(moved, mapper.createObjectNode().put("SubnetId", "subnet-other").put("AllocationId", ALLOC_ID), ctx(NAT_ID));
|
||||
assertEquals("nat-replacement", moved.getPhysicalId());
|
||||
@@ -607,9 +621,8 @@ class Ec2NetworkCfnProvisionerTest {
|
||||
|
||||
@Test
|
||||
void aPrivateNatGatewayNeedsNoAllocationAndAPublicOneRequiresIt() {
|
||||
NatGateway nat = new NatGateway();
|
||||
nat.setNatGatewayId(NAT_ID);
|
||||
when(ec2.createNatGateway(eq(REGION), eq(SUBNET_ID), isNull(), eq("private"), any())).thenReturn(nat);
|
||||
NatGateway nat = natGateway(NAT_ID);
|
||||
when(ec2.createNatGateway(eq(REGION), eq(SUBNET_ID), isNull(), eq("private"), isNull(), any())).thenReturn(nat);
|
||||
StackResource r = resource("AWS::EC2::NatGateway", "Nat");
|
||||
|
||||
provisioner.provision(r, mapper.createObjectNode().put("SubnetId", SUBNET_ID).put("ConnectivityType", "private"), ctx());
|
||||
@@ -623,37 +636,113 @@ class Ec2NetworkCfnProvisionerTest {
|
||||
assertEquals("ValidationError", e2.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aRegionalNatGatewayIsRefusedRatherThanCreatedZonal() {
|
||||
// A regional template names its VPC and no subnet; one that names a subnet as well must not
|
||||
// quietly get a zonal gateway either.
|
||||
for (ObjectNode props : List.of(
|
||||
mapper.createObjectNode().put("AvailabilityMode", "regional").put("VpcId", VPC_ID),
|
||||
mapper.createObjectNode().put("AvailabilityMode", "regional").put("SubnetId", SUBNET_ID)
|
||||
.put("AllocationId", ALLOC_ID))) {
|
||||
AwsException e = assertThrows(AwsException.class,
|
||||
() -> provisioner.provision(resource("AWS::EC2::NatGateway", "Nat"), props, ctx()));
|
||||
assertEquals("UnsupportedOperation", e.getErrorCode());
|
||||
}
|
||||
// The schema's enum is exact-case, so a value that is neither mode is invalid, not zonal.
|
||||
AwsException invalid = assertThrows(AwsException.class, () -> provisioner.provision(resource("AWS::EC2::NatGateway", "Nat"),
|
||||
mapper.createObjectNode().put("AvailabilityMode", "Regional").put("SubnetId", SUBNET_ID)
|
||||
.put("ConnectivityType", "private"), ctx()));
|
||||
assertEquals("InvalidParameterValue", invalid.getErrorCode());
|
||||
verify(ec2, never()).createNatGateway(any(), any(), any(), any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aNatGatewayWithSecondaryAddressesIsRefusedRatherThanCreatedWithoutThem() {
|
||||
ObjectNode withAllocations = mapper.createObjectNode().put("SubnetId", SUBNET_ID).put("AllocationId", ALLOC_ID);
|
||||
withAllocations.putArray("SecondaryAllocationIds").add("eipalloc-0fedcba9876543210");
|
||||
ObjectNode withAddresses = mapper.createObjectNode().put("SubnetId", SUBNET_ID).put("ConnectivityType", "private");
|
||||
withAddresses.putArray("SecondaryPrivateIpAddresses").add("10.0.1.70");
|
||||
for (ObjectNode props : List.of(withAllocations, withAddresses,
|
||||
mapper.createObjectNode().put("SubnetId", SUBNET_ID).put("ConnectivityType", "private")
|
||||
.put("SecondaryPrivateIpAddressCount", 2))) {
|
||||
AwsException e = assertThrows(AwsException.class,
|
||||
() -> provisioner.provision(resource("AWS::EC2::NatGateway", "Nat"), props, ctx()));
|
||||
assertEquals("UnsupportedOperation", e.getErrorCode());
|
||||
}
|
||||
verify(ec2, never()).createNatGateway(any(), any(), any(), any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aNatGatewayIsCreatedWithThePrivateIpAddressTheTemplateNames() {
|
||||
when(ec2.createNatGateway(REGION, SUBNET_ID, null, "private", "10.0.1.50", List.of()))
|
||||
.thenReturn(natGateway(NAT_ID, "eni-0123456789abcdef0", "10.0.1.50"));
|
||||
StackResource r = resource("AWS::EC2::NatGateway", "Nat");
|
||||
|
||||
provisioner.provision(r, mapper.createObjectNode().put("SubnetId", SUBNET_ID).put("ConnectivityType", "private")
|
||||
.put("PrivateIpAddress", "10.0.1.50"), ctx());
|
||||
|
||||
verify(ec2).createNatGateway(REGION, SUBNET_ID, null, "private", "10.0.1.50", List.of());
|
||||
assertEquals(NAT_ID, r.getPhysicalId());
|
||||
}
|
||||
|
||||
/**
|
||||
* PrivateIpAddress is createOnly: the gateway's own address keeps it, in whatever form the
|
||||
* template writes it, and so does a template that names none. A different address replaces it.
|
||||
*/
|
||||
@Test
|
||||
void aNatGatewayKeepsItsPrivateIpAddressAndAChangedOneReplacesIt() {
|
||||
NatGateway nat = natGateway(NAT_ID, "eni-0123456789abcdef0", "10.0.1.50");
|
||||
nat.setSubnetId(SUBNET_ID);
|
||||
nat.setConnectivityType("private");
|
||||
when(ec2.describeNatGateways(REGION, List.of(NAT_ID), Map.of())).thenReturn(List.of(nat));
|
||||
ObjectNode privateNat = mapper.createObjectNode().put("SubnetId", SUBNET_ID).put("ConnectivityType", "private");
|
||||
for (ObjectNode props : List.of(privateNat.deepCopy().put("PrivateIpAddress", "10.0.1.50"),
|
||||
privateNat.deepCopy().put("PrivateIpAddress", "10.0.1.050"), privateNat)) {
|
||||
StackResource kept = prior("AWS::EC2::NatGateway", "Nat", NAT_ID, Map.of("NatGatewayId", NAT_ID));
|
||||
provisioner.provision(kept, props, ctx(NAT_ID));
|
||||
assertEquals(NAT_ID, kept.getPhysicalId());
|
||||
}
|
||||
verify(ec2, never()).createNatGateway(any(), any(), any(), any(), any(), any());
|
||||
|
||||
when(ec2.createNatGateway(REGION, SUBNET_ID, null, "private", "10.0.1.60", List.of()))
|
||||
.thenReturn(natGateway("nat-replacement", "eni-0fedcba9876543210", "10.0.1.60"));
|
||||
StackResource moved = prior("AWS::EC2::NatGateway", "Nat", NAT_ID, Map.of("NatGatewayId", NAT_ID));
|
||||
provisioner.provision(moved, privateNat.deepCopy().put("PrivateIpAddress", "10.0.1.60"), ctx(NAT_ID));
|
||||
assertEquals("nat-replacement", moved.getPhysicalId());
|
||||
assertEquals(NAT_ID, provisioner.updateCleanupPhysicalId(moved));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aNatGatewayWhoseConnectivityTypeChangedIsReplaced() {
|
||||
NatGateway nat = new NatGateway();
|
||||
nat.setNatGatewayId(NAT_ID);
|
||||
NatGateway nat = natGateway(NAT_ID);
|
||||
nat.setSubnetId(SUBNET_ID);
|
||||
nat.setAllocationId(ALLOC_ID);
|
||||
nat.setConnectivityType("public");
|
||||
when(ec2.describeNatGateways(REGION, List.of(NAT_ID), Map.of())).thenReturn(List.of(nat));
|
||||
NatGateway replacement = new NatGateway();
|
||||
replacement.setNatGatewayId("nat-private");
|
||||
when(ec2.createNatGateway(eq(REGION), eq(SUBNET_ID), isNull(), eq("private"), any())).thenReturn(replacement);
|
||||
StackResource r = prior("AWS::EC2::NatGateway", "Nat", NAT_ID, Map.of("NatGatewayId", NAT_ID));
|
||||
NatGateway replacement = natGateway("nat-private", "eni-0fedcba9876543210", "10.0.1.11");
|
||||
when(ec2.createNatGateway(eq(REGION), eq(SUBNET_ID), isNull(), eq("private"), isNull(), any())).thenReturn(replacement);
|
||||
StackResource r = prior("AWS::EC2::NatGateway", "Nat", NAT_ID,
|
||||
Map.of("NatGatewayId", NAT_ID, "EniId", "eni-0123456789abcdef0"));
|
||||
|
||||
provisioner.provision(r, mapper.createObjectNode().put("SubnetId", SUBNET_ID).put("ConnectivityType", "private"), ctx(NAT_ID));
|
||||
|
||||
assertEquals("nat-private", r.getPhysicalId());
|
||||
assertEquals(NAT_ID, provisioner.updateCleanupPhysicalId(r));
|
||||
// The update arrives with the displaced gateway's attributes; its interface goes with it.
|
||||
assertEquals("eni-0fedcba9876543210", r.getAttributes().get("EniId"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void natGatewayTagsGoThroughItsCreateCall() {
|
||||
NatGateway nat = new NatGateway();
|
||||
nat.setNatGatewayId(NAT_ID);
|
||||
when(ec2.createNatGateway(eq(REGION), eq(SUBNET_ID), eq(ALLOC_ID), eq("public"), any())).thenReturn(nat);
|
||||
NatGateway nat = natGateway(NAT_ID);
|
||||
when(ec2.createNatGateway(eq(REGION), eq(SUBNET_ID), eq(ALLOC_ID), eq("public"), isNull(), any())).thenReturn(nat);
|
||||
|
||||
provisioner.provision(resource("AWS::EC2::NatGateway", "Nat"),
|
||||
withTags(mapper.createObjectNode().put("SubnetId", SUBNET_ID).put("AllocationId", ALLOC_ID), "Name", "nat"), ctx());
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
ArgumentCaptor<List<Tag>> tags = ArgumentCaptor.forClass(List.class);
|
||||
verify(ec2).createNatGateway(eq(REGION), eq(SUBNET_ID), eq(ALLOC_ID), eq("public"), tags.capture());
|
||||
verify(ec2).createNatGateway(eq(REGION), eq(SUBNET_ID), eq(ALLOC_ID), eq("public"), isNull(), tags.capture());
|
||||
assertEquals(List.of("Name=nat"), pairs(tags.getValue()));
|
||||
verify(ec2, never()).createTags(any(), any(), any());
|
||||
}
|
||||
@@ -766,7 +855,7 @@ class Ec2NetworkCfnProvisionerTest {
|
||||
verify(ec2, never()).createRouteTable(any(), any());
|
||||
verify(ec2, never()).associateRouteTable(any(), any(), any());
|
||||
verify(ec2, never()).createRoute(any(), any(), any(), any(), any(), any(), any(), any(), any());
|
||||
verify(ec2, never()).createNatGateway(any(), any(), any(), any(), any());
|
||||
verify(ec2, never()).createNatGateway(any(), any(), any(), any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -988,8 +1077,7 @@ class Ec2NetworkCfnProvisionerTest {
|
||||
|
||||
@Test
|
||||
void aFailedNatGatewayTagChangePutsThePriorTagsBack() {
|
||||
NatGateway nat = new NatGateway();
|
||||
nat.setNatGatewayId(NAT_ID);
|
||||
NatGateway nat = natGateway(NAT_ID);
|
||||
nat.setSubnetId(SUBNET_ID);
|
||||
nat.setAllocationId(ALLOC_ID);
|
||||
nat.setTags(List.of(new Tag("Name", "old")));
|
||||
|
||||
+160
-190
@@ -4,7 +4,10 @@ import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.response.Response;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static io.github.hectorvent.floci.testing.Ec2Requests.ec2;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.matchesRegex;
|
||||
@@ -20,47 +23,27 @@ import static org.hamcrest.Matchers.startsWith;
|
||||
@QuarkusTest
|
||||
class Ec2NatGatewayAddressIntegrationTest {
|
||||
|
||||
private static final String AUTH_HEADER =
|
||||
"AWS4-HMAC-SHA256 Credential=test/20260205/us-east-1/ec2/aws4_request";
|
||||
private static final String ACCOUNT_ID = "000000000000";
|
||||
private static final String REGION = "us-east-1";
|
||||
|
||||
private String subnetId() {
|
||||
String vpcId = given()
|
||||
.formParam("Action", "CreateVpc")
|
||||
.formParam("CidrBlock", "10.94.0.0/16")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when().post("/")
|
||||
String vpcId = ec2("CreateVpc", ACCOUNT_ID, REGION).formParam("CidrBlock", "10.94.0.0/16").post("/")
|
||||
.then().statusCode(200)
|
||||
.extract().path("CreateVpcResponse.vpc.vpcId");
|
||||
|
||||
return given()
|
||||
.formParam("Action", "CreateSubnet")
|
||||
.formParam("VpcId", vpcId)
|
||||
.formParam("CidrBlock", "10.94.1.0/24")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when().post("/")
|
||||
return ec2("CreateSubnet", ACCOUNT_ID, REGION)
|
||||
.formParam("VpcId", vpcId).formParam("CidrBlock", "10.94.1.0/24").post("/")
|
||||
.then().statusCode(200)
|
||||
.extract().path("CreateSubnetResponse.subnet.subnetId");
|
||||
}
|
||||
|
||||
@Test
|
||||
void aPublicGatewayReportsItsElasticIpPrivateIpAndInterface() {
|
||||
String subnetId = subnetId();
|
||||
Response allocation = given()
|
||||
.formParam("Action", "AllocateAddress")
|
||||
.formParam("Domain", "vpc")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when().post("/")
|
||||
.then().statusCode(200).extract().response();
|
||||
Response allocation = allocation();
|
||||
String allocationId = allocation.path("AllocateAddressResponse.allocationId");
|
||||
String publicIp = allocation.path("AllocateAddressResponse.publicIp");
|
||||
|
||||
String natGatewayId = given()
|
||||
.formParam("Action", "CreateNatGateway")
|
||||
.formParam("SubnetId", subnetId)
|
||||
.formParam("AllocationId", allocationId)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when().post("/")
|
||||
.then()
|
||||
String natGatewayId = createNat(allocationId).then()
|
||||
.statusCode(200)
|
||||
.body("CreateNatGatewayResponse.natGateway.natGatewayAddressSet.item.allocationId",
|
||||
equalTo(allocationId))
|
||||
@@ -74,20 +57,18 @@ class Ec2NatGatewayAddressIntegrationTest {
|
||||
startsWith("eipassoc-"))
|
||||
.body("CreateNatGatewayResponse.natGateway.natGatewayAddressSet.item.status",
|
||||
equalTo("succeeded"))
|
||||
.body("CreateNatGatewayResponse.natGateway.availabilityMode", equalTo("zonal"))
|
||||
.extract().path("CreateNatGatewayResponse.natGateway.natGatewayId");
|
||||
|
||||
// Describe is the read Terraform refreshes from, so it has to agree, not just Create.
|
||||
given()
|
||||
.formParam("Action", "DescribeNatGateways")
|
||||
.formParam("NatGatewayId.1", natGatewayId)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when().post("/")
|
||||
ec2("DescribeNatGateways", ACCOUNT_ID, REGION).formParam("NatGatewayId.1", natGatewayId).post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("DescribeNatGatewaysResponse.natGatewaySet.item.natGatewayAddressSet.item.publicIp",
|
||||
equalTo(publicIp))
|
||||
.body("DescribeNatGatewaysResponse.natGatewaySet.item.natGatewayAddressSet.item.networkInterfaceId",
|
||||
startsWith("eni-"));
|
||||
startsWith("eni-"))
|
||||
.body("DescribeNatGatewaysResponse.natGatewaySet.item.availabilityMode", equalTo("zonal"));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -98,28 +79,14 @@ class Ec2NatGatewayAddressIntegrationTest {
|
||||
@Test
|
||||
void aGatewaysInterfaceResolvesAndItsElasticIpReadsAsAssociated() {
|
||||
String subnetId = subnetId();
|
||||
String allocationId = given()
|
||||
.formParam("Action", "AllocateAddress")
|
||||
.formParam("Domain", "vpc")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when().post("/")
|
||||
.then().statusCode(200)
|
||||
.extract().path("AllocateAddressResponse.allocationId");
|
||||
String allocationId = allocate();
|
||||
|
||||
String natGatewayId = given()
|
||||
.formParam("Action", "CreateNatGateway")
|
||||
.formParam("SubnetId", subnetId)
|
||||
.formParam("AllocationId", allocationId)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when().post("/")
|
||||
.then().statusCode(200)
|
||||
String natGatewayId = createNat(Map.of("SubnetId", subnetId, "AllocationId", allocationId)).then()
|
||||
.statusCode(200)
|
||||
.extract().path("CreateNatGatewayResponse.natGateway.natGatewayId");
|
||||
|
||||
Response described = given()
|
||||
.formParam("Action", "DescribeNatGateways")
|
||||
.formParam("NatGatewayId.1", natGatewayId)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when().post("/")
|
||||
Response described = ec2("DescribeNatGateways", ACCOUNT_ID, REGION)
|
||||
.formParam("NatGatewayId.1", natGatewayId).post("/")
|
||||
.then().statusCode(200).extract().response();
|
||||
String prefix = "DescribeNatGatewaysResponse.natGatewaySet.item.";
|
||||
String eniId = described.path(prefix + "natGatewayAddressSet.item.networkInterfaceId");
|
||||
@@ -129,11 +96,7 @@ class Ec2NatGatewayAddressIntegrationTest {
|
||||
|
||||
// Twice: the id must be stable, not re-minted per read.
|
||||
for (int i = 0; i < 2; i++) {
|
||||
given()
|
||||
.formParam("Action", "DescribeNetworkInterfaces")
|
||||
.formParam("NetworkInterfaceId.1", eniId)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when().post("/")
|
||||
ec2("DescribeNetworkInterfaces", ACCOUNT_ID, REGION).formParam("NetworkInterfaceId.1", eniId).post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("DescribeNetworkInterfacesResponse.networkInterfaceSet.item.networkInterfaceId",
|
||||
@@ -150,38 +113,22 @@ class Ec2NatGatewayAddressIntegrationTest {
|
||||
+ ".privateIpAddressesSet.item.association.allocationId", equalTo(allocationId));
|
||||
}
|
||||
|
||||
given()
|
||||
.formParam("Action", "DescribeAddresses")
|
||||
.formParam("AllocationId.1", allocationId)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when().post("/")
|
||||
ec2("DescribeAddresses", ACCOUNT_ID, REGION).formParam("AllocationId.1", allocationId).post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("DescribeAddressesResponse.addressesSet.item.associationId", equalTo(associationId))
|
||||
.body("DescribeAddressesResponse.addressesSet.item.networkInterfaceId", equalTo(eniId))
|
||||
.body("DescribeAddressesResponse.addressesSet.item.privateIpAddress", equalTo(privateIp));
|
||||
|
||||
given()
|
||||
.formParam("Action", "DeleteNatGateway")
|
||||
.formParam("NatGatewayId", natGatewayId)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when().post("/")
|
||||
ec2("DeleteNatGateway", ACCOUNT_ID, REGION).formParam("NatGatewayId", natGatewayId).post("/")
|
||||
.then().statusCode(200);
|
||||
|
||||
given()
|
||||
.formParam("Action", "DescribeNetworkInterfaces")
|
||||
.formParam("NetworkInterfaceId.1", eniId)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when().post("/")
|
||||
ec2("DescribeNetworkInterfaces", ACCOUNT_ID, REGION).formParam("NetworkInterfaceId.1", eniId).post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("Response.Errors.Error.Code", equalTo("InvalidNetworkInterfaceID.NotFound"));
|
||||
|
||||
given()
|
||||
.formParam("Action", "DescribeAddresses")
|
||||
.formParam("AllocationId.1", allocationId)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when().post("/")
|
||||
ec2("DescribeAddresses", ACCOUNT_ID, REGION).formParam("AllocationId.1", allocationId).post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("DescribeAddressesResponse.addressesSet.item.allocationId", equalTo(allocationId))
|
||||
@@ -191,15 +138,10 @@ class Ec2NatGatewayAddressIntegrationTest {
|
||||
|
||||
@Test
|
||||
void aPrivateGatewayHasAPrivateAddressAndNoElasticIp() {
|
||||
given()
|
||||
.formParam("Action", "CreateNatGateway")
|
||||
.formParam("SubnetId", subnetId())
|
||||
.formParam("ConnectivityType", "private")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when().post("/")
|
||||
.then()
|
||||
createNat(Map.of("SubnetId", subnetId(), "ConnectivityType", "private")).then()
|
||||
.statusCode(200)
|
||||
.body("CreateNatGatewayResponse.natGateway.connectivityType", equalTo("private"))
|
||||
.body("CreateNatGatewayResponse.natGateway.availabilityMode", equalTo("zonal"))
|
||||
.body("CreateNatGatewayResponse.natGateway.natGatewayAddressSet.item.privateIp",
|
||||
matchesRegex("10\\.94\\.1\\.\\d+"))
|
||||
.body("CreateNatGatewayResponse.natGateway.natGatewayAddressSet.item.networkInterfaceId",
|
||||
@@ -215,43 +157,129 @@ class Ec2NatGatewayAddressIntegrationTest {
|
||||
*/
|
||||
@Test
|
||||
void aPrivateGatewayCannotBeGivenAnElasticIp() {
|
||||
String allocationId = given()
|
||||
.formParam("Action", "AllocateAddress")
|
||||
.formParam("Domain", "vpc")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when().post("/")
|
||||
.then().statusCode(200)
|
||||
.extract().path("AllocateAddressResponse.allocationId");
|
||||
|
||||
given()
|
||||
.formParam("Action", "CreateNatGateway")
|
||||
.formParam("SubnetId", subnetId())
|
||||
.formParam("ConnectivityType", "private")
|
||||
.formParam("AllocationId", allocationId)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when().post("/")
|
||||
.then()
|
||||
createNat(Map.of("SubnetId", subnetId(), "ConnectivityType", "private", "AllocationId", allocate())).then()
|
||||
.statusCode(400)
|
||||
.body("Response.Errors.Error.Code", equalTo("InvalidParameterCombination"));
|
||||
}
|
||||
|
||||
private String allocate() {
|
||||
return given()
|
||||
.formParam("Action", "AllocateAddress")
|
||||
.formParam("Domain", "vpc")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when().post("/")
|
||||
/**
|
||||
* Every gateway Floci creates is zonal. A regional request names its VPC and no subnet, and
|
||||
* is refused rather than failing on the missing subnet; one that names a subnet as well is
|
||||
* refused rather than created as zonal. AWS's enum is exact-case, so a value that is neither
|
||||
* mode is invalid rather than zonal.
|
||||
*/
|
||||
@Test
|
||||
void aRegionalOrUnknownAvailabilityModeIsRefused() {
|
||||
createNat(Map.of("AvailabilityMode", "regional", "VpcId", "vpc-0123456789abcdef0")).then()
|
||||
.statusCode(400)
|
||||
.body("Response.Errors.Error.Code", equalTo("UnsupportedOperation"));
|
||||
|
||||
String subnetId = subnetId();
|
||||
createNat(Map.of("AvailabilityMode", "regional", "SubnetId", subnetId, "AllocationId", allocate())).then()
|
||||
.statusCode(400)
|
||||
.body("Response.Errors.Error.Code", equalTo("UnsupportedOperation"));
|
||||
createNat(Map.of("AvailabilityMode", "Regional", "SubnetId", subnetId, "ConnectivityType", "private")).then()
|
||||
.statusCode(400)
|
||||
.body("Response.Errors.Error.Code", equalTo("InvalidParameterValue"));
|
||||
assertNatGatewaysIn(subnetId, 0);
|
||||
|
||||
createNat(Map.of("AvailabilityMode", "zonal", "SubnetId", subnetId, "ConnectivityType", "private")).then()
|
||||
.statusCode(200)
|
||||
.body("CreateNatGatewayResponse.natGateway.availabilityMode", equalTo("zonal"));
|
||||
}
|
||||
|
||||
/**
|
||||
* PrivateIpAddress is the gateway's address, and every read that reports it has to agree. The
|
||||
* Terraform AWS provider reads private_ip back, and it forces a new gateway, so a different
|
||||
* address would replace the gateway on every plan.
|
||||
*/
|
||||
@Test
|
||||
void aRequestedPrivateIpIsTheAddressEveryReadReports() {
|
||||
String allocationId = allocate();
|
||||
Response created = createNat(Map.of("SubnetId", subnetId(), "AllocationId", allocationId,
|
||||
"PrivateIpAddress", "10.94.1.50")).then()
|
||||
.statusCode(200)
|
||||
.body("CreateNatGatewayResponse.natGateway.natGatewayAddressSet.item.privateIp", equalTo("10.94.1.50"))
|
||||
.extract().response();
|
||||
String natGatewayId = created.path("CreateNatGatewayResponse.natGateway.natGatewayId");
|
||||
String eniId = created.path("CreateNatGatewayResponse.natGateway.natGatewayAddressSet.item.networkInterfaceId");
|
||||
|
||||
ec2("DescribeNatGateways", ACCOUNT_ID, REGION).formParam("NatGatewayId.1", natGatewayId).post("/")
|
||||
.then().statusCode(200)
|
||||
.extract().path("AllocateAddressResponse.allocationId");
|
||||
.body("DescribeNatGatewaysResponse.natGatewaySet.item.natGatewayAddressSet.item.privateIp",
|
||||
equalTo("10.94.1.50"));
|
||||
ec2("DescribeNetworkInterfaces", ACCOUNT_ID, REGION).formParam("NetworkInterfaceId.1", eniId).post("/")
|
||||
.then().statusCode(200)
|
||||
.body("DescribeNetworkInterfacesResponse.networkInterfaceSet.item.privateIpAddress",
|
||||
equalTo("10.94.1.50"));
|
||||
ec2("DescribeAddresses", ACCOUNT_ID, REGION).formParam("AllocationId.1", allocationId).post("/")
|
||||
.then().statusCode(200)
|
||||
.body("DescribeAddressesResponse.addressesSet.item.privateIpAddress", equalTo("10.94.1.50"));
|
||||
}
|
||||
|
||||
/** The RunInstances rules: an assignable address of the subnet that nothing else holds. */
|
||||
@Test
|
||||
void aRequestedPrivateIpOutsideTheSubnetOrInUseIsRefused() {
|
||||
String subnetId = subnetId();
|
||||
for (String unassignable : List.of("10.95.1.50", "10.94.1.1")) {
|
||||
createNat(Map.of("SubnetId", subnetId, "ConnectivityType", "private", "PrivateIpAddress", unassignable))
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("Response.Errors.Error.Code", equalTo("InvalidParameterValue"));
|
||||
}
|
||||
assertNatGatewaysIn(subnetId, 0);
|
||||
|
||||
Map<String, String> request = Map.of("SubnetId", subnetId, "ConnectivityType", "private",
|
||||
"PrivateIpAddress", "10.94.1.60");
|
||||
createNat(request).then().statusCode(200);
|
||||
createNat(request).then()
|
||||
.statusCode(400)
|
||||
.body("Response.Errors.Error.Code", equalTo("InvalidIPAddress.InUse"))
|
||||
.body("Response.Errors.Error.Message", equalTo("Address 10.94.1.60 is in use."));
|
||||
assertNatGatewaysIn(subnetId, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* A gateway gets its primary address only, so the secondary-address parameters are refused
|
||||
* rather than answered with a gateway that has fewer addresses than it was asked for.
|
||||
*/
|
||||
@Test
|
||||
void theSecondaryAddressParametersAreRefused() {
|
||||
String subnetId = subnetId();
|
||||
for (Map<String, String> request : List.of(
|
||||
Map.of("SubnetId", subnetId, "AllocationId", allocate(), "SecondaryAllocationId.1", allocate()),
|
||||
Map.of("SubnetId", subnetId, "ConnectivityType", "private", "SecondaryPrivateIpAddress.1", "10.94.1.70"),
|
||||
Map.of("SubnetId", subnetId, "ConnectivityType", "private", "SecondaryPrivateIpAddressCount", "2"))) {
|
||||
createNat(request).then()
|
||||
.statusCode(400)
|
||||
.body("Response.Errors.Error.Code", equalTo("UnsupportedOperation"));
|
||||
}
|
||||
assertNatGatewaysIn(subnetId, 0);
|
||||
}
|
||||
|
||||
private static Response allocation() {
|
||||
return ec2("AllocateAddress", ACCOUNT_ID, REGION).formParam("Domain", "vpc").post("/")
|
||||
.then().statusCode(200).extract().response();
|
||||
}
|
||||
|
||||
private static String allocate() {
|
||||
return allocation().path("AllocateAddressResponse.allocationId");
|
||||
}
|
||||
|
||||
private Response createNat(String allocationId) {
|
||||
return given()
|
||||
.formParam("Action", "CreateNatGateway")
|
||||
.formParam("SubnetId", subnetId())
|
||||
.formParam("AllocationId", allocationId)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when().post("/");
|
||||
return createNat(Map.of("SubnetId", subnetId(), "AllocationId", allocationId));
|
||||
}
|
||||
|
||||
private static Response createNat(Map<String, String> params) {
|
||||
return ec2("CreateNatGateway", ACCOUNT_ID, REGION).formParams(params).post("/");
|
||||
}
|
||||
|
||||
/** A refused request must not leave a gateway behind in the subnet it named. */
|
||||
private static void assertNatGatewaysIn(String subnetId, int expected) {
|
||||
ec2("DescribeNatGateways", ACCOUNT_ID, REGION)
|
||||
.formParam("Filter.1.Name", "subnet-id").formParam("Filter.1.Value.1", subnetId).post("/")
|
||||
.then().statusCode(200)
|
||||
.body("DescribeNatGatewaysResponse.natGatewaySet.item.size()", equalTo(expected));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -265,11 +293,7 @@ class Ec2NatGatewayAddressIntegrationTest {
|
||||
String failedId = createNat(heldByGateway).then().statusCode(200)
|
||||
.body("CreateNatGatewayResponse.natGateway.state", equalTo("failed"))
|
||||
.extract().path("CreateNatGatewayResponse.natGateway.natGatewayId");
|
||||
given()
|
||||
.formParam("Action", "DescribeNatGateways")
|
||||
.formParam("NatGatewayId.1", failedId)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when().post("/")
|
||||
ec2("DescribeNatGateways", ACCOUNT_ID, REGION).formParam("NatGatewayId.1", failedId).post("/")
|
||||
.then().statusCode(200)
|
||||
.body("DescribeNatGatewaysResponse.natGatewaySet.item.state", equalTo("failed"))
|
||||
.body("DescribeNatGatewaysResponse.natGatewaySet.item.failureCode",
|
||||
@@ -279,58 +303,31 @@ class Ec2NatGatewayAddressIntegrationTest {
|
||||
.body(not(containsString("<networkInterfaceId>")));
|
||||
|
||||
String heldByInstance = allocate();
|
||||
given()
|
||||
.formParam("Action", "AssociateAddress")
|
||||
.formParam("AllocationId", heldByInstance)
|
||||
.formParam("InstanceId", "i-0123456789abcdef0")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when().post("/")
|
||||
ec2("AssociateAddress", ACCOUNT_ID, REGION)
|
||||
.formParam("AllocationId", heldByInstance).formParam("InstanceId", "i-0123456789abcdef0").post("/")
|
||||
.then().statusCode(200);
|
||||
createNat(heldByInstance).then().statusCode(200)
|
||||
.body("CreateNatGatewayResponse.natGateway.state", equalTo("failed"))
|
||||
.body("CreateNatGatewayResponse.natGateway.failureCode", equalTo("Resource.AlreadyAssociated"));
|
||||
String instanceAssociation = given()
|
||||
.formParam("Action", "DescribeAddresses")
|
||||
.formParam("AllocationId.1", heldByInstance)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when().post("/")
|
||||
String instanceAssociation = ec2("DescribeAddresses", ACCOUNT_ID, REGION)
|
||||
.formParam("AllocationId.1", heldByInstance).post("/")
|
||||
.then().statusCode(200)
|
||||
.body("DescribeAddressesResponse.addressesSet.item.instanceId", equalTo("i-0123456789abcdef0"))
|
||||
.extract().path("DescribeAddressesResponse.addressesSet.item.associationId");
|
||||
// The failed gateway does not hold the address: once the instance lets go it can be released.
|
||||
given()
|
||||
.formParam("Action", "DisassociateAddress")
|
||||
.formParam("AssociationId", instanceAssociation)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when().post("/")
|
||||
ec2("DisassociateAddress", ACCOUNT_ID, REGION).formParam("AssociationId", instanceAssociation).post("/")
|
||||
.then().statusCode(200);
|
||||
given()
|
||||
.formParam("Action", "ReleaseAddress")
|
||||
.formParam("AllocationId", heldByInstance)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when().post("/")
|
||||
ec2("ReleaseAddress", ACCOUNT_ID, REGION).formParam("AllocationId", heldByInstance).post("/")
|
||||
.then().statusCode(200);
|
||||
|
||||
// The failed gateway claimed nothing, and deleting it leaves the first gateway's association alone.
|
||||
String held = given()
|
||||
.formParam("Action", "DescribeAddresses")
|
||||
.formParam("AllocationId.1", heldByGateway)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when().post("/")
|
||||
String held = ec2("DescribeAddresses", ACCOUNT_ID, REGION).formParam("AllocationId.1", heldByGateway).post("/")
|
||||
.then().statusCode(200)
|
||||
.body("DescribeAddressesResponse.addressesSet.item.associationId", startsWith("eipassoc-"))
|
||||
.extract().path("DescribeAddressesResponse.addressesSet.item.associationId");
|
||||
given()
|
||||
.formParam("Action", "DeleteNatGateway")
|
||||
.formParam("NatGatewayId", failedId)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when().post("/")
|
||||
ec2("DeleteNatGateway", ACCOUNT_ID, REGION).formParam("NatGatewayId", failedId).post("/")
|
||||
.then().statusCode(200);
|
||||
given()
|
||||
.formParam("Action", "DescribeAddresses")
|
||||
.formParam("AllocationId.1", heldByGateway)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when().post("/")
|
||||
ec2("DescribeAddresses", ACCOUNT_ID, REGION).formParam("AllocationId.1", heldByGateway).post("/")
|
||||
.then().statusCode(200)
|
||||
.body("DescribeAddressesResponse.addressesSet.item.associationId", equalTo(held));
|
||||
}
|
||||
@@ -341,57 +338,30 @@ class Ec2NatGatewayAddressIntegrationTest {
|
||||
String allocationId = allocate();
|
||||
String natGatewayId = createNat(allocationId).then().statusCode(200)
|
||||
.extract().path("CreateNatGatewayResponse.natGateway.natGatewayId");
|
||||
String associationId = given()
|
||||
.formParam("Action", "DescribeAddresses")
|
||||
.formParam("AllocationId.1", allocationId)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when().post("/")
|
||||
String associationId = ec2("DescribeAddresses", ACCOUNT_ID, REGION)
|
||||
.formParam("AllocationId.1", allocationId).post("/")
|
||||
.then().statusCode(200)
|
||||
.extract().path("DescribeAddressesResponse.addressesSet.item.associationId");
|
||||
|
||||
given()
|
||||
.formParam("Action", "DisassociateAddress")
|
||||
.formParam("AssociationId", associationId)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when().post("/")
|
||||
ec2("DisassociateAddress", ACCOUNT_ID, REGION).formParam("AssociationId", associationId).post("/")
|
||||
.then().statusCode(400)
|
||||
.body("Response.Errors.Error.Code", equalTo("InvalidParameterValue"));
|
||||
|
||||
given()
|
||||
.formParam("Action", "DescribeAddresses")
|
||||
.formParam("AllocationId.1", allocationId)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when().post("/")
|
||||
ec2("DescribeAddresses", ACCOUNT_ID, REGION).formParam("AllocationId.1", allocationId).post("/")
|
||||
.then().statusCode(200)
|
||||
.body("DescribeAddressesResponse.addressesSet.item.associationId", equalTo(associationId));
|
||||
|
||||
// Nor released out from under the gateway: AWS answers InvalidIPAddress.InUse.
|
||||
given()
|
||||
.formParam("Action", "ReleaseAddress")
|
||||
.formParam("AllocationId", allocationId)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when().post("/")
|
||||
ec2("ReleaseAddress", ACCOUNT_ID, REGION).formParam("AllocationId", allocationId).post("/")
|
||||
.then().statusCode(400)
|
||||
.body("Response.Errors.Error.Code", equalTo("InvalidIPAddress.InUse"));
|
||||
given()
|
||||
.formParam("Action", "DescribeAddresses")
|
||||
.formParam("AllocationId.1", allocationId)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when().post("/")
|
||||
ec2("DescribeAddresses", ACCOUNT_ID, REGION).formParam("AllocationId.1", allocationId).post("/")
|
||||
.then().statusCode(200)
|
||||
.body("DescribeAddressesResponse.addressesSet.item.associationId", equalTo(associationId));
|
||||
|
||||
given()
|
||||
.formParam("Action", "DeleteNatGateway")
|
||||
.formParam("NatGatewayId", natGatewayId)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when().post("/")
|
||||
ec2("DeleteNatGateway", ACCOUNT_ID, REGION).formParam("NatGatewayId", natGatewayId).post("/")
|
||||
.then().statusCode(200);
|
||||
given()
|
||||
.formParam("Action", "ReleaseAddress")
|
||||
.formParam("AllocationId", allocationId)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when().post("/")
|
||||
ec2("ReleaseAddress", ACCOUNT_ID, REGION).formParam("AllocationId", allocationId).post("/")
|
||||
.then().statusCode(200);
|
||||
}
|
||||
}
|
||||
@@ -4559,6 +4559,26 @@ class Ec2ServiceTest {
|
||||
assertEquals(List.of("10.50.1.11"), launchAddresses(service, subnetId, 1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aNatGatewaysRequestedPrivateIpTakesNoLeaseAndIsStillNotHandedOut() {
|
||||
// DeleteNatGateway returns no lease, so one taken for the gateway's PrivateIpAddress would
|
||||
// keep the address from the network's allocator after the gateway is gone. The walk steps
|
||||
// around it while the stored gateway holds it.
|
||||
VpcNetworkManager vpcNetworks = mock(VpcNetworkManager.class);
|
||||
Ec2Service service = mockedVpcNetworkService(vpcNetworks);
|
||||
String subnetId = privateIpSubnet(service);
|
||||
when(vpcNetworks.effectiveSubnetCidr("us-east-1", subnetId)).thenReturn(Optional.of("10.50.1.0/24"));
|
||||
|
||||
assertEquals("10.50.1.10", service.createNatGateway("us-east-1", subnetId, null, "private",
|
||||
"10.50.1.10", List.of()).getNatGatewayAddresses().getFirst().getPrivateIp());
|
||||
verify(vpcNetworks, never()).claimPrivateIp(anyString(), anyString(), anyString());
|
||||
verify(vpcNetworks, never()).reservePrivateIp(anyString(), anyString(), anyString());
|
||||
|
||||
when(vpcNetworks.allocatePrivateIp("us-east-1", subnetId))
|
||||
.thenReturn(Optional.of("10.50.1.10"), Optional.of("10.50.1.11"), Optional.empty());
|
||||
assertEquals(List.of("10.50.1.11"), launchAddresses(service, subnetId, 1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anAddressAnotherAccountsInterfaceWasCreatedWithIsNotHandedOutOnASharedVpcNetwork() {
|
||||
// Default subnet ids are the same in every account, so their Docker network is shared, and
|
||||
|
||||
Reference in new issue
Block a user