mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(rds): rotate cluster managed credentials and secret versions together (#7003)
* fix(rds): rotate cluster managed credentials and secret versions together
* test(rds): load the PostgreSQL driver before the first DriverManager call
DriverManager only offers drivers the caller's class loader can see. After a
Quarkus restart, the first getConnection in
RdsPostgresStartupParametersIntegrationTest found none ("No suitable driver
found"), which has been failing CI shard 1 on main. Load PgJDBC through the
test's own loader first, as RdsBackendEndpointRefreshIntegrationTest already
does, and do the same in the new managed-password rotation Docker test.
---------
Co-authored-by: Priamo Germosen <priamo.germosen@starling.co>
This commit is contained in:
1 parent
955a935bad
commit
83ec0b82c3
8 files changed
+475
-9
No files matched your search
@@ -1484,7 +1484,9 @@ public class RdsQueryHandler {
|
||||
DbCluster cluster = service.modifyDbCluster(id, newPassword, iamEnabled,
|
||||
serverlessV2Min, serverlessV2Max, serverlessV2SecondsUntilAutoPause,
|
||||
manageMasterUserPassword, masterUserSecretKmsKeyId,
|
||||
params.getFirst("DBClusterParameterGroupName"), region);
|
||||
params.getFirst("DBClusterParameterGroupName"),
|
||||
Boolean.TRUE.equals(parseOptionalBoolean(params, "RotateMasterUserPassword")),
|
||||
Boolean.TRUE.equals(parseOptionalBoolean(params, "ApplyImmediately")), region);
|
||||
if (enableHttpEndpoint != null) {
|
||||
cluster = service.setHttpEndpoint(region, id, enableHttpEndpoint);
|
||||
}
|
||||
|
||||
@@ -3327,10 +3327,14 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
}
|
||||
|
||||
private static String managedMasterSecretString(DbCluster cluster) {
|
||||
return managedMasterSecretString(cluster, cluster.getMasterPassword());
|
||||
}
|
||||
|
||||
private static String managedMasterSecretString(DbCluster cluster, String password) {
|
||||
try {
|
||||
return JSON.writeValueAsString(Map.of(
|
||||
"username", cluster.getMasterUsername(),
|
||||
"password", cluster.getMasterPassword(),
|
||||
"password", password,
|
||||
"engine", cluster.getEngineIdentifier() != null && !cluster.getEngineIdentifier().isBlank()
|
||||
? cluster.getEngineIdentifier().toLowerCase()
|
||||
: cluster.getEngine().name().toLowerCase(),
|
||||
@@ -5472,6 +5476,17 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
Integer serverlessV2SecondsUntilAutoPause,
|
||||
Boolean manageMasterUserPassword, String masterUserSecretKmsKeyId,
|
||||
String dbClusterParameterGroupName, String region) {
|
||||
return modifyDbCluster(id, newPassword, iamEnabled, serverlessV2MinCapacity, serverlessV2MaxCapacity,
|
||||
serverlessV2SecondsUntilAutoPause, manageMasterUserPassword, masterUserSecretKmsKeyId,
|
||||
dbClusterParameterGroupName, false, false, region);
|
||||
}
|
||||
|
||||
public DbCluster modifyDbCluster(String id, String newPassword, Boolean iamEnabled,
|
||||
Double serverlessV2MinCapacity, Double serverlessV2MaxCapacity,
|
||||
Integer serverlessV2SecondsUntilAutoPause,
|
||||
Boolean manageMasterUserPassword, String masterUserSecretKmsKeyId,
|
||||
String dbClusterParameterGroupName, boolean rotateMasterUserPassword,
|
||||
boolean applyImmediately, String region) {
|
||||
String effectiveRegion = effectiveRegion(region);
|
||||
while (true) {
|
||||
DbCluster cluster = getDbCluster(id, effectiveRegion);
|
||||
@@ -5484,7 +5499,7 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
return doModifyDbCluster(id, newPassword, iamEnabled, serverlessV2MinCapacity,
|
||||
serverlessV2MaxCapacity, serverlessV2SecondsUntilAutoPause,
|
||||
manageMasterUserPassword, masterUserSecretKmsKeyId,
|
||||
dbClusterParameterGroupName, region);
|
||||
dbClusterParameterGroupName, rotateMasterUserPassword, applyImmediately, region);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5494,9 +5509,25 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
Double serverlessV2MinCapacity, Double serverlessV2MaxCapacity,
|
||||
Integer serverlessV2SecondsUntilAutoPause,
|
||||
Boolean manageMasterUserPassword, String masterUserSecretKmsKeyId,
|
||||
String dbClusterParameterGroupName, String region) {
|
||||
String dbClusterParameterGroupName, boolean rotateMasterUserPassword,
|
||||
boolean applyImmediately, String region) {
|
||||
String effectiveRegion = effectiveRegion(region);
|
||||
DbCluster cluster = getDbCluster(id, effectiveRegion);
|
||||
if (rotateMasterUserPassword) {
|
||||
if (!applyImmediately || newPassword != null || Boolean.FALSE.equals(manageMasterUserPassword)) {
|
||||
throw new AwsException("InvalidParameterCombination",
|
||||
"RotateMasterUserPassword requires ApplyImmediately and cannot set or disable the password.", 400);
|
||||
}
|
||||
if (cluster.getMasterUserSecretArn() == null || secretsManagerService == null) {
|
||||
throw new AwsException("InvalidParameterCombination",
|
||||
"RotateMasterUserPassword requires an existing RDS-managed master user secret.", 400);
|
||||
}
|
||||
if (cluster.getStatus() != DbInstanceStatus.AVAILABLE) {
|
||||
throw new AwsException("InvalidDBClusterStateFault",
|
||||
"The DB cluster must be available to rotate its master user password.", 400);
|
||||
}
|
||||
newPassword = generatedMasterPassword();
|
||||
}
|
||||
boolean changesParameterGroup = dbClusterParameterGroupName != null
|
||||
&& !dbClusterParameterGroupName.isBlank();
|
||||
if (changesParameterGroup) {
|
||||
@@ -5534,18 +5565,31 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
String oldPassword = cluster.getMasterPassword();
|
||||
boolean rotateBackend = hasNewPassword && !config.services().rds().mock()
|
||||
&& cluster.getContainerId() != null && !newPassword.equals(oldPassword);
|
||||
String acceptedPassword = newPassword;
|
||||
Runnable changeCredentials = () -> {
|
||||
// Keep the old credential and secret until the running backend accepts the change.
|
||||
if (rotateBackend) {
|
||||
containerManager.rotateMasterPassword(
|
||||
cluster.getDockerVolumeName(), cluster.getContainerId(),
|
||||
cluster.getEngine(), cluster.getMasterUsername(), oldPassword, newPassword);
|
||||
cluster.getEngine(), cluster.getMasterUsername(), oldPassword, acceptedPassword);
|
||||
}
|
||||
};
|
||||
boolean releasesManagedSecret = Boolean.FALSE.equals(manageMasterUserPassword)
|
||||
&& cluster.getMasterUserSecretArn() != null;
|
||||
boolean credentialsChanged = true;
|
||||
if (releasesManagedSecret) {
|
||||
if (rotateMasterUserPassword) {
|
||||
String rotatedValue = managedMasterSecretString(cluster, acceptedPassword);
|
||||
secretsManagerService.rotateManagedSecretAfter(cluster.getMasterUserSecretArn(),
|
||||
MANAGED_SECRET_OWNING_SERVICE, effectiveRegion, () -> {
|
||||
changeCredentials.run();
|
||||
return rotatedValue;
|
||||
}, () -> {
|
||||
if (rotateBackend) {
|
||||
containerManager.rotateMasterPassword(cluster.getDockerVolumeName(), cluster.getContainerId(),
|
||||
cluster.getEngine(), cluster.getMasterUsername(), acceptedPassword, oldPassword);
|
||||
}
|
||||
});
|
||||
} else if (releasesManagedSecret) {
|
||||
if (secretsManagerService == null) {
|
||||
throw new AwsException("InvalidParameterCombination",
|
||||
"ManageMasterUserPassword requires Secrets Manager support.", 400);
|
||||
@@ -5596,10 +5640,10 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
// Running backends have already accepted the rotation before metadata is published.
|
||||
if (passwordChanged) {
|
||||
proxyManager.updateMasterPassword(
|
||||
rdsResourceRelayKey(cluster.getDbClusterArn(), id), newPassword);
|
||||
rdsResourceRelayKey(cluster.getDbClusterArn(), id), acceptedPassword);
|
||||
updateClusterMembers(cluster, effectiveRegion,
|
||||
member -> member.setMasterPassword(newPassword),
|
||||
relayKey -> proxyManager.updateMasterPassword(relayKey, newPassword));
|
||||
member -> member.setMasterPassword(acceptedPassword),
|
||||
relayKey -> proxyManager.updateMasterPassword(relayKey, acceptedPassword));
|
||||
}
|
||||
|
||||
if (iamEnabled != null) {
|
||||
|
||||
+59
@@ -794,6 +794,65 @@ public class SecretsManagerService implements ResourceProvider {
|
||||
}
|
||||
}
|
||||
|
||||
/** Publishes a service-owned credential only after its backend accepts it, with rollback on publication failure. */
|
||||
public void rotateManagedSecretAfter(String secretArn, String owningService, String region,
|
||||
Supplier<String> changeCredentials, Runnable rollbackCredentials) {
|
||||
Secret secret;
|
||||
Map<String, SecretVersion> previousVersions = new HashMap<>();
|
||||
String previousCurrent;
|
||||
Instant previousChanged;
|
||||
Instant previousRotated;
|
||||
synchronized (lockFor(secretArn)) {
|
||||
secret = resolveSecret(secretArn, region);
|
||||
throwIfManagedRelease(secretArn);
|
||||
throwIfPendingDeletion(secret);
|
||||
throwIfReplica(secret);
|
||||
if (!owningService.equals(secret.getOwningService()) || managedRotationTokens.containsKey(secretArn)) {
|
||||
throw new AwsException("InvalidRequestException", "The managed secret cannot be rotated now.", 400);
|
||||
}
|
||||
secret.getVersions().forEach((id, version) -> previousVersions.put(id, copyVersion(version)));
|
||||
previousCurrent = secret.getCurrentVersionId();
|
||||
previousChanged = secret.getLastChangedDate();
|
||||
previousRotated = secret.getLastRotatedDate();
|
||||
managedReleases.add(secretArn);
|
||||
}
|
||||
boolean credentialsChanged = false;
|
||||
try {
|
||||
String value = changeCredentials.get();
|
||||
credentialsChanged = true;
|
||||
synchronized (lockFor(secretArn)) {
|
||||
putManagedRotationVersion(secret, UUID.randomUUID().toString(), previousCurrent, value);
|
||||
secret.setLastChangedDate(Instant.now());
|
||||
secret.setLastRotatedDate(Instant.now());
|
||||
persist(secret, region);
|
||||
}
|
||||
} catch (RuntimeException failure) {
|
||||
if (credentialsChanged) {
|
||||
try {
|
||||
rollbackCredentials.run();
|
||||
} catch (RuntimeException rollbackFailure) {
|
||||
failure.addSuppressed(rollbackFailure);
|
||||
}
|
||||
synchronized (lockFor(secretArn)) {
|
||||
secret.setVersions(previousVersions);
|
||||
secret.setCurrentVersionId(previousCurrent);
|
||||
secret.setLastChangedDate(previousChanged);
|
||||
secret.setLastRotatedDate(previousRotated);
|
||||
try {
|
||||
persist(secret, region);
|
||||
} catch (RuntimeException rollbackFailure) {
|
||||
failure.addSuppressed(rollbackFailure);
|
||||
}
|
||||
}
|
||||
}
|
||||
throw failure;
|
||||
} finally {
|
||||
synchronized (lockFor(secretArn)) {
|
||||
managedReleases.remove(secretArn);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void throwIfManagedRelease(String secretArn) {
|
||||
if (managedReleases.contains(secretArn)) {
|
||||
throw new AwsException("InvalidRequestException",
|
||||
|
||||
Reference in new issue
Block a user