mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix(rds): populate MasterUserSecret for Aurora clusters with ManageMasterUserPassword (#3148)
* fix(rds): populate MasterUserSecret for Aurora clusters with ManageMasterUserPassword CreateDBCluster ignored ManageMasterUserPassword, so aws_rds_cluster with manage_master_user_password=true always returned an empty master_user_secret, breaking any Terraform reference to master_user_secret[0]. The instance path already generates the managed secret; the cluster path never parsed the option. Mirror the instance behavior on the cluster path: - CreateDBCluster / ModifyDBCluster parse ManageMasterUserPassword and MasterUserSecretKmsKeyId and generate a Secrets Manager secret (rds!<clusterResourceId>, OwningService=rds, tagged aws:rds:primaryDBClusterArn) - DescribeDBClusters and the other cluster responses render <MasterUserSecret> - ModifyDBCluster enables the secret on demand and removes it when the option is turned off with a caller-supplied password - DeleteDBCluster deletes the managed secret - Startup ownership backfill also covers persisted clusters Closes #3061 * fix(rds): roll back cluster managed secret on failed create and honor KMS re-key Addresses review feedback on the cluster managed-master-user-password path: - doCreateDbCluster now deletes the just-created managed secret if proxy startup or persistence fails afterwards. The cluster is never stored in that case, so DeleteDBCluster could not otherwise reach the orphaned secret. - ModifyDBCluster with MasterUserSecretKmsKeyId on a cluster that already has a managed secret re-keys the existing secret via updateSecret instead of silently dropping the request.
This commit is contained in:
1 parent
504203f8e8
commit
6345c37bad
6 files changed
+438
-22
No files matched your search
@@ -453,6 +453,8 @@ public class RdsQueryHandler {
|
||||
String dbSubnetGroupName = params.getFirst("DBSubnetGroupName");
|
||||
String availabilityZone = params.getFirst("AvailabilityZone");
|
||||
boolean multiAz = "true".equalsIgnoreCase(params.getFirst("MultiAZ"));
|
||||
boolean manageMasterUserPassword = "true".equalsIgnoreCase(params.getFirst("ManageMasterUserPassword"));
|
||||
String masterUserSecretKmsKeyId = params.getFirst("MasterUserSecretKmsKeyId");
|
||||
|
||||
if (engineVersion == null) {
|
||||
engineVersion = defaultEngineVersion(engine);
|
||||
@@ -466,7 +468,8 @@ public class RdsQueryHandler {
|
||||
DbCluster cluster = service.createDbCluster(id, engine, engineVersion, masterUsername,
|
||||
masterPassword, databaseName, iamEnabled, paramGroupName,
|
||||
dbSubnetGroupName, availabilityZone, multiAz, region,
|
||||
serverlessV2Min, serverlessV2Max, serverlessV2SecondsUntilAutoPause);
|
||||
serverlessV2Min, serverlessV2Max, serverlessV2SecondsUntilAutoPause,
|
||||
manageMasterUserPassword, masterUserSecretKmsKeyId);
|
||||
String result = dbClusterXml(cluster);
|
||||
return Response.ok(AwsQueryResponse.envelope("CreateDBCluster", AwsNamespaces.RDS, result)).build();
|
||||
} catch (AwsException e) {
|
||||
@@ -568,13 +571,17 @@ public class RdsQueryHandler {
|
||||
String newPassword = params.getFirst("MasterUserPassword");
|
||||
String iamStr = params.getFirst("EnableIAMDatabaseAuthentication");
|
||||
Boolean iamEnabled = iamStr != null ? Boolean.parseBoolean(iamStr) : null;
|
||||
String manageStr = params.getFirst("ManageMasterUserPassword");
|
||||
Boolean manageMasterUserPassword = manageStr != null ? Boolean.parseBoolean(manageStr) : null;
|
||||
String masterUserSecretKmsKeyId = params.getFirst("MasterUserSecretKmsKeyId");
|
||||
try {
|
||||
Double serverlessV2Min = parseDoubleParam(params, "ServerlessV2ScalingConfiguration.MinCapacity");
|
||||
Double serverlessV2Max = parseDoubleParam(params, "ServerlessV2ScalingConfiguration.MaxCapacity");
|
||||
Integer serverlessV2SecondsUntilAutoPause = parseIntegerParam(
|
||||
params, "ServerlessV2ScalingConfiguration.SecondsUntilAutoPause");
|
||||
DbCluster cluster = service.modifyDbCluster(id, newPassword, iamEnabled,
|
||||
serverlessV2Min, serverlessV2Max, serverlessV2SecondsUntilAutoPause, region);
|
||||
serverlessV2Min, serverlessV2Max, serverlessV2SecondsUntilAutoPause,
|
||||
manageMasterUserPassword, masterUserSecretKmsKeyId, region);
|
||||
String result = dbClusterXml(cluster);
|
||||
return Response.ok(AwsQueryResponse.envelope("ModifyDBCluster", AwsNamespaces.RDS, result)).build();
|
||||
} catch (AwsException e) {
|
||||
@@ -1466,6 +1473,15 @@ public class RdsQueryHandler {
|
||||
.elem("DBSubnetGroup", c.getDbSubnetGroupName() != null ? c.getDbSubnetGroupName() : "default")
|
||||
.elem("DbClusterResourceId", c.getDbClusterResourceId())
|
||||
.elem("DBClusterArn", c.getDbClusterArn());
|
||||
if (c.getMasterUserSecretArn() != null && !c.getMasterUserSecretArn().isBlank()) {
|
||||
xml.start("MasterUserSecret")
|
||||
.elem("SecretArn", c.getMasterUserSecretArn())
|
||||
.elem("SecretStatus", c.getMasterUserSecretStatus() == null ? "active" : c.getMasterUserSecretStatus());
|
||||
if (c.getMasterUserSecretKmsKeyId() != null && !c.getMasterUserSecretKmsKeyId().isBlank()) {
|
||||
xml.elem("KmsKeyId", c.getMasterUserSecretKmsKeyId());
|
||||
}
|
||||
xml.end("MasterUserSecret");
|
||||
}
|
||||
if (c.getServerlessV2MinCapacity() != null || c.getServerlessV2MaxCapacity() != null) {
|
||||
xml.start("ServerlessV2ScalingConfiguration");
|
||||
if (c.getServerlessV2MinCapacity() != null) {
|
||||
|
||||
@@ -904,6 +904,17 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
LOG.debugv(e, "Could not mark master user secret {0} as service-managed", secretArn);
|
||||
}
|
||||
}
|
||||
for (DbCluster cluster : allClusters()) {
|
||||
String secretArn = cluster.getMasterUserSecretArn();
|
||||
if (secretArn == null) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
secretsManagerService.markOwnedByService(secretArn, MANAGED_SECRET_OWNING_SERVICE);
|
||||
} catch (RuntimeException e) {
|
||||
LOG.debugv(e, "Could not mark master user secret {0} as service-managed", secretArn);
|
||||
}
|
||||
}
|
||||
} catch (RuntimeException e) {
|
||||
LOG.warnv(e, "Skipped the master user secret ownership backfill");
|
||||
}
|
||||
@@ -992,6 +1003,81 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
}
|
||||
}
|
||||
|
||||
private void attachManagedMasterUserSecret(DbCluster cluster, String region, String kmsKeyId) {
|
||||
if (secretsManagerService == null) {
|
||||
throw new AwsException("InvalidParameterCombination",
|
||||
"ManageMasterUserPassword requires Secrets Manager support.", 400);
|
||||
}
|
||||
String secretName = "rds!" + cluster.getDbClusterResourceId();
|
||||
// RDS owns the secret it manages: it rotates the master password itself, so the secret
|
||||
// carries no rotation Lambda. AWS marks that with OwningService and these two tags.
|
||||
List<Secret.Tag> tags = List.of(
|
||||
new Secret.Tag("aws:rds:primaryDBClusterArn", cluster.getDbClusterArn()),
|
||||
new Secret.Tag("aws:secretsmanager:owningService", MANAGED_SECRET_OWNING_SERVICE));
|
||||
Secret secret = secretsManagerService.createSecret(
|
||||
secretName,
|
||||
managedMasterSecretString(cluster),
|
||||
null,
|
||||
"Managed RDS master user secret for " + cluster.getDbClusterIdentifier(),
|
||||
kmsKeyId,
|
||||
tags,
|
||||
MANAGED_SECRET_OWNING_SERVICE,
|
||||
region);
|
||||
cluster.setMasterUserSecretArn(secret.getArn());
|
||||
cluster.setMasterUserSecretStatus("active");
|
||||
cluster.setMasterUserSecretKmsKeyId(kmsKeyId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the Secrets Manager secret RDS manages for a cluster's master user and clears the
|
||||
* reference. A missing or already-deleted secret is tolerated: the cluster is being torn down or
|
||||
* switched back to a caller-supplied password either way.
|
||||
*/
|
||||
private void detachManagedMasterUserSecret(DbCluster cluster, String region) {
|
||||
String secretArn = cluster.getMasterUserSecretArn();
|
||||
if (secretArn == null || secretsManagerService == null) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
secretsManagerService.deleteSecret(secretArn, null, true, region);
|
||||
} catch (RuntimeException e) {
|
||||
LOG.debugv(e, "Managed master user secret {0} could not be deleted", secretArn);
|
||||
}
|
||||
cluster.setMasterUserSecretArn(null);
|
||||
cluster.setMasterUserSecretStatus(null);
|
||||
cluster.setMasterUserSecretKmsKeyId(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies a new KMS key to a cluster's existing managed master user secret. AWS re-encrypts the
|
||||
* secret in place on {@code ModifyDBCluster}; silently keeping the old key would make the call
|
||||
* report a success it did not perform.
|
||||
*/
|
||||
private void rekeyManagedMasterUserSecret(DbCluster cluster, String kmsKeyId, String region) {
|
||||
if (secretsManagerService == null) {
|
||||
return;
|
||||
}
|
||||
secretsManagerService.updateSecret(cluster.getMasterUserSecretArn(), null, kmsKeyId, region);
|
||||
cluster.setMasterUserSecretKmsKeyId(kmsKeyId);
|
||||
}
|
||||
|
||||
private static String managedMasterSecretString(DbCluster cluster) {
|
||||
try {
|
||||
return JSON.writeValueAsString(Map.of(
|
||||
"username", cluster.getMasterUsername(),
|
||||
"password", cluster.getMasterPassword(),
|
||||
"engine", cluster.getEngineIdentifier() != null && !cluster.getEngineIdentifier().isBlank()
|
||||
? cluster.getEngineIdentifier().toLowerCase()
|
||||
: cluster.getEngine().name().toLowerCase(),
|
||||
"host", cluster.getEndpoint().address(),
|
||||
"port", cluster.getEndpoint().port(),
|
||||
"dbname", cluster.getDatabaseName() == null ? "" : cluster.getDatabaseName(),
|
||||
"dbClusterIdentifier", cluster.getDbClusterIdentifier()));
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new IllegalStateException("Unable to serialize RDS master user secret", e);
|
||||
}
|
||||
}
|
||||
|
||||
private static String generatedMasterPassword() {
|
||||
return "floci-" + java.util.UUID.randomUUID().toString().replace("-", "");
|
||||
}
|
||||
@@ -1424,6 +1510,20 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
String availabilityZone, boolean multiAz, String region,
|
||||
Double serverlessV2MinCapacity, Double serverlessV2MaxCapacity,
|
||||
Integer serverlessV2SecondsUntilAutoPause) {
|
||||
return createDbCluster(id, engineParam, engineVersion, masterUsername, masterPassword,
|
||||
databaseName, iamEnabled, paramGroupName, dbSubnetGroupName, availabilityZone,
|
||||
multiAz, region, serverlessV2MinCapacity, serverlessV2MaxCapacity,
|
||||
serverlessV2SecondsUntilAutoPause, false, 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 provisioningKey = "cluster:" + currentAccountId() + ":"
|
||||
+ dbResourceKey(effectiveRegion(region), id);
|
||||
if (!provisioningIds.add(provisioningKey)) {
|
||||
@@ -1434,7 +1534,7 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
return doCreateDbCluster(id, engineParam, engineVersion, masterUsername, masterPassword,
|
||||
databaseName, iamEnabled, paramGroupName, dbSubnetGroupName, availabilityZone,
|
||||
multiAz, region, serverlessV2MinCapacity, serverlessV2MaxCapacity,
|
||||
serverlessV2SecondsUntilAutoPause);
|
||||
serverlessV2SecondsUntilAutoPause, manageMasterUserPassword, masterUserSecretKmsKeyId);
|
||||
} finally {
|
||||
provisioningIds.remove(provisioningKey);
|
||||
}
|
||||
@@ -1446,7 +1546,8 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
String paramGroupName, String dbSubnetGroupName,
|
||||
String availabilityZone, boolean multiAz, String region,
|
||||
Double serverlessV2MinCapacity, Double serverlessV2MaxCapacity,
|
||||
Integer serverlessV2SecondsUntilAutoPause) {
|
||||
Integer serverlessV2SecondsUntilAutoPause,
|
||||
boolean manageMasterUserPassword, String masterUserSecretKmsKeyId) {
|
||||
String effectiveRegion = effectiveRegion(region);
|
||||
String clusterResourceId = "cluster-" + java.util.UUID.randomUUID().toString()
|
||||
.replace("-", "").substring(0, 24).toUpperCase();
|
||||
@@ -1470,6 +1571,9 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
// 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 (manageMasterUserPassword && (masterPassword == null || masterPassword.isBlank())) {
|
||||
masterPassword = generatedMasterPassword();
|
||||
}
|
||||
DbEndpoint endpoint = mock ? new DbEndpoint("localhost", proxyPort) : proxyEndpoint(proxyPort);
|
||||
DbCluster cluster = new DbCluster(id, engine, engineVersion, masterUsername, masterPassword,
|
||||
databaseName, DbInstanceStatus.AVAILABLE, endpoint, endpoint,
|
||||
@@ -1500,23 +1604,36 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
cluster.setDbClusterResourceId(clusterResourceId);
|
||||
cluster.setDbClusterArn(clusterArn);
|
||||
|
||||
if (!mock) {
|
||||
String effectiveMasterUser = masterUsername != null ? masterUsername : "root";
|
||||
final String accountId = accountIdFromArn(cluster.getDbClusterArn());
|
||||
final String clusterRegion = regionFromArn(cluster.getDbClusterArn());
|
||||
proxyManager.startProxy(rdsResourceRelayKey(cluster.getDbClusterArn(), id),
|
||||
engine, iamEnabled, proxyPort,
|
||||
cluster.getContainerHost(), cluster.getContainerPort(),
|
||||
cluster.getEndpoint().address(),
|
||||
effectiveMasterUser, masterPassword, databaseName,
|
||||
(user, pw) -> validateDbClusterPasswordForScope(
|
||||
accountId, clusterRegion, id, user, pw));
|
||||
if (manageMasterUserPassword) {
|
||||
attachManagedMasterUserSecret(cluster, effectiveRegion, masterUserSecretKmsKeyId);
|
||||
}
|
||||
|
||||
cluster.setServerlessV2MinCapacity(serverlessV2MinCapacity);
|
||||
cluster.setServerlessV2MaxCapacity(serverlessV2MaxCapacity);
|
||||
cluster.setServerlessV2SecondsUntilAutoPause(effectiveAutoPauseSeconds);
|
||||
putClusterForScope(currentAccountId(), effectiveRegion, id, cluster);
|
||||
try {
|
||||
if (!mock) {
|
||||
String effectiveMasterUser = masterUsername != null ? masterUsername : "root";
|
||||
final String accountId = accountIdFromArn(cluster.getDbClusterArn());
|
||||
final String clusterRegion = regionFromArn(cluster.getDbClusterArn());
|
||||
proxyManager.startProxy(rdsResourceRelayKey(cluster.getDbClusterArn(), id),
|
||||
engine, iamEnabled, proxyPort,
|
||||
cluster.getContainerHost(), cluster.getContainerPort(),
|
||||
cluster.getEndpoint().address(),
|
||||
effectiveMasterUser, masterPassword, databaseName,
|
||||
(user, pw) -> validateDbClusterPasswordForScope(
|
||||
accountId, clusterRegion, id, user, pw));
|
||||
}
|
||||
|
||||
cluster.setServerlessV2MinCapacity(serverlessV2MinCapacity);
|
||||
cluster.setServerlessV2MaxCapacity(serverlessV2MaxCapacity);
|
||||
cluster.setServerlessV2SecondsUntilAutoPause(effectiveAutoPauseSeconds);
|
||||
putClusterForScope(currentAccountId(), effectiveRegion, id, cluster);
|
||||
} catch (RuntimeException e) {
|
||||
// The cluster is not persisted, so DeleteDBCluster cannot reach the secret we just
|
||||
// created: roll it back here rather than leave an orphaned RDS-owned secret.
|
||||
if (manageMasterUserPassword) {
|
||||
detachManagedMasterUserSecret(cluster, effectiveRegion);
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
LOG.infov("DB cluster {0} created (mock={1}), engine={2}, endpoint={3}:{4}",
|
||||
id, String.valueOf(mock), engine, endpoint.address(), String.valueOf(endpoint.port()));
|
||||
return cluster;
|
||||
@@ -1673,9 +1790,18 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
return modifyDbCluster(id, newPassword, iamEnabled, null, null, null, region);
|
||||
}
|
||||
|
||||
public synchronized DbCluster modifyDbCluster(String id, String newPassword, Boolean iamEnabled,
|
||||
public DbCluster modifyDbCluster(String id, String newPassword, Boolean iamEnabled,
|
||||
Double serverlessV2MinCapacity, Double serverlessV2MaxCapacity,
|
||||
Integer serverlessV2SecondsUntilAutoPause, String region) {
|
||||
return modifyDbCluster(id, newPassword, iamEnabled, serverlessV2MinCapacity,
|
||||
serverlessV2MaxCapacity, serverlessV2SecondsUntilAutoPause, null, null, region);
|
||||
}
|
||||
|
||||
public synchronized DbCluster modifyDbCluster(String id, String newPassword, Boolean iamEnabled,
|
||||
Double serverlessV2MinCapacity, Double serverlessV2MaxCapacity,
|
||||
Integer serverlessV2SecondsUntilAutoPause,
|
||||
Boolean manageMasterUserPassword, String masterUserSecretKmsKeyId,
|
||||
String region) {
|
||||
String effectiveRegion = effectiveRegion(region);
|
||||
DbCluster cluster = getDbCluster(id, effectiveRegion);
|
||||
boolean modifiesServerlessV2Scaling = serverlessV2MinCapacity != null
|
||||
@@ -1721,6 +1847,18 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
if (iamEnabled != null) {
|
||||
cluster.setIamDatabaseAuthenticationEnabled(iamEnabled);
|
||||
}
|
||||
if (Boolean.TRUE.equals(manageMasterUserPassword) && cluster.getMasterUserSecretArn() == null) {
|
||||
if (cluster.getMasterPassword() == null || cluster.getMasterPassword().isBlank()) {
|
||||
cluster.setMasterPassword(generatedMasterPassword());
|
||||
}
|
||||
attachManagedMasterUserSecret(cluster, effectiveRegion, masterUserSecretKmsKeyId);
|
||||
} else if (Boolean.FALSE.equals(manageMasterUserPassword) && cluster.getMasterUserSecretArn() != null) {
|
||||
detachManagedMasterUserSecret(cluster, effectiveRegion);
|
||||
} else if (cluster.getMasterUserSecretArn() != null
|
||||
&& masterUserSecretKmsKeyId != null
|
||||
&& !masterUserSecretKmsKeyId.equals(cluster.getMasterUserSecretKmsKeyId())) {
|
||||
rekeyManagedMasterUserSecret(cluster, masterUserSecretKmsKeyId, effectiveRegion);
|
||||
}
|
||||
if (modifiesServerlessV2Scaling) {
|
||||
cluster.setServerlessV2MinCapacity(effectiveMinCapacity);
|
||||
cluster.setServerlessV2MaxCapacity(effectiveMaxCapacity);
|
||||
@@ -1774,6 +1912,8 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
"DB cluster " + id + " is registered with a DB proxy target group.", 400);
|
||||
}
|
||||
|
||||
detachManagedMasterUserSecret(cluster, effectiveRegion);
|
||||
|
||||
cluster.setStatus(DbInstanceStatus.DELETING);
|
||||
putClusterForScope(currentAccountId(), effectiveRegion, id, cluster);
|
||||
|
||||
|
||||
@@ -31,6 +31,9 @@ public class DbCluster {
|
||||
private Map<String, String> subnetAvailabilityZones = new LinkedHashMap<>();
|
||||
private String dbClusterResourceId;
|
||||
private String dbClusterArn;
|
||||
private String masterUserSecretArn;
|
||||
private String masterUserSecretStatus;
|
||||
private String masterUserSecretKmsKeyId;
|
||||
private Instant createdAt;
|
||||
private int proxyPort;
|
||||
private Map<String, String> tags = new LinkedHashMap<>();
|
||||
@@ -138,6 +141,17 @@ public class DbCluster {
|
||||
public String getDbClusterArn() { return dbClusterArn; }
|
||||
public void setDbClusterArn(String dbClusterArn) { this.dbClusterArn = dbClusterArn; }
|
||||
|
||||
public String getMasterUserSecretArn() { return masterUserSecretArn; }
|
||||
public void setMasterUserSecretArn(String masterUserSecretArn) { this.masterUserSecretArn = masterUserSecretArn; }
|
||||
|
||||
public String getMasterUserSecretStatus() { return masterUserSecretStatus; }
|
||||
public void setMasterUserSecretStatus(String masterUserSecretStatus) { this.masterUserSecretStatus = masterUserSecretStatus; }
|
||||
|
||||
public String getMasterUserSecretKmsKeyId() { return masterUserSecretKmsKeyId; }
|
||||
public void setMasterUserSecretKmsKeyId(String masterUserSecretKmsKeyId) {
|
||||
this.masterUserSecretKmsKeyId = masterUserSecretKmsKeyId;
|
||||
}
|
||||
|
||||
public Instant getCreatedAt() { return createdAt; }
|
||||
public void setCreatedAt(Instant createdAt) { this.createdAt = createdAt; }
|
||||
|
||||
|
||||
Reference in new issue
Block a user