feat(rds): report the requested Port in Describe* behind an opt-in setting (#8760)

Floci serves each RDS database through a proxy in the 7001-7099 range,
and Endpoint.Port and DBCluster.Port reported that proxy port even when
the request set Port=5432. The Terraform AWS provider sets port from
those fields, so every aws_db_instance and aws_rds_cluster with a
conventional port planned port 7001 -> 5432 on each run.

floci.services.rds.advertise-requested-port
(FLOCI_SERVICES_RDS_ADVERTISE_REQUESTED_PORT), off by default, makes
DBInstance.Endpoint.Port and DBCluster.Port report the requested Port,
while the proxy keeps listening in its range. A cluster member without
its own Port reports its cluster's. The managed master-user secret and a
CloudFormation secret target attachment keep the connectable proxy (or
published) port. The requested port is stored, so a restart or a backend
retry rebuilds the same advertised endpoint, a stopped database follows
the setting if it changed across a restart, and a blue/green switchover
moves the requested port with the endpoint it belongs to.

Closes #4575

Co-authored-by: Allen Sanborn <allen.sanborn@gmail.com>
This commit is contained in:
Priamo GermosenandAllen Sanborn authored and GitHub committed 2026-11-17 12:50:42 -04:00
1 parent 483dbb0931
commit d1d03a4ad8
12 files changed
+459 -18

No files matched your search

@@ -67,6 +67,11 @@ class SecretTargetAttachmentCfnProvisionerTest {
docDbService = mock(DocDbService.class);
when(secretsManagerService.claimTargetAttachment(any(), any(), any())).thenReturn(true);
when(secretsManagerService.canManageTargetAttachment(any(), any(), any())).thenReturn(true);
// Where clients connect, which is the endpoint's own port unless it advertises the requested Port.
when(rdsService.connectablePort(any(DbInstance.class)))
.thenAnswer(invocation -> invocation.<DbInstance>getArgument(0).getEndpoint().port());
when(rdsService.connectablePort(any(DbCluster.class)))
.thenAnswer(invocation -> invocation.<DbCluster>getArgument(0).getEndpoint().port());
provisioner = CfnProvisionerFixture.builder()
.secretsManager(secretsManagerService)
.objectMapper(mapper)
@@ -123,6 +128,30 @@ class SecretTargetAttachmentCfnProvisionerTest {
SECRET_ARN, "stack/Attachment", REGION);
}
@Test
void anAttachedSecretCarriesThePortClientsConnectToRatherThanTheAdvertisedOne() throws Exception {
stubSecretValues("{\"username\":\"admin\",\"password\":\"secret\"}");
DbInstance instance = dbInstance();
DbCluster cluster = new DbCluster();
cluster.setDbClusterIdentifier("cluster");
cluster.setEngine(DatabaseEngine.MYSQL);
cluster.setEndpoint(new DbEndpoint("cluster.local", 3306));
when(rdsService.getDbInstance("database", REGION)).thenReturn(instance);
when(rdsService.getDbCluster("cluster", REGION)).thenReturn(cluster);
// The endpoints advertise the requested 5432 and 3306; the proxies listen on 7001 and 7002.
when(rdsService.connectablePort(instance)).thenReturn(7001);
when(rdsService.connectablePort(cluster)).thenReturn(7002);
provision(instanceProperties());
provision(properties("AWS::RDS::DBCluster", "cluster"));
ArgumentCaptor<String> values = ArgumentCaptor.forClass(String.class);
verify(secretsManagerService, Mockito.times(2)).putSecretValue(
eq(SECRET_ARN), values.capture(), isNull(), isNull(), eq(REGION), isNull());
assertEquals(7001, mapper.readTree(values.getAllValues().get(0)).path("port").asInt());
assertEquals(7002, mapper.readTree(values.getAllValues().get(1)).path("port").asInt());
}
@Test
void dbClusterAttachmentUsesClusterConnectionFields() throws Exception {
stubSecretValues("{\"username\":\"admin\",\"password\":\"secret\"}");
@@ -0,0 +1,52 @@
package io.floci.aws.services.rds;
import io.floci.aws.testing.RdsAdvertisePortProfile;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.TestProfile;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import static io.floci.aws.testing.RdsRequests.rds;
import static org.hamcrest.Matchers.containsString;
/** With advertise-requested-port on, Describe* reports the Port the client asked for. */
@QuarkusTest
@TestProfile(RdsAdvertisePortProfile.class)
class RdsAdvertiseRequestedPortIntegrationTest {
@AfterEach
void cleanUp() {
rds("DeleteDBInstance").formParam("DBInstanceIdentifier", "adv-instance")
.formParam("SkipFinalSnapshot", "true").when().post("/");
rds("DeleteDBCluster").formParam("DBClusterIdentifier", "adv-cluster")
.formParam("SkipFinalSnapshot", "true").when().post("/");
}
@Test
void instanceAndClusterDescribeReportRequestedPort() {
rds("CreateDBInstance")
.formParam("DBInstanceIdentifier", "adv-instance")
.formParam("Engine", "postgres")
.formParam("DBInstanceClass", "db.t3.micro")
.formParam("AllocatedStorage", "20")
.formParam("MasterUsername", "admin")
.formParam("MasterUserPassword", "password123")
.formParam("Port", "5432")
.when().post("/").then().statusCode(200);
rds("DescribeDBInstances").formParam("DBInstanceIdentifier", "adv-instance")
.when().post("/").then().statusCode(200)
.body(containsString("<Port>5432</Port>"));
rds("CreateDBCluster")
.formParam("DBClusterIdentifier", "adv-cluster")
.formParam("Engine", "aurora-mysql")
.formParam("MasterUsername", "admin")
.formParam("MasterUserPassword", "password123")
.formParam("Port", "3306")
.when().post("/").then().statusCode(200)
.body(containsString("<Port>3306</Port>"));
rds("DescribeDBClusters").formParam("DBClusterIdentifier", "adv-cluster")
.when().post("/").then().statusCode(200)
.body(containsString("<Port>3306</Port>"));
}
}
@@ -27,6 +27,7 @@ import io.floci.aws.services.rds.container.RdsBackendGate;
import io.floci.aws.services.rds.container.RdsContainerHandle;
import io.floci.aws.services.rds.container.RdsContainerManager;
import io.floci.aws.services.rds.export.SnapshotExportSource;
import io.floci.aws.services.rds.model.BlueGreenDeployment;
import io.floci.aws.services.rds.model.DatabaseEngine;
import io.floci.aws.services.rds.model.DbCluster;
import io.floci.aws.services.rds.model.DbClusterEndpoint;
@@ -1870,6 +1871,232 @@ class RdsServiceTest {
assertThrows(AwsException.class, () -> rdsService.getDbInstance("protected-db"));
}
@Test
void advertiseRequestedPortReportsRequestedInstancePortAndKeepsProxyInRange() {
when(rdsConfig.advertiseRequestedPort()).thenReturn(true);
DbInstance instance = createInstanceWithPort("advertised", 5432);
assertEquals(5432, instance.getEndpoint().port());
assertTrue(instance.getProxyPort() >= 7000 && instance.getProxyPort() <= 7099);
assertEquals(5432, rdsService.getDbInstance("advertised").getEndpoint().port());
}
@Test
void advertiseRequestedPortReportsRequestedClusterPort() {
when(rdsConfig.advertiseRequestedPort()).thenReturn(true);
rdsService.createDbCluster("advertised-cluster", "aurora-mysql", "8.0", "admin", "password",
"dbname", false, null, null, null, false, null, null, null, null, false, null,
null, false, 3306);
DbCluster cluster = rdsService.getDbCluster("advertised-cluster");
assertEquals(3306, cluster.getEndpoint().port());
assertEquals(3306, cluster.getReaderEndpoint().port());
assertTrue(cluster.getProxyPort() >= 7000 && cluster.getProxyPort() <= 7099);
}
@Test
void requestedPortOutsideProxyRangeIsReportedAsProxyPortWhenFlagOff() {
DbInstance instance = createInstanceWithPort("not-advertised", 5432);
assertEquals(instance.getProxyPort(), instance.getEndpoint().port());
}
/** Each restore branch rebuilds the endpoint, so each one needs its own run. */
@ParameterizedTest
@CsvSource({"false,AVAILABLE", "true,AVAILABLE", "false,STOPPED"})
void advertisedRequestedPortSurvivesRestartThroughJsonStorage(
boolean mock, DbInstanceStatus status, @TempDir Path tempDir) {
when(rdsConfig.advertiseRequestedPort()).thenReturn(true);
when(rdsConfig.mock()).thenReturn(mock);
Path instanceFile = tempDir.resolve("instances.json");
Path clusterFile = tempDir.resolve("clusters.json");
PersistentStorage<String, DbInstance> instances =
new PersistentStorage<>(instanceFile, new TypeReference<Map<String, DbInstance>>() {});
PersistentStorage<String, DbCluster> clusters =
new PersistentStorage<>(clusterFile, new TypeReference<Map<String, DbCluster>>() {});
RdsService first = newService(containerManager, proxyManager, instances, clusters,
new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>());
first.createDbInstance("restart-instance", "postgres", "13", "admin", "password",
"dbname", "db.t3.micro", 20, false, null, null, null, null, false, false, null,
Map.of(), List.of(), null, null, true, DbInstanceSettings.defaults(), null, 5432);
first.createDbCluster("restart-cluster", "aurora-mysql", "8.0", "admin", "password",
"dbname", false, null, null, null, false, null, null, null, null, false, null,
null, false, 3306);
DbInstance instance = first.getDbInstance("restart-instance");
DbCluster cluster = first.getDbCluster("restart-cluster");
instance.setStatus(status);
cluster.setStatus(status);
if (status == DbInstanceStatus.STOPPED) {
// A stopped record only has its endpoint rebuilt when its port is no longer claimable,
// as with a record that has no port.
instance.setProxyPort(0);
cluster.setProxyPort(0);
}
instances.put("us-east-1::restart-instance", instance);
clusters.put("us-east-1::restart-cluster", cluster);
// Fresh storage objects read back from disk, so only what the JSON carries survives.
PersistentStorage<String, DbInstance> reloadedInstances =
new PersistentStorage<>(instanceFile, new TypeReference<Map<String, DbInstance>>() {});
reloadedInstances.load();
PersistentStorage<String, DbCluster> reloadedClusters =
new PersistentStorage<>(clusterFile, new TypeReference<Map<String, DbCluster>>() {});
reloadedClusters.load();
assertEquals(5432, reloadedInstances.get("us-east-1::restart-instance").orElseThrow().getRequestedPort());
RdsService restarted = newService(containerManager, proxyManager, reloadedInstances,
reloadedClusters, new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>());
restarted.restorePersistedRuntime();
assertEquals(5432, restarted.getDbInstance("restart-instance").getEndpoint().port());
assertEquals(3306, restarted.getDbCluster("restart-cluster").getEndpoint().port());
assertEquals(3306, restarted.getDbCluster("restart-cluster").getReaderEndpoint().port());
}
@Test
void managedSecretCarriesTheConnectablePortNotTheAdvertisedOne() {
when(rdsConfig.advertiseRequestedPort()).thenReturn(true);
SecretsManagerService secretsManager = mock(SecretsManagerService.class);
when(secretsManager.createSecret(any(), any(), any(), any(), any(), any(), any(), any()))
.thenReturn(new Secret());
RdsService service = newService(containerManager, proxyManager,
new InMemoryStorage<>(), new InMemoryStorage<>(),
new InMemoryStorage<>(), new InMemoryStorage<>(), secretsManager);
DbInstance instance = service.createDbInstance("secret-instance", "postgres", "13", "admin",
null, "dbname", "db.t3.micro", 20, false, null, null, null, null, false, true, null,
Map.of(), List.of(), null, null, true, DbInstanceSettings.defaults(), null, 5432);
DbCluster cluster = service.createDbCluster("secret-cluster", "aurora-mysql", "8.0", "admin",
null, "dbname", false, null, null, null, false, null, null, null, null, true, null,
null, false, 3306);
ArgumentCaptor<String> secretString = ArgumentCaptor.forClass(String.class);
verify(secretsManager, times(2)).createSecret(any(), secretString.capture(), any(), any(),
any(), any(), any(), any());
assertEquals(5432, instance.getEndpoint().port());
assertEquals(3306, cluster.getEndpoint().port());
assertTrue(secretString.getAllValues().get(0).contains("\"port\":" + instance.getProxyPort()));
assertTrue(secretString.getAllValues().get(1).contains("\"port\":" + cluster.getProxyPort()));
}
@Test
void clusterMemberReportsTheClusterRequestedPort() {
when(rdsConfig.advertiseRequestedPort()).thenReturn(true);
rdsService.createDbCluster("member-cluster", "aurora-mysql", "8.0", "admin", "password",
"dbname", false, null, null, null, false, null, null, null, null, false, null,
null, false, 3306);
DbInstance member = rdsService.createDbInstance("member", "aurora-mysql", "8.0", null, null,
"dbname", "db.r5.large", 20, false, null, null, "member-cluster", null, false, false,
null, Map.of(), List.of(), null, null, true, DbInstanceSettings.defaults(), null, null);
assertEquals(3306, member.getEndpoint().port());
}
@Test
void theAdvertisedPortFollowsTheEndpointThroughABlueGreenSwitchoverAndARestart(@TempDir Path tempDir) {
when(rdsConfig.advertiseRequestedPort()).thenReturn(true);
when(rdsConfig.mock()).thenReturn(true);
Path instanceFile = tempDir.resolve("instances.json");
PersistentStorage<String, DbInstance> instances =
new PersistentStorage<>(instanceFile, new TypeReference<Map<String, DbInstance>>() {});
RdsService service = newService(containerManager, proxyManager, instances, new InMemoryStorage<>(),
new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>());
DbInstance blue = service.createDbInstance("bg-blue", "postgres", "16.3", "admin", "password",
"dbname", "db.t3.micro", 20, false, null, null, null, null, false, false, null,
Map.of(), List.of(), null, null, true,
new DbInstanceSettings(null, null, 1, null, null, null), null, 5432);
BlueGreenDeployment deployment = service.createBlueGreenDeployment(
new RdsService.BlueGreenDeploymentRequest("bg", blue.getDbInstanceArn(), "17.4", null, null, null,
null, null, Map.of()), "us-east-1");
service.switchoverBlueGreenDeployment(deployment.getBlueGreenDeploymentIdentifier(), null, "us-east-1");
assertEquals(5432, service.getDbInstance("bg-blue").getEndpoint().port());
PersistentStorage<String, DbInstance> reloaded =
new PersistentStorage<>(instanceFile, new TypeReference<Map<String, DbInstance>>() {});
reloaded.load();
RdsService restarted = newService(containerManager, proxyManager, reloaded, new InMemoryStorage<>(),
new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>());
restarted.restorePersistedRuntime();
DbInstance production = restarted.getDbInstance("bg-blue");
assertEquals("17.4", production.getEngineVersion());
assertEquals(5432, production.getEndpoint().port(), "the upgraded instance keeps the blue Port");
DbInstance retired = restarted.getDbInstance("bg-blue-old1");
assertEquals(retired.getProxyPort(), retired.getEndpoint().port(),
"the old blue keeps the endpoint green had, which asked for no Port");
}
@Test
void theConnectablePortIsTheProxysOnlyWhileTheEndpointAdvertisesTheRequestedPort() {
DbInstance plain = createInstanceWithPort("plain", 5432);
assertEquals(plain.getEndpoint().port(), rdsService.connectablePort(plain));
when(rdsConfig.advertiseRequestedPort()).thenReturn(true);
DbInstance advertising = createInstanceWithPort("advertising", 5432);
DbCluster cluster = rdsService.createDbCluster("advertising-cluster", "aurora-mysql", "8.0", "admin",
"password", "dbname", false, null, null, null, false, null, null, null, null, false, null,
null, false, 3306);
assertEquals(5432, advertising.getEndpoint().port());
assertEquals(advertising.getProxyPort(), rdsService.connectablePort(advertising));
assertEquals(3306, cluster.getEndpoint().port());
assertEquals(cluster.getProxyPort(), rdsService.connectablePort(cluster));
}
/** A stopped record keeps its proxy port across a restart, and must still follow a changed setting. */
@ParameterizedTest
@CsvSource({"true,false", "false,true"})
void aStoppedDatabaseFollowsTheSettingAcrossARestart(boolean before, boolean after, @TempDir Path tempDir) {
when(rdsConfig.advertiseRequestedPort()).thenReturn(before);
Path instanceFile = tempDir.resolve("instances.json");
Path clusterFile = tempDir.resolve("clusters.json");
PersistentStorage<String, DbInstance> instances =
new PersistentStorage<>(instanceFile, new TypeReference<Map<String, DbInstance>>() {});
PersistentStorage<String, DbCluster> clusters =
new PersistentStorage<>(clusterFile, new TypeReference<Map<String, DbCluster>>() {});
RdsService first = newService(containerManager, proxyManager, instances, clusters,
new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>());
DbInstance instance = first.createDbInstance("stopped-instance", "postgres", "13", "admin", "password",
"dbname", "db.t3.micro", 20, false, null, null, null, null, false, false, null,
Map.of(), List.of(), null, null, true, DbInstanceSettings.defaults(), null, 5432);
DbCluster cluster = first.createDbCluster("stopped-cluster", "aurora-mysql", "8.0", "admin", "password",
"dbname", false, null, null, null, false, null, null, null, null, false, null,
null, false, 3306);
instance.setStatus(DbInstanceStatus.STOPPED);
cluster.setStatus(DbInstanceStatus.STOPPED);
instances.put("us-east-1::stopped-instance", instance);
clusters.put("us-east-1::stopped-cluster", cluster);
int instanceProxyPort = instance.getProxyPort();
int clusterProxyPort = cluster.getProxyPort();
when(rdsConfig.advertiseRequestedPort()).thenReturn(after);
PersistentStorage<String, DbInstance> reloadedInstances =
new PersistentStorage<>(instanceFile, new TypeReference<Map<String, DbInstance>>() {});
reloadedInstances.load();
PersistentStorage<String, DbCluster> reloadedClusters =
new PersistentStorage<>(clusterFile, new TypeReference<Map<String, DbCluster>>() {});
reloadedClusters.load();
RdsService restarted = newService(containerManager, proxyManager, reloadedInstances, reloadedClusters,
new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>());
restarted.restorePersistedRuntime();
DbInstance stoppedInstance = restarted.getDbInstance("stopped-instance");
DbCluster stoppedCluster = restarted.getDbCluster("stopped-cluster");
assertEquals(instanceProxyPort, stoppedInstance.getProxyPort(), "the proxy port was reclaimed unchanged");
assertEquals(after ? 5432 : instanceProxyPort, stoppedInstance.getEndpoint().port());
assertEquals(after ? 3306 : clusterProxyPort, stoppedCluster.getEndpoint().port());
assertEquals(after ? 3306 : clusterProxyPort, stoppedCluster.getReaderEndpoint().port());
}
private DbInstance createInstanceWithPort(String id, int port) {
return rdsService.createDbInstance(id, "postgres", "13", "admin", "password",
"dbname", "db.t3.micro", 20, false, null, null, null, null, false, false, null,
Map.of(), List.of(), null, null, true, DbInstanceSettings.defaults(), null, port);
}
@Test
void failedInstanceStorageDeleteRetainsRecordAndPortForRetry() {
when(rdsConfig.proxyMaxPort()).thenReturn(7000);
@@ -8227,6 +8454,34 @@ class RdsServiceTest {
assertEquals(0, failed.getProxyPort());
}
@Test
void retryOfEndpointlessInstanceAndClusterAdvertisesTheRequestedPort() {
when(rdsConfig.advertiseRequestedPort()).thenReturn(true);
InMemoryStorage<String, DbInstance> instances = new InMemoryStorage<>();
InMemoryStorage<String, DbCluster> clusters = new InMemoryStorage<>();
DbInstance instance = persistedInstance("legacy", "123456789012", "secret", 7000);
instance.setStatus(DbInstanceStatus.FAILED);
instance.setEndpoint(null);
instance.setProxyPort(0);
instance.setRequestedPort(5432);
instances.put("legacy", instance);
DbCluster cluster = persistedCluster("123456789012", "secret", 7000);
cluster.setStatus(DbInstanceStatus.FAILED);
cluster.setEndpoint(null);
cluster.setReaderEndpoint(null);
cluster.setProxyPort(0);
cluster.setRequestedPort(3306);
clusters.put("cluster1", cluster);
RdsContainerManager manager = mock(RdsContainerManager.class);
when(manager.tryStart(any(), any(), any(), any(), any(), any(), any(), any(), any(), any()))
.thenReturn(new RdsContainerHandle("recovered-container", "arn", "legacy", "127.0.0.1", 15432));
RdsService service = newService(manager, mock(RdsProxyManager.class), instances, clusters,
new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>());
assertEquals(5432, service.ensureInstanceBackend("legacy", "us-east-1").getEndpoint().port());
assertEquals(3306, service.ensureClusterBackend("cluster1", "us-east-1").getEndpoint().port());
}
@Test
void failedStartupBackendRetriesOnItsRetainedEndpoint() {
InMemoryStorage<String, DbInstance> instances = new InMemoryStorage<>();
@@ -0,0 +1,15 @@
package io.floci.aws.testing;
import io.quarkus.test.junit.QuarkusTestProfile;
import java.util.Map;
/** Mocks the RDS container layer and reports the requested Port instead of the proxy port. */
public class RdsAdvertisePortProfile implements QuarkusTestProfile {
@Override
public Map<String, String> getConfigOverrides() {
return Map.of("floci.services.rds.mock", "true",
"floci.services.rds.advertise-requested-port", "true");
}
}