fix(ec2): preserve endpoint IP types across updates and templates (#5438)

* fix(ec2): preserve endpoint IP types across updates and templates

* fix(ec2): honor omitted endpoint address types in templates
This commit is contained in:
David Phillips authored and GitHub committed 2026-10-11 09:06:25 -04:00
1 parent f4bce30269
commit 80e8b7465d
8 files changed
+184 -20

No files matched your search

@@ -3,6 +3,8 @@ package io.github.hectorvent.floci.services.cloudformation;
import io.github.hectorvent.floci.core.common.XmlParser;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
@@ -24,6 +26,101 @@ class CloudFormationVpcEndpointIntegrationTest {
private static final String EC2_AUTH =
"AWS4-HMAC-SHA256 Credential=test/20260205/us-east-1/ec2/aws4_request";
@Test
void addressTypeResolvesParameterOnCreateAndUpdate() {
String stack = "cfn-vpce-ip-" + Long.toString(System.nanoTime(), 36);
String vpc = XmlParser.extractFirst(given().header("Authorization", EC2_AUTH)
.formParam("Action", "CreateVpc").formParam("CidrBlock", "10.63.0.0/16")
.post("/").then().statusCode(200).extract().asString(), "vpcId", null);
String template = """
{"Parameters":{"AddressType":{"Type":"String"}},"Resources":{
"Endpoint":{"Type":"AWS::EC2::VPCEndpoint","Properties":{
"VpcId":"%s","ServiceName":"com.amazonaws.us-east-1.s3",
"IpAddressType":{"Ref":"AddressType"}}}},
"Outputs":{"EndpointId":{"Value":{"Ref":"Endpoint"}}}}
""".formatted(vpc);
try {
String action = "CreateStack";
for (String type : new String[]{"ipv6", "dualstack", "not-specified"}) {
given().header("Authorization", CFN_AUTH).formParam("Action", action)
.formParam("StackName", stack).formParam("TemplateBody", template)
.formParam("Parameters.member.1.ParameterKey", "AddressType")
.formParam("Parameters.member.1.ParameterValue", type)
.post("/").then().statusCode(200);
assertEquals("CreateStack".equals(action) ? "CREATE_COMPLETE" : "UPDATE_COMPLETE",
CfnStackWaits.awaitTerminal(stack, CFN_AUTH).status());
String described = given().header("Authorization", CFN_AUTH)
.formParam("Action", "DescribeStacks").formParam("StackName", stack)
.post("/").then().statusCode(200).extract().asString();
String id = XmlParser.extractFirst(described, "OutputValue", null);
String endpoint = given().header("Authorization", EC2_AUTH)
.formParam("Action", "DescribeVpcEndpoints").formParam("VpcEndpointId.1", id)
.post("/").then().statusCode(200).extract().asString();
assertEquals("not-specified".equals(type) ? "ipv4" : type,
XmlParser.extractFirst(endpoint, "ipAddressType", null));
action = "UpdateStack";
}
} finally {
given().header("Authorization", CFN_AUTH).formParam("Action", "DeleteStack")
.formParam("StackName", stack).post("/").then().statusCode(200);
CfnStackWaits.awaitStackDeleted(stack, CFN_AUTH);
given().header("Authorization", EC2_AUTH).formParam("Action", "DeleteVpc")
.formParam("VpcId", vpc).post("/").then().statusCode(200);
}
}
@ParameterizedTest
@ValueSource(strings = {"omit", "dualstack", ""})
void conditionalAddressTypeOmitsOnlyNoValue(String initialType) {
String stack = "cfn-vpce-conditional-ip-" + Long.toString(System.nanoTime(), 36);
String vpc = XmlParser.extractFirst(given().header("Authorization", EC2_AUTH)
.formParam("Action", "CreateVpc").formParam("CidrBlock", "10.64.0.0/16")
.post("/").then().statusCode(200).extract().asString(), "vpcId", null);
String template = """
{"Parameters":{"AddressType":{"Type":"String"}},
"Conditions":{"UseType":{"Fn::Not":[{"Fn::Equals":[{"Ref":"AddressType"},"omit"]}]}},
"Resources":{
"Endpoint":{"Type":"AWS::EC2::VPCEndpoint","Properties":{
"VpcId":"%s","ServiceName":"com.amazonaws.us-east-1.s3",
"IpAddressType":{"Fn::If":["UseType",{"Ref":"AddressType"},{"Ref":"AWS::NoValue"}]}}}},
"Outputs":{"EndpointId":{"Value":{"Ref":"Endpoint"}}}}
""".formatted(vpc);
try {
String action = "CreateStack";
for (String type : new String[]{initialType, "omit".equals(initialType) ? "dualstack" : "omit", initialType}) {
given().header("Authorization", CFN_AUTH).formParam("Action", action)
.formParam("StackName", stack).formParam("TemplateBody", template)
.formParam("Parameters.member.1.ParameterKey", "AddressType")
.formParam("Parameters.member.1.ParameterValue", type)
.post("/").then().statusCode(200);
if (type.isEmpty()) {
CfnStackWaits.StackState failed = CfnStackWaits.awaitTerminal(stack, CFN_AUTH);
assertEquals("ROLLBACK_COMPLETE", failed.status(), failed.reason());
assertTrue(failed.reason().contains("IpAddressType"), failed.reason());
break;
}
assertEquals("CreateStack".equals(action) ? "CREATE_COMPLETE" : "UPDATE_COMPLETE",
CfnStackWaits.awaitTerminal(stack, CFN_AUTH).status());
String described = given().header("Authorization", CFN_AUTH)
.formParam("Action", "DescribeStacks").formParam("StackName", stack)
.post("/").then().statusCode(200).extract().asString();
String id = XmlParser.extractFirst(described, "OutputValue", null);
String endpoint = given().header("Authorization", EC2_AUTH)
.formParam("Action", "DescribeVpcEndpoints").formParam("VpcEndpointId.1", id)
.post("/").then().statusCode(200).extract().asString();
assertEquals("omit".equals(type) ? "ipv4" : type,
XmlParser.extractFirst(endpoint, "ipAddressType", null));
action = "UpdateStack";
}
} finally {
given().header("Authorization", CFN_AUTH).formParam("Action", "DeleteStack")
.formParam("StackName", stack).post("/").then().statusCode(200);
CfnStackWaits.awaitStackDeleted(stack, CFN_AUTH);
given().header("Authorization", EC2_AUTH).formParam("Action", "DeleteVpc")
.formParam("VpcId", vpc).post("/").then().statusCode(200);
}
}
@Test
void createStackProvisionsGatewayEndpointAndDeleteRemovesIt() {
String suffix = Long.toString(System.nanoTime(), 36);
@@ -44,6 +44,7 @@ class Ec2VpcEndpointCfnProvisionerTest {
CloudFormationTemplateEngine engine = mock(CloudFormationTemplateEngine.class);
when(engine.resolve(any())).thenAnswer(inv -> fakeResolve(inv.getArgument(0)));
when(engine.resolveNode(any())).thenAnswer(inv -> fakeResolveNode(inv.getArgument(0)));
when(engine.resolveNodeOmittingNoValue(any())).thenAnswer(inv -> fakeResolveNode(inv.getArgument(0)));
// Same contract as the real resolveJsonAttribute: resolve the tree, then unwrap a
// textual result rather than re-serialising it (#2317's double-encoding trap).
when(engine.resolveJsonAttribute(any())).thenAnswer(inv -> {
@@ -113,7 +114,7 @@ class Ec2VpcEndpointCfnProvisionerTest {
@Test
void gatewayEndpointSetsPhysicalIdAndResolvesRefs() {
when(ec2.createVpcEndpoint(eq("us-east-1"), eq("resolved-Vpc"), eq("com.amazonaws.us-east-1.s3"),
eq("Gateway"), eq(List.of("resolved-Rt")), eq(List.of()), eq(List.of()), isNull(), isNull(), anyList()))
eq("Gateway"), eq(List.of("resolved-Rt")), eq(List.of()), eq(List.of()), isNull(), isNull(), anyList(), anyList(), any()))
.thenReturn(endpoint("vpce-123"));
StackResource r = resource();
ObjectNode props = mapper.createObjectNode()
@@ -132,7 +133,7 @@ class Ec2VpcEndpointCfnProvisionerTest {
@Test
void dnsEntriesPairTheZoneWithTheNameAndJoinOnCommas() {
when(ec2.createVpcEndpoint(anyString(), anyString(), anyString(), anyString(),
anyList(), anyList(), anyList(), any(), any(), anyList()))
anyList(), anyList(), anyList(), any(), any(), anyList(), anyList(), any()))
.thenReturn(endpoint("vpce-entries"));
when(ec2.endpointDnsEntries(any())).thenReturn(List.of(
new VpcEndpointDnsEntry("vpce-entries-ab12cd34.s3.us-east-1.vpce.amazonaws.com", "ZREGIONAL"),
@@ -151,7 +152,7 @@ class Ec2VpcEndpointCfnProvisionerTest {
@Test
void privateDnsEnabledResolvesThroughEngine() {
when(ec2.createVpcEndpoint(anyString(), anyString(), anyString(), anyString(),
anyList(), anyList(), anyList(), eq(Boolean.TRUE), any(), anyList()))
anyList(), anyList(), anyList(), eq(Boolean.TRUE), any(), anyList(), anyList(), any()))
.thenReturn(endpoint("vpce-dns"));
StackResource r = resource();
ObjectNode props = mapper.createObjectNode()
@@ -165,7 +166,7 @@ class Ec2VpcEndpointCfnProvisionerTest {
assertEquals("vpce-dns", r.getPhysicalId());
verify(ec2).createVpcEndpoint(eq("us-east-1"), eq("vpc-1"), eq("com.amazonaws.us-east-1.ecr.api"),
eq("Interface"), anyList(), anyList(), anyList(), eq(Boolean.TRUE), any(), anyList());
eq("Interface"), anyList(), anyList(), anyList(), eq(Boolean.TRUE), any(), anyList(), anyList(), any());
}
@Test
@@ -174,7 +175,7 @@ class Ec2VpcEndpointCfnProvisionerTest {
// Passing null here would drop a CloudFormation-declared policy exactly as the
// EC2 API path used to.
when(ec2.createVpcEndpoint(anyString(), anyString(), anyString(), anyString(),
anyList(), anyList(), anyList(), any(), any(), anyList()))
anyList(), anyList(), anyList(), any(), any(), anyList(), anyList(), any()))
.thenReturn(endpoint("vpce-policy"));
StackResource r = resource();
ObjectNode props = mapper.createObjectNode()
@@ -193,7 +194,7 @@ class Ec2VpcEndpointCfnProvisionerTest {
verify(ec2).createVpcEndpoint(eq("us-east-1"), eq("vpc-1"), eq("com.amazonaws.us-east-1.s3"),
anyString(), anyList(), anyList(), anyList(), any(),
eq(policy.toString()), anyList());
eq(policy.toString()), anyList(), anyList(), any());
}
@Test
@@ -201,7 +202,7 @@ class Ec2VpcEndpointCfnProvisionerTest {
// A PolicyDocument carrying Fn::Sub/Ref must reach Ec2Service resolved, or
// DescribeVpcEndpoints hands the caller back literal template text.
when(ec2.createVpcEndpoint(anyString(), anyString(), anyString(), anyString(),
anyList(), anyList(), anyList(), any(), any(), anyList()))
anyList(), anyList(), anyList(), any(), any(), anyList(), anyList(), any()))
.thenReturn(endpoint("vpce-sub"));
StackResource r = resource();
ObjectNode props = mapper.createObjectNode()
@@ -221,7 +222,7 @@ class Ec2VpcEndpointCfnProvisionerTest {
ArgumentCaptor<String> stored = ArgumentCaptor.forClass(String.class);
verify(ec2).createVpcEndpoint(anyString(), anyString(), anyString(), anyString(),
anyList(), anyList(), anyList(), any(), stored.capture(), anyList());
anyList(), anyList(), anyList(), any(), stored.capture(), anyList(), anyList(), any());
String document = stored.getValue();
assertTrue(document.contains("arn:aws:s3:::us-east-1-artifacts/*"), document);
assertTrue(document.contains("resolved-AppRole"), document);
@@ -232,7 +233,7 @@ class Ec2VpcEndpointCfnProvisionerTest {
@Test
void anAbsentPolicyDocumentStaysNull() {
when(ec2.createVpcEndpoint(anyString(), anyString(), anyString(), anyString(),
anyList(), anyList(), anyList(), any(), isNull(), anyList()))
anyList(), anyList(), anyList(), any(), isNull(), anyList(), anyList(), any()))
.thenReturn(endpoint("vpce-nopolicy"));
StackResource r = resource();
ObjectNode props = mapper.createObjectNode()
@@ -242,13 +243,13 @@ class Ec2VpcEndpointCfnProvisionerTest {
provisioner.provision(r, props, ctx());
verify(ec2).createVpcEndpoint(anyString(), anyString(), anyString(), anyString(),
anyList(), anyList(), anyList(), any(), isNull(), anyList());
anyList(), anyList(), anyList(), any(), isNull(), anyList(), anyList(), any());
}
@Test
void updateReplacesAndDeletesPreviousEndpoint() {
when(ec2.createVpcEndpoint(anyString(), anyString(), anyString(), anyString(),
anyList(), anyList(), anyList(), any(), any(), anyList()))
anyList(), anyList(), anyList(), any(), any(), anyList(), anyList(), any()))
.thenReturn(endpoint("vpce-new"));
StackResource r = resource();
r.setPhysicalId("vpce-old");
@@ -77,6 +77,38 @@ class Ec2ModifyVpcEndpointTest {
return ec2("DescribeVpcEndpoints", "VpcEndpointId.1", endpointId);
}
@ParameterizedTest
@ValueSource(strings = {"Gateway", "Interface"})
void modifyingAddressTypePersistsAndOmissionPreservesIt(String endpointType) {
String vpcId = xmlValue(ec2("CreateVpc", "CidrBlock", "10.81.0.0/16"), "vpcId");
String id = xmlValue(ec2("CreateVpcEndpoint", "VpcId", vpcId,
"ServiceName", "com.amazonaws.us-east-1.s3", "VpcEndpointType", endpointType), "vpcEndpointId");
for (String type : List.of("ipv6", "dualstack", "ipv4")) {
ec2("ModifyVpcEndpoint", "VpcEndpointId", id, "IpAddressType", type);
assertEquals(type, xmlValue(describe(id), "ipAddressType"));
ec2("ModifyVpcEndpoint", "VpcEndpointId", id, "PrivateDnsEnabled", "false");
assertEquals(type, xmlValue(describe(id), "ipAddressType"));
}
}
@ParameterizedTest
@ValueSource(strings = {"bogus", "IPv4", "", " ", "not-specified"})
void invalidModifiedAddressTypeDoesNotApplyOtherChanges(String ipAddressType) {
Fixture f = fixture("10.80.0.0/16");
given().header("Authorization", AUTH_HEADER)
.formParam("Action", "ModifyVpcEndpoint")
.formParam("VpcEndpointId", f.endpointId())
.formParam("IpAddressType", ipAddressType)
.formParam("AddRouteTableId.1", f.routeTableId())
.formParam("PolicyDocument", POLICY)
.when().post("/").then().statusCode(400)
.body(hasXPath("//*[local-name()='Code']", equalTo("InvalidParameterValue")));
VpcEndpoint saved = service.describeVpcEndpoints("us-east-1", List.of(f.endpointId()), Map.of()).getFirst();
assertEquals("ipv4", saved.getIpAddressType());
assertEquals(List.of(), saved.getRouteTableIds());
assertNull(saved.getPolicyDocument());
}
@Test
void endpointStatesUseAwsWireValues() {
Fixture f = fixture("10.89.0.0/16");