mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
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:
1 parent
483dbb0931
commit
d1d03a4ad8
12 files changed
+459
-18
No files matched your search
@@ -2022,6 +2022,11 @@ public interface EmulatorConfig {
|
||||
@WithDefault("7099")
|
||||
int proxyMaxPort();
|
||||
|
||||
/** When true, Describe* reports the Port a client requested instead of the proxy port.
|
||||
* The proxy still listens in the proxy port range; connect there. */
|
||||
@WithDefault("false")
|
||||
boolean advertiseRequestedPort();
|
||||
|
||||
/** Empty when Floci should adapt its built-in image to the requested engine version. */
|
||||
Optional<String> defaultPostgresImage();
|
||||
|
||||
|
||||
+3
-2
@@ -274,7 +274,8 @@ public class SecretTargetAttachmentCfnProvisioner implements CfnResourceProvisio
|
||||
return new SecretTargetConnection(
|
||||
instance.getEngine().name().toLowerCase(Locale.ROOT),
|
||||
instance.getEndpoint().address(),
|
||||
instance.getEndpoint().port(),
|
||||
// Where clients connect, which differs from the endpoint while it advertises the requested Port.
|
||||
rdsService.connectablePort(instance),
|
||||
instance.getDbName(),
|
||||
"dbInstanceIdentifier",
|
||||
instance.getDbInstanceIdentifier());
|
||||
@@ -293,7 +294,7 @@ public class SecretTargetAttachmentCfnProvisioner implements CfnResourceProvisio
|
||||
return new SecretTargetConnection(
|
||||
cluster.getEngine().name().toLowerCase(Locale.ROOT),
|
||||
cluster.getEndpoint().address(),
|
||||
cluster.getEndpoint().port(),
|
||||
rdsService.connectablePort(cluster),
|
||||
cluster.getDatabaseName(),
|
||||
"dbClusterIdentifier",
|
||||
cluster.getDbClusterIdentifier());
|
||||
|
||||
@@ -918,11 +918,16 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
|
||||
DbEndpoint endpoint;
|
||||
DbInstance instance;
|
||||
// A cluster member reports its cluster's port, as AWS does, unless it asks for its own.
|
||||
Integer advertisedPort = requestedPort != null || publicationCluster == null
|
||||
? requestedPort : publicationCluster.getRequestedPort();
|
||||
try {
|
||||
endpoint = mock ? new DbEndpoint("localhost", proxyPort) : proxyEndpoint(proxyPort);
|
||||
endpoint = advertised(mock ? new DbEndpoint("localhost", proxyPort) : proxyEndpoint(proxyPort),
|
||||
advertisedPort);
|
||||
instance = new DbInstance(id, engine, engineVersion, masterUsername, masterPassword,
|
||||
dbName, dbInstanceClass, allocatedStorage, DbInstanceStatus.CREATING,
|
||||
endpoint, iamEnabled, paramGroupName, dbClusterIdentifier, Instant.now(), proxyPort);
|
||||
instance.setRequestedPort(advertisedPort);
|
||||
instance.setOptionGroupName(optionGroupName);
|
||||
instance.setDbSubnetGroupName(dbSubnetGroupName);
|
||||
instance.setContainerId(containerId);
|
||||
@@ -3705,7 +3710,7 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
instance.setManagedSecretCleanupRequired(false);
|
||||
}
|
||||
|
||||
private static String managedMasterSecretString(DbInstance instance) {
|
||||
private String managedMasterSecretString(DbInstance instance) {
|
||||
try {
|
||||
return JSON.writeValueAsString(Map.of(
|
||||
"username", instance.getMasterUsername(),
|
||||
@@ -3714,7 +3719,7 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
? instance.getEngineIdentifier().toLowerCase()
|
||||
: instance.getEngine().name().toLowerCase(),
|
||||
"host", instance.getEndpoint().address(),
|
||||
"port", instance.getEndpoint().port(),
|
||||
"port", connectablePort(instance),
|
||||
"dbname", instance.getDbName() == null ? "" : instance.getDbName(),
|
||||
"dbInstanceIdentifier", instance.getDbInstanceIdentifier()));
|
||||
} catch (JsonProcessingException e) {
|
||||
@@ -3851,11 +3856,11 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
cluster.setMasterUserSecretKmsKeyId(kmsKeyId);
|
||||
}
|
||||
|
||||
private static String managedMasterSecretString(DbCluster cluster) {
|
||||
private String managedMasterSecretString(DbCluster cluster) {
|
||||
return managedMasterSecretString(cluster, cluster.getMasterPassword());
|
||||
}
|
||||
|
||||
private static String managedMasterSecretString(DbCluster cluster, String password) {
|
||||
private String managedMasterSecretString(DbCluster cluster, String password) {
|
||||
try {
|
||||
return JSON.writeValueAsString(Map.of(
|
||||
"username", cluster.getMasterUsername(),
|
||||
@@ -3864,7 +3869,7 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
? cluster.getEngineIdentifier().toLowerCase()
|
||||
: cluster.getEngine().name().toLowerCase(),
|
||||
"host", cluster.getEndpoint().address(),
|
||||
"port", cluster.getEndpoint().port(),
|
||||
"port", connectablePort(cluster),
|
||||
"dbname", cluster.getDatabaseName() == null ? "" : cluster.getDatabaseName(),
|
||||
"dbClusterIdentifier", cluster.getDbClusterIdentifier()));
|
||||
} catch (JsonProcessingException e) {
|
||||
@@ -5261,7 +5266,7 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
if (relayEndpoint == null) {
|
||||
relayPort = allocateProxyPort();
|
||||
allocatedRelayPort = true;
|
||||
relayEndpoint = proxyEndpoint(relayPort);
|
||||
relayEndpoint = advertised(proxyEndpoint(relayPort), instance.getRequestedPort());
|
||||
}
|
||||
proxyManager.startProxy(rdsResourceRelayKey(instance.getDbInstanceArn(), id),
|
||||
backendIdentity.runtimeId(),
|
||||
@@ -5387,7 +5392,7 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
if (relayEndpoint == null) {
|
||||
relayPort = allocateProxyPort();
|
||||
allocatedRelayPort = true;
|
||||
relayEndpoint = proxyEndpoint(relayPort);
|
||||
relayEndpoint = advertised(proxyEndpoint(relayPort), cluster.getRequestedPort());
|
||||
}
|
||||
proxyManager.startProxy(rdsResourceRelayKey(cluster.getDbClusterArn(), id),
|
||||
cluster.getDbClusterArn(),
|
||||
@@ -5820,10 +5825,12 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
if (manageMasterUserPassword && (masterPassword == null || masterPassword.isBlank())) {
|
||||
masterPassword = generatedMasterPassword();
|
||||
}
|
||||
DbEndpoint endpoint = mock ? new DbEndpoint("localhost", proxyPort) : proxyEndpoint(proxyPort);
|
||||
DbEndpoint endpoint = advertised(mock ? new DbEndpoint("localhost", proxyPort) : proxyEndpoint(proxyPort),
|
||||
requestedPort);
|
||||
DbCluster cluster = new DbCluster(id, engine, engineVersion, masterUsername, masterPassword,
|
||||
databaseName, DbInstanceStatus.AVAILABLE, endpoint, endpoint,
|
||||
iamEnabled, new ArrayList<>(), paramGroupName, Instant.now(), proxyPort);
|
||||
cluster.setRequestedPort(requestedPort);
|
||||
cluster.setEngineIdentifier(effectiveEngineName(engineParam).toLowerCase(Locale.ROOT));
|
||||
cluster.setEngineMode(engineMode != null && !engineMode.isBlank() ? engineMode : "provisioned");
|
||||
cluster.setStorageEncrypted(storageEncrypted);
|
||||
@@ -9263,6 +9270,42 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
usedPorts.remove(port);
|
||||
}
|
||||
|
||||
/**
|
||||
* With {@code advertise-requested-port} on, reports the Port the client asked for instead of the
|
||||
* proxy port. The proxy still listens on its own port; real clients connect there.
|
||||
*/
|
||||
private DbEndpoint advertised(DbEndpoint endpoint, Integer requestedPort) {
|
||||
return advertises(requestedPort) ? new DbEndpoint(endpoint.address(), requestedPort) : endpoint;
|
||||
}
|
||||
|
||||
private boolean advertises(Integer requestedPort) {
|
||||
return requestedPort != null && config.services().rds().advertiseRequestedPort();
|
||||
}
|
||||
|
||||
/**
|
||||
* The port a client connects to {@code instance} on, which a connection secret must carry: its endpoint's, unless
|
||||
* the endpoint advertises the requested Port, which nothing listens on.
|
||||
*/
|
||||
public int connectablePort(DbInstance instance) {
|
||||
return advertises(instance.getRequestedPort())
|
||||
? connectablePort(instance.getProxyPort()) : instance.getEndpoint().port();
|
||||
}
|
||||
|
||||
/** {@link #connectablePort(DbInstance)} for a cluster. */
|
||||
public int connectablePort(DbCluster cluster) {
|
||||
return advertises(cluster.getRequestedPort())
|
||||
? connectablePort(cluster.getProxyPort()) : cluster.getEndpoint().port();
|
||||
}
|
||||
|
||||
private int connectablePort(int proxyPort) {
|
||||
return config.services().rds().mock() ? proxyPort : proxyEndpoint(proxyPort).port();
|
||||
}
|
||||
|
||||
/** The port an endpoint on {@code proxyPort} reports under the current setting. */
|
||||
private int reportedPort(int proxyPort, Integer requestedPort) {
|
||||
return advertises(requestedPort) ? requestedPort : connectablePort(proxyPort);
|
||||
}
|
||||
|
||||
private DbEndpoint proxyEndpoint(int proxyPort) {
|
||||
Optional<String> endpointHost = config.services().rds().endpointHost()
|
||||
.filter(host -> !host.isBlank());
|
||||
@@ -9951,7 +9994,17 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
persistedPortOwners, cluster.getDbClusterArn(), cluster.getProxyPort());
|
||||
if (port != cluster.getProxyPort()) {
|
||||
cluster.setProxyPort(port);
|
||||
DbEndpoint endpoint = proxyEndpoint(port);
|
||||
DbEndpoint endpoint = advertised(proxyEndpoint(port), cluster.getRequestedPort());
|
||||
cluster.setEndpoint(endpoint);
|
||||
cluster.setReaderEndpoint(endpoint);
|
||||
putClusterForScope(accountIdFromArn(cluster.getDbClusterArn()),
|
||||
regionFromArn(cluster.getDbClusterArn()), cluster.getDbClusterIdentifier(), cluster);
|
||||
} else if (cluster.getEndpoint() != null
|
||||
&& cluster.getEndpoint().port() != reportedPort(port, cluster.getRequestedPort())) {
|
||||
// advertise-requested-port changed across the restart: report the port it names now, at the
|
||||
// address clients know.
|
||||
DbEndpoint endpoint = new DbEndpoint(cluster.getEndpoint().address(),
|
||||
reportedPort(port, cluster.getRequestedPort()));
|
||||
cluster.setEndpoint(endpoint);
|
||||
cluster.setReaderEndpoint(endpoint);
|
||||
putClusterForScope(accountIdFromArn(cluster.getDbClusterArn()),
|
||||
@@ -9975,14 +10028,15 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
portReserved = true;
|
||||
cluster.setProxyPort(proxyPort);
|
||||
if (config.services().rds().mock()) {
|
||||
cluster.setEndpoint(new DbEndpoint("localhost", proxyPort));
|
||||
cluster.setReaderEndpoint(new DbEndpoint("localhost", proxyPort));
|
||||
DbEndpoint mockEndpoint = advertised(new DbEndpoint("localhost", proxyPort), cluster.getRequestedPort());
|
||||
cluster.setEndpoint(mockEndpoint);
|
||||
cluster.setReaderEndpoint(mockEndpoint);
|
||||
cluster.setStatus(DbInstanceStatus.AVAILABLE);
|
||||
putClusterForScope(accountId, clusterRegion,
|
||||
cluster.getDbClusterIdentifier(), cluster);
|
||||
continue;
|
||||
}
|
||||
DbEndpoint endpoint = proxyEndpoint(proxyPort);
|
||||
DbEndpoint endpoint = advertised(proxyEndpoint(proxyPort), cluster.getRequestedPort());
|
||||
cluster.setEndpoint(endpoint);
|
||||
cluster.setReaderEndpoint(endpoint);
|
||||
String image = imageForEngine(cluster.getEngine(), cluster.getEngineVersion());
|
||||
@@ -10066,7 +10120,15 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
persistedPortOwners, instance.getDbInstanceArn(), instance.getProxyPort());
|
||||
if (port != instance.getProxyPort()) {
|
||||
instance.setProxyPort(port);
|
||||
instance.setEndpoint(proxyEndpoint(port));
|
||||
instance.setEndpoint(advertised(proxyEndpoint(port), instance.getRequestedPort()));
|
||||
putInstanceForScope(accountIdFromArn(instance.getDbInstanceArn()),
|
||||
regionFromArn(instance.getDbInstanceArn()), instance.getDbInstanceIdentifier(), instance);
|
||||
} else if (instance.getEndpoint() != null
|
||||
&& instance.getEndpoint().port() != reportedPort(port, instance.getRequestedPort())) {
|
||||
// advertise-requested-port changed across the restart: report the port it names now, at the
|
||||
// address clients know.
|
||||
instance.setEndpoint(new DbEndpoint(instance.getEndpoint().address(),
|
||||
reportedPort(port, instance.getRequestedPort())));
|
||||
putInstanceForScope(accountIdFromArn(instance.getDbInstanceArn()),
|
||||
regionFromArn(instance.getDbInstanceArn()), instance.getDbInstanceIdentifier(), instance);
|
||||
}
|
||||
@@ -10097,13 +10159,13 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
portReserved = true;
|
||||
instance.setProxyPort(proxyPort);
|
||||
if (config.services().rds().mock()) {
|
||||
instance.setEndpoint(new DbEndpoint("localhost", proxyPort));
|
||||
instance.setEndpoint(advertised(new DbEndpoint("localhost", proxyPort), instance.getRequestedPort()));
|
||||
instance.setStatus(DbInstanceStatus.AVAILABLE);
|
||||
putInstanceForScope(accountId, instanceRegion,
|
||||
instance.getDbInstanceIdentifier(), instance);
|
||||
continue;
|
||||
}
|
||||
instance.setEndpoint(proxyEndpoint(proxyPort));
|
||||
instance.setEndpoint(advertised(proxyEndpoint(proxyPort), instance.getRequestedPort()));
|
||||
String backendHost;
|
||||
int backendPort;
|
||||
if (clusterId != null && !clusterId.isBlank()) {
|
||||
@@ -12723,10 +12785,14 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
|
||||
DbEndpoint blueEndpoint = blue.getEndpoint();
|
||||
int bluePort = blue.getProxyPort();
|
||||
// The Port an endpoint advertises moves with it, so a rebuilt endpoint keeps reporting it.
|
||||
Integer blueRequestedPort = blue.getRequestedPort();
|
||||
blue.setEndpoint(green.getEndpoint());
|
||||
blue.setProxyPort(green.getProxyPort());
|
||||
blue.setRequestedPort(green.getRequestedPort());
|
||||
green.setEndpoint(blueEndpoint);
|
||||
green.setProxyPort(bluePort);
|
||||
green.setRequestedPort(blueRequestedPort);
|
||||
|
||||
// Blue moves off its ARN first, so it is free when green takes it.
|
||||
deleteInstanceForScope(accountId, effectiveRegion, blueId);
|
||||
|
||||
@@ -42,6 +42,7 @@ public class DbCluster {
|
||||
private String masterUserSecretKmsKeyId;
|
||||
private Instant createdAt;
|
||||
private int proxyPort;
|
||||
private Integer requestedPort;
|
||||
private Map<String, String> tags = new LinkedHashMap<>();
|
||||
private List<DbRoleAssociation> associatedRoles = new ArrayList<>();
|
||||
private String engineMode;
|
||||
@@ -189,6 +190,9 @@ public class DbCluster {
|
||||
public int getProxyPort() { return proxyPort; }
|
||||
public void setProxyPort(int proxyPort) { this.proxyPort = proxyPort; }
|
||||
|
||||
public Integer getRequestedPort() { return requestedPort; }
|
||||
public void setRequestedPort(Integer requestedPort) { this.requestedPort = requestedPort; }
|
||||
|
||||
public List<DbRoleAssociation> getAssociatedRoles() { return associatedRoles; }
|
||||
public void setAssociatedRoles(List<DbRoleAssociation> associatedRoles) {
|
||||
this.associatedRoles = associatedRoles != null ? new ArrayList<>(associatedRoles) : new ArrayList<>();
|
||||
|
||||
@@ -75,6 +75,7 @@ public class DbInstance {
|
||||
private List<DbRoleAssociation> associatedRoles = new ArrayList<>();
|
||||
private Instant createdAt;
|
||||
private int proxyPort;
|
||||
private Integer requestedPort;
|
||||
// Read replication links, kept on both ends the way DescribeDBInstances reports them: a
|
||||
// replica names its source, a source lists its replicas, by identifier within a Region and
|
||||
// by ARN across Regions. Null and empty on a standalone. The status is the "read
|
||||
@@ -288,6 +289,9 @@ public class DbInstance {
|
||||
public int getProxyPort() { return proxyPort; }
|
||||
public void setProxyPort(int proxyPort) { this.proxyPort = proxyPort; }
|
||||
|
||||
public Integer getRequestedPort() { return requestedPort; }
|
||||
public void setRequestedPort(Integer requestedPort) { this.requestedPort = requestedPort; }
|
||||
|
||||
public String getDockerVolumeName() { return dockerVolumeName; }
|
||||
public void setDockerVolumeName(String dockerVolumeName) { this.dockerVolumeName = dockerVolumeName; }
|
||||
|
||||
|
||||
Reference in new issue
Block a user