feat(route53): support cross-account VPC associations (#3116)

* feat(route53): support cross-account VPC associations

* test(route53): add SDK cross-account compatibility coverage

* fix(route53): harden cross-account VPC ownership

* fix(ec2): scope default resources by account
This commit is contained in:
Franklin G. Mendoza authored and GitHub committed 2026-09-06 00:05:31 -04:00
1 parent 3927c66040
commit 162211de0c
14 files changed
+962 -48

No files matched your search

@@ -1,5 +1,6 @@
package io.github.hectorvent.floci.services.route53;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.services.route53.model.VpcAssociation;
import io.quarkus.test.junit.QuarkusTest;
import jakarta.inject.Inject;
@@ -9,11 +10,15 @@ import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
@QuarkusTest
class Route53VpcAssociationIntegrationTest {
private static final String XML = "application/xml";
private static final String ZONE_ACCOUNT = "000000000301";
private static final String VPC_ACCOUNT = "000000000302";
private static final String OTHER_ACCOUNT = "000000000303";
@Inject
Route53Service service;
@@ -158,7 +163,7 @@ class Route53VpcAssociationIntegrationTest {
}
@Test
void createAndDeleteVpcAssociationAuthorization() {
void createListAndDeleteVpcAssociationAuthorization() {
String zoneId = createPrivateZone("authz.internal.", "vpc-assoc-authorization", "vpc-authz-primary");
given().contentType(XML)
@@ -170,10 +175,26 @@ class Route53VpcAssociationIntegrationTest {
.body(containsString("<VPCId>vpc-authz-guest</VPCId>"))
.body(containsString("<VPCRegion>us-east-1</VPCRegion>"));
given().get("/2013-04-01/hostedzone/" + zoneId + "/authorizevpcassociation")
.then().statusCode(200)
.body(containsString("<ListVPCAssociationAuthorizationsResponse"))
.body(containsString("<HostedZoneId>" + zoneId + "</HostedZoneId>"))
.body(containsString("<VPCId>vpc-authz-guest</VPCId>"));
given().contentType(XML)
.body(vpcRequest("DeleteVPCAssociationAuthorizationRequest", "vpc-authz-guest", ""))
.post("/2013-04-01/hostedzone/" + zoneId + "/deauthorizevpcassociation")
.then().statusCode(200);
given().get("/2013-04-01/hostedzone/" + zoneId + "/authorizevpcassociation")
.then().statusCode(200)
.body(not(containsString("<VPCId>vpc-authz-guest</VPCId>")));
given().contentType(XML)
.body(vpcRequest("DeleteVPCAssociationAuthorizationRequest", "vpc-authz-guest", ""))
.post("/2013-04-01/hostedzone/" + zoneId + "/deauthorizevpcassociation")
.then().statusCode(404)
.body(containsString("<Code>VPCAssociationAuthorizationNotFound</Code>"));
}
@Test
@@ -374,6 +395,240 @@ class Route53VpcAssociationIntegrationTest {
.body(containsString("<DisassociateVPCFromHostedZoneResponse"));
}
@Test
void crossAccountAuthorizationEnforcesVpcOwnershipAndCleanupLifecycle() {
String zoneVpc = createVpcForAccount(ZONE_ACCOUNT, "10.81.0.0/16");
String spokeVpc = createVpcForAccount(VPC_ACCOUNT, "10.82.0.0/16");
String zoneId = createPrivateZoneForAccount(
ZONE_ACCOUNT, "cross-account.internal.", "vpc-assoc-cross-account", zoneVpc);
// AWS requires the VPC owner to associate, and requires the hosted-zone owner to
// authorize first. A different account without authorization gets NotAuthorizedException.
given().contentType(XML)
.header("Authorization", route53Auth(VPC_ACCOUNT))
.body(vpcRequest("AssociateVPCWithHostedZoneRequest", spokeVpc, ""))
.post("/2013-04-01/hostedzone/" + zoneId + "/associatevpc")
.then().statusCode(401)
.body(containsString("<Code>NotAuthorizedException</Code>"));
given().contentType(XML)
.header("Authorization", route53Auth(ZONE_ACCOUNT))
.body(vpcRequest("CreateVPCAssociationAuthorizationRequest", spokeVpc, ""))
.post("/2013-04-01/hostedzone/" + zoneId + "/authorizevpcassociation")
.then().statusCode(200);
// The authorization is specifically for the account that owns this VPC. A third account
// cannot reuse the hosted-zone owner's authorization by copying the VPC id.
given().contentType(XML)
.header("Authorization", route53Auth(OTHER_ACCOUNT))
.body(vpcRequest("AssociateVPCWithHostedZoneRequest", spokeVpc, ""))
.post("/2013-04-01/hostedzone/" + zoneId + "/associatevpc")
.then().statusCode(400)
.body(containsString("<Code>InvalidVPCId</Code>"));
given().contentType(XML)
.header("Authorization", route53Auth(VPC_ACCOUNT))
.body(vpcRequest("AssociateVPCWithHostedZoneRequest", spokeVpc, ""))
.post("/2013-04-01/hostedzone/" + zoneId + "/associatevpc")
.then().statusCode(200);
// ListHostedZonesByVPC is cross-account and reports the account that owns the zone.
given().header("Authorization", route53Auth(VPC_ACCOUNT))
.get("/2013-04-01/hostedzonesbyvpc?vpcid=" + spokeVpc + "&vpcregion=us-east-1")
.then().statusCode(200)
.body(containsString("<HostedZoneId>" + zoneId + "</HostedZoneId>"))
.body(containsString("<OwningAccount>" + ZONE_ACCOUNT + "</OwningAccount>"));
// AWS documents ListHostedZonesByVPC as cross-account discovery: callers can list
// associations for a VPC regardless of which account owns the VPC or hosted zone.
given().header("Authorization", route53Auth(OTHER_ACCOUNT))
.get("/2013-04-01/hostedzonesbyvpc?vpcid=" + spokeVpc + "&vpcregion=us-east-1")
.then().statusCode(200)
.body(containsString("<HostedZoneId>" + zoneId + "</HostedZoneId>"))
.body(containsString("<OwningAccount>" + ZONE_ACCOUNT + "</OwningAccount>"));
// AWS recommends deleting the authorization after association. Success has an empty body,
// and deleting it must not remove the established association.
given().contentType(XML)
.header("Authorization", route53Auth(ZONE_ACCOUNT))
.body(vpcRequest("DeleteVPCAssociationAuthorizationRequest", spokeVpc, ""))
.post("/2013-04-01/hostedzone/" + zoneId + "/deauthorizevpcassociation")
.then().statusCode(200)
.body(org.hamcrest.Matchers.emptyOrNullString());
given().header("Authorization", route53Auth(VPC_ACCOUNT))
.get("/2013-04-01/hostedzonesbyvpc?vpcid=" + spokeVpc + "&vpcregion=us-east-1")
.then().statusCode(200)
.body(containsString("<HostedZoneId>" + zoneId + "</HostedZoneId>"));
// AWS permits either the hosted-zone owner or the VPC owner to disassociate.
given().contentType(XML)
.header("Authorization", route53Auth(ZONE_ACCOUNT))
.body(vpcRequest("DisassociateVPCFromHostedZoneRequest", spokeVpc, ""))
.post("/2013-04-01/hostedzone/" + zoneId + "/disassociatevpc")
.then().statusCode(200);
// With the authorization cleaned up, a later reassociation requires a new authorization.
given().contentType(XML)
.header("Authorization", route53Auth(VPC_ACCOUNT))
.body(vpcRequest("AssociateVPCWithHostedZoneRequest", spokeVpc, ""))
.post("/2013-04-01/hostedzone/" + zoneId + "/associatevpc")
.then().statusCode(401)
.body(containsString("<Code>NotAuthorizedException</Code>"));
}
@Test
void unownedAuthorizationCannotBeConsumedCrossAccount() {
String zoneVpc = createVpcForAccount(ZONE_ACCOUNT, "10.83.0.0/16");
String zoneId = createPrivateZoneForAccount(
ZONE_ACCOUNT, "unowned-auth.internal.", "vpc-assoc-unowned-auth", zoneVpc);
String unknownVpc = "vpc-unmodelled-cross-account";
given().contentType(XML)
.header("Authorization", route53Auth(ZONE_ACCOUNT))
.body(vpcRequest("CreateVPCAssociationAuthorizationRequest", unknownVpc, ""))
.post("/2013-04-01/hostedzone/" + zoneId + "/authorizevpcassociation")
.then().statusCode(200);
given().contentType(XML)
.header("Authorization", route53Auth(OTHER_ACCOUNT))
.body(vpcRequest("AssociateVPCWithHostedZoneRequest", unknownVpc, ""))
.post("/2013-04-01/hostedzone/" + zoneId + "/associatevpc")
.then().statusCode(400)
.body(containsString("<Code>InvalidVPCId</Code>"));
}
@Test
void legacyAssociationWithoutOwnerStillRequiresVpcOrZoneOwner() {
String zoneVpc = createVpcForAccount(ZONE_ACCOUNT, "10.84.0.0/16");
String spokeVpc = createVpcForAccount(VPC_ACCOUNT, "10.85.0.0/16");
String zoneId = createPrivateZoneForAccount(
ZONE_ACCOUNT, "legacy-association.internal.", "vpc-assoc-legacy-owner", zoneVpc);
given().contentType(XML)
.header("Authorization", route53Auth(ZONE_ACCOUNT))
.body(vpcRequest("CreateVPCAssociationAuthorizationRequest", spokeVpc, ""))
.post("/2013-04-01/hostedzone/" + zoneId + "/authorizevpcassociation")
.then().statusCode(200);
given().contentType(XML)
.header("Authorization", route53Auth(VPC_ACCOUNT))
.body(vpcRequest("AssociateVPCWithHostedZoneRequest", spokeVpc, ""))
.post("/2013-04-01/hostedzone/" + zoneId + "/associatevpc")
.then().statusCode(200);
var zone = service.listHostedZonesByVpc(spokeVpc, "us-east-1").stream()
.filter(candidate -> candidate.getId().equals(zoneId))
.findFirst().orElseThrow();
zone.getVpcAssociations().stream()
.filter(association -> spokeVpc.equals(association.getVpcId()))
.findFirst().orElseThrow()
.setOwnerAccountId(null);
given().contentType(XML)
.header("Authorization", route53Auth(OTHER_ACCOUNT))
.body(vpcRequest("DisassociateVPCFromHostedZoneRequest", spokeVpc, ""))
.post("/2013-04-01/hostedzone/" + zoneId + "/disassociatevpc")
.then().statusCode(400)
.body(containsString("<Code>InvalidVPCId</Code>"));
given().contentType(XML)
.header("Authorization", route53Auth(VPC_ACCOUNT))
.body(vpcRequest("DisassociateVPCFromHostedZoneRequest", spokeVpc, ""))
.post("/2013-04-01/hostedzone/" + zoneId + "/disassociatevpc")
.then().statusCode(200);
}
@Test
void legacyNonDefaultZoneInfersOwnerFromStoragePartition() {
String zoneVpc = createVpcForAccount(ZONE_ACCOUNT, "10.86.0.0/16");
String zoneId = createPrivateZoneForAccount(
ZONE_ACCOUNT, "legacy-zone.internal.", "vpc-assoc-legacy-zone", zoneVpc);
var legacyZone = service.listHostedZonesByVpc(zoneVpc, "us-east-1").stream()
.filter(candidate -> candidate.getId().equals(zoneId))
.findFirst().orElseThrow();
legacyZone.setOwnerAccountId(null);
given().contentType(XML)
.header("Authorization", route53Auth(ZONE_ACCOUNT))
.body(vpcRequest("CreateVPCAssociationAuthorizationRequest", "vpc-legacy-zone-guest", ""))
.post("/2013-04-01/hostedzone/" + zoneId + "/authorizevpcassociation")
.then().statusCode(200);
given().header("Authorization", route53Auth(ZONE_ACCOUNT))
.get("/2013-04-01/hostedzone/" + zoneId + "/authorizevpcassociation")
.then().statusCode(200)
.body(containsString("<VPCId>vpc-legacy-zone-guest</VPCId>"));
}
@Test
void documentedVpcAssociationQuotaReturnsLimitsExceeded() {
String zoneId = createPrivateZone(
"association-limit.internal.", "vpc-assoc-limit", "vpc-limit-primary");
for (int i = 1; i < 300; i++) {
service.associateVpcWithHostedZone(
zoneId, new VpcAssociation("vpc-limit-" + i, "us-east-1"), null);
}
AwsException error = assertThrows(AwsException.class, () ->
service.associateVpcWithHostedZone(
zoneId, new VpcAssociation("vpc-limit-overflow", "us-east-1"), null));
assertEquals("LimitsExceeded", error.getErrorCode());
assertEquals(400, error.getHttpStatus());
}
@Test
void documentedAuthorizationQuotaReturnsTooManyAuthorizations() {
String zoneId = createPrivateZone(
"authorization-limit.internal.", "vpc-authz-limit", "vpc-authz-limit-primary");
for (int i = 0; i < 1000; i++) {
service.createVpcAssociationAuthorization(
zoneId, new VpcAssociation("vpc-authz-limit-" + i, "us-east-1"));
}
AwsException error = assertThrows(AwsException.class, () ->
service.createVpcAssociationAuthorization(
zoneId, new VpcAssociation("vpc-authz-limit-overflow", "us-east-1")));
assertEquals("TooManyVPCAssociationAuthorizations", error.getErrorCode());
assertEquals(400, error.getHttpStatus());
}
private static String createVpcForAccount(String accountId, String cidr) {
return given()
.formParam("Action", "CreateVpc")
.formParam("CidrBlock", cidr)
.header("Authorization", ec2Auth(accountId))
.post("/")
.then().statusCode(200)
.extract().path("CreateVpcResponse.vpc.vpcId");
}
private static String createPrivateZoneForAccount(String accountId, String name,
String callerReference, String vpcId) {
String create = """
<CreateHostedZoneRequest xmlns="https://route53.amazonaws.com/doc/2013-04-01/">
<Name>%s</Name>
<CallerReference>%s</CallerReference>
<VPC><VPCRegion>us-east-1</VPCRegion><VPCId>%s</VPCId></VPC>
</CreateHostedZoneRequest>
""".formatted(name, callerReference, vpcId);
return zoneIdFrom(given().contentType(XML)
.header("Authorization", route53Auth(accountId))
.body(create)
.post("/2013-04-01/hostedzone")
.then().statusCode(201).extract().header("Location"));
}
private static String route53Auth(String accountId) {
return "AWS4-HMAC-SHA256 Credential=" + accountId
+ "/20260905/us-east-1/route53/aws4_request, SignedHeaders=host, Signature=abc";
}
private static String ec2Auth(String accountId) {
return "AWS4-HMAC-SHA256 Credential=" + accountId
+ "/20260905/us-east-1/ec2/aws4_request, SignedHeaders=host, Signature=abc";
}
private static String createPrivateZone(String name, String callerReference, String vpcId) {
String create = """
<CreateHostedZoneRequest xmlns="https://route53.amazonaws.com/doc/2013-04-01/">
@@ -0,0 +1,119 @@
package io.github.hectorvent.floci.services.route53;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.QuarkusTestProfile;
import io.quarkus.test.junit.TestProfile;
import org.junit.jupiter.api.Test;
import java.time.Duration;
import java.util.Map;
import static io.restassured.RestAssured.given;
import static org.awaitility.Awaitility.await;
import static org.hamcrest.Matchers.containsString;
@QuarkusTest
@TestProfile(Route53VpcAssociationTransientErrorIntegrationTest.DelayProfile.class)
class Route53VpcAssociationTransientErrorIntegrationTest {
private static final String XML = "application/xml";
public static final class DelayProfile implements QuarkusTestProfile {
@Override
public Map<String, String> getConfigOverrides() {
return Map.of(
"floci.storage.mode", "memory",
"floci.services.route53.vpc-association-control-plane-delay-ms", "150");
}
}
@Test
void associationOverlapReturnsPriorRequestNotCompleteThenRetrySucceeds() {
String zoneId = createPrivateZone(
"retry-association.internal.", "retry-association", "vpc-retry-primary");
associate(zoneId, "vpc-retry-secondary")
.statusCode(200);
associate(zoneId, "vpc-retry-tertiary")
.statusCode(400)
.body(containsString("<Code>PriorRequestNotComplete</Code>"));
await().atMost(Duration.ofSeconds(2))
.pollInterval(Duration.ofMillis(25))
.untilAsserted(() -> associate(zoneId, "vpc-retry-tertiary")
.statusCode(200));
}
@Test
void authorizationOverlapReturnsConcurrentModificationThenRetrySucceeds() {
String zoneId = createPrivateZone(
"retry-authorization.internal.", "retry-authorization", "vpc-authz-primary");
authorize(zoneId, "vpc-authz-one")
.statusCode(200);
authorize(zoneId, "vpc-authz-two")
.statusCode(400)
.body(containsString("<Code>ConcurrentModification</Code>"));
await().atMost(Duration.ofSeconds(2))
.pollInterval(Duration.ofMillis(25))
.untilAsserted(() -> authorize(zoneId, "vpc-authz-two")
.statusCode(200));
deauthorize(zoneId, "vpc-authz-one")
.statusCode(400)
.body(containsString("<Code>ConcurrentModification</Code>"));
await().atMost(Duration.ofSeconds(2))
.pollInterval(Duration.ofMillis(25))
.untilAsserted(() -> deauthorize(zoneId, "vpc-authz-one")
.statusCode(200));
}
private static io.restassured.response.ValidatableResponse associate(String zoneId, String vpcId) {
return given().contentType(XML)
.body(vpcRequest("AssociateVPCWithHostedZoneRequest", vpcId))
.post("/2013-04-01/hostedzone/" + zoneId + "/associatevpc")
.then();
}
private static io.restassured.response.ValidatableResponse authorize(String zoneId, String vpcId) {
return given().contentType(XML)
.body(vpcRequest("CreateVPCAssociationAuthorizationRequest", vpcId))
.post("/2013-04-01/hostedzone/" + zoneId + "/authorizevpcassociation")
.then();
}
private static io.restassured.response.ValidatableResponse deauthorize(String zoneId, String vpcId) {
return given().contentType(XML)
.body(vpcRequest("DeleteVPCAssociationAuthorizationRequest", vpcId))
.post("/2013-04-01/hostedzone/" + zoneId + "/deauthorizevpcassociation")
.then();
}
private static String createPrivateZone(String name, String callerReference, String vpcId) {
String create = """
<CreateHostedZoneRequest xmlns="https://route53.amazonaws.com/doc/2013-04-01/">
<Name>%s</Name>
<CallerReference>%s</CallerReference>
<VPC><VPCRegion>us-east-1</VPCRegion><VPCId>%s</VPCId></VPC>
</CreateHostedZoneRequest>
""".formatted(name, callerReference, vpcId);
String location = given().contentType(XML)
.body(create)
.post("/2013-04-01/hostedzone")
.then().statusCode(201)
.extract().header("Location");
return location.substring(location.lastIndexOf('/') + 1);
}
private static String vpcRequest(String root, String vpcId) {
return """
<%s xmlns="https://route53.amazonaws.com/doc/2013-04-01/">
<VPC><VPCRegion>us-east-1</VPCRegion><VPCId>%s</VPCId></VPC>
</%s>
""".formatted(root, vpcId, root);
}
}