mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(cloudformation): keep NAT gateway secondary addresses the stack never declared (#7669)
* fix(cloudformation): keep NAT gateway secondary addresses the stack never declared * test(cloudformation): keep NAT gateway secondary addresses the stack never declared * fix(cloudformation): a null NAT gateway secondary property counts as not declared * test(cloudformation): a null NAT gateway secondary property counts as not declared
This commit is contained in:
1 parent
f8670f64fb
commit
0cb45a86df
5 files changed
+289
-9
No files matched your search
+93
@@ -1,10 +1,12 @@
|
||||
package io.github.hectorvent.floci.services.cloudformation;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.XmlParser;
|
||||
import io.github.hectorvent.floci.services.ec2.Ec2Service;
|
||||
import io.github.hectorvent.floci.testing.CfnStackWaits;
|
||||
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.specification.RequestSpecification;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@@ -42,6 +44,9 @@ class CloudFormationEc2NetworkingIntegrationTest {
|
||||
"AWS4-HMAC-SHA256 Credential=test/20260907/us-east-1/ec2/aws4_request";
|
||||
private static final String STACK = "ec2-networking-it";
|
||||
|
||||
@Inject
|
||||
Ec2Service ec2Service;
|
||||
|
||||
private static String template(String vpcId, String publicSubnetCidr) {
|
||||
return template(vpcId, publicSubnetCidr, "{\"Key\": \"tier\", \"Value\": \"web\"}");
|
||||
}
|
||||
@@ -245,6 +250,94 @@ class CloudFormationEc2NetworkingIntegrationTest {
|
||||
ec2("DeleteVpc", Map.of("VpcId", vpcId));
|
||||
}
|
||||
|
||||
private static final String TAGGED_PUBLIC_NAT = PUBLIC_NAT + ", \"Tags\": [{\"Key\": \"%s\", \"Value\": \"v\"}]";
|
||||
|
||||
/**
|
||||
* A property the stack never declared keeps the value set outside it. The three secondary-address
|
||||
* properties form one set, so a gateway whose templates declared none of them keeps a secondary
|
||||
* Elastic IP added through the service, which is what AssociateNatGatewayAddress does (the
|
||||
* query action is not exposed, so the test calls the service it maps to).
|
||||
*/
|
||||
@Test
|
||||
void aNatGatewaySecondaryElasticIpAddedOutsideTheStackSurvivesAnUnrelatedUpdate() {
|
||||
String stack = "ec2-nat-secondary-outside-it";
|
||||
String vpcId = between(ec2("CreateVpc", Map.of("CidrBlock", "10.97.0.0/16")), "<vpcId>", "</vpcId>");
|
||||
cloudFormation(stack, "CreateStack", secondaryNatTemplate(vpcId, "10.97.0.0/24",
|
||||
TAGGED_PUBLIC_NAT.formatted("before"), false));
|
||||
String natId = XmlParser.extractPairs(describeStacks(stack, "CREATE_COMPLETE"),
|
||||
"Outputs", "OutputKey", "OutputValue").get("NatId");
|
||||
String outside = between(ec2("AllocateAddress", Map.of("Domain", "vpc")), "<allocationId>", "</allocationId>");
|
||||
ec2Service.updateNatGatewaySecondaryAddresses("us-east-1", natId, List.of(outside), List.of(), null);
|
||||
assertTrue(natXml(natId).contains("<allocationId>" + outside + "</allocationId>"), natXml(natId));
|
||||
|
||||
cloudFormation(stack, "UpdateStack", secondaryNatTemplate(vpcId, "10.97.0.0/24",
|
||||
TAGGED_PUBLIC_NAT.formatted("after"), false));
|
||||
|
||||
assertEquals(natId, XmlParser.extractPairs(describeStacks(stack, "UPDATE_COMPLETE"),
|
||||
"Outputs", "OutputKey", "OutputValue").get("NatId"), "the gateway is kept");
|
||||
String nat = natXml(natId);
|
||||
assertTrue(nat.contains("<allocationId>" + outside + "</allocationId>"),
|
||||
"the secondary added outside the stack must survive: " + nat);
|
||||
assertTrue(ec2("DescribeAddresses", Map.of("AllocationId.1", outside)).contains("<associationId>"),
|
||||
"the Elastic IP added outside the stack is still held");
|
||||
|
||||
cloudFormation(stack, "DeleteStack", null);
|
||||
CfnStackWaits.awaitStackDeleted(stack);
|
||||
ec2("ReleaseAddress", Map.of("AllocationId", outside));
|
||||
ec2("DeleteVpc", Map.of("VpcId", vpcId));
|
||||
}
|
||||
|
||||
/** {@code AWS::NoValue} is removed before the provisioner runs, so such a property was never declared. */
|
||||
@Test
|
||||
void aSecondaryPropertyThatIsNoValueCountsAsNeverDeclared() {
|
||||
String stack = "ec2-nat-secondary-novalue-it";
|
||||
String vpcId = between(ec2("CreateVpc", Map.of("CidrBlock", "10.98.0.0/16")), "<vpcId>", "</vpcId>");
|
||||
String noValue = PUBLIC_NAT + ", \"SecondaryAllocationIds\": {\"Ref\": \"AWS::NoValue\"}";
|
||||
cloudFormation(stack, "CreateStack", secondaryNatTemplate(vpcId, "10.98.0.0/24", noValue, false));
|
||||
String natId = XmlParser.extractPairs(describeStacks(stack, "CREATE_COMPLETE"),
|
||||
"Outputs", "OutputKey", "OutputValue").get("NatId");
|
||||
String outside = between(ec2("AllocateAddress", Map.of("Domain", "vpc")), "<allocationId>", "</allocationId>");
|
||||
ec2Service.updateNatGatewaySecondaryAddresses("us-east-1", natId, List.of(outside), List.of(), null);
|
||||
|
||||
cloudFormation(stack, "UpdateStack", secondaryNatTemplate(vpcId, "10.98.0.0/24",
|
||||
noValue + ", \"Tags\": [{\"Key\": \"k\", \"Value\": \"v\"}]", false));
|
||||
|
||||
describeStacks(stack, "UPDATE_COMPLETE");
|
||||
String nat = natXml(natId);
|
||||
assertTrue(nat.contains("<allocationId>" + outside + "</allocationId>"),
|
||||
"a NoValue property never declared the set, so the secondary added outside must survive: " + nat);
|
||||
|
||||
cloudFormation(stack, "DeleteStack", null);
|
||||
CfnStackWaits.awaitStackDeleted(stack);
|
||||
ec2("ReleaseAddress", Map.of("AllocationId", outside));
|
||||
ec2("DeleteVpc", Map.of("VpcId", vpcId));
|
||||
}
|
||||
|
||||
/** The contrast: a secondary Elastic IP the stack declared is released when a template drops the property. */
|
||||
@Test
|
||||
void aSecondaryElasticIpTheStackDeclaredIsClearedWhenATemplateDropsIt() {
|
||||
String stack = "ec2-nat-secondary-dropped-it";
|
||||
String vpcId = between(ec2("CreateVpc", Map.of("CidrBlock", "10.99.0.0/16")), "<vpcId>", "</vpcId>");
|
||||
cloudFormation(stack, "CreateStack", secondaryNatTemplate(vpcId, "10.99.0.0/24",
|
||||
PUBLIC_NAT + ", \"SecondaryAllocationIds\": [{\"Fn::GetAtt\": [\"Eip2\", \"AllocationId\"]}]", false));
|
||||
Map<String, String> out = XmlParser.extractPairs(describeStacks(stack, "CREATE_COMPLETE"),
|
||||
"Outputs", "OutputKey", "OutputValue");
|
||||
String natId = out.get("NatId");
|
||||
String eip2 = out.get("Eip2Id");
|
||||
assertTrue(natXml(natId).contains("<allocationId>" + eip2 + "</allocationId>"), natXml(natId));
|
||||
|
||||
cloudFormation(stack, "UpdateStack", secondaryNatTemplate(vpcId, "10.99.0.0/24", PUBLIC_NAT, false));
|
||||
|
||||
describeStacks(stack, "UPDATE_COMPLETE");
|
||||
String nat = natXml(natId);
|
||||
assertFalse(nat.contains(eip2) || !secondaryIps(nat).isEmpty(), "the declared secondary is cleared: " + nat);
|
||||
assertFalse(ec2("DescribeAddresses", Map.of("AllocationId.1", eip2)).contains("<associationId>"));
|
||||
|
||||
cloudFormation(stack, "DeleteStack", null);
|
||||
CfnStackWaits.awaitStackDeleted(stack);
|
||||
ec2("DeleteVpc", Map.of("VpcId", vpcId));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aPrivateNatGatewaysSecondaryAddressesChangeInPlace() {
|
||||
String stack = "ec2-nat-secondary-private-it";
|
||||
|
||||
+181
-1
@@ -698,13 +698,193 @@ class Ec2NetworkCfnProvisionerTest {
|
||||
.thenReturn(natGateway(NAT_ID, "eni-0123456789abcdef0", "10.0.1.50"));
|
||||
StackResource changed = prior("AWS::EC2::NatGateway", "Nat", NAT_ID, Map.of("NatGatewayId", NAT_ID));
|
||||
provisioner.provision(changed, mapper.createObjectNode().put("SubnetId", SUBNET_ID).put("AllocationId", ALLOC_ID),
|
||||
ctx(NAT_ID));
|
||||
ctx(NAT_ID, props));
|
||||
verify(ec2).updateNatGatewaySecondaryAddresses(REGION, NAT_ID, List.of(), List.of(), null);
|
||||
verify(ec2).createNatGateway(any(), any(), any(), any(), any(), any(), any(), any(), any());
|
||||
assertEquals(NAT_ID, changed.getPhysicalId(), "changed secondaries keep the gateway");
|
||||
assertFalse(changed.getAttributes().containsKey(CfnRollback.USES_ATTR_PREFIX + "SecondaryPublicIp.0"));
|
||||
}
|
||||
|
||||
private static final String OUTSIDE_ALLOC = "eipalloc-0aaaaaaaaaaaaaaaa";
|
||||
private static final String OUTSIDE_PUBLIC_IP = "203.0.113.80";
|
||||
|
||||
/** A gateway holding a secondary address that no template of the stack declared. */
|
||||
private static NatGateway natGatewayHoldingAnOutsideSecondary(String connectivityType) {
|
||||
NatGateway nat = natGateway(NAT_ID, "eni-0123456789abcdef0", "10.0.1.50");
|
||||
nat.setSubnetId(SUBNET_ID);
|
||||
nat.setConnectivityType(connectivityType);
|
||||
NatGatewayAddress secondary = new NatGatewayAddress();
|
||||
secondary.setPrimary(false);
|
||||
secondary.setNetworkInterfaceId("eni-0123456789abcdef0");
|
||||
secondary.setPrivateIp("10.0.1.80");
|
||||
if ("public".equals(connectivityType)) {
|
||||
nat.setAllocationId(ALLOC_ID);
|
||||
nat.getNatGatewayAddresses().get(0).setAllocationId(ALLOC_ID);
|
||||
secondary.setAllocationId(OUTSIDE_ALLOC);
|
||||
secondary.setPublicIp(OUTSIDE_PUBLIC_IP);
|
||||
}
|
||||
nat.getNatGatewayAddresses().add(secondary);
|
||||
return nat;
|
||||
}
|
||||
|
||||
private ObjectNode publicNatProps() {
|
||||
return mapper.createObjectNode().put("SubnetId", SUBNET_ID).put("AllocationId", ALLOC_ID);
|
||||
}
|
||||
|
||||
/**
|
||||
* A property the stack never declared keeps the value set outside it: with none of the three
|
||||
* secondary-address properties in either template, the update leaves the gateway's secondaries
|
||||
* alone, and the attributes list what it holds.
|
||||
*/
|
||||
@Test
|
||||
void aNatGatewayWhoseTemplatesNeverDeclaredSecondariesKeepsTheOnesAddedOutsideTheStack() {
|
||||
when(ec2.describeNatGateways(REGION, List.of(NAT_ID), Map.of()))
|
||||
.thenReturn(List.of(natGatewayHoldingAnOutsideSecondary("public")));
|
||||
// Only reached by an implementation that clears the set: it reports a gateway with none left.
|
||||
when(ec2.updateNatGatewaySecondaryAddresses(any(), any(), any(), any(), any()))
|
||||
.thenReturn(natGateway(NAT_ID, "eni-0123456789abcdef0", "10.0.1.50"));
|
||||
|
||||
for (ProvisionContext context : List.of(ctx(NAT_ID, publicNatProps()), ctx(NAT_ID))) {
|
||||
StackResource r = prior("AWS::EC2::NatGateway", "Nat", NAT_ID, Map.of("NatGatewayId", NAT_ID));
|
||||
provisioner.provision(r, publicNatProps(), context);
|
||||
|
||||
assertEquals(NAT_ID, r.getPhysicalId());
|
||||
assertEquals(OUTSIDE_PUBLIC_IP, r.getAttributes().get(CfnRollback.USES_ATTR_PREFIX + "SecondaryPublicIp.0"));
|
||||
}
|
||||
verify(ec2, never()).updateNatGatewaySecondaryAddresses(any(), any(), any(), any(), any());
|
||||
verify(ec2, never()).createNatGateway(any(), any(), any(), any(), any(), any(), any(), any(), any());
|
||||
}
|
||||
|
||||
/** A JSON null counts as not declared, as {@code AWS::NoValue} does, so the set is not the stack's. */
|
||||
@Test
|
||||
void aNullSecondaryPropertyCountsAsNotDeclaredAndKeepsTheSecondariesAddedOutsideTheStack() {
|
||||
ObjectNode props = publicNatProps();
|
||||
props.putNull("SecondaryAllocationIds");
|
||||
when(ec2.describeNatGateways(REGION, List.of(NAT_ID), Map.of()))
|
||||
.thenReturn(List.of(natGatewayHoldingAnOutsideSecondary("public")));
|
||||
// Only reached by an implementation that clears the set: it reports a gateway with none left.
|
||||
when(ec2.updateNatGatewaySecondaryAddresses(any(), any(), any(), any(), any()))
|
||||
.thenReturn(natGateway(NAT_ID, "eni-0123456789abcdef0", "10.0.1.50"));
|
||||
StackResource r = prior("AWS::EC2::NatGateway", "Nat", NAT_ID, Map.of("NatGatewayId", NAT_ID));
|
||||
|
||||
provisioner.provision(r, props, ctx(NAT_ID, publicNatProps()));
|
||||
|
||||
assertEquals(NAT_ID, r.getPhysicalId());
|
||||
assertEquals(OUTSIDE_PUBLIC_IP, r.getAttributes().get(CfnRollback.USES_ATTR_PREFIX + "SecondaryPublicIp.0"));
|
||||
verify(ec2, never()).updateNatGatewaySecondaryAddresses(any(), any(), any(), any(), any());
|
||||
}
|
||||
|
||||
/** Each of the three properties makes the set the stack's once the previous template declared it. */
|
||||
@Test
|
||||
void droppingAnyOneOfTheSecondaryPropertiesThePreviousTemplateDeclaredClearsTheSet() {
|
||||
ObjectNode previousAlloc = publicNatProps();
|
||||
previousAlloc.putArray("SecondaryAllocationIds").add(OUTSIDE_ALLOC);
|
||||
ObjectNode previousIp = publicNatProps();
|
||||
previousIp.putArray("SecondaryPrivateIpAddresses").add("10.0.1.80");
|
||||
when(ec2.describeNatGateways(REGION, List.of(NAT_ID), Map.of()))
|
||||
.thenReturn(List.of(natGatewayHoldingAnOutsideSecondary("public")));
|
||||
when(ec2.updateNatGatewaySecondaryAddresses(REGION, NAT_ID, List.of(), List.of(), null))
|
||||
.thenReturn(natGateway(NAT_ID, "eni-0123456789abcdef0", "10.0.1.50"));
|
||||
|
||||
for (ObjectNode previous : List.of(previousAlloc, previousIp)) {
|
||||
StackResource r = prior("AWS::EC2::NatGateway", "Nat", NAT_ID, Map.of("NatGatewayId", NAT_ID));
|
||||
provisioner.provision(r, publicNatProps(), ctx(NAT_ID, previous));
|
||||
|
||||
assertEquals(NAT_ID, r.getPhysicalId(), "the gateway is kept");
|
||||
assertFalse(r.getAttributes().containsKey(CfnRollback.USES_ATTR_PREFIX + "SecondaryPublicIp.0"));
|
||||
}
|
||||
verify(ec2, times(2)).updateNatGatewaySecondaryAddresses(REGION, NAT_ID, List.of(), List.of(), null);
|
||||
}
|
||||
|
||||
@Test
|
||||
void droppingTheSecondaryAddressCountThePreviousTemplateDeclaredClearsTheSet() {
|
||||
ObjectNode privateNat = mapper.createObjectNode().put("SubnetId", SUBNET_ID).put("ConnectivityType", "private");
|
||||
ObjectNode previous = privateNat.deepCopy().put("SecondaryPrivateIpAddressCount", 1);
|
||||
when(ec2.describeNatGateways(REGION, List.of(NAT_ID), Map.of()))
|
||||
.thenReturn(List.of(natGatewayHoldingAnOutsideSecondary("private")));
|
||||
when(ec2.updateNatGatewaySecondaryAddresses(REGION, NAT_ID, List.of(), List.of(), null))
|
||||
.thenReturn(natGateway(NAT_ID, "eni-0123456789abcdef0", "10.0.1.50"));
|
||||
StackResource r = prior("AWS::EC2::NatGateway", "Nat", NAT_ID, Map.of("NatGatewayId", NAT_ID));
|
||||
|
||||
provisioner.provision(r, privateNat, ctx(NAT_ID, previous));
|
||||
|
||||
verify(ec2).updateNatGatewaySecondaryAddresses(REGION, NAT_ID, List.of(), List.of(), null);
|
||||
assertEquals(NAT_ID, r.getPhysicalId());
|
||||
}
|
||||
|
||||
/** An explicit empty list is a declaration: it asks for no secondary, and a template that never declared one still gets it. */
|
||||
@Test
|
||||
void anExplicitlyEmptySecondaryListClearsTheSecondariesAddedOutsideTheStack() {
|
||||
ObjectNode props = publicNatProps();
|
||||
props.putArray("SecondaryAllocationIds");
|
||||
when(ec2.describeNatGateways(REGION, List.of(NAT_ID), Map.of()))
|
||||
.thenReturn(List.of(natGatewayHoldingAnOutsideSecondary("public")));
|
||||
when(ec2.updateNatGatewaySecondaryAddresses(REGION, NAT_ID, List.of(), List.of(), null))
|
||||
.thenReturn(natGateway(NAT_ID, "eni-0123456789abcdef0", "10.0.1.50"));
|
||||
StackResource r = prior("AWS::EC2::NatGateway", "Nat", NAT_ID, Map.of("NatGatewayId", NAT_ID));
|
||||
|
||||
provisioner.provision(r, props, ctx(NAT_ID, publicNatProps()));
|
||||
|
||||
verify(ec2).updateNatGatewaySecondaryAddresses(REGION, NAT_ID, List.of(), List.of(), null);
|
||||
assertEquals(NAT_ID, r.getPhysicalId());
|
||||
assertFalse(r.getAttributes().containsKey(CfnRollback.USES_ATTR_PREFIX + "SecondaryPublicIp.0"));
|
||||
}
|
||||
|
||||
/**
|
||||
* The three properties are one address set: a template that declares only the Elastic IP list
|
||||
* manages the private-address and count properties too, so a secondary added outside the stack
|
||||
* goes when the update brings the set to what the template says.
|
||||
*/
|
||||
@Test
|
||||
void aDeclaredSecondaryPropertyManagesTheWholeSetAndRemovesAnOutsideSecondary() {
|
||||
String declaredAlloc = "eipalloc-0bbbbbbbbbbbbbbbb";
|
||||
ObjectNode props = publicNatProps();
|
||||
props.putArray("SecondaryAllocationIds").add(declaredAlloc);
|
||||
NatGateway held = natGatewayHoldingAnOutsideSecondary("public");
|
||||
NatGatewayAddress declared = new NatGatewayAddress();
|
||||
declared.setPrimary(false);
|
||||
declared.setNetworkInterfaceId("eni-0123456789abcdef0");
|
||||
declared.setPrivateIp("10.0.1.81");
|
||||
declared.setAllocationId(declaredAlloc);
|
||||
declared.setPublicIp("203.0.113.81");
|
||||
held.getNatGatewayAddresses().add(declared);
|
||||
when(ec2.describeNatGateways(REGION, List.of(NAT_ID), Map.of())).thenReturn(List.of(held));
|
||||
NatGateway reconciled = natGateway(NAT_ID, "eni-0123456789abcdef0", "10.0.1.50");
|
||||
reconciled.getNatGatewayAddresses().add(declared);
|
||||
when(ec2.updateNatGatewaySecondaryAddresses(REGION, NAT_ID, List.of(declaredAlloc), List.of(), null))
|
||||
.thenReturn(reconciled);
|
||||
StackResource r = prior("AWS::EC2::NatGateway", "Nat", NAT_ID, Map.of("NatGatewayId", NAT_ID));
|
||||
|
||||
provisioner.provision(r, props, ctx(NAT_ID, props));
|
||||
|
||||
verify(ec2).updateNatGatewaySecondaryAddresses(REGION, NAT_ID, List.of(declaredAlloc), List.of(), null);
|
||||
assertEquals(NAT_ID, r.getPhysicalId());
|
||||
assertEquals("203.0.113.81", r.getAttributes().get(CfnRollback.USES_ATTR_PREFIX + "SecondaryPublicIp.0"));
|
||||
assertFalse(r.getAttributes().containsKey(CfnRollback.USES_ATTR_PREFIX + "SecondaryPublicIp.1"),
|
||||
"the secondary added outside the stack is gone");
|
||||
}
|
||||
|
||||
/** Declaring a secondary for the first time on a kept gateway brings the set to the template's. */
|
||||
@Test
|
||||
void aSecondaryDeclaredForTheFirstTimeOnAKeptGatewayIsAppliedInPlace() {
|
||||
ObjectNode props = publicNatProps();
|
||||
props.putArray("SecondaryAllocationIds").add(OUTSIDE_ALLOC);
|
||||
NatGateway bare = natGateway(NAT_ID, "eni-0123456789abcdef0", "10.0.1.50");
|
||||
bare.setSubnetId(SUBNET_ID);
|
||||
bare.setAllocationId(ALLOC_ID);
|
||||
bare.getNatGatewayAddresses().get(0).setAllocationId(ALLOC_ID);
|
||||
when(ec2.describeNatGateways(REGION, List.of(NAT_ID), Map.of())).thenReturn(List.of(bare));
|
||||
when(ec2.updateNatGatewaySecondaryAddresses(REGION, NAT_ID, List.of(OUTSIDE_ALLOC), List.of(), null))
|
||||
.thenReturn(natGatewayHoldingAnOutsideSecondary("public"));
|
||||
StackResource r = prior("AWS::EC2::NatGateway", "Nat", NAT_ID, Map.of("NatGatewayId", NAT_ID));
|
||||
|
||||
provisioner.provision(r, props, ctx(NAT_ID, publicNatProps()));
|
||||
|
||||
verify(ec2).updateNatGatewaySecondaryAddresses(REGION, NAT_ID, List.of(OUTSIDE_ALLOC), List.of(), null);
|
||||
assertEquals(NAT_ID, r.getPhysicalId());
|
||||
assertEquals(OUTSIDE_PUBLIC_IP, r.getAttributes().get(CfnRollback.USES_ATTR_PREFIX + "SecondaryPublicIp.0"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aPrivateNatGatewayPassesItsSecondaryAddressCount() {
|
||||
when(ec2.createNatGateway(REGION, SUBNET_ID, null, "private", null, List.of(), List.of(), "2", List.of()))
|
||||
|
||||
Reference in new issue
Block a user