mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(rds): inherit Aurora member master credentials (#5667)
* fix(rds): inherit Aurora member master credentials * fix(rds): retain defaults for incomplete cluster credentials * test(rds): keep credential checks compatible with Java 17 * fix(rds): keep Aurora managed secrets on the cluster * fix(rds): reject member credentials and publish offline changes * fix(rds): match the member username error message * fix(rds): release proxy listeners before reboot * test(rds): shorten listener retirement failure checks
This commit is contained in:
1 parent
1883855c94
commit
92b2cf74f7
10 files changed
+863
-75
No files matched your search
+106
@@ -0,0 +1,106 @@
|
||||
package com.floci.test;
|
||||
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import software.amazon.awssdk.services.rds.RdsClient;
|
||||
import software.amazon.awssdk.services.rds.model.CreateDbInstanceRequest;
|
||||
import software.amazon.awssdk.services.rds.model.DBInstance;
|
||||
import software.amazon.awssdk.services.rds.model.RdsException;
|
||||
import software.amazon.awssdk.services.secretsmanager.SecretsManagerClient;
|
||||
import software.amazon.awssdk.services.secretsmanager.model.ResourceNotFoundException;
|
||||
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
@DisplayName("Aurora member master credentials")
|
||||
class RdsMemberCredentialsTest {
|
||||
private static final Logger LOG = Logger.getLogger(RdsMemberCredentialsTest.class.getName());
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({"aurora-postgresql, false", "aurora-postgresql, true",
|
||||
"aurora-mysql, false", "aurora-mysql, true"})
|
||||
@DisplayName("Members inherit cluster credentials after explicit credentials are rejected")
|
||||
void memberMasterUserComesFromCluster(String engine, boolean conflicting) {
|
||||
String clusterId = TestFixtures.uniqueName("member-credentials");
|
||||
String memberId = clusterId + "-member";
|
||||
try (RdsClient rds = TestFixtures.rdsClient()) {
|
||||
try {
|
||||
rds.createDBCluster(b -> b.dbClusterIdentifier(clusterId).engine(engine)
|
||||
.masterUsername("clusteradmin").masterUserPassword("ClusterPassword!"));
|
||||
CreateDbInstanceRequest.Builder request = CreateDbInstanceRequest.builder()
|
||||
.dbInstanceIdentifier(memberId).dbClusterIdentifier(clusterId)
|
||||
.engine(engine).dbInstanceClass("db.serverless");
|
||||
if (conflicting) {
|
||||
request.masterUsername("differentuser").masterUserPassword("DifferentPassword!");
|
||||
assertThatThrownBy(() -> rds.createDBInstance(request.build()))
|
||||
.isInstanceOfSatisfying(RdsException.class, error -> {
|
||||
assertThat(error.statusCode()).isEqualTo(400);
|
||||
assertThat(error.awsErrorDetails().errorCode()).isEqualTo("InvalidParameterCombination");
|
||||
});
|
||||
request.masterUsername(null).masterUserPassword(null);
|
||||
}
|
||||
DBInstance created = rds.createDBInstance(request.build()).dbInstance();
|
||||
assertThat(created.masterUsername()).isEqualTo("clusteradmin");
|
||||
assertThat(rds.describeDBInstances(b -> b.dbInstanceIdentifier(memberId))
|
||||
.dbInstances().get(0).masterUsername()).isEqualTo("clusteradmin");
|
||||
} finally {
|
||||
try {
|
||||
rds.deleteDBInstance(b -> b.dbInstanceIdentifier(memberId).skipFinalSnapshot(true));
|
||||
} catch (RdsException e) {
|
||||
LOG.log(Level.WARNING, "Could not clean up member " + memberId, e);
|
||||
}
|
||||
try {
|
||||
rds.deleteDBCluster(b -> b.dbClusterIdentifier(clusterId).skipFinalSnapshot(true));
|
||||
} catch (RdsException e) {
|
||||
LOG.log(Level.WARNING, "Could not clean up cluster " + clusterId, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"aurora-postgresql", "aurora-mysql"})
|
||||
@DisplayName("Managed Aurora credentials remain owned by the cluster")
|
||||
void memberDoesNotCreateASeparateManagedSecret(String engine) {
|
||||
String clusterId = TestFixtures.uniqueName("cluster-secret-owner");
|
||||
String memberId = clusterId + "-member";
|
||||
try (RdsClient rds = TestFixtures.rdsClient();
|
||||
SecretsManagerClient secrets = TestFixtures.secretsManagerClient()) {
|
||||
try {
|
||||
String clusterArn = rds.createDBCluster(request -> request.dbClusterIdentifier(clusterId)
|
||||
.engine(engine).masterUsername("clusteradmin").manageMasterUserPassword(true))
|
||||
.dbCluster().masterUserSecret().secretArn();
|
||||
String original = secrets.getSecretValue(request -> request.secretId(clusterArn)).secretString();
|
||||
DBInstance member = rds.createDBInstance(request -> request.dbInstanceIdentifier(memberId)
|
||||
.dbClusterIdentifier(clusterId).engine(engine).dbInstanceClass("db.serverless")
|
||||
.manageMasterUserPassword(true)).dbInstance();
|
||||
assertThat(member.masterUsername()).isEqualTo("clusteradmin");
|
||||
assertThat(member.masterUserSecret()).isNull();
|
||||
assertThat(rds.describeDBInstances(request -> request.dbInstanceIdentifier(memberId))
|
||||
.dbInstances().get(0).masterUserSecret()).isNull();
|
||||
assertThatThrownBy(() -> secrets.getSecretValue(request -> request.secretId("rds!" + member.dbiResourceId())))
|
||||
.isInstanceOfSatisfying(ResourceNotFoundException.class,
|
||||
error -> assertThat(error.statusCode()).isEqualTo(400));
|
||||
rds.deleteDBInstance(request -> request.dbInstanceIdentifier(memberId).skipFinalSnapshot(true));
|
||||
assertThat(secrets.getSecretValue(request -> request.secretId(clusterArn)).secretString()).isEqualTo(original);
|
||||
} finally {
|
||||
try {
|
||||
try {
|
||||
rds.deleteDBInstance(request -> request.dbInstanceIdentifier(memberId).skipFinalSnapshot(true));
|
||||
} catch (RdsException error) {
|
||||
if (!"DBInstanceNotFound".equals(error.awsErrorDetails().errorCode())) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
rds.deleteDBCluster(request -> request.dbClusterIdentifier(clusterId).skipFinalSnapshot(true));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+13
-5
@@ -751,11 +751,19 @@ The RDS auth proxy validates the master username and password at the proxy layer
|
||||
|
||||
IAM database authentication is also supported. Set `--enable-iam-database-authentication` at instance creation time and use `aws rds generate-db-auth-token` to obtain a token.
|
||||
|
||||
New Aurora members inherit IAM database authentication from their cluster.
|
||||
`EnableIAMDatabaseAuthentication` on `CreateDBInstance` does not override the
|
||||
cluster setting, including when the instance parameter is omitted.
|
||||
Member IAM connections currently require the cluster master username and password
|
||||
to be supplied when creating the member; those credentials are not yet inherited.
|
||||
Aurora members use their cluster's IAM database authentication setting and master
|
||||
username and password for backend connections. `EnableIAMDatabaseAuthentication`
|
||||
on `CreateDBInstance` does not override the cluster setting. A member request with
|
||||
`MasterUsername` or `MasterUserPassword` returns `InvalidParameterCombination`;
|
||||
configure those credentials on the cluster and omit them from the member request.
|
||||
Managed master secrets belong to the cluster; `ManageMasterUserPassword` on a member
|
||||
does not create a separate member secret. When a cluster record lacks a credential,
|
||||
the member retains its normalized default for that field.
|
||||
|
||||
Accepted cluster password changes update stored member credentials even when the
|
||||
cluster has no backend or runs in mock mode. An unchanged password does not update
|
||||
relays. Rotating an initialized data volume requires a running backend; changing
|
||||
stored metadata alone does not rotate that volume.
|
||||
|
||||
Changing `EnableIAMDatabaseAuthentication` with `ModifyDBCluster` updates existing
|
||||
Aurora members and applies to new connections through the cluster and member
|
||||
|
||||
@@ -702,6 +702,19 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
"DB instance " + id + " already exists.", 400);
|
||||
}
|
||||
|
||||
if (dbClusterIdentifier != null && !dbClusterIdentifier.isBlank()) {
|
||||
if (masterPassword != null) {
|
||||
throw new AwsException("InvalidParameterCombination",
|
||||
"The requested DB Instance will be a member of a DB Cluster. "
|
||||
+ "Set master user password for the DB Cluster.", 400);
|
||||
}
|
||||
if (masterUsername != null) {
|
||||
throw new AwsException("InvalidParameterCombination",
|
||||
"The requested DB Instance will be a member of a DB Cluster. "
|
||||
+ "Set master user name for the DB Cluster.", 400);
|
||||
}
|
||||
}
|
||||
|
||||
DatabaseEngine engine = resolveEngine(engineParam);
|
||||
if (engine == DatabaseEngine.SQLSERVER && dbName != null && !dbName.isBlank()) {
|
||||
throw new AwsException("InvalidParameterCombination",
|
||||
@@ -726,10 +739,12 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
throw new AwsException("InvalidParameterValue",
|
||||
"MasterUsername must begin with a letter and contain only alphanumeric characters or underscores.", 400);
|
||||
}
|
||||
if (manageMasterUserPassword && (masterPassword == null || masterPassword.isBlank())) {
|
||||
boolean manageInstancePassword = manageMasterUserPassword
|
||||
&& (dbClusterIdentifier == null || dbClusterIdentifier.isBlank());
|
||||
if (manageInstancePassword && (masterPassword == null || masterPassword.isBlank())) {
|
||||
masterPassword = generatedMasterPassword();
|
||||
}
|
||||
String effectiveSecretKmsKeyId = manageMasterUserPassword
|
||||
String effectiveSecretKmsKeyId = manageInstancePassword
|
||||
? resolveManagedSecretKmsKeyId(masterUserSecretKmsKeyId, effectiveRegion) : null;
|
||||
// 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.
|
||||
@@ -860,9 +875,16 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
instance.setContainerPort(backendPort);
|
||||
iamEnabled = currentCluster.isIamDatabaseAuthenticationEnabled();
|
||||
instance.setIamDatabaseAuthenticationEnabled(iamEnabled);
|
||||
// Incomplete cluster records retain the member's existing defaults for missing credentials.
|
||||
if (currentCluster.getMasterUsername() != null) {
|
||||
instance.setMasterUsername(currentCluster.getMasterUsername());
|
||||
}
|
||||
if (currentCluster.getMasterPassword() != null) {
|
||||
instance.setMasterPassword(currentCluster.getMasterPassword());
|
||||
}
|
||||
}
|
||||
}
|
||||
if (manageMasterUserPassword) {
|
||||
if (manageInstancePassword) {
|
||||
attachManagedMasterUserSecret(instance, effectiveRegion, masterUserSecretKmsKeyId, effectiveSecretKmsKeyId);
|
||||
}
|
||||
putInstanceForScope(accountId, effectiveRegion, id, instance);
|
||||
@@ -873,7 +895,7 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
proxyManager.startProxy(rdsResourceRelayKey(instance.getDbInstanceArn(), id),
|
||||
engine, iamEnabled, proxyPort, backendHost, backendPort,
|
||||
instance.getEndpoint().address(),
|
||||
masterUsername, masterPassword, dbName,
|
||||
instance.getMasterUsername(), instance.getMasterPassword(), dbName,
|
||||
(user, pw) -> validateDbPasswordForScope(
|
||||
accountId, instanceRegion, id, user, pw),
|
||||
proxyBinding(engine, instance.getEndpoint().address(), instance.getEndpoint().port(),
|
||||
@@ -4753,8 +4775,9 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
} else {
|
||||
changeCredentials.run();
|
||||
}
|
||||
boolean passwordRotated = credentialsChanged && rotateBackend;
|
||||
if (credentialsChanged && hasNewPassword) {
|
||||
boolean passwordAccepted = credentialsChanged && hasNewPassword;
|
||||
boolean passwordChanged = passwordAccepted && !newPassword.equals(oldPassword);
|
||||
if (passwordAccepted) {
|
||||
cluster.setMasterPassword(newPassword);
|
||||
}
|
||||
if (iamEnabled != null) {
|
||||
@@ -4786,11 +4809,9 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
applyAutoPause(cluster);
|
||||
}
|
||||
|
||||
// A cluster rotation applies to every endpoint: the cluster's own proxy and each member
|
||||
// instance's proxy hold start-time password snapshots, and member endpoints validate
|
||||
// against the member's stored password, so propagate the rotated credential to all of
|
||||
// them (AWS applies the cluster master password to every member endpoint).
|
||||
if (passwordRotated) {
|
||||
// Accepted credentials belong to every endpoint even before a backend is provisioned.
|
||||
// Running backends have already accepted the rotation before metadata is published.
|
||||
if (passwordChanged) {
|
||||
proxyManager.updateMasterPassword(
|
||||
rdsResourceRelayKey(cluster.getDbClusterArn(), id), newPassword);
|
||||
updateClusterMembers(cluster, effectiveRegion,
|
||||
|
||||
@@ -8,6 +8,7 @@ import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.time.Duration;
|
||||
import java.util.concurrent.Semaphore;
|
||||
|
||||
/**
|
||||
@@ -39,6 +40,8 @@ public class RdsAuthProxy {
|
||||
|
||||
private volatile boolean running;
|
||||
private ServerSocket serverSocket;
|
||||
private Thread acceptThread;
|
||||
Duration acceptRetirementTimeout = Duration.ofSeconds(5);
|
||||
|
||||
public RdsAuthProxy(String instanceId, String backendHost, int backendPort,
|
||||
DatabaseEngine engine, boolean iamEnabled,
|
||||
@@ -99,7 +102,8 @@ public class RdsAuthProxy {
|
||||
serverSocket.setReuseAddress(true);
|
||||
serverSocket.bind(new InetSocketAddress(proxyPort));
|
||||
running = true;
|
||||
Thread.ofVirtual().name("rds-proxy-accept-" + instanceId).start(this::acceptLoop);
|
||||
acceptThread = Thread.ofVirtual().name("rds-proxy-accept-" + instanceId).unstarted(this::acceptLoop);
|
||||
acceptThread.start();
|
||||
LOG.infov("RDS proxy started for instance {0} on port {1} → {2}:{3}",
|
||||
instanceId, String.valueOf(proxyPort), backendHost, String.valueOf(backendPort));
|
||||
}
|
||||
@@ -125,12 +129,29 @@ public class RdsAuthProxy {
|
||||
throw new RuntimeException(
|
||||
"Failed to stop RDS proxy for instance " + instanceId, e);
|
||||
}
|
||||
// A socket closed during accept can retain its native descriptor until accept exits.
|
||||
// Retire that loop before a reboot binds its replacement to the same port.
|
||||
Thread accepting = acceptThread;
|
||||
if (accepting != null && accepting != Thread.currentThread() && accepting.isAlive()) {
|
||||
try {
|
||||
if (!accepting.join(acceptRetirementTimeout)) {
|
||||
throw new IllegalStateException("RDS proxy accept loop did not stop for instance " + instanceId);
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new IllegalStateException("Interrupted while stopping RDS proxy for instance " + instanceId, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void acceptLoop() {
|
||||
while (running) {
|
||||
try {
|
||||
Socket client = serverSocket.accept();
|
||||
if (!running) {
|
||||
closeQuietly(client);
|
||||
break;
|
||||
}
|
||||
if (!connectionPermits.tryAcquire()) {
|
||||
LOG.warnv("Refusing RDS connection for instance {0}: connection limit reached",
|
||||
instanceId);
|
||||
|
||||
+67
@@ -1,13 +1,21 @@
|
||||
package io.github.hectorvent.floci.services.cloudformation;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.testing.RdsMockProfile;
|
||||
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.TestProfile;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static io.github.hectorvent.floci.testing.RdsRequests.rds;
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
@@ -36,6 +44,65 @@ class CloudFormationRdsDynamicReferenceIntegrationTest {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"MasterUsername", "MasterUserPassword"})
|
||||
void memberCreationRejectsExplicitCredentialsAndAllowsOmittedRetry(String credential) throws Exception {
|
||||
String suffix = Long.toString(System.nanoTime(), 36);
|
||||
String clusterId = "cfn-credential-cluster-" + suffix;
|
||||
String memberId = "cfn-credential-member-" + suffix;
|
||||
String rejectedStack = "cfn-member-input-" + suffix;
|
||||
String validStack = rejectedStack + "-valid";
|
||||
rds("CreateDBCluster").formParam("DBClusterIdentifier", clusterId)
|
||||
.formParam("Engine", "aurora-postgresql").formParam("MasterUsername", "clusteradmin")
|
||||
.formParam("MasterUserPassword", "ClusterPassword!").post("/").then().statusCode(200);
|
||||
boolean rejectedCreated = false;
|
||||
boolean validCreated = false;
|
||||
try {
|
||||
Map<String, Object> properties = new LinkedHashMap<>();
|
||||
properties.put("DBInstanceIdentifier", memberId);
|
||||
properties.put("DBClusterIdentifier", clusterId);
|
||||
properties.put("Engine", "aurora-postgresql");
|
||||
properties.put("DBInstanceClass", "db.serverless");
|
||||
properties.put(credential, "MasterUsername".equals(credential) ? "clusteradmin" : "ClusterPassword!");
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
createStack(rejectedStack, mapper.writeValueAsString(Map.of("Resources", Map.of("Member",
|
||||
Map.of("Type", "AWS::RDS::DBInstance", "Properties", properties)))));
|
||||
rejectedCreated = true;
|
||||
assertEquals("ROLLBACK_COMPLETE", CfnStackWaits.awaitTerminal(rejectedStack, CFN_AUTH).status());
|
||||
String events = given().contentType("application/x-www-form-urlencoded").header("Authorization", CFN_AUTH)
|
||||
.formParam("Action", "DescribeStackEvents").formParam("StackName", rejectedStack)
|
||||
.post("/").then().statusCode(200).extract().asString();
|
||||
assertTrue(events.contains("will be a member of a DB Cluster"), events);
|
||||
rds("DescribeDBInstances").formParam("DBInstanceIdentifier", memberId)
|
||||
.post("/").then().statusCode(404);
|
||||
properties.remove(credential);
|
||||
createStack(validStack, mapper.writeValueAsString(Map.of("Resources", Map.of("Member",
|
||||
Map.of("Type", "AWS::RDS::DBInstance", "Properties", properties)))));
|
||||
validCreated = true;
|
||||
assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(validStack, CFN_AUTH).status());
|
||||
assertEquals("clusteradmin", rds("DescribeDBInstances").formParam("DBInstanceIdentifier", memberId)
|
||||
.post("/").then().statusCode(200).extract().xmlPath()
|
||||
.getString("DescribeDBInstancesResponse.DescribeDBInstancesResult.DBInstances.DBInstance.MasterUsername"));
|
||||
} finally {
|
||||
try {
|
||||
try {
|
||||
if (validCreated) {
|
||||
deleteStack(validStack);
|
||||
CfnStackWaits.awaitStackDeleted(validStack);
|
||||
}
|
||||
} finally {
|
||||
if (rejectedCreated) {
|
||||
deleteStack(rejectedStack);
|
||||
CfnStackWaits.awaitStackDeleted(rejectedStack);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
rds("DeleteDBCluster").formParam("DBClusterIdentifier", clusterId).formParam("SkipFinalSnapshot", true)
|
||||
.post("/").then().statusCode(200);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The regression gate. A master password reading from an SSM {@code SecureString} must reach
|
||||
* {@code CREATE_COMPLETE}. If the credential properties stop opting out of the general stage,
|
||||
|
||||
+110
-11
@@ -1,9 +1,12 @@
|
||||
package io.github.hectorvent.floci.services.rds;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.github.dockerjava.api.DockerClient;
|
||||
import io.github.hectorvent.floci.services.iam.IamService;
|
||||
import io.github.hectorvent.floci.testutil.SigV4TokenTestHelper;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.response.Response;
|
||||
import io.restassured.specification.RequestSpecification;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Tag;
|
||||
@@ -18,9 +21,11 @@ import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
import static io.github.hectorvent.floci.testing.RdsRequests.rds;
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.awaitility.Awaitility.await;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
@@ -123,8 +128,6 @@ class RdsIamAuthLambdaSessionIntegrationTest {
|
||||
.formParam("DBClusterIdentifier", clusterId)
|
||||
.formParam("DBInstanceClass", "db.r5.large")
|
||||
.formParam("Engine", "aurora-postgresql")
|
||||
.formParam("MasterUsername", MASTER_USER)
|
||||
.formParam("MasterUserPassword", MASTER_PASSWORD)
|
||||
.formParam("DBName", DATABASE)
|
||||
.formParam("AllocatedStorage", "20")
|
||||
.when().post("/").then().statusCode(200)
|
||||
@@ -178,8 +181,6 @@ class RdsIamAuthLambdaSessionIntegrationTest {
|
||||
.formParam("DBClusterIdentifier", clusterId)
|
||||
.formParam("DBInstanceClass", "db.r5.large")
|
||||
.formParam("Engine", "aurora-postgresql")
|
||||
.formParam("MasterUsername", MASTER_USER)
|
||||
.formParam("MasterUserPassword", MASTER_PASSWORD)
|
||||
.formParam("DBName", DATABASE)
|
||||
.formParam("EnableIAMDatabaseAuthentication", Boolean.toString(!enabled))
|
||||
.when().post("/").then().statusCode(200)
|
||||
@@ -198,9 +199,103 @@ class RdsIamAuthLambdaSessionIntegrationTest {
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void auroraMemberUsesClusterCredentialsForIamLogin(boolean conflicting) throws Exception {
|
||||
String suffix = Long.toString(System.nanoTime(), 36);
|
||||
String clusterId = "iam-creds-cluster-" + suffix;
|
||||
String memberId = "iam-creds-member-" + suffix;
|
||||
String accessKeyId = "ASIA" + suffix.toUpperCase();
|
||||
try {
|
||||
rds("CreateDBCluster").formParam("DBClusterIdentifier", clusterId)
|
||||
.formParam("Engine", "aurora-postgresql")
|
||||
.formParam("MasterUsername", MASTER_USER)
|
||||
.formParam("MasterUserPassword", MASTER_PASSWORD)
|
||||
.formParam("DatabaseName", DATABASE)
|
||||
.formParam("EnableIAMDatabaseAuthentication", "true")
|
||||
.when().post("/").then().statusCode(200);
|
||||
RequestSpecification createMember = rds("CreateDBInstance")
|
||||
.formParam("DBInstanceIdentifier", memberId)
|
||||
.formParam("DBClusterIdentifier", clusterId)
|
||||
.formParam("DBInstanceClass", "db.serverless")
|
||||
.formParam("Engine", "aurora-postgresql")
|
||||
.formParam("DBName", DATABASE);
|
||||
if (conflicting) {
|
||||
rds("CreateDBInstance").formParam("DBInstanceIdentifier", memberId)
|
||||
.formParam("DBClusterIdentifier", clusterId).formParam("DBInstanceClass", "db.serverless")
|
||||
.formParam("Engine", "aurora-postgresql").formParam("DBName", DATABASE)
|
||||
.formParam("MasterUsername", "differentuser").formParam("MasterUserPassword", "DifferentPassword!")
|
||||
.when().post("/").then().statusCode(400)
|
||||
.body(containsString("<Code>InvalidParameterCombination</Code>"));
|
||||
}
|
||||
int port = createMember.when().post("/").then().statusCode(200).extract().xmlPath()
|
||||
.getInt("CreateDBInstanceResponse.CreateDBInstanceResult.DBInstance.Endpoint.Port");
|
||||
iamService.registerLambdaExecutionRoleSession(ACCOUNT_ID, accessKeyId, SESSION_SECRET, SESSION_TOKEN,
|
||||
"arn:aws:iam::" + ACCOUNT_ID + ":role/connect-with-iam");
|
||||
awaitPasswordReady(port);
|
||||
String token = SigV4TokenTestHelper.createRdsToken("127.0.0.1", port, MASTER_USER,
|
||||
accessKeyId, SESSION_SECRET, Instant.now().minusSeconds(60), 900, SESSION_TOKEN);
|
||||
assertCurrentUser(port, token);
|
||||
rds("DescribeDBInstances").formParam("DBInstanceIdentifier", memberId)
|
||||
.when().post("/").then().statusCode(200)
|
||||
.body(containsString("<MasterUsername>" + MASTER_USER + "</MasterUsername>"));
|
||||
rds("RebootDBInstance").formParam("DBInstanceIdentifier", memberId)
|
||||
.when().post("/").then().statusCode(200);
|
||||
assertCurrentUser(port, token);
|
||||
} finally {
|
||||
iamService.unregisterSession(ACCOUNT_ID, accessKeyId);
|
||||
rds("DeleteDBInstance").formParam("DBInstanceIdentifier", memberId)
|
||||
.formParam("SkipFinalSnapshot", "true").when().post("/");
|
||||
rds("DeleteDBCluster").formParam("DBClusterIdentifier", clusterId)
|
||||
.formParam("SkipFinalSnapshot", "true").when().post("/");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void memberUsesTheClusterManagedSecretBeforeAndAfterReboot() throws Exception {
|
||||
String suffix = Long.toString(System.nanoTime(), 36);
|
||||
String clusterId = "managed-cluster-" + suffix;
|
||||
String memberId = "managed-member-" + suffix;
|
||||
try {
|
||||
String secretArn = rds("CreateDBCluster").formParam("DBClusterIdentifier", clusterId)
|
||||
.formParam("Engine", "aurora-postgresql").formParam("MasterUsername", MASTER_USER)
|
||||
.formParam("ManageMasterUserPassword", true).formParam("DatabaseName", DATABASE)
|
||||
.when().post("/").then().statusCode(200).extract().xmlPath()
|
||||
.getString("CreateDBClusterResponse.CreateDBClusterResult.DBCluster.MasterUserSecret.SecretArn");
|
||||
String value = given().header("Authorization", "AWS4-HMAC-SHA256 Credential=" + ACCOUNT_ID
|
||||
+ "/20260215/us-east-1/secretsmanager/aws4_request, SignedHeaders=host, Signature=abc")
|
||||
.header("X-Amz-Target", "secretsmanager.GetSecretValue").contentType("application/x-amz-json-1.1")
|
||||
.body(new ObjectMapper().writeValueAsBytes(Map.of("SecretId", secretArn)))
|
||||
.post("/").then().statusCode(200).extract().path("SecretString");
|
||||
String password = new ObjectMapper().readTree(value).get("password").asText();
|
||||
Response member = rds("CreateDBInstance").formParam("DBInstanceIdentifier", memberId)
|
||||
.formParam("DBClusterIdentifier", clusterId).formParam("DBInstanceClass", "db.serverless")
|
||||
.formParam("Engine", "aurora-postgresql").formParam("DBName", DATABASE)
|
||||
.formParam("ManageMasterUserPassword", true)
|
||||
.when().post("/").then().statusCode(200).extract().response();
|
||||
String root = "CreateDBInstanceResponse.CreateDBInstanceResult.DBInstance.";
|
||||
assertEquals("", member.xmlPath().getString(root + "MasterUserSecret.SecretArn"));
|
||||
int port = member.xmlPath().getInt(root + "Endpoint.Port");
|
||||
awaitPasswordReady(port, password);
|
||||
assertCurrentUser(port, password);
|
||||
rds("RebootDBInstance").formParam("DBInstanceIdentifier", memberId)
|
||||
.when().post("/").then().statusCode(200);
|
||||
assertCurrentUser(port, password);
|
||||
} finally {
|
||||
rds("DeleteDBInstance").formParam("DBInstanceIdentifier", memberId)
|
||||
.formParam("SkipFinalSnapshot", true).when().post("/");
|
||||
rds("DeleteDBCluster").formParam("DBClusterIdentifier", clusterId)
|
||||
.formParam("SkipFinalSnapshot", true).when().post("/");
|
||||
}
|
||||
}
|
||||
|
||||
private static void awaitPasswordReady(int port) {
|
||||
awaitPasswordReady(port, MASTER_PASSWORD);
|
||||
}
|
||||
|
||||
private static void awaitPasswordReady(int port, String password) {
|
||||
await().atMost(Duration.ofSeconds(60)).ignoreExceptions().until(() -> {
|
||||
try (Connection connection = connect(port, MASTER_PASSWORD)) {
|
||||
try (Connection connection = connect(port, password)) {
|
||||
return connection.isValid(5);
|
||||
}
|
||||
});
|
||||
@@ -210,17 +305,21 @@ class RdsIamAuthLambdaSessionIntegrationTest {
|
||||
String token = SigV4TokenTestHelper.createRdsToken("127.0.0.1", port, MASTER_USER,
|
||||
accessKeyId, SESSION_SECRET, Instant.now().minusSeconds(60), 900, SESSION_TOKEN);
|
||||
if (enabled) {
|
||||
try (Connection connection = connect(port, token);
|
||||
Statement statement = connection.createStatement();
|
||||
ResultSet rows = statement.executeQuery("SELECT current_user")) {
|
||||
assertTrue(rows.next());
|
||||
assertEquals(MASTER_USER, rows.getString(1));
|
||||
}
|
||||
assertCurrentUser(port, token);
|
||||
} else {
|
||||
assertThrows(SQLException.class, () -> connect(port, token).close());
|
||||
}
|
||||
}
|
||||
|
||||
private static void assertCurrentUser(int port, String password) throws SQLException {
|
||||
try (Connection connection = connect(port, password);
|
||||
Statement statement = connection.createStatement();
|
||||
ResultSet rows = statement.executeQuery("SELECT current_user")) {
|
||||
assertTrue(rows.next());
|
||||
assertEquals(MASTER_USER, rows.getString(1));
|
||||
}
|
||||
}
|
||||
|
||||
private static Connection connect(int port, String password) throws SQLException {
|
||||
Properties properties = new Properties();
|
||||
properties.setProperty("user", MASTER_USER);
|
||||
|
||||
+119
-7
@@ -1,5 +1,6 @@
|
||||
package io.github.hectorvent.floci.services.rds;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.core.common.RequestScopes;
|
||||
import io.github.hectorvent.floci.services.kms.KmsService;
|
||||
import io.github.hectorvent.floci.testing.RdsMockProfile;
|
||||
@@ -14,13 +15,20 @@ import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
|
||||
import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient;
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
import software.amazon.awssdk.services.rds.RdsClient;
|
||||
import software.amazon.awssdk.services.rds.model.CreateDbInstanceRequest;
|
||||
import software.amazon.awssdk.services.rds.model.DBCluster;
|
||||
import software.amazon.awssdk.services.rds.model.DBInstance;
|
||||
import software.amazon.awssdk.services.rds.model.RdsException;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@QuarkusTest
|
||||
@TestProfile(RdsMockProfile.class)
|
||||
@@ -32,18 +40,69 @@ class RdsManagedSecretIntegrationTest {
|
||||
@Inject
|
||||
RdsService rdsService;
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({"aurora-postgresql, username, true", "aurora-postgresql, username, false",
|
||||
"aurora-postgresql, password, true", "aurora-postgresql, password, false",
|
||||
"aurora-mysql, username, true", "aurora-mysql, username, false",
|
||||
"aurora-mysql, password, true", "aurora-mysql, password, false"})
|
||||
void sdkRejectsMemberCredentialParametersAndAllowsOmittedRetry(String engine, String field, boolean matching) {
|
||||
String id = "member-input-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
String memberId = id + "-member";
|
||||
try (RdsClient rds = client("us-east-1", "000000000000")) {
|
||||
rds.createDBCluster(request -> request.dbClusterIdentifier(id).engine(engine)
|
||||
.masterUsername("clusteradmin").masterUserPassword("ClusterPassword!"));
|
||||
try {
|
||||
CreateDbInstanceRequest.Builder request = CreateDbInstanceRequest.builder()
|
||||
.dbInstanceIdentifier(memberId).dbClusterIdentifier(id).engine(engine)
|
||||
.dbInstanceClass("db.serverless");
|
||||
if ("username".equals(field)) {
|
||||
request.masterUsername(matching ? "clusteradmin" : "differentadmin");
|
||||
} else {
|
||||
request.masterUserPassword(matching ? "ClusterPassword!" : "DifferentPassword!");
|
||||
}
|
||||
RdsException error = assertThrows(RdsException.class, () -> rds.createDBInstance(request.build()));
|
||||
assertEquals("InvalidParameterCombination", error.awsErrorDetails().errorCode());
|
||||
assertEquals(400, error.statusCode());
|
||||
assertEquals("The requested DB Instance will be a member of a DB Cluster. "
|
||||
+ ("password".equals(field) ? "Set master user password for the DB Cluster."
|
||||
: "Set master user name for the DB Cluster."), error.awsErrorDetails().errorMessage());
|
||||
assertEquals("DBInstanceNotFound", assertThrows(RdsException.class,
|
||||
() -> rds.describeDBInstances(value -> value.dbInstanceIdentifier(memberId)))
|
||||
.awsErrorDetails().errorCode());
|
||||
assertTrue(rds.describeDBClusters(value -> value.dbClusterIdentifier(id))
|
||||
.dbClusters().getFirst().dbClusterMembers().isEmpty());
|
||||
request.masterUsername(null).masterUserPassword(null);
|
||||
DBInstance member = rds.createDBInstance(request.build()).dbInstance();
|
||||
assertEquals("clusteradmin", member.masterUsername());
|
||||
} finally {
|
||||
try {
|
||||
try {
|
||||
rds.deleteDBInstance(value -> value.dbInstanceIdentifier(memberId).skipFinalSnapshot(true));
|
||||
} catch (RdsException error) {
|
||||
if (!"DBInstanceNotFound".equals(error.awsErrorDetails().errorCode())) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
rds.deleteDBCluster(value -> value.dbClusterIdentifier(id).skipFinalSnapshot(true));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static RdsClient client(String region, String account) {
|
||||
return RdsClient.builder().endpointOverride(URI.create("http://localhost:" + RestAssured.port))
|
||||
.httpClientBuilder(UrlConnectionHttpClient.builder()).region(Region.of(region))
|
||||
.credentialsProvider(StaticCredentialsProvider.create(
|
||||
AwsBasicCredentials.create(account, "test-secret-key"))).build();
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({"us-east-1,000000000000", "cn-north-1,000000000000",
|
||||
"us-east-1,222222222222", "cn-north-1,222222222222"})
|
||||
void sdkReportsTheActualDefaultManagedKeyForInstancesAndClusters(String region, String account) {
|
||||
String id = "managed-key-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
try (RdsClient rds = RdsClient.builder()
|
||||
.endpointOverride(URI.create("http://localhost:" + RestAssured.port))
|
||||
.httpClientBuilder(UrlConnectionHttpClient.builder())
|
||||
.region(Region.of(region))
|
||||
.credentialsProvider(StaticCredentialsProvider.create(
|
||||
AwsBasicCredentials.create(account, "test-secret-key")))
|
||||
.build()) {
|
||||
try (RdsClient rds = client(region, account)) {
|
||||
DBInstance instance = rds.createDBInstance(request -> request
|
||||
.dbInstanceIdentifier(id)
|
||||
.dbInstanceClass("db.t4g.small")
|
||||
@@ -83,4 +142,57 @@ class RdsManagedSecretIntegrationTest {
|
||||
}
|
||||
}
|
||||
}
|
||||
@ParameterizedTest
|
||||
@CsvSource({"aurora-postgresql,us-east-1,000000000000", "aurora-mysql,us-east-1,000000000000",
|
||||
"aurora-postgresql,cn-north-1,000000000000", "aurora-mysql,cn-north-1,000000000000",
|
||||
"aurora-postgresql,us-east-1,222222222222", "aurora-mysql,us-east-1,222222222222",
|
||||
"aurora-postgresql,cn-north-1,222222222222", "aurora-mysql,cn-north-1,222222222222"})
|
||||
void sdkClusterMemberLeavesTheManagedSecretOnTheCluster(String engine, String region, String account)
|
||||
throws Exception {
|
||||
String id = "cluster-managed-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
try (RdsClient rds = client(region, account)) {
|
||||
DBCluster cluster = rds.createDBCluster(request -> request.dbClusterIdentifier(id).engine(engine)
|
||||
.masterUsername("clusteradmin").manageMasterUserPassword(true)).dbCluster();
|
||||
try {
|
||||
String clusterArn = cluster.masterUserSecret().secretArn();
|
||||
String original = secretValue(region, account, clusterArn);
|
||||
DBInstance member = rds.createDBInstance(request -> request.dbInstanceIdentifier(id + "-member")
|
||||
.dbClusterIdentifier(id).engine(engine).dbInstanceClass("db.r5.large")
|
||||
.manageMasterUserPassword(true)).dbInstance();
|
||||
try {
|
||||
assertEquals("clusteradmin", member.masterUsername());
|
||||
assertNull(member.masterUserSecret());
|
||||
assertNull(rds.describeDBInstances(request -> request.dbInstanceIdentifier(id + "-member"))
|
||||
.dbInstances().getFirst().masterUserSecret());
|
||||
assertEquals("ResourceNotFoundException", given()
|
||||
.header("Authorization", secretAuthorization(region, account))
|
||||
.header("X-Amz-Target", "secretsmanager.GetSecretValue")
|
||||
.contentType("application/x-amz-json-1.1")
|
||||
.body(new ObjectMapper().writeValueAsBytes(Map.of("SecretId", "rds!" + member.dbiResourceId())))
|
||||
.post("/").then().statusCode(400).extract().path("__type"));
|
||||
assertEquals(original, secretValue(region, account, clusterArn));
|
||||
} finally {
|
||||
rds.deleteDBInstance(request -> request.dbInstanceIdentifier(id + "-member").skipFinalSnapshot(true));
|
||||
}
|
||||
assertEquals(original, secretValue(region, account, clusterArn));
|
||||
assertEquals(clusterArn, rds.describeDBClusters(request -> request.dbClusterIdentifier(id))
|
||||
.dbClusters().getFirst().masterUserSecret().secretArn());
|
||||
} finally {
|
||||
rds.deleteDBCluster(request -> request.dbClusterIdentifier(id).skipFinalSnapshot(true));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static String secretValue(String region, String account, String arn) throws Exception {
|
||||
return given().header("Authorization", secretAuthorization(region, account))
|
||||
.header("X-Amz-Target", "secretsmanager.GetSecretValue").contentType("application/x-amz-json-1.1")
|
||||
.body(new ObjectMapper().writeValueAsBytes(Map.of("SecretId", arn))).post("/")
|
||||
.then().statusCode(200).extract().path("SecretString");
|
||||
}
|
||||
|
||||
private static String secretAuthorization(String region, String account) {
|
||||
return "AWS4-HMAC-SHA256 Credential=" + account + "/20260215/" + region
|
||||
+ "/secretsmanager/aws4_request, SignedHeaders=host, Signature=abc";
|
||||
}
|
||||
|
||||
}
|
||||
-2
@@ -83,8 +83,6 @@ class RdsMemberIamResourceIntegrationTest {
|
||||
.formParam("DBClusterIdentifier", clusterId)
|
||||
.formParam("DBInstanceClass", "db.serverless")
|
||||
.formParam("Engine", "aurora-postgresql")
|
||||
.formParam("MasterUsername", MASTER_USER)
|
||||
.formParam("MasterUserPassword", MASTER_PASSWORD)
|
||||
.formParam("DBName", DATABASE)
|
||||
.formParam("EnableIAMDatabaseAuthentication", "true")
|
||||
.when().post("/").then().statusCode(200).extract().xmlPath();
|
||||
|
||||
@@ -301,7 +301,7 @@ class RdsServiceTest {
|
||||
"admin", "password", "dbname", true, null);
|
||||
clearInvocations(proxyManager);
|
||||
DbInstance member = rdsService.createDbInstance("iam-member", engine, null,
|
||||
"admin", "password", "dbname", "db.serverless",
|
||||
null, null, "dbname", "db.serverless",
|
||||
0, true, null, null, "iam-cluster");
|
||||
if (!"create".equals(path)) {
|
||||
clearInvocations(proxyManager);
|
||||
@@ -369,7 +369,7 @@ class RdsServiceTest {
|
||||
DbCluster cluster = rdsService.createDbCluster("iam-cluster", engine, version,
|
||||
"admin", "password", "dbname", false, null);
|
||||
DbInstance member = rdsService.createDbInstance("iam-member", engine, version,
|
||||
"admin", "password", "dbname", "db.r5.large",
|
||||
null, null, "dbname", "db.r5.large",
|
||||
20, false, null, null, "iam-cluster", null, false, false, null,
|
||||
Map.of(), List.of(), null, null, true, DbInstanceSettings.defaults());
|
||||
cluster.getDbClusterMembers().add("missing-member");
|
||||
@@ -398,7 +398,7 @@ class RdsServiceTest {
|
||||
DbCluster cluster = rdsService.createDbCluster("iam-cluster", engine, version,
|
||||
"admin", "password", "dbname", false, null);
|
||||
DbInstance member = rdsService.createDbInstance("iam-member", engine, version,
|
||||
"admin", "password", "dbname", "db.r5.large",
|
||||
null, null, "dbname", "db.r5.large",
|
||||
20, false, null, null, "iam-cluster", null, false, false, null,
|
||||
Map.of(), List.of(), null, null, true, DbInstanceSettings.defaults());
|
||||
rdsService.createDbInstance("standalone", engine, version,
|
||||
@@ -420,7 +420,7 @@ class RdsServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void modifyClusterIamAuthenticationKeepsOtherAccountsAndRegionsUnchanged() {
|
||||
void modifyClusterCredentialsAndIamKeepOtherAccountsAndRegionsUnchanged() {
|
||||
InMemoryStorage<String, DbInstance> rawInstances = new InMemoryStorage<>();
|
||||
InMemoryStorage<String, DbCluster> rawClusters = new InMemoryStorage<>();
|
||||
String accountA = "111111111111";
|
||||
@@ -440,13 +440,13 @@ class RdsServiceTest {
|
||||
service.createDbCluster("shared-cluster", "aurora-postgresql", "16.3",
|
||||
"admin", "password", "dbname", false, null, null, null, false, region);
|
||||
service.createDbInstance("shared-member", "aurora-postgresql", "16.3",
|
||||
"admin", "password", "dbname", "db.r5.large", 20, false, null, null,
|
||||
null, null, "dbname", "db.r5.large", 20, false, null, null,
|
||||
"shared-cluster", null, false, false, null, Map.of(), List.of(), region);
|
||||
}
|
||||
}
|
||||
clearInvocations(proxyManager);
|
||||
|
||||
serviceA.modifyDbCluster("shared-cluster", null, true, "us-east-1");
|
||||
serviceA.modifyDbCluster("shared-cluster", "ScopedPassword!", true, "us-east-1");
|
||||
|
||||
for (String account : List.of(accountA, accountB)) {
|
||||
for (String region : List.of("us-east-1", "us-west-2")) {
|
||||
@@ -455,6 +455,12 @@ class RdsServiceTest {
|
||||
DbInstance member = rawInstances.get(account + "/" + region + "::shared-member").orElseThrow();
|
||||
assertEquals(enabled, cluster.isIamDatabaseAuthenticationEnabled());
|
||||
assertEquals(enabled, member.isIamDatabaseAuthenticationEnabled());
|
||||
assertEquals(enabled ? "ScopedPassword!" : "password", cluster.getMasterPassword());
|
||||
assertEquals(enabled ? "ScopedPassword!" : "password", member.getMasterPassword());
|
||||
verify(proxyManager, times(enabled ? 1 : 0))
|
||||
.updateMasterPassword("rds-resource:" + cluster.getDbClusterArn(), "ScopedPassword!");
|
||||
verify(proxyManager, times(enabled ? 1 : 0))
|
||||
.updateMasterPassword("rds-resource:" + member.getDbInstanceArn(), "ScopedPassword!");
|
||||
verify(proxyManager, times(enabled ? 1 : 0))
|
||||
.updateIamEnabled("rds-resource:" + cluster.getDbClusterArn(), true);
|
||||
verify(proxyManager, times(enabled ? 1 : 0))
|
||||
@@ -481,7 +487,7 @@ class RdsServiceTest {
|
||||
ExecutorService worker = Executors.newSingleThreadExecutor();
|
||||
try {
|
||||
Future<DbInstance> creation = worker.submit(() -> rdsService.createDbInstance("replacement-member",
|
||||
"aurora-postgresql", "16.3", "admin", "password", "dbname", "db.r5.large", 20,
|
||||
"aurora-postgresql", "16.3", null, null, "dbname", "db.r5.large", 20,
|
||||
true, null, null, "replaced-cluster", null, false, false, null,
|
||||
Map.of(), List.of(), null, null, true, DbInstanceSettings.defaults()));
|
||||
assertTrue(endpoint.await(5, TimeUnit.SECONDS));
|
||||
@@ -496,7 +502,7 @@ class RdsServiceTest {
|
||||
() -> rdsService.getDbInstance("replacement-member")).getErrorCode());
|
||||
assertTrue(rdsService.getDbCluster("replaced-cluster").getDbClusterMembers().isEmpty());
|
||||
DbInstance retry = rdsService.createDbInstance("replacement-member", "aurora-postgresql", "16.3",
|
||||
"admin", "password", "dbname", "db.r5.large", 20, false, null, null, "replaced-cluster",
|
||||
null, null, "dbname", "db.r5.large", 20, false, null, null, "replaced-cluster",
|
||||
null, false, false, null, Map.of(), List.of(), null, null, true,
|
||||
DbInstanceSettings.defaults(), true, 7001, false);
|
||||
assertEquals(7001, retry.getProxyPort());
|
||||
@@ -591,7 +597,7 @@ class RdsServiceTest {
|
||||
AtomicReference<Thread> deletionThread = new AtomicReference<>();
|
||||
try {
|
||||
Future<DbInstance> creation = workers.submit(() -> service.createDbInstance("race-member",
|
||||
"aurora-postgresql", "16.3", "admin", "password", "dbname", "db.r5.large", 20,
|
||||
"aurora-postgresql", "16.3", null, null, "dbname", "db.r5.large", 20,
|
||||
!initiallyEnabled, null, null, "race-cluster", null, false, false, null,
|
||||
Map.of(), List.of(), null, null, true, DbInstanceSettings.defaults()));
|
||||
assertTrue(published.await(5, TimeUnit.SECONDS));
|
||||
@@ -703,7 +709,7 @@ class RdsServiceTest {
|
||||
AtomicReference<Thread> modifierThread = new AtomicReference<>();
|
||||
try {
|
||||
Future<DbInstance> creation = workers.submit(() -> service.createDbInstance("race-member",
|
||||
"aurora-postgresql", "16.3", "admin", "password", "dbname", "db.r5.large", 20,
|
||||
"aurora-postgresql", "16.3", null, null, "dbname", "db.r5.large", 20,
|
||||
true, null, null, "race-cluster", null, false, false, null,
|
||||
Map.of(), List.of(), null, null, true, DbInstanceSettings.defaults()));
|
||||
assertTrue(published.await(5, TimeUnit.SECONDS));
|
||||
@@ -757,6 +763,76 @@ class RdsServiceTest {
|
||||
}
|
||||
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({"aurora-postgresql, username, true", "aurora-postgresql, username, false",
|
||||
"aurora-postgresql, password, true", "aurora-postgresql, password, false",
|
||||
"aurora-mysql, username, true", "aurora-mysql, username, false",
|
||||
"aurora-mysql, password, true", "aurora-mysql, password, false"})
|
||||
void clusterMemberRejectsExplicitMasterCredentialsBeforeAllocation(String engine, String field, boolean matching) {
|
||||
when(rdsConfig.mock()).thenReturn(true);
|
||||
when(rdsConfig.proxyMaxPort()).thenReturn(7001);
|
||||
DbCluster cluster = rdsService.createDbCluster("reject-member-credentials", engine, null,
|
||||
"clusteradmin", "ClusterPassword!", "dbname", false, null);
|
||||
String username = "username".equals(field) ? matching ? "clusteradmin" : "differentadmin" : null;
|
||||
String password = "password".equals(field) ? matching ? "ClusterPassword!" : "DifferentPassword!" : null;
|
||||
clearInvocations(containerManager, proxyManager, kmsService);
|
||||
AwsException rejected = assertThrows(AwsException.class, () -> rdsService.createDbInstance(
|
||||
"rejected-member", engine, null, username, password,
|
||||
"dbname", "db.serverless", 0, false, null, null, "reject-member-credentials"));
|
||||
assertEquals("InvalidParameterCombination", rejected.getErrorCode());
|
||||
assertEquals(400, rejected.getHttpStatus());
|
||||
assertEquals("The requested DB Instance will be a member of a DB Cluster. "
|
||||
+ ("password".equals(field) ? "Set master user password for the DB Cluster."
|
||||
: "Set master user name for the DB Cluster."), rejected.getMessage());
|
||||
assertEquals("DBInstanceNotFound", assertThrows(AwsException.class,
|
||||
() -> rdsService.getDbInstance("rejected-member")).getErrorCode());
|
||||
assertTrue(cluster.getDbClusterMembers().isEmpty());
|
||||
verifyNoInteractions(containerManager, proxyManager, kmsService);
|
||||
DbInstance retry = rdsService.createDbInstance("rejected-member", engine, null, null, null,
|
||||
"dbname", "db.serverless", 0, false, null, null, "reject-member-credentials");
|
||||
assertEquals(7001, retry.getProxyPort());
|
||||
assertEquals(cluster.getMasterUsername(), retry.getMasterUsername());
|
||||
assertEquals(cluster.getMasterPassword(), retry.getMasterPassword());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({"aurora-postgresql, false", "aurora-postgresql, true",
|
||||
"aurora-mysql, false", "aurora-mysql, true"})
|
||||
void acceptedPasswordForUnprovisionedClusterUpdatesCanonicalMembers(String engine, boolean mockMode) {
|
||||
when(rdsConfig.mock()).thenReturn(true);
|
||||
DbCluster cluster = rdsService.createDbCluster("offline-credentials", engine, null,
|
||||
"clusteradmin", "OldPassword!", "dbname", false, null);
|
||||
DbInstance member = rdsService.createDbInstance("offline-member", engine, null, null, null,
|
||||
"dbname", "db.serverless", 0, false, null, null, "offline-credentials");
|
||||
DbCluster other = rdsService.createDbCluster("other-offline-credentials", engine, null,
|
||||
"otheradmin", "OtherPassword!", "dbname", false, null);
|
||||
DbInstance sibling = rdsService.createDbInstance("other-offline-member", engine, null, null, null,
|
||||
"dbname", "db.serverless", 0, false, null, null, "other-offline-credentials");
|
||||
assertNull(cluster.getContainerId());
|
||||
assertNull(cluster.getDockerVolumeName());
|
||||
cluster.getDbClusterMembers().clear();
|
||||
when(rdsConfig.mock()).thenReturn(mockMode);
|
||||
clearInvocations(containerManager, proxyManager);
|
||||
rdsService.modifyDbCluster("offline-credentials", "AcceptedPassword!", null);
|
||||
assertEquals("AcceptedPassword!", cluster.getMasterPassword());
|
||||
assertEquals("AcceptedPassword!", member.getMasterPassword());
|
||||
assertEquals("AcceptedPassword!", rdsService.getDbInstance("offline-member").getMasterPassword());
|
||||
assertEquals("OtherPassword!", other.getMasterPassword());
|
||||
assertEquals("OtherPassword!", sibling.getMasterPassword());
|
||||
verify(containerManager, never()).rotateMasterPassword(any(), any(), any(), any(), any(), any());
|
||||
verify(proxyManager).updateMasterPassword("rds-resource:" + cluster.getDbClusterArn(), "AcceptedPassword!");
|
||||
verify(proxyManager).updateMasterPassword("rds-resource:" + member.getDbInstanceArn(), "AcceptedPassword!");
|
||||
verify(proxyManager, never()).updateMasterPassword(eq("rds-resource:" + sibling.getDbInstanceArn()), any());
|
||||
when(rdsConfig.mock()).thenReturn(false);
|
||||
clearInvocations(containerManager, proxyManager);
|
||||
DbInstance started = rdsService.ensureInstanceBackend("offline-member", "us-east-1");
|
||||
assertNotNull(started.getContainerId());
|
||||
verify(containerManager).tryStart(eq(cluster.getDbClusterArn()), eq("offline-credentials"), any(), any(),
|
||||
eq(cluster.getEngine()), any(), eq("clusteradmin"), eq("AcceptedPassword!"), eq("dbname"));
|
||||
verify(proxyManager).startProxy(eq("rds-resource:" + member.getDbInstanceArn()), any(), anyBoolean(),
|
||||
anyInt(), any(), anyInt(), any(), eq("clusteradmin"), eq("AcceptedPassword!"), eq("dbname"), any(), any());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({"aurora-postgresql,16.3,false,false", "aurora-postgresql,16.3,true,false",
|
||||
"aurora-mysql,8.0,false,false", "aurora-mysql,8.0,true,false",
|
||||
@@ -770,7 +846,7 @@ class RdsServiceTest {
|
||||
clearInvocations(proxyManager);
|
||||
|
||||
DbInstance member = rdsService.createDbInstance("iam-member", engine, version,
|
||||
"admin", "password", "dbname", "db.r5.large", 20, !clusterEnabled,
|
||||
null, null, "dbname", "db.r5.large", 20, !clusterEnabled,
|
||||
null, null, "iam-cluster", null, false, false, null,
|
||||
Map.of(), List.of(), null, null, true, DbInstanceSettings.defaults());
|
||||
|
||||
@@ -784,6 +860,105 @@ class RdsServiceTest {
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({"aurora-postgresql, username", "aurora-postgresql, password", "aurora-postgresql, both",
|
||||
"aurora-mysql, username", "aurora-mysql, password", "aurora-mysql, both"})
|
||||
void missingClusterCredentialsDoNotEraseExistingMemberDefaults(String engine, String missing) {
|
||||
when(rdsConfig.mock()).thenReturn(false);
|
||||
String clusterUser = "username".equals(missing) || "both".equals(missing) ? null : "clusteradmin";
|
||||
String clusterPassword = "password".equals(missing) || "both".equals(missing) ? null : "ClusterPassword!";
|
||||
rdsService.createDbCluster("incomplete-credentials-cluster", engine, null,
|
||||
clusterUser, clusterPassword, "dbname", true, null);
|
||||
clearInvocations(proxyManager);
|
||||
DbInstance member = rdsService.createDbInstance("incomplete-credentials-member", engine, null,
|
||||
null, null, "dbname", "db.serverless", 0, true,
|
||||
null, null, "incomplete-credentials-cluster");
|
||||
String expectedUser = clusterUser != null ? clusterUser : "root";
|
||||
String expectedPassword = clusterPassword;
|
||||
assertEquals(expectedUser, member.getMasterUsername());
|
||||
assertEquals(expectedPassword, member.getMasterPassword());
|
||||
if (expectedPassword != null) {
|
||||
assertTrue(rdsService.validateDbPassword("incomplete-credentials-member", expectedUser, expectedPassword));
|
||||
} else {
|
||||
assertFalse(rdsService.validateDbPassword("incomplete-credentials-member", expectedUser, "invented-password"));
|
||||
}
|
||||
verify(proxyManager).startProxy(eq("rds-resource:" + member.getDbInstanceArn()), any(), eq(true),
|
||||
anyInt(), any(), anyInt(), any(), eq(expectedUser), eq(expectedPassword),
|
||||
eq("dbname"), any(), any());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"aurora-postgresql", "aurora-mysql"})
|
||||
void memberCreationUsesCredentialsRotatedBeforePublication(String engine) throws Exception {
|
||||
when(rdsConfig.mock()).thenReturn(false);
|
||||
DbCluster cluster = rdsService.createDbCluster("credential-race-cluster", engine, null,
|
||||
"clusteradmin", "OriginalPassword!", "dbname", true, null);
|
||||
clearInvocations(proxyManager);
|
||||
CountDownLatch endpoint = new CountDownLatch(1);
|
||||
CountDownLatch release = new CountDownLatch(1);
|
||||
AtomicBoolean first = new AtomicBoolean(true);
|
||||
when(rdsConfig.endpointHost()).thenAnswer(call -> {
|
||||
if (first.compareAndSet(true, false)) {
|
||||
endpoint.countDown();
|
||||
assertTrue(release.await(5, TimeUnit.SECONDS));
|
||||
}
|
||||
return Optional.empty();
|
||||
});
|
||||
ExecutorService worker = Executors.newSingleThreadExecutor();
|
||||
try {
|
||||
Future<DbInstance> creation = worker.submit(() -> rdsService.createDbInstance("credential-race-member",
|
||||
engine, null, null, null, "dbname", "db.serverless", 0, true, null, null,
|
||||
"credential-race-cluster"));
|
||||
assertTrue(endpoint.await(5, TimeUnit.SECONDS));
|
||||
rdsService.modifyDbCluster("credential-race-cluster", "RotatedPassword!", null);
|
||||
assertEquals("RotatedPassword!", cluster.getMasterPassword());
|
||||
release.countDown();
|
||||
DbInstance member = creation.get(5, TimeUnit.SECONDS);
|
||||
assertEquals("clusteradmin", member.getMasterUsername());
|
||||
assertEquals("RotatedPassword!", member.getMasterPassword());
|
||||
assertEquals("RotatedPassword!", rdsService.getDbInstance("credential-race-member").getMasterPassword());
|
||||
verify(proxyManager).startProxy(eq("rds-resource:" + member.getDbInstanceArn()), any(), eq(true),
|
||||
anyInt(), any(), anyInt(), any(), eq("clusteradmin"), eq("RotatedPassword!"),
|
||||
eq("dbname"), any(), any());
|
||||
} finally {
|
||||
release.countDown();
|
||||
worker.shutdownNow();
|
||||
assertTrue(worker.awaitTermination(5, TimeUnit.SECONDS));
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({
|
||||
"aurora-postgresql, false, false", "aurora-postgresql, false, true",
|
||||
"aurora-postgresql, true, false", "aurora-postgresql, true, true",
|
||||
"aurora-mysql, false, false", "aurora-mysql, false, true",
|
||||
"aurora-mysql, true, false", "aurora-mysql, true, true"
|
||||
})
|
||||
void newClusterMemberUsesClusterMasterCredentials(String engine, boolean mockMode, boolean conflicting) {
|
||||
when(rdsConfig.mock()).thenReturn(mockMode);
|
||||
DbCluster cluster = rdsService.createDbCluster("credentials-cluster", engine, null,
|
||||
"clusteradmin", "ClusterPassword!", "dbname", true, null);
|
||||
clearInvocations(proxyManager);
|
||||
if (conflicting) {
|
||||
AwsException rejected = assertThrows(AwsException.class, () -> rdsService.createDbInstance(
|
||||
"credentials-member", engine, null, "differentuser", "DifferentPassword!",
|
||||
"dbname", "db.serverless", 0, true, null, null, "credentials-cluster"));
|
||||
assertEquals("InvalidParameterCombination", rejected.getErrorCode());
|
||||
}
|
||||
DbInstance member = rdsService.createDbInstance("credentials-member", engine, null, null, null,
|
||||
"dbname", "db.serverless", 0, true, null, null, "credentials-cluster");
|
||||
assertEquals(cluster.getMasterUsername(), member.getMasterUsername());
|
||||
assertEquals(cluster.getMasterPassword(), member.getMasterPassword());
|
||||
assertEquals("clusteradmin", rdsService.getDbInstance("credentials-member").getMasterUsername());
|
||||
if (mockMode) {
|
||||
verifyNoInteractions(proxyManager);
|
||||
} else {
|
||||
verify(proxyManager).startProxy(eq("rds-resource:" + member.getDbInstanceArn()),
|
||||
any(), eq(true), anyInt(), any(), anyInt(), any(),
|
||||
eq("clusteradmin"), eq("ClusterPassword!"), eq("dbname"), any(), any());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void createDbInstanceLeavesPostgresIamTokensUnboundWhenTheBindingIsTurnedOff() {
|
||||
when(rdsConfig.iamTokenEndpointBinding()).thenReturn(false);
|
||||
@@ -1120,7 +1295,7 @@ class RdsServiceTest {
|
||||
"admin", "password", "dbname", false, null);
|
||||
|
||||
DbInstance member = rdsService.createDbInstance("aurora-member", "aurora-postgresql", "16.3",
|
||||
"admin", "password", "dbname", "db.r5.large",
|
||||
null, null, "dbname", "db.r5.large",
|
||||
20, false, null, null, "aurora-cluster", null, false, false, null,
|
||||
Map.of(), List.of(), null, null, true, DbInstanceSettings.defaults());
|
||||
|
||||
@@ -1960,7 +2135,7 @@ class RdsServiceTest {
|
||||
String arn = cluster.getMasterUserSecretArn();
|
||||
String key = cluster.getMasterUserSecretKmsKeyId();
|
||||
DbInstance member = service.createDbInstance("optout-member", engine, version,
|
||||
"admin", oldPassword, "dbname", "db.r5.large", 20, false, null, null, "optout");
|
||||
null, null, "dbname", "db.r5.large", 20, false, null, null, "optout");
|
||||
secrets.replicateSecretToRegions(arn,
|
||||
List.of(new SecretsManagerService.ReplicaRegion("us-west-2", null)), false, "us-east-1");
|
||||
String secretValue = secrets.getSecretValue(arn, null, null, "us-east-1").getSecretString();
|
||||
@@ -2026,7 +2201,7 @@ class RdsServiceTest {
|
||||
String arn = cluster.getMasterUserSecretArn();
|
||||
String oldPassword = cluster.getMasterPassword();
|
||||
DbInstance member = service.createDbInstance("retry-member", engine, version,
|
||||
"admin", oldPassword, "dbname", "db.r5.large", 20, false, null, null, "optout-retry");
|
||||
null, null, "dbname", "db.r5.large", 20, false, null, null, "optout-retry");
|
||||
secrets.replicateSecretToRegions(arn,
|
||||
List.of(new SecretsManagerService.ReplicaRegion("us-west-2", null)), false, "us-east-1");
|
||||
service.modifyDbCluster("optout-retry", "replacement-password", null,
|
||||
@@ -2064,7 +2239,7 @@ class RdsServiceTest {
|
||||
cluster.setMasterUserSecretStatus("active");
|
||||
cluster.setMasterUserSecretKmsKeyId("managed-key");
|
||||
DbInstance member = rdsService.createDbInstance("missing-secrets-member", engine, version,
|
||||
"admin", "original-password", "dbname", "db.r5.large", 20, false, null, null, "missing-secrets");
|
||||
null, null, "dbname", "db.r5.large", 20, false, null, null, "missing-secrets");
|
||||
clearInvocations(containerManager, proxyManager);
|
||||
|
||||
AwsException error = assertThrows(AwsException.class, () -> rdsService.modifyDbCluster(
|
||||
@@ -2115,6 +2290,48 @@ class RdsServiceTest {
|
||||
return new SecretsManagerService(factory, config, regionResolver, null, new ObjectMapper(), null);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({"aurora-postgresql,false", "aurora-postgresql,true",
|
||||
"aurora-mysql,false", "aurora-mysql,true"})
|
||||
void clusterMemberDoesNotCreateItsOwnManagedSecret(String engine, boolean mockMode) {
|
||||
when(config.services().rds().mock()).thenReturn(mockMode);
|
||||
SecretsManagerService secrets = mock(SecretsManagerService.class);
|
||||
Secret unexpected = new Secret();
|
||||
unexpected.setArn("arn:aws:secretsmanager:us-east-1:123456789012:secret:unexpected-member");
|
||||
when(secrets.createSecret(any(), any(), any(), any(), any(), any(), any(), any())).thenReturn(unexpected);
|
||||
RdsService service = newService(containerManager, proxyManager,
|
||||
new InMemoryStorage<>(), new InMemoryStorage<>(),
|
||||
new InMemoryStorage<>(), new InMemoryStorage<>(), secrets);
|
||||
service.createDbCluster("managed-member-cluster", engine, null,
|
||||
"clusteradmin", "ClusterPassword!", "dbname", false, null);
|
||||
clearInvocations(kmsService);
|
||||
DbInstance member = service.createDbInstance("managed-member", engine, null,
|
||||
null, null, "dbname", "db.r5.large", 20, false, null, null,
|
||||
"managed-member-cluster", true, null);
|
||||
assertEquals("clusteradmin", member.getMasterUsername());
|
||||
assertEquals("ClusterPassword!", member.getMasterPassword());
|
||||
assertNull(member.getMasterUserSecretArn());
|
||||
assertNull(member.getMasterUserSecretStatus());
|
||||
assertNull(member.getMasterUserSecretKmsKeyId());
|
||||
verifyNoInteractions(secrets, kmsService);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"aurora-postgresql", "aurora-mysql"})
|
||||
void clusterMemberSecretFlagDoesNotRequireSecretsManager(String engine) {
|
||||
RdsService service = newService(containerManager, proxyManager,
|
||||
new InMemoryStorage<>(), new InMemoryStorage<>(),
|
||||
new InMemoryStorage<>(), new InMemoryStorage<>(), (SecretsManagerService) null);
|
||||
service.createDbCluster("no-member-secrets", engine, null,
|
||||
"clusteradmin", "ClusterPassword!", "dbname", false, null);
|
||||
DbInstance member = assertDoesNotThrow(() -> service.createDbInstance("member-no-secrets", engine, null,
|
||||
null, null, "dbname", "db.r5.large", 20, false, null, null,
|
||||
"no-member-secrets", true, null));
|
||||
assertEquals("clusteradmin", member.getMasterUsername());
|
||||
assertEquals("ClusterPassword!", member.getMasterPassword());
|
||||
assertNull(member.getMasterUserSecretArn());
|
||||
}
|
||||
|
||||
@Test
|
||||
void modifyDbClusterRekeysExistingManagedSecret() {
|
||||
when(config.services().rds().mock()).thenReturn(true);
|
||||
@@ -2475,7 +2692,7 @@ class RdsServiceTest {
|
||||
cluster.setContainerId("container-1");
|
||||
cluster.setDockerVolumeName("volume-1");
|
||||
DbInstance member = rdsService.createDbInstance("member-1", "aurora-postgresql", "16.3",
|
||||
"admin", "original-password", "dbname", "db.r5.large",
|
||||
null, null, "dbname", "db.r5.large",
|
||||
20, false, null, null, "cluster1");
|
||||
|
||||
DbCluster modified = rdsService.modifyDbCluster("cluster1", "rotated-password", null);
|
||||
@@ -2501,7 +2718,7 @@ class RdsServiceTest {
|
||||
cluster.setContainerId("container-1");
|
||||
cluster.setDockerVolumeName("volume-1");
|
||||
DbInstance member = rdsService.createDbInstance("member-1", "aurora-postgresql", "16.3",
|
||||
"admin", "original-password", "dbname", "db.r5.large",
|
||||
null, null, "dbname", "db.r5.large",
|
||||
20, false, null, null, "cluster1");
|
||||
cluster.getDbClusterMembers().add("missing-member");
|
||||
cluster.getDbClusterMembers().clear();
|
||||
@@ -2533,7 +2750,7 @@ class RdsServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void modifyDbClusterPasswordRotationSkipsBackendInMockMode() {
|
||||
void modifyDbClusterPasswordChangePublishesCredentialsWithoutBackendInMockMode() {
|
||||
when(config.services().rds().mock()).thenReturn(true);
|
||||
rdsService.createDbCluster("cluster1", "aurora-postgresql", "16.3",
|
||||
"admin", "original-password", "dbname", false, null);
|
||||
@@ -2543,7 +2760,7 @@ class RdsServiceTest {
|
||||
assertEquals("rotated-password", modified.getMasterPassword());
|
||||
verify(containerManager, never()).rotateMasterPassword(
|
||||
anyString(), anyString(), any(), anyString(), anyString(), anyString());
|
||||
verify(proxyManager, never()).updateMasterPassword(anyString(), anyString());
|
||||
verify(proxyManager).updateMasterPassword("rds-resource:" + modified.getDbClusterArn(), "rotated-password");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -3224,7 +3441,7 @@ class RdsServiceTest {
|
||||
}
|
||||
|
||||
private static DbInstance createMember(RdsService service, String id, String instanceClass, String clusterId) {
|
||||
return service.createDbInstance(id, "aurora-postgresql", "16.3", "admin", "password", "dbname",
|
||||
return service.createDbInstance(id, "aurora-postgresql", "16.3", null, null, "dbname",
|
||||
instanceClass, 20, false, null, null, clusterId);
|
||||
}
|
||||
|
||||
@@ -3365,7 +3582,7 @@ class RdsServiceTest {
|
||||
"admin", "password", "dbname", false, null);
|
||||
|
||||
DbInstance instance = rdsService.createDbInstance("inst1", "aurora-postgresql", "16.3",
|
||||
"admin", "password", "dbname", "db.serverless",
|
||||
null, null, "dbname", "db.serverless",
|
||||
0, false, null, null, "cluster1");
|
||||
|
||||
assertEquals(DbInstanceStatus.AVAILABLE, instance.getStatus());
|
||||
@@ -3459,7 +3676,7 @@ class RdsServiceTest {
|
||||
DbCluster cluster = rdsService.createDbCluster("probe-cluster", "aurora-postgresql", "16.3",
|
||||
"admin", "password", "dbname", false, null);
|
||||
DbInstance member = rdsService.createDbInstance("probe-member", "aurora-postgresql", "16.3",
|
||||
"admin", "password", "dbname", "db.serverless",
|
||||
null, null, "dbname", "db.serverless",
|
||||
0, false, null, null, "probe-cluster");
|
||||
|
||||
assertEquals(DbInstanceStatus.AVAILABLE, cluster.getStatus());
|
||||
@@ -3557,7 +3774,7 @@ class RdsServiceTest {
|
||||
DbCluster cluster = rdsService.createDbCluster("probe-cluster", "aurora-postgresql", "16.3",
|
||||
"admin", "password", "dbname", false, null);
|
||||
DbInstance member = rdsService.createDbInstance("probe-member", "aurora-postgresql", "16.3",
|
||||
"admin", "password", "dbname", "db.serverless",
|
||||
null, null, "dbname", "db.serverless",
|
||||
0, false, null, null, "probe-cluster");
|
||||
|
||||
cluster.setStatus(DbInstanceStatus.FAILED);
|
||||
@@ -3648,7 +3865,7 @@ class RdsServiceTest {
|
||||
initialService.createDbCluster("cluster1", "aurora-postgresql", "16.3",
|
||||
"admin", "secret", "app", false, null, null, null, false);
|
||||
initialService.createDbInstance("member1", "aurora-postgresql", "16.3",
|
||||
"admin", "secret", "app", "db.t3.medium",
|
||||
null, null, "app", "db.t3.medium",
|
||||
20, false, null, null, "cluster1", null, false);
|
||||
|
||||
RdsContainerManager daemonlessContainerManager = mock(RdsContainerManager.class);
|
||||
@@ -3801,7 +4018,7 @@ class RdsServiceTest {
|
||||
rdsService.createDbCluster("aurora-cluster", "aurora-postgresql", "16.3",
|
||||
"admin", "password", "dbname", false, null);
|
||||
rdsService.createDbInstance("aurora-member", "aurora-postgresql", "16.3",
|
||||
"admin", "password", "dbname", "db.r6g.large",
|
||||
null, null, "dbname", "db.r6g.large",
|
||||
20, false, null, null, "aurora-cluster", null, false, false, null,
|
||||
Map.of(), List.of(), null, null, true, DbInstanceSettings.defaults());
|
||||
AwsException member = assertThrows(AwsException.class, () -> rdsService.stopDbInstance("aurora-member", null));
|
||||
@@ -3832,7 +4049,7 @@ class RdsServiceTest {
|
||||
DbCluster created = rdsService.createDbCluster("aurora-cluster", "aurora-postgresql", "16.3",
|
||||
"admin", "password", "dbname", false, null);
|
||||
rdsService.createDbInstance("aurora-member", "aurora-postgresql", "16.3",
|
||||
"admin", "password", "dbname", "db.r6g.large",
|
||||
null, null, "dbname", "db.r6g.large",
|
||||
20, false, null, null, "aurora-cluster", null, false, false, null,
|
||||
Map.of(), List.of(), null, null, true, DbInstanceSettings.defaults());
|
||||
int port = created.getEndpoint().port();
|
||||
@@ -4576,7 +4793,7 @@ class RdsServiceTest {
|
||||
DbCluster cluster = initialService.createDbCluster("cluster1", "aurora-postgresql", "16.3",
|
||||
"admin", "secret", "app", false, null, null, null, false);
|
||||
DbInstance member = initialService.createDbInstance("member1", "aurora-postgresql", "16.3",
|
||||
"admin", "secret", "app", "db.t3.medium",
|
||||
null, null, "app", "db.t3.medium",
|
||||
20, false, null, null, "cluster1", null, false);
|
||||
|
||||
RdsContainerManager restoredContainerManager = mock(RdsContainerManager.class);
|
||||
@@ -5750,11 +5967,11 @@ class RdsServiceTest {
|
||||
DbCluster cluster = rdsService.createDbCluster("retry-cluster", "aurora-postgresql", "16.3",
|
||||
"admin", "password", "dbname", false, null);
|
||||
DbInstance first = rdsService.createDbInstance("first-member", "aurora-postgresql", "16.3",
|
||||
"admin", "password", "dbname", "db.serverless", 0, false, null, null, "retry-cluster");
|
||||
null, null, "dbname", "db.serverless", 0, false, null, null, "retry-cluster");
|
||||
DbInstance second = rdsService.createDbInstance("second-member", "aurora-postgresql", "16.3",
|
||||
"admin", "password", "dbname", "db.serverless", 0, false, null, null, "retry-cluster");
|
||||
null, null, "dbname", "db.serverless", 0, false, null, null, "retry-cluster");
|
||||
DbInstance stopped = rdsService.createDbInstance("stopped-member", "aurora-postgresql", "16.3",
|
||||
"admin", "password", "dbname", "db.serverless", 0, false, null, null, "retry-cluster");
|
||||
null, null, "dbname", "db.serverless", 0, false, null, null, "retry-cluster");
|
||||
stopped.setStatus(DbInstanceStatus.STOPPED);
|
||||
first.setEndpoint(null);
|
||||
first.setProxyPort(0);
|
||||
@@ -5793,9 +6010,9 @@ class RdsServiceTest {
|
||||
DbCluster cluster = rdsService.createDbCluster("log-cluster", "aurora-postgresql", "16.3",
|
||||
"admin", "password", "dbname", false, null);
|
||||
DbInstance first = rdsService.createDbInstance("log-first", "aurora-postgresql", "16.3",
|
||||
"admin", "password", "dbname", "db.serverless", 0, false, null, null, "log-cluster");
|
||||
null, null, "dbname", "db.serverless", 0, false, null, null, "log-cluster");
|
||||
DbInstance second = rdsService.createDbInstance("log-second", "aurora-postgresql", "16.3",
|
||||
"admin", "password", "dbname", "db.serverless", 0, false, null, null, "log-cluster");
|
||||
null, null, "dbname", "db.serverless", 0, false, null, null, "log-cluster");
|
||||
IllegalStateException firstFailure = new IllegalStateException("first relay failed");
|
||||
IllegalStateException secondFailure = new IllegalStateException("second relay failed");
|
||||
IllegalStateException afterRecoveryFailure = new IllegalStateException("relay failed again");
|
||||
@@ -5835,7 +6052,7 @@ class RdsServiceTest {
|
||||
DbCluster cluster = rdsService.createDbCluster("restart-log-cluster", "aurora-postgresql", "16.3",
|
||||
"admin", "password", "dbname", false, null);
|
||||
DbInstance member = rdsService.createDbInstance("restart-log-member", "aurora-postgresql", "16.3",
|
||||
"admin", "password", "dbname", "db.serverless", 0, false, null, null,
|
||||
null, null, "dbname", "db.serverless", 0, false, null, null,
|
||||
"restart-log-cluster");
|
||||
IllegalStateException firstFailure = new IllegalStateException("first relay failed");
|
||||
IllegalStateException ongoingFailure = new IllegalStateException("relay still failed after restart");
|
||||
@@ -5877,9 +6094,9 @@ class RdsServiceTest {
|
||||
DbCluster cluster = rdsService.createDbCluster("control-cluster", "aurora-postgresql", "16.3",
|
||||
"admin", "password", "dbname", false, null);
|
||||
DbInstance first = rdsService.createDbInstance("control-first", "aurora-postgresql", "16.3",
|
||||
"admin", "password", "dbname", "db.serverless", 0, false, null, null, "control-cluster");
|
||||
null, null, "dbname", "db.serverless", 0, false, null, null, "control-cluster");
|
||||
DbInstance second = rdsService.createDbInstance("control-second", "aurora-postgresql", "16.3",
|
||||
"admin", "password", "dbname", "db.serverless", 0, false, null, null, "control-cluster");
|
||||
null, null, "dbname", "db.serverless", 0, false, null, null, "control-cluster");
|
||||
doThrow(new IllegalStateException("temporary member relay error")).doNothing()
|
||||
.when(proxyManager).startProxy(eq("rds-resource:" + first.getDbInstanceArn()),
|
||||
any(), anyBoolean(), anyInt(), any(), anyInt(), any(), any(), any(), any(), any(), any());
|
||||
@@ -8932,10 +9149,10 @@ class RdsServiceTest {
|
||||
rdsService.createDbCluster("aurora", "aurora-postgresql", null, "admin", "secret99password",
|
||||
null, false, null);
|
||||
rdsService.createDbInstance("member", "aurora-postgresql", "16",
|
||||
"admin", "password", null, "db.t3.medium",
|
||||
null, null, null, "db.t3.medium",
|
||||
20, false, null, null, "aurora", null, false);
|
||||
rdsService.createDbInstance("member-2", null, "16",
|
||||
"admin", "password", null, "db.t3.medium",
|
||||
null, null, null, "db.t3.medium",
|
||||
20, false, null, null, "aurora", null, false);
|
||||
rdsService.createDbInstance("plain", "postgres", "13",
|
||||
"admin", "password", "dbname", "db.t3.micro",
|
||||
@@ -9831,7 +10048,7 @@ class RdsServiceTest {
|
||||
rdsService.createDbCluster("Mixed-Cluster", "aurora-postgresql", "16.3", "admin", "password", "appdb", false, null);
|
||||
for (String member : List.of("Writer-1", "Reader-1")) {
|
||||
rdsService.createDbInstance(member, "aurora-postgresql", "16.3",
|
||||
"admin", "password", "appdb", "db.r5.large",
|
||||
null, null, "appdb", "db.r5.large",
|
||||
20, false, null, null, "Mixed-Cluster", null, false, false, null,
|
||||
Map.of(), List.of(), null, null, true, DbInstanceSettings.defaults());
|
||||
}
|
||||
|
||||
@@ -5,15 +5,24 @@ import io.github.hectorvent.floci.services.rds.model.DatabaseEngine;
|
||||
import io.github.hectorvent.floci.testutil.FreePorts;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.time.Duration;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
@@ -24,6 +33,119 @@ import static org.mockito.Mockito.when;
|
||||
|
||||
class RdsProxyManagerTest {
|
||||
|
||||
@Test
|
||||
void interruptedRetirementRetainsOwnershipAndCanBeRetried() throws Exception {
|
||||
RdsProxyManager manager = new RdsProxyManager(
|
||||
mock(RdsSigV4Validator.class), mock(RdsProxyTlsCertificates.class), testConfig());
|
||||
RdsAuthProxy proxy = authProxy("proxy");
|
||||
CountDownLatch release = new CountDownLatch(1);
|
||||
Thread accepting = blockedRetirement(release);
|
||||
AtomicReference<RuntimeException> failure = new AtomicReference<>();
|
||||
AtomicBoolean interrupted = new AtomicBoolean();
|
||||
ConcurrentHashMap<String, RdsAuthProxy> owned = registry(manager);
|
||||
try (ServerSocket listener = reusableSocket(0)) {
|
||||
int port = listener.getLocalPort();
|
||||
setServerSocket(proxy, listener);
|
||||
setAcceptThread(proxy, accepting);
|
||||
owned.put("proxy", proxy);
|
||||
Thread stopping = Thread.ofVirtual().start(() -> {
|
||||
Thread.currentThread().interrupt();
|
||||
try {
|
||||
manager.stopProxy("proxy");
|
||||
} catch (RuntimeException e) {
|
||||
failure.set(e);
|
||||
} finally {
|
||||
interrupted.set(Thread.currentThread().isInterrupted());
|
||||
}
|
||||
});
|
||||
assertTrue(stopping.join(Duration.ofSeconds(2)));
|
||||
assertInstanceOf(InterruptedException.class, failure.get().getCause());
|
||||
assertTrue(interrupted.get());
|
||||
assertSame(proxy, owned.get("proxy"));
|
||||
assertPortAvailable(port);
|
||||
release.countDown();
|
||||
assertTrue(accepting.join(Duration.ofSeconds(2)));
|
||||
manager.stopProxy("proxy");
|
||||
assertFalse(owned.containsKey("proxy"));
|
||||
assertPortAvailable(port);
|
||||
} finally {
|
||||
release.countDown();
|
||||
assertTrue(accepting.join(Duration.ofSeconds(2)));
|
||||
manager.stopAll();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void retirementTimeoutRetainsOwnershipAndCanBeRetried() throws Exception {
|
||||
RdsProxyManager manager = new RdsProxyManager(
|
||||
mock(RdsSigV4Validator.class), mock(RdsProxyTlsCertificates.class), testConfig());
|
||||
RdsAuthProxy proxy = authProxy("proxy");
|
||||
proxy.acceptRetirementTimeout = Duration.ofMillis(10);
|
||||
CountDownLatch release = new CountDownLatch(1);
|
||||
Thread accepting = blockedRetirement(release);
|
||||
ConcurrentHashMap<String, RdsAuthProxy> owned = registry(manager);
|
||||
try (ServerSocket listener = reusableSocket(0)) {
|
||||
int port = listener.getLocalPort();
|
||||
setServerSocket(proxy, listener);
|
||||
setAcceptThread(proxy, accepting);
|
||||
owned.put("proxy", proxy);
|
||||
assertThrows(IllegalStateException.class, () -> manager.stopProxy("proxy"));
|
||||
assertSame(proxy, owned.get("proxy"));
|
||||
assertPortAvailable(port);
|
||||
release.countDown();
|
||||
assertTrue(accepting.join(Duration.ofSeconds(2)));
|
||||
manager.stopProxy("proxy");
|
||||
assertFalse(owned.containsKey("proxy"));
|
||||
assertPortAvailable(port);
|
||||
} finally {
|
||||
release.countDown();
|
||||
assertTrue(accepting.join(Duration.ofSeconds(2)));
|
||||
manager.stopAll();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void samePortRebootDoesNotWaitForASeparateReleasePoll() throws IOException {
|
||||
RdsProxyManager manager = new RdsProxyManager(
|
||||
mock(RdsSigV4Validator.class), mock(RdsProxyTlsCertificates.class), testConfig());
|
||||
int port = FreePorts.anyInterfacePort();
|
||||
try {
|
||||
for (int attempt = 0; attempt < 100; attempt++) {
|
||||
start(manager, "reboot", port);
|
||||
try (Socket admitted = new Socket("127.0.0.1", port)) {
|
||||
admitted.setSoTimeout(1000);
|
||||
ByteArrayOutputStream startup = new ByteArrayOutputStream();
|
||||
DataOutputStream fields = new DataOutputStream(startup);
|
||||
fields.writeInt(196608);
|
||||
fields.writeBytes("user");
|
||||
fields.writeByte(0);
|
||||
fields.writeBytes("admin");
|
||||
fields.writeByte(0);
|
||||
fields.writeBytes("database");
|
||||
fields.writeByte(0);
|
||||
fields.writeBytes("app");
|
||||
fields.writeByte(0);
|
||||
fields.writeByte(0);
|
||||
DataOutputStream request = new DataOutputStream(admitted.getOutputStream());
|
||||
request.writeInt(startup.size() + 4);
|
||||
startup.writeTo(request);
|
||||
request.flush();
|
||||
DataInputStream response = new DataInputStream(admitted.getInputStream());
|
||||
assertEquals('R', response.readUnsignedByte());
|
||||
assertEquals(8, response.readInt());
|
||||
assertEquals(3, response.readInt());
|
||||
manager.stopProxy("reboot");
|
||||
start(manager, "reboot", port);
|
||||
assertPortUnavailable(port);
|
||||
manager.stopProxy("reboot");
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
manager.stopAll();
|
||||
}
|
||||
assertPortAvailable(port);
|
||||
}
|
||||
|
||||
@Test
|
||||
void replacingProxyStopsPreviousListenerAndKeepsReplacementOwned() throws IOException {
|
||||
RdsProxyManager manager = new RdsProxyManager(
|
||||
@@ -208,6 +330,23 @@ class RdsProxyManagerTest {
|
||||
return (String) field.get(proxy);
|
||||
}
|
||||
|
||||
/** Holds retirement after socket close without depending on native scheduler timing. */
|
||||
private static Thread blockedRetirement(CountDownLatch release) {
|
||||
return Thread.ofVirtual().start(() -> {
|
||||
try {
|
||||
release.await();
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static void setAcceptThread(RdsAuthProxy proxy, Thread thread) throws Exception {
|
||||
Field field = RdsAuthProxy.class.getDeclaredField("acceptThread");
|
||||
field.setAccessible(true);
|
||||
field.set(proxy, thread);
|
||||
}
|
||||
|
||||
private static void setServerSocket(RdsAuthProxy proxy, ServerSocket socket) throws Exception {
|
||||
Field field = RdsAuthProxy.class.getDeclaredField("serverSocket");
|
||||
field.setAccessible(true);
|
||||
|
||||
Reference in new issue
Block a user