fix(rds): honor Port on cluster snapshot restore (#4326)

* fix(rds): honor requested port when restoring a cluster snapshot

Reserve an explicit RestoreDBClusterFromSnapshot port for the cluster endpoint and proxy, validate its AWS range, and reject local port collisions. Keep automatic allocation when Port is omitted.

* fix(rds): keep requested ports within published proxy range

* fix(rds): reject occupied requested ports and preserve handler calls
This commit is contained in:
Aniket Shukla authored and GitHub committed 2026-09-26 01:34:59 -05:00
1 parent c357d37fa9
commit df67d421f9
5 files changed
+367 -27

No files matched your search

@@ -0,0 +1,145 @@
package io.github.hectorvent.floci.services.rds;
import io.github.hectorvent.floci.testing.RdsMockProfile;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.TestProfile;
import io.restassured.specification.RequestSpecification;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import static io.restassured.RestAssured.given;
import static io.restassured.http.ContentType.URLENC;
import static org.hamcrest.Matchers.containsString;
/** RDS creation and restore responses must expose usable proxy ports on the wire. */
@QuarkusTest
@TestProfile(RdsMockProfile.class)
class RdsClusterSnapshotRestorePortIntegrationTest {
private static final String SOURCE = "restore-port-source";
private static final String SNAPSHOT = "restore-port-snapshot";
private static final String RESTORED = "restore-port-target";
private static final String COLLISION = "restore-port-collision";
private static final String INSTANCE = "restore-port-instance";
private static final String INSTANCE_SNAPSHOT = "restore-port-instance-snapshot";
private static final String RESTORED_INSTANCE = "restore-port-instance-target";
private static RequestSpecification rds(String action) {
return given().header("Authorization",
"AWS4-HMAC-SHA256 Credential=test/20260924/us-east-1/rds/aws4_request, "
+ "SignedHeaders=content-type;host, Signature=test")
.contentType(URLENC)
.formParam("Action", action)
.formParam("Version", "2014-10-31");
}
@AfterEach
void cleanUp() {
for (String id : new String[]{RESTORED_INSTANCE, INSTANCE}) {
rds("DeleteDBInstance").formParam("DBInstanceIdentifier", id)
.formParam("SkipFinalSnapshot", "true").when().post("/");
}
rds("DeleteDBSnapshot").formParam("DBSnapshotIdentifier", INSTANCE_SNAPSHOT)
.when().post("/");
for (String id : new String[]{COLLISION, RESTORED, SOURCE}) {
rds("DeleteDBCluster").formParam("DBClusterIdentifier", id)
.formParam("SkipFinalSnapshot", "true").when().post("/");
}
rds("DeleteDBClusterSnapshot").formParam("DBClusterSnapshotIdentifier", SNAPSHOT)
.when().post("/");
}
@Test
void requestedPortIsUsedInRestoreResponseAndDescribe() {
rds("CreateDBCluster")
.formParam("DBClusterIdentifier", SOURCE)
.formParam("Engine", "aurora-postgresql")
.formParam("EngineVersion", "16.3")
.formParam("MasterUsername", "admin")
.formParam("MasterUserPassword", "password123")
.when().post("/").then().statusCode(200);
rds("CreateDBClusterSnapshot")
.formParam("DBClusterIdentifier", SOURCE)
.formParam("DBClusterSnapshotIdentifier", SNAPSHOT)
.when().post("/").then().statusCode(200);
rds("RestoreDBClusterFromSnapshot")
.formParam("DBClusterIdentifier", RESTORED)
.formParam("SnapshotIdentifier", SNAPSHOT)
.formParam("Engine", "aurora-postgresql")
.formParam("Port", "7005")
.when().post("/").then().statusCode(200)
.body(containsString("<Port>7005</Port>"));
rds("DescribeDBClusters")
.formParam("DBClusterIdentifier", RESTORED)
.when().post("/").then().statusCode(200)
.body(containsString("<Port>7005</Port>"));
rds("RestoreDBClusterFromSnapshot")
.formParam("DBClusterIdentifier", COLLISION)
.formParam("SnapshotIdentifier", SNAPSHOT)
.formParam("Engine", "aurora-postgresql")
.formParam("Port", "7005")
.when().post("/").then().statusCode(400)
.body(containsString("<Code>InvalidParameterValue</Code>"));
}
@Test
void portOutsidePublishedRangeFallsBackOnCreateAndRestore() {
rds("CreateDBCluster")
.formParam("DBClusterIdentifier", SOURCE)
.formParam("Engine", "aurora-postgresql")
.formParam("EngineVersion", "16.3")
.formParam("MasterUsername", "admin")
.formParam("MasterUserPassword", "password123")
.formParam("Port", "5432")
.when().post("/").then().statusCode(200)
.body(containsString("<Port>7001</Port>"));
rds("CreateDBClusterSnapshot")
.formParam("DBClusterIdentifier", SOURCE)
.formParam("DBClusterSnapshotIdentifier", SNAPSHOT)
.when().post("/").then().statusCode(200);
rds("RestoreDBClusterFromSnapshot")
.formParam("DBClusterIdentifier", RESTORED)
.formParam("SnapshotIdentifier", SNAPSHOT)
.formParam("Engine", "aurora-postgresql")
.formParam("Port", "5432")
.when().post("/").then().statusCode(200)
.body(containsString("<Port>7002</Port>"));
}
@Test
void encryptedInstanceRestorePreservesEncryptionAndRequestedPort() {
rds("CreateDBInstance")
.formParam("DBInstanceIdentifier", INSTANCE)
.formParam("Engine", "postgres")
.formParam("MasterUsername", "admin")
.formParam("MasterUserPassword", "password123")
.formParam("DBInstanceClass", "db.t3.micro")
.formParam("StorageEncrypted", "true")
.formParam("Port", "7005")
.when().post("/").then().statusCode(200)
.body(containsString("<Port>7005</Port>"))
.body(containsString("<StorageEncrypted>true</StorageEncrypted>"));
rds("CreateDBSnapshot")
.formParam("DBInstanceIdentifier", INSTANCE)
.formParam("DBSnapshotIdentifier", INSTANCE_SNAPSHOT)
.when().post("/").then().statusCode(200)
.body(containsString("<Encrypted>true</Encrypted>"));
rds("RestoreDBInstanceFromDBSnapshot")
.formParam("DBInstanceIdentifier", RESTORED_INSTANCE)
.formParam("DBSnapshotIdentifier", INSTANCE_SNAPSHOT)
.formParam("Port", "7006")
.when().post("/").then().statusCode(200)
.body(containsString("<Port>7006</Port>"))
.body(containsString("<StorageEncrypted>true</StorageEncrypted>"));
rds("DescribeDBInstances")
.formParam("DBInstanceIdentifier", RESTORED_INSTANCE)
.when().post("/").then().statusCode(200)
.body(containsString("<Port>7006</Port>"))
.body(containsString("<StorageEncrypted>true</StorageEncrypted>"));
}
}
@@ -290,6 +290,23 @@ class RdsServiceTest {
assertThrows(AwsException.class, () -> rdsService.getDbInstance("mypostgres"));
}
@Test
void invalidMasterUsernameDoesNotReserveRequestedPort() {
assertEquals("InvalidParameterValue", assertThrows(AwsException.class,
() -> rdsService.createDbInstance("invalid-user", "postgres", "13",
"1invalid", "secret123", null, "db.t3.micro",
20, false, null, null, null, null, false,
false, null, Map.of(), List.of(), null, "us-east-1",
true, DbInstanceSettings.defaults(), null, 7005)).getErrorCode());
DbInstance retried = rdsService.createDbInstance("valid-user", "postgres", "13",
"admin", "secret123", null, "db.t3.micro",
20, false, null, null, null, null, false,
false, null, Map.of(), List.of(), null, "us-east-1",
true, DbInstanceSettings.defaults(), null, 7005);
assertEquals(7005, retried.getProxyPort());
}
@Test
void createDbInstanceRejectsLegacyBareAuroraEngine() {
// "aurora" (bare) is the retired Aurora MySQL 5.6 identifier; real AWS no
@@ -3555,6 +3572,42 @@ class RdsServiceTest {
() -> rdsService.deleteDbClusterSnapshot("csnap-copy")).getErrorCode());
}
@Test
void restoreClusterFromSnapshotUsesRequestedPortAndReleasesItOnDelete() {
clusterSnapshotOfNewCluster("csnap");
DbCluster restored = rdsService.restoreDbClusterFromSnapshot("restored-cluster", "csnap",
"aurora-postgresql", null, 7005, null, null, null, null, null, null,
null, "us-east-1");
assertEquals(7005, restored.getEndpoint().port());
assertEquals(7005, restored.getProxyPort());
assertEquals(7005, rdsService.getDbCluster("restored-cluster").getEndpoint().port());
verify(proxyManager).startProxy(any(), any(), anyBoolean(), eq(7005), any(), anyInt(),
any(), any(), any(), any(), any(), any());
assertEquals("InvalidParameterValue", assertThrows(AwsException.class,
() -> rdsService.restoreDbClusterFromSnapshot("duplicate-port", "csnap",
"aurora-postgresql", null, 7005, null, null, null, null, null, null,
null, "us-east-1")).getErrorCode());
DbCluster outsideRange = rdsService.restoreDbClusterFromSnapshot("outside-range", "csnap",
"aurora-postgresql", null, 5432, null, null, null, null, null, null,
null, "us-east-1");
assertNotEquals(5432, outsideRange.getProxyPort());
assertTrue(outsideRange.getProxyPort() >= 7000 && outsideRange.getProxyPort() <= 7099);
assertNotEquals(restored.getProxyPort(), outsideRange.getProxyPort());
assertEquals("InvalidParameterValue", assertThrows(AwsException.class,
() -> rdsService.restoreDbClusterFromSnapshot("invalid-port", "csnap",
"aurora-postgresql", null, 1149, null, null, null, null, null, null,
null, "us-east-1")).getErrorCode());
rdsService.deleteDbCluster("restored-cluster");
DbCluster reused = rdsService.restoreDbClusterFromSnapshot("reused-port", "csnap",
"aurora-postgresql", null, 7005, null, null, null, null, null, null,
null, "us-east-1");
assertEquals(7005, reused.getEndpoint().port());
}
@Test
void clusterSnapshotRestoreAttributeIsSharedLikeAnInstanceSnapshots() {
clusterSnapshotOfNewCluster("csnap");