mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
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:
1 parent
c357d37fa9
commit
df67d421f9
5 files changed
+367
-27
No files matched your search
@@ -179,6 +179,15 @@ they belong to, as the model requires. An omitted `Enabled` activates the subscr
|
||||
|
||||
RDS requires the Docker socket and port range exposure. For private registry authentication and other Docker settings see [Docker Configuration](../configuration/docker.md).
|
||||
|
||||
`CreateDBInstance`, `CreateDBCluster`, `RestoreDBInstanceFromDBSnapshot`, and
|
||||
`RestoreDBClusterFromSnapshot` honor a requested `Port` only within the configured
|
||||
RDS proxy range. A port outside that range causes Floci to assign the next free port
|
||||
and return it in the endpoint. A port inside the range that is already in use is
|
||||
rejected. This differs from AWS because Floci's local proxy must use a published
|
||||
host port; for example, requesting `5432` with the default
|
||||
`7001`-`7099` range returns a port in that range. Ports outside AWS's valid
|
||||
`1150`-`65535` interval are rejected.
|
||||
|
||||
When Docker publishes RDS proxy ports dynamically, set `FLOCI_SERVICES_RDS_ENDPOINT_HOST` to the
|
||||
hostname used by clients. Floci inspects its own container through the Docker socket and returns the
|
||||
corresponding published port from `DescribeDBInstances` and `DescribeDBClusters`. Leave the setting
|
||||
|
||||
@@ -220,11 +220,19 @@ public class RdsQueryHandler {
|
||||
try {
|
||||
DbInstanceSettings settings = instanceSettings(params);
|
||||
List<String> vpcSecurityGroupIds = vpcSecurityGroupIds(params);
|
||||
DbInstance instance = service.createDbInstance(id, engine, engineVersion, masterUsername,
|
||||
masterPassword, dbName, dbInstanceClass, allocatedStorage, iamEnabled,
|
||||
paramGroupName, dbSubnetGroupName, dbClusterIdentifier, availabilityZone, multiAz,
|
||||
manageMasterUserPassword, masterUserSecretKmsKeyId, tags, vpcSecurityGroupIds,
|
||||
optionGroupName, region, autoMinorVersionUpgrade, settings, publiclyAccessible);
|
||||
Integer requestedPort = parseIntegerParam(params, "Port");
|
||||
DbInstance instance = requestedPort == null
|
||||
? service.createDbInstance(id, engine, engineVersion, masterUsername,
|
||||
masterPassword, dbName, dbInstanceClass, allocatedStorage, iamEnabled,
|
||||
paramGroupName, dbSubnetGroupName, dbClusterIdentifier, availabilityZone, multiAz,
|
||||
manageMasterUserPassword, masterUserSecretKmsKeyId, tags, vpcSecurityGroupIds,
|
||||
optionGroupName, region, autoMinorVersionUpgrade, settings, publiclyAccessible)
|
||||
: service.createDbInstance(id, engine, engineVersion, masterUsername,
|
||||
masterPassword, dbName, dbInstanceClass, allocatedStorage, iamEnabled,
|
||||
paramGroupName, dbSubnetGroupName, dbClusterIdentifier, availabilityZone, multiAz,
|
||||
manageMasterUserPassword, masterUserSecretKmsKeyId, tags, vpcSecurityGroupIds,
|
||||
optionGroupName, region, autoMinorVersionUpgrade, settings, publiclyAccessible,
|
||||
requestedPort);
|
||||
String result = dbInstanceXml(instance);
|
||||
return Response.ok(AwsQueryResponse.envelope("CreateDBInstance", AwsNamespaces.RDS, result)).build();
|
||||
} catch (AwsException e) {
|
||||
@@ -901,18 +909,36 @@ public class RdsQueryHandler {
|
||||
Double serverlessV2Max = parseDoubleParam(params, "ServerlessV2ScalingConfiguration.MaxCapacity");
|
||||
Integer serverlessV2SecondsUntilAutoPause = parseIntegerParam(
|
||||
params, "ServerlessV2ScalingConfiguration.SecondsUntilAutoPause");
|
||||
Integer requestedPort = parseIntegerParam(params, "Port");
|
||||
String globalClusterIdentifier = params.getFirst("GlobalClusterIdentifier");
|
||||
DbCluster cluster = globalClusterIdentifier != null && !globalClusterIdentifier.isBlank()
|
||||
DbCluster cluster;
|
||||
if (globalClusterIdentifier != null && !globalClusterIdentifier.isBlank()) {
|
||||
cluster = requestedPort == null
|
||||
? service.createDbClusterInGlobalCluster(globalClusterIdentifier, id, engine,
|
||||
params.getFirst("EngineVersion"), masterUsername, masterPassword, databaseName,
|
||||
iamEnabled, paramGroupName, dbSubnetGroupName, availabilityZone, multiAz, region,
|
||||
serverlessV2Min, serverlessV2Max, serverlessV2SecondsUntilAutoPause,
|
||||
manageMasterUserPassword, masterUserSecretKmsKeyId, engineMode, storageEncrypted)
|
||||
: service.createDbClusterInGlobalCluster(globalClusterIdentifier, id, engine,
|
||||
params.getFirst("EngineVersion"), masterUsername, masterPassword, databaseName,
|
||||
iamEnabled, paramGroupName, dbSubnetGroupName, availabilityZone, multiAz, region,
|
||||
serverlessV2Min, serverlessV2Max, serverlessV2SecondsUntilAutoPause,
|
||||
manageMasterUserPassword, masterUserSecretKmsKeyId, engineMode, storageEncrypted,
|
||||
requestedPort);
|
||||
} else {
|
||||
cluster = requestedPort == null
|
||||
? service.createDbCluster(id, engine, engineVersion, masterUsername,
|
||||
masterPassword, databaseName, iamEnabled, paramGroupName,
|
||||
dbSubnetGroupName, availabilityZone, multiAz, region,
|
||||
serverlessV2Min, serverlessV2Max, serverlessV2SecondsUntilAutoPause,
|
||||
manageMasterUserPassword, masterUserSecretKmsKeyId, engineMode, storageEncrypted)
|
||||
: service.createDbCluster(id, engine, engineVersion, masterUsername,
|
||||
masterPassword, databaseName, iamEnabled, paramGroupName,
|
||||
dbSubnetGroupName, availabilityZone, multiAz, region,
|
||||
serverlessV2Min, serverlessV2Max, serverlessV2SecondsUntilAutoPause,
|
||||
manageMasterUserPassword, masterUserSecretKmsKeyId, engineMode, storageEncrypted);
|
||||
manageMasterUserPassword, masterUserSecretKmsKeyId, engineMode, storageEncrypted,
|
||||
requestedPort);
|
||||
}
|
||||
String result = dbClusterXml(cluster);
|
||||
return Response.ok(AwsQueryResponse.envelope("CreateDBCluster", AwsNamespaces.RDS, result)).build();
|
||||
} catch (AwsException e) {
|
||||
@@ -1574,17 +1600,24 @@ public class RdsQueryHandler {
|
||||
boolean multiAz = multiAzStr != null && Boolean.parseBoolean(multiAzStr);
|
||||
String dbSubnetGroupName = params.getFirst("DBSubnetGroupName");
|
||||
|
||||
java.util.List<String> vpcSecurityGroupIds = new java.util.ArrayList<>();
|
||||
List<String> vpcSecurityGroupIds = new ArrayList<>();
|
||||
for (int i = 1; ; i++) {
|
||||
String sg = params.getFirst("VpcSecurityGroupIds.VpcSecurityGroupId." + i);
|
||||
if (sg == null) break;
|
||||
vpcSecurityGroupIds.add(sg);
|
||||
}
|
||||
|
||||
java.util.Map<String, String> tags = parseTags(params);
|
||||
Map<String, String> tags = parseTags(params);
|
||||
|
||||
try {
|
||||
DbInstance instance = service.restoreDbInstanceFromDbSnapshot(instanceId, snapshotId, dbInstanceClass, availabilityZone, multiAz, dbSubnetGroupName, vpcSecurityGroupIds, tags, region);
|
||||
Integer requestedPort = parseIntegerParam(params, "Port");
|
||||
DbInstance instance = requestedPort == null
|
||||
? service.restoreDbInstanceFromDbSnapshot(instanceId, snapshotId,
|
||||
dbInstanceClass, availabilityZone, multiAz, dbSubnetGroupName,
|
||||
vpcSecurityGroupIds, tags, region)
|
||||
: service.restoreDbInstanceFromDbSnapshot(instanceId, snapshotId,
|
||||
dbInstanceClass, availabilityZone, multiAz, dbSubnetGroupName,
|
||||
vpcSecurityGroupIds, tags, region, requestedPort);
|
||||
String result = dbInstanceXml(instance);
|
||||
return Response.ok(AwsQueryResponse.envelope("RestoreDBInstanceFromDBSnapshot", AwsNamespaces.RDS, result)).build();
|
||||
} catch (AwsException e) {
|
||||
|
||||
@@ -592,6 +592,29 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
boolean autoMinorVersionUpgrade,
|
||||
DbInstanceSettings settings,
|
||||
Boolean publiclyAccessible) {
|
||||
return createDbInstance(id, engineParam, engineVersion, masterUsername, masterPassword,
|
||||
dbName, dbInstanceClass, allocatedStorage, iamEnabled, paramGroupName,
|
||||
dbSubnetGroupName, dbClusterIdentifier, availabilityZone, multiAz,
|
||||
manageMasterUserPassword, masterUserSecretKmsKeyId, tags, vpcSecurityGroupIds,
|
||||
optionGroupName, region, autoMinorVersionUpgrade, settings, publiclyAccessible, null);
|
||||
}
|
||||
|
||||
public DbInstance createDbInstance(String id, String engineParam, String engineVersion,
|
||||
String masterUsername, String masterPassword,
|
||||
String dbName, String dbInstanceClass,
|
||||
int allocatedStorage, boolean iamEnabled,
|
||||
String paramGroupName, String dbSubnetGroupName,
|
||||
String dbClusterIdentifier, String availabilityZone,
|
||||
boolean multiAz, boolean manageMasterUserPassword,
|
||||
String masterUserSecretKmsKeyId,
|
||||
Map<String, String> tags,
|
||||
List<String> vpcSecurityGroupIds,
|
||||
String optionGroupName,
|
||||
String region,
|
||||
boolean autoMinorVersionUpgrade,
|
||||
DbInstanceSettings settings,
|
||||
Boolean publiclyAccessible,
|
||||
Integer requestedPort) {
|
||||
validateInstanceSettings(settings);
|
||||
String provisioningKey = "instance:" + currentAccountId() + ":"
|
||||
+ dbResourceKey(effectiveRegion(region), id);
|
||||
@@ -605,7 +628,7 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
paramGroupName, dbSubnetGroupName, dbClusterIdentifier, availabilityZone,
|
||||
multiAz, manageMasterUserPassword, masterUserSecretKmsKeyId, tags,
|
||||
vpcSecurityGroupIds, optionGroupName, region, autoMinorVersionUpgrade,
|
||||
settings, publiclyAccessible);
|
||||
settings, publiclyAccessible, requestedPort);
|
||||
} finally {
|
||||
provisioningIds.remove(provisioningKey);
|
||||
}
|
||||
@@ -625,7 +648,7 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
String region,
|
||||
boolean autoMinorVersionUpgrade,
|
||||
DbInstanceSettings settings,
|
||||
Boolean publiclyAccessible) {
|
||||
Boolean publiclyAccessible, Integer requestedPort) {
|
||||
String effectiveRegion = effectiveRegion(region);
|
||||
String dbiResourceId = "db-" + java.util.UUID.randomUUID().toString()
|
||||
.replace("-", "").substring(0, 24).toUpperCase();
|
||||
@@ -653,9 +676,6 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
DbInstanceSettings.validateMonitoringPairOnCreate(
|
||||
settings.monitoringInterval(), settings.monitoringRoleArn());
|
||||
boolean mock = config.services().rds().mock();
|
||||
// Always reserve a unique port (even in mock) so endpoints stay distinct and usedPorts
|
||||
// is consistent; mock mode only skips starting the container and auth proxy.
|
||||
int proxyPort = allocateProxyPort();
|
||||
if (masterUsername == null || masterUsername.isBlank()) {
|
||||
masterUsername = "root";
|
||||
} else if (masterUsername.length() > engine.maxMasterUsernameLength()
|
||||
@@ -666,6 +686,9 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
if (manageMasterUserPassword && (masterPassword == null || masterPassword.isBlank())) {
|
||||
masterPassword = generatedMasterPassword();
|
||||
}
|
||||
// Always reserve a unique port (even in mock) so endpoints stay distinct and usedPorts
|
||||
// is consistent; mock mode only skips starting the container and auth proxy.
|
||||
int proxyPort = reserveProxyPort(requestedPort);
|
||||
|
||||
String backendHost = null;
|
||||
int backendPort = 0;
|
||||
@@ -1067,15 +1090,24 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
|
||||
private record SnapshotReference(String accountId, String region, DbSnapshot snapshot) {}
|
||||
|
||||
public DbInstance restoreDbInstanceFromDbSnapshot(String instanceId, String snapshotId, String dbInstanceClass, String availabilityZone, boolean multiAz, String dbSubnetGroupName, java.util.List<String> vpcSecurityGroupIds, java.util.Map<String, String> tags) {
|
||||
public DbInstance restoreDbInstanceFromDbSnapshot(String instanceId, String snapshotId, String dbInstanceClass, String availabilityZone, boolean multiAz, String dbSubnetGroupName, List<String> vpcSecurityGroupIds, Map<String, String> tags) {
|
||||
return restoreDbInstanceFromDbSnapshot(instanceId, snapshotId, dbInstanceClass, availabilityZone,
|
||||
multiAz, dbSubnetGroupName, vpcSecurityGroupIds, tags, regionResolver.getDefaultRegion());
|
||||
}
|
||||
|
||||
public DbInstance restoreDbInstanceFromDbSnapshot(String instanceId, String snapshotId, String dbInstanceClass,
|
||||
String availabilityZone, boolean multiAz, String dbSubnetGroupName,
|
||||
java.util.List<String> vpcSecurityGroupIds,
|
||||
java.util.Map<String, String> tags, String region) {
|
||||
List<String> vpcSecurityGroupIds,
|
||||
Map<String, String> tags, String region) {
|
||||
return restoreDbInstanceFromDbSnapshot(instanceId, snapshotId, dbInstanceClass,
|
||||
availabilityZone, multiAz, dbSubnetGroupName, vpcSecurityGroupIds, tags, region, null);
|
||||
}
|
||||
|
||||
public DbInstance restoreDbInstanceFromDbSnapshot(String instanceId, String snapshotId, String dbInstanceClass,
|
||||
String availabilityZone, boolean multiAz, String dbSubnetGroupName,
|
||||
List<String> vpcSecurityGroupIds,
|
||||
Map<String, String> tags, String region,
|
||||
Integer requestedPort) {
|
||||
String effectiveRegion = effectiveRegion(region);
|
||||
DbSnapshot snapshot = Optional.ofNullable(findSnapshotForScope(currentAccountId(), effectiveRegion, snapshotId))
|
||||
.orElseThrow(() -> new AwsException("DBSnapshotNotFound", "DBSnapshot " + snapshotId + " not found.", 404));
|
||||
@@ -1093,8 +1125,8 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
DbInstance instance = createDbInstance(instanceId, snapshot.getEngine().name().toLowerCase(), snapshot.getEngineVersion(),
|
||||
snapshot.getMasterUsername(), snapshot.getMasterPassword(),
|
||||
snapshot.getDbName(), targetClass, snapshot.getAllocatedStorage(), snapshot.isIamDatabaseAuthenticationEnabled(),
|
||||
null, dbSubnetGroupName, null, availabilityZone, multiAz, false, null, tags,
|
||||
vpcSecurityGroupIds, null, effectiveRegion, true, restoreSettings);
|
||||
null, dbSubnetGroupName, null, availabilityZone, multiAz, false, null, tags, vpcSecurityGroupIds,
|
||||
null, effectiveRegion, true, restoreSettings, null, requestedPort);
|
||||
|
||||
if (!config.services().rds().mock()) {
|
||||
try {
|
||||
@@ -1695,7 +1727,7 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
.orElseThrow(() -> new AwsException("DBClusterSnapshotNotFoundFault",
|
||||
"DB cluster snapshot data for " + resolvedSnapshotId + " not found.", 404));
|
||||
|
||||
DbCluster cluster = createDbCluster(clusterId, engine,
|
||||
DbCluster cluster = createDbClusterWithPort(clusterId, engine,
|
||||
engineVersion != null && !engineVersion.isBlank() ? engineVersion : snapshot.getEngineVersion(),
|
||||
snapshot.getMasterUsername(), snapshot.getMasterPassword(),
|
||||
databaseName != null && !databaseName.isBlank() ? databaseName : snapshot.getDatabaseName(),
|
||||
@@ -1703,7 +1735,7 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
parameterGroupName, dbSubnetGroupName, availabilityZone, false, effectiveRegion,
|
||||
null, null, null, false, null,
|
||||
engineMode != null && !engineMode.isBlank() ? engineMode : snapshot.getEngineMode(),
|
||||
snapshot.isStorageEncrypted());
|
||||
snapshot.isStorageEncrypted(), port);
|
||||
if (tags != null && !tags.isEmpty()) {
|
||||
cluster.getTags().putAll(tags);
|
||||
putClusterForScope(accountId, effectiveRegion, clusterId, cluster);
|
||||
@@ -3394,6 +3426,38 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
Integer serverlessV2SecondsUntilAutoPause,
|
||||
boolean manageMasterUserPassword, String masterUserSecretKmsKeyId,
|
||||
String engineMode, boolean storageEncrypted) {
|
||||
return createDbCluster(id, engineParam, engineVersion, masterUsername, masterPassword,
|
||||
databaseName, iamEnabled, paramGroupName, dbSubnetGroupName, availabilityZone,
|
||||
multiAz, region, serverlessV2MinCapacity, serverlessV2MaxCapacity,
|
||||
serverlessV2SecondsUntilAutoPause, manageMasterUserPassword, masterUserSecretKmsKeyId,
|
||||
engineMode, storageEncrypted, null);
|
||||
}
|
||||
|
||||
public DbCluster createDbCluster(String id, String engineParam, String engineVersion,
|
||||
String masterUsername, String masterPassword,
|
||||
String databaseName, boolean iamEnabled,
|
||||
String paramGroupName, String dbSubnetGroupName,
|
||||
String availabilityZone, boolean multiAz, String region,
|
||||
Double serverlessV2MinCapacity, Double serverlessV2MaxCapacity,
|
||||
Integer serverlessV2SecondsUntilAutoPause,
|
||||
boolean manageMasterUserPassword, String masterUserSecretKmsKeyId,
|
||||
String engineMode, boolean storageEncrypted, Integer requestedPort) {
|
||||
return createDbClusterWithPort(id, engineParam, engineVersion, masterUsername, masterPassword,
|
||||
databaseName, iamEnabled, paramGroupName, dbSubnetGroupName, availabilityZone,
|
||||
multiAz, region, serverlessV2MinCapacity, serverlessV2MaxCapacity,
|
||||
serverlessV2SecondsUntilAutoPause, manageMasterUserPassword, masterUserSecretKmsKeyId,
|
||||
engineMode, storageEncrypted, requestedPort);
|
||||
}
|
||||
|
||||
private DbCluster createDbClusterWithPort(String id, String engineParam, String engineVersion,
|
||||
String masterUsername, String masterPassword,
|
||||
String databaseName, boolean iamEnabled,
|
||||
String paramGroupName, String dbSubnetGroupName,
|
||||
String availabilityZone, boolean multiAz, String region,
|
||||
Double serverlessV2MinCapacity, Double serverlessV2MaxCapacity,
|
||||
Integer serverlessV2SecondsUntilAutoPause,
|
||||
boolean manageMasterUserPassword, String masterUserSecretKmsKeyId,
|
||||
String engineMode, boolean storageEncrypted, Integer requestedPort) {
|
||||
String provisioningKey = "cluster:" + currentAccountId() + ":"
|
||||
+ dbResourceKey(effectiveRegion(region), id);
|
||||
if (!provisioningIds.add(provisioningKey)) {
|
||||
@@ -3405,7 +3469,7 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
databaseName, iamEnabled, paramGroupName, dbSubnetGroupName, availabilityZone,
|
||||
multiAz, region, serverlessV2MinCapacity, serverlessV2MaxCapacity,
|
||||
serverlessV2SecondsUntilAutoPause, manageMasterUserPassword, masterUserSecretKmsKeyId,
|
||||
engineMode, storageEncrypted);
|
||||
engineMode, storageEncrypted, requestedPort);
|
||||
} finally {
|
||||
provisioningIds.remove(provisioningKey);
|
||||
}
|
||||
@@ -3419,7 +3483,7 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
Double serverlessV2MinCapacity, Double serverlessV2MaxCapacity,
|
||||
Integer serverlessV2SecondsUntilAutoPause,
|
||||
boolean manageMasterUserPassword, String masterUserSecretKmsKeyId,
|
||||
String engineMode, boolean storageEncrypted) {
|
||||
String engineMode, boolean storageEncrypted, Integer requestedPort) {
|
||||
String effectiveRegion = effectiveRegion(region);
|
||||
String clusterResourceId = "cluster-" + java.util.UUID.randomUUID().toString()
|
||||
.replace("-", "").substring(0, 24).toUpperCase();
|
||||
@@ -3442,7 +3506,7 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
boolean mock = config.services().rds().mock();
|
||||
// Always reserve a unique port (even in mock) so endpoints stay distinct and usedPorts
|
||||
// is consistent; mock mode only skips starting the container and auth proxy.
|
||||
int proxyPort = allocateProxyPort();
|
||||
int proxyPort = reserveProxyPort(requestedPort);
|
||||
if (manageMasterUserPassword && (masterPassword == null || masterPassword.isBlank())) {
|
||||
masterPassword = generatedMasterPassword();
|
||||
}
|
||||
@@ -4083,6 +4147,23 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
Integer serverlessV2SecondsUntilAutoPause,
|
||||
boolean manageMasterUserPassword, String masterUserSecretKmsKeyId,
|
||||
String engineMode, boolean storageEncrypted) {
|
||||
return createDbClusterInGlobalCluster(globalClusterIdentifier, id, engineParam, engineVersion,
|
||||
masterUsername, masterPassword, databaseName, iamEnabled, paramGroupName,
|
||||
dbSubnetGroupName, availabilityZone, multiAz, region, serverlessV2MinCapacity,
|
||||
serverlessV2MaxCapacity, serverlessV2SecondsUntilAutoPause, manageMasterUserPassword,
|
||||
masterUserSecretKmsKeyId, engineMode, storageEncrypted, null);
|
||||
}
|
||||
|
||||
public DbCluster createDbClusterInGlobalCluster(String globalClusterIdentifier, String id,
|
||||
String engineParam, String engineVersion,
|
||||
String masterUsername, String masterPassword,
|
||||
String databaseName, boolean iamEnabled,
|
||||
String paramGroupName, String dbSubnetGroupName,
|
||||
String availabilityZone, boolean multiAz, String region,
|
||||
Double serverlessV2MinCapacity, Double serverlessV2MaxCapacity,
|
||||
Integer serverlessV2SecondsUntilAutoPause,
|
||||
boolean manageMasterUserPassword, String masterUserSecretKmsKeyId,
|
||||
String engineMode, boolean storageEncrypted, Integer requestedPort) {
|
||||
String effectiveRegion = effectiveRegion(region);
|
||||
String accountId = currentAccountId();
|
||||
GlobalCluster global = requireGlobalCluster(accountId, globalClusterIdentifier);
|
||||
@@ -4103,7 +4184,7 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
paramGroupName, dbSubnetGroupName, availabilityZone, multiAz, effectiveRegion,
|
||||
serverlessV2MinCapacity, serverlessV2MaxCapacity, serverlessV2SecondsUntilAutoPause,
|
||||
manageMasterUserPassword, masterUserSecretKmsKeyId, engineMode,
|
||||
storageEncrypted || global.isStorageEncrypted());
|
||||
storageEncrypted || global.isStorageEncrypted(), requestedPort);
|
||||
} else {
|
||||
if (hasText(masterUsername) || hasText(masterPassword) || manageMasterUserPassword) {
|
||||
throw new AwsException("InvalidParameterCombination",
|
||||
@@ -4138,7 +4219,7 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
primary.getDatabaseName(), iamEnabled, paramGroupName, dbSubnetGroupName,
|
||||
availabilityZone, multiAz, effectiveRegion, serverlessV2MinCapacity,
|
||||
serverlessV2MaxCapacity, serverlessV2SecondsUntilAutoPause, false, null,
|
||||
engineMode, primary.isStorageEncrypted());
|
||||
engineMode, primary.isStorageEncrypted(), requestedPort);
|
||||
}
|
||||
try {
|
||||
attachToGlobalCluster(accountId, global.getGlobalClusterIdentifier(), cluster, primary == null);
|
||||
@@ -6297,6 +6378,25 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
return imageName + ":" + requestedTag;
|
||||
}
|
||||
|
||||
private int reserveProxyPort(Integer requestedPort) {
|
||||
if (requestedPort == null) {
|
||||
return allocateProxyPort();
|
||||
}
|
||||
if (requestedPort < 1150 || requestedPort > 65535) {
|
||||
throw new AwsException("InvalidParameterValue", "Port must be between 1150 and 65535.", 400);
|
||||
}
|
||||
int base = config.services().rds().proxyBasePort();
|
||||
int max = config.services().rds().proxyMaxPort();
|
||||
if (requestedPort >= base && requestedPort <= max) {
|
||||
if (!usedPorts.add(requestedPort)) {
|
||||
throw new AwsException("InvalidParameterValue",
|
||||
"Port " + requestedPort + " is already in use by another RDS resource.", 400);
|
||||
}
|
||||
return requestedPort;
|
||||
}
|
||||
return allocateProxyPort();
|
||||
}
|
||||
|
||||
private int allocateProxyPort() {
|
||||
int base = config.services().rds().proxyBasePort();
|
||||
int max = config.services().rds().proxyMaxPort();
|
||||
|
||||
+145
@@ -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");
|
||||
|
||||
Reference in new issue
Block a user