diff --git a/compatibility-tests/sdk-test-go/.dockerignore b/compatibility-tests/sdk-test-go/.dockerignore new file mode 100644 index 0000000000..bf0b144501 --- /dev/null +++ b/compatibility-tests/sdk-test-go/.dockerignore @@ -0,0 +1,3 @@ +go.mod +go.sum +test-results.xml diff --git a/compatibility-tests/sdk-test-go/internal/testutil/fixtures.go b/compatibility-tests/sdk-test-go/internal/testutil/fixtures.go index 05801120c9..7d6ca75b96 100644 --- a/compatibility-tests/sdk-test-go/internal/testutil/fixtures.go +++ b/compatibility-tests/sdk-test-go/internal/testutil/fixtures.go @@ -19,6 +19,7 @@ import ( "github.com/aws/aws-sdk-go-v2/service/kinesis" "github.com/aws/aws-sdk-go-v2/service/kms" "github.com/aws/aws-sdk-go-v2/service/lambda" + "github.com/aws/aws-sdk-go-v2/service/rds" "github.com/aws/aws-sdk-go-v2/service/s3" "github.com/aws/aws-sdk-go-v2/service/secretsmanager" "github.com/aws/aws-sdk-go-v2/service/sns" @@ -126,6 +127,30 @@ func PipesClient() *pipes.Client { return pipes.NewFromConfig(Config()) } +// RDSClient returns a new RDS client. +func RDSClient() *rds.Client { + return rds.NewFromConfig(Config()) +} + +// ProxyHost returns the host to use for direct TCP connections to RDS/ElastiCache proxies. +func ProxyHost() string { + ep := Endpoint() + // Strip scheme — ep is "http://host:port" or "http://host" + if len(ep) > 7 && ep[:7] == "http://" { + ep = ep[7:] + } + // Strip port if present + if i := len(ep) - 1; i > 0 { + for i >= 0 && ep[i] != ':' { + i-- + } + if i > 0 { + return ep[:i] + } + } + return ep +} + // MinimalZip returns a minimal Lambda deployment zip with a Node.js handler. func MinimalZip() []byte { code := `exports.handler = async (event) => { diff --git a/compatibility-tests/sdk-test-go/tests/rds_test.go b/compatibility-tests/sdk-test-go/tests/rds_test.go new file mode 100644 index 0000000000..5849b0c344 --- /dev/null +++ b/compatibility-tests/sdk-test-go/tests/rds_test.go @@ -0,0 +1,216 @@ +package tests + +import ( + "context" + "database/sql" + "fmt" + "testing" + "time" + + "floci-sdk-test-go/internal/testutil" + + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/rds" + _ "github.com/lib/pq" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +const ( + rdsUsername = "admin" + rdsPassword = "secret123" + rdsDatabase = "app" +) + +// TestRDSInstance covers management-plane CRUD for a DB instance (fix #567 — delete +// cleans up properly) and direct proxy connectivity (fix #503 — non-master pass-through). +func TestRDSInstance(t *testing.T) { + ctx := context.Background() + svc := testutil.RDSClient() + instanceId := fmt.Sprintf("go-rds-%d", time.Now().UnixMilli()%100000) + + var proxyPort int32 + + t.Cleanup(func() { + svc.DeleteDBInstance(ctx, &rds.DeleteDBInstanceInput{ //nolint:errcheck + DBInstanceIdentifier: aws.String(instanceId), + SkipFinalSnapshot: aws.Bool(true), + }) + }) + + t.Run("CreateDBInstance", func(t *testing.T) { + out, err := svc.CreateDBInstance(ctx, &rds.CreateDBInstanceInput{ + DBInstanceIdentifier: aws.String(instanceId), + DBInstanceClass: aws.String("db.t3.micro"), + Engine: aws.String("postgres"), + MasterUsername: aws.String(rdsUsername), + MasterUserPassword: aws.String(rdsPassword), + DBName: aws.String(rdsDatabase), + AllocatedStorage: aws.Int32(20), + EnableIAMDatabaseAuthentication: aws.Bool(false), + }) + require.NoError(t, err) + require.NotNil(t, out.DBInstance) + assert.Equal(t, instanceId, aws.ToString(out.DBInstance.DBInstanceIdentifier)) + assert.Equal(t, "postgres", aws.ToString(out.DBInstance.Engine)) + + proxyPort = aws.ToInt32(out.DBInstance.Endpoint.Port) + assert.Greater(t, proxyPort, int32(0)) + }) + + t.Run("DescribeDBInstance", func(t *testing.T) { + out, err := svc.DescribeDBInstances(ctx, &rds.DescribeDBInstancesInput{ + DBInstanceIdentifier: aws.String(instanceId), + }) + require.NoError(t, err) + require.Len(t, out.DBInstances, 1) + + inst := out.DBInstances[0] + assert.Equal(t, instanceId, aws.ToString(inst.DBInstanceIdentifier)) + assert.Equal(t, "postgres", aws.ToString(inst.Engine)) + assert.Equal(t, rdsUsername, aws.ToString(inst.MasterUsername)) + }) + + // Connect master user via the RDS proxy using plain password. + t.Run("ConnectMasterUser", func(t *testing.T) { + if proxyPort == 0 { + t.Skip("proxy port not set — CreateDBInstance step skipped") + } + conn := awaitPostgresConn(t, testutil.ProxyHost(), int(proxyPort), rdsUsername, rdsPassword, rdsDatabase) + defer conn.Close() + + var result int + require.NoError(t, conn.QueryRowContext(ctx, "SELECT 1").Scan(&result)) + assert.Equal(t, 1, result) + }) + + // Fix #503: non-master users must pass through to the backend without + // the proxy intercepting or rejecting their credentials. + t.Run("ConnectNonMasterUser", func(t *testing.T) { + if proxyPort == 0 { + t.Skip("proxy port not set — CreateDBInstance step skipped") + } + masterConn := awaitPostgresConn(t, testutil.ProxyHost(), int(proxyPort), rdsUsername, rdsPassword, rdsDatabase) + defer masterConn.Close() + + _, err := masterConn.ExecContext(ctx, + "CREATE USER rds_testuser WITH PASSWORD 'testpass' LOGIN") + require.NoError(t, err) + + userConn, err := openPostgresConn(testutil.ProxyHost(), int(proxyPort), "rds_testuser", "testpass", rdsDatabase) + require.NoError(t, err, "non-master user should connect via proxy pass-through") + defer userConn.Close() + + var result int + require.NoError(t, userConn.QueryRowContext(ctx, "SELECT 1").Scan(&result)) + assert.Equal(t, 1, result) + }) + + // Fix #567: deleting an instance must remove it from the describe list. + t.Run("DeleteDBInstance", func(t *testing.T) { + _, err := svc.DeleteDBInstance(ctx, &rds.DeleteDBInstanceInput{ + DBInstanceIdentifier: aws.String(instanceId), + SkipFinalSnapshot: aws.Bool(true), + }) + require.NoError(t, err) + + out, err := svc.DescribeDBInstances(ctx, &rds.DescribeDBInstancesInput{ + DBInstanceIdentifier: aws.String(instanceId), + }) + require.NoError(t, err) + assert.Empty(t, out.DBInstances, "deleted instance must not appear in describe") + }) +} + +// TestRDSCluster covers management-plane CRUD for a DB cluster and validates +// that DBSubnetGroup is returned as a plain string field (fix #548). +func TestRDSCluster(t *testing.T) { + ctx := context.Background() + svc := testutil.RDSClient() + clusterId := fmt.Sprintf("go-cluster-%d", time.Now().UnixMilli()%100000) + + t.Cleanup(func() { + svc.DeleteDBCluster(ctx, &rds.DeleteDBClusterInput{ //nolint:errcheck + DBClusterIdentifier: aws.String(clusterId), + SkipFinalSnapshot: aws.Bool(true), + }) + }) + + t.Run("CreateDBCluster", func(t *testing.T) { + out, err := svc.CreateDBCluster(ctx, &rds.CreateDBClusterInput{ + DBClusterIdentifier: aws.String(clusterId), + Engine: aws.String("aurora-postgresql"), + MasterUsername: aws.String(rdsUsername), + MasterUserPassword: aws.String(rdsPassword), + DatabaseName: aws.String(rdsDatabase), + }) + require.NoError(t, err) + require.NotNil(t, out.DBCluster) + assert.Equal(t, clusterId, aws.ToString(out.DBCluster.DBClusterIdentifier)) + assert.Equal(t, rdsUsername, aws.ToString(out.DBCluster.MasterUsername)) + }) + + // Fix #548: DBCluster.DBSubnetGroup must unmarshal as a plain *string, not a nested + // struct. The AWS service model defines it as shape: String. If the XML had nested + // elements (DBSubnetGroupName, etc.), the Go SDK would return nil for this field. + t.Run("DescribeDBCluster_DBSubnetGroupIsString", func(t *testing.T) { + out, err := svc.DescribeDBClusters(ctx, &rds.DescribeDBClustersInput{ + DBClusterIdentifier: aws.String(clusterId), + }) + require.NoError(t, err) + require.Len(t, out.DBClusters, 1) + + cluster := out.DBClusters[0] + assert.Equal(t, clusterId, aws.ToString(cluster.DBClusterIdentifier)) + assert.Equal(t, rdsUsername, aws.ToString(cluster.MasterUsername)) + assert.NotNil(t, cluster.DBSubnetGroup, + "DBCluster.DBSubnetGroup must be a non-nil string (shape: String in AWS service model)") + }) + + // Fix #567: deleting a cluster must remove it from the describe list. + t.Run("DeleteDBCluster", func(t *testing.T) { + _, err := svc.DeleteDBCluster(ctx, &rds.DeleteDBClusterInput{ + DBClusterIdentifier: aws.String(clusterId), + SkipFinalSnapshot: aws.Bool(true), + }) + require.NoError(t, err) + + out, err := svc.DescribeDBClusters(ctx, &rds.DescribeDBClustersInput{ + DBClusterIdentifier: aws.String(clusterId), + }) + require.NoError(t, err) + assert.Empty(t, out.DBClusters, "deleted cluster must not appear in describe") + }) +} + +// ── Helpers ────────────────────────────────────────────────────────────────── + +func openPostgresConn(host string, port int, user, password, dbname string) (*sql.DB, error) { + dsn := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable connect_timeout=5", + host, port, user, password, dbname) + db, err := sql.Open("postgres", dsn) + if err != nil { + return nil, err + } + if err := db.Ping(); err != nil { + db.Close() + return nil, err + } + return db, nil +} + +func awaitPostgresConn(t *testing.T, host string, port int, user, password, dbname string) *sql.DB { + t.Helper() + deadline := time.Now().Add(60 * time.Second) + var lastErr error + for time.Now().Before(deadline) { + db, err := openPostgresConn(host, port, user, password, dbname) + if err == nil { + return db + } + lastErr = err + time.Sleep(time.Second) + } + t.Fatalf("timed out waiting for postgres connection on %s:%d: %v", host, port, lastErr) + return nil +} diff --git a/docs/configuration/storage.md b/docs/configuration/storage.md index 32c7f15110..96b341a52a 100644 --- a/docs/configuration/storage.md +++ b/docs/configuration/storage.md @@ -67,6 +67,12 @@ Override the global mode for individual services via environment variables. When | `FLOCI_STORAGE_SERVICES_ACM_FLUSH_INTERVAL_MS` | `5000` | ACM flush interval (ms) | | `FLOCI_STORAGE_SERVICES_OPENSEARCH_MODE` | global default | OpenSearch storage mode | | `FLOCI_STORAGE_SERVICES_OPENSEARCH_FLUSH_INTERVAL_MS` | `5000` | OpenSearch flush interval (ms) | +| `FLOCI_STORAGE_SERVICES_RDS_MODE` | global default | RDS storage mode (see note below) | + +!!! note "RDS storage mode" + For RDS, `FLOCI_STORAGE_SERVICES_RDS_MODE` controls whether DB containers get a persistent Docker volume. + When set to `memory` (or when the global mode is `memory`), no volume is created and data is lost when the container stops. + Any other mode (`hybrid`, `persistent`, `wal`) causes a per-instance named volume (`floci-rds-`) to be created and removed when the instance is deleted. ## Environment Variable Override diff --git a/docs/services/rds.md b/docs/services/rds.md index 8cdbf654e8..ccd6bf532a 100644 --- a/docs/services/rds.md +++ b/docs/services/rds.md @@ -104,3 +104,27 @@ mysql -h 127.0.0.1 -P 7002 -u root -psecret123 | `mariadb` | `mariadb:11` | Override the image per-instance with the `--engine-version` flag or globally via environment variables. + +## Persistence + +By default, each DB instance or cluster gets its own named Docker volume (`floci-rds-`). The volume is created when the instance is created and removed when the instance is deleted. + +Set `FLOCI_STORAGE_SERVICES_RDS_MODE=memory` (or set the global `FLOCI_STORAGE_MODE=memory`) to disable volume creation entirely — DB containers become ephemeral and data is lost on restart. This is the recommended setting for CI. + +```bash +# CI — no volumes, fastest startup +FLOCI_STORAGE_SERVICES_RDS_MODE=memory + +# Local dev — persist DB data across Floci restarts +FLOCI_STORAGE_SERVICES_RDS_MODE=hybrid +FLOCI_STORAGE_HOST_PERSISTENT_PATH=/absolute/host/path/data +``` + +!!! note "Docker Desktop on macOS" + Floci uses named Docker volumes (not bind mounts) for RDS persistence. This works correctly on Docker Desktop for macOS where bind-mounting paths inside the Floci container is not supported. + +## Authentication + +The RDS auth proxy validates the master username and password at the proxy layer. All other database users are passed through directly to the backend engine — create them with standard SQL (`CREATE USER`) and connect as normal. + +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. diff --git a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java index 32391c8433..d89cd1b14a 100644 --- a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java +++ b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java @@ -115,6 +115,7 @@ public interface EmulatorConfig { AppConfigStorageConfig appconfig(); AppConfigDataStorageConfig appconfigdata(); ElastiCacheStorageConfig elasticache(); + RdsStorageConfig rds(); } interface SsmStorageConfig { @@ -209,6 +210,10 @@ public interface EmulatorConfig { long flushIntervalMs(); } + interface RdsStorageConfig { + Optional mode(); + } + interface WalConfig { @WithDefault("30000") long compactionIntervalMs(); diff --git a/src/main/java/io/github/hectorvent/floci/core/common/ResolvedServiceCatalog.java b/src/main/java/io/github/hectorvent/floci/core/common/ResolvedServiceCatalog.java index 56a7425de8..f3e1ae7e6f 100644 --- a/src/main/java/io/github/hectorvent/floci/core/common/ResolvedServiceCatalog.java +++ b/src/main/java/io/github/hectorvent/floci/core/common/ResolvedServiceCatalog.java @@ -81,7 +81,8 @@ public class ResolvedServiceCatalog { protocols(ServiceProtocol.QUERY), Set.of(), Set.of("elasticache"), Set.of(), Set.of()), descriptor("rds", "rds", config.services().rds().enabled(), true, - null, null, 5000L, AwsNamespaces.RDS, ServiceProtocol.QUERY, + "rds", storageMode(config.storage().services().rds().mode(), config.storage().mode()), + 5000L, AwsNamespaces.RDS, ServiceProtocol.QUERY, protocols(ServiceProtocol.QUERY), Set.of(), Set.of("rds"), Set.of(), Set.of()), descriptor("events", "eventbridge", config.services().eventbridge().enabled(), true, diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/RdsQueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/rds/RdsQueryHandler.java index afb1c386eb..aff7318ee9 100644 --- a/src/main/java/io/github/hectorvent/floci/services/rds/RdsQueryHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/rds/RdsQueryHandler.java @@ -444,20 +444,7 @@ public class RdsQueryHandler { .elem("Status", "active") .end("VpcSecurityGroupMembership") .end("VpcSecurityGroups") - .start("DBSubnetGroup") - .elem("DBSubnetGroupName", "default") - .elem("VpcId", "vpc-00000000") - .elem("SubnetGroupStatus", "Complete") - .start("Subnets") - .start("member") - .elem("SubnetIdentifier", "subnet-00000000") - .start("SubnetAvailabilityZone") - .elem("Name", config.defaultAvailabilityZone()) - .end("SubnetAvailabilityZone") - .elem("SubnetStatus", "Active") - .end("member") - .end("Subnets") - .end("DBSubnetGroup") + .elem("DBSubnetGroup", "default") .elem("DbClusterResourceId", c.getDbClusterResourceId()) .elem("DBClusterArn", c.getDbClusterArn()) .start("DBClusterMembers"); diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/RdsService.java b/src/main/java/io/github/hectorvent/floci/services/rds/RdsService.java index b259a198fe..1bd1d3b37f 100644 --- a/src/main/java/io/github/hectorvent/floci/services/rds/RdsService.java +++ b/src/main/java/io/github/hectorvent/floci/services/rds/RdsService.java @@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.rds; import io.github.hectorvent.floci.config.EmulatorConfig; import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.core.common.RegionResolver; +import io.github.hectorvent.floci.core.common.ServiceConfigAccess; import io.github.hectorvent.floci.core.storage.InMemoryStorage; import io.github.hectorvent.floci.core.storage.StorageBackend; import io.github.hectorvent.floci.services.rds.container.RdsContainerHandle; @@ -41,17 +42,20 @@ public class RdsService { private final RdsProxyManager proxyManager; private final RegionResolver regionResolver; private final EmulatorConfig config; + private final ServiceConfigAccess serviceConfigAccess; private final Set usedPorts = ConcurrentHashMap.newKeySet(); @Inject public RdsService(RdsContainerManager containerManager, RdsProxyManager proxyManager, RegionResolver regionResolver, - EmulatorConfig config) { + EmulatorConfig config, + ServiceConfigAccess serviceConfigAccess) { this.containerManager = containerManager; this.proxyManager = proxyManager; this.regionResolver = regionResolver; this.config = config; + this.serviceConfigAccess = serviceConfigAccess; this.instances = new InMemoryStorage<>(); this.clusters = new InMemoryStorage<>(); this.parameterGroups = new InMemoryStorage<>(); @@ -106,6 +110,9 @@ public class RdsService { instance.setContainerId(containerId); instance.setContainerHost(containerHost); instance.setContainerPort(containerPort); + if (dbClusterIdentifier == null || dbClusterIdentifier.isBlank()) { + instance.setDockerVolumeName(isInMemory() ? null : "floci-rds-" + id); + } String region = regionResolver.getDefaultRegion(); instance.setDbiResourceId("db-" + java.util.UUID.randomUUID().toString().replace("-", "").substring(0, 24).toUpperCase()); @@ -211,10 +218,13 @@ public class RdsService { String clusterId = instance.getDbClusterIdentifier(); if (clusterId == null || clusterId.isBlank()) { - // Standalone — stop its container + // Standalone — stop its container and clean up its Docker volume if (instance.getContainerId() != null) { containerManager.stop(buildHandle(instance)); } + if (instance.getDockerVolumeName() != null) { + containerManager.removeVolume(instance.getDbInstanceIdentifier()); + } } else { // Cluster member — remove from cluster's member list DbCluster cluster = clusters.get(clusterId).orElse(null); @@ -253,6 +263,7 @@ public class RdsService { cluster.setContainerId(handle.getContainerId()); cluster.setContainerHost(handle.getHost()); cluster.setContainerPort(handle.getPort()); + cluster.setDockerVolumeName(isInMemory() ? null : "floci-rds-" + id); String region = regionResolver.getDefaultRegion(); cluster.setDbClusterResourceId("cluster-" + java.util.UUID.randomUUID().toString().replace("-", "").substring(0, 24).toUpperCase()); @@ -312,6 +323,9 @@ public class RdsService { if (cluster.getContainerId() != null) { containerManager.stop(buildClusterHandle(cluster)); } + if (cluster.getDockerVolumeName() != null) { + containerManager.removeVolume(id); + } releaseProxyPort(cluster.getProxyPort()); clusters.delete(id); @@ -368,6 +382,9 @@ public class RdsService { if (instance == null) { return false; } + if (!instance.getMasterUsername().equals(clientUser)) { + return true; // non-master user: backend is the authority + } return password != null && password.equals(instance.getMasterPassword()); } @@ -376,11 +393,18 @@ public class RdsService { if (cluster == null) { return false; } + if (!cluster.getMasterUsername().equals(clientUser)) { + return true; // non-master user: backend is the authority + } return password != null && password.equals(cluster.getMasterPassword()); } // ── Helpers ─────────────────────────────────────────────────────────────── + private boolean isInMemory() { + return "memory".equals(serviceConfigAccess.storageMode("rds")); + } + private DatabaseEngine resolveEngine(String engineParam) { if (engineParam == null) { return DatabaseEngine.POSTGRES; diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManager.java b/src/main/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManager.java index 5d5483d1bc..c33512a52a 100644 --- a/src/main/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManager.java +++ b/src/main/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManager.java @@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.rds.container; import io.github.hectorvent.floci.config.EmulatorConfig; import io.github.hectorvent.floci.core.common.RegionResolver; +import io.github.hectorvent.floci.core.common.ServiceConfigAccess; import io.github.hectorvent.floci.core.common.docker.ContainerBuilder; import io.github.hectorvent.floci.core.common.docker.ContainerDetector; import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager; @@ -38,6 +39,7 @@ public class RdsContainerManager { private final ContainerDetector containerDetector; private final EmulatorConfig config; private final RegionResolver regionResolver; + private final ServiceConfigAccess serviceConfigAccess; private final Map activeContainers = new ConcurrentHashMap<>(); @Inject @@ -46,13 +48,15 @@ public class RdsContainerManager { ContainerLogStreamer logStreamer, ContainerDetector containerDetector, EmulatorConfig config, - RegionResolver regionResolver) { + RegionResolver regionResolver, + ServiceConfigAccess serviceConfigAccess) { this.containerBuilder = containerBuilder; this.lifecycleManager = lifecycleManager; this.logStreamer = logStreamer; this.containerDetector = containerDetector; this.config = config; this.regionResolver = regionResolver; + this.serviceConfigAccess = serviceConfigAccess; } public RdsContainerHandle start(String instanceId, DatabaseEngine engine, @@ -140,36 +144,66 @@ public class RdsContainerManager { private void addPersistenceMounts(ContainerBuilder.Builder specBuilder, String instanceId, DatabaseEngine engine, List envVars) { + if ("memory".equals(serviceConfigAccess.storageMode("rds"))) { + return; // ephemeral mode — no mount needed + } + String hostPersistentPath = config.storage().hostPersistentPath(); - boolean isVolume = !hostPersistentPath.startsWith("/") && !hostPersistentPath.startsWith("."); - if (isVolume) { - // Named volume mode: mount the whole volume and tell the engine where to write + if (!hostPersistentPath.startsWith("/") && !hostPersistentPath.startsWith(".")) { + // Explicit Docker named volume (e.g. "floci-data"): mount the shared volume String internalMountPath = "/app/data"; - specBuilder.withBind(hostPersistentPath, internalMountPath); - + specBuilder.withNamedVolume(hostPersistentPath, internalMountPath); String dataPath = internalMountPath + "/rds/" + instanceId; - if (engine == DatabaseEngine.POSTGRES) { - envVars.add("PGDATA=" + dataPath); - } else { - LOG.warnv("Volume-based persistence for RDS engine {0} is currently only optimized for POSTGRES. " + - "Using default non-persistent path.", engine); - } - } else { - // Directory mode: mount the specific subdirectory directly - String dataPath = Path.of(config.storage().persistentPath(), "rds", instanceId) - .toAbsolutePath().toString(); + applyEngineDataPath(specBuilder, engine, envVars, instanceId, dataPath); + + } else if (hostPersistentPath.startsWith("/")) { + // Absolute bind-mount path: user explicitly configured this host path + String hostDataPath = Path.of(hostPersistentPath, "rds", instanceId).toString(); try { - Files.createDirectories(Path.of(dataPath)); - String hostDataPath = Path.of(hostPersistentPath, "rds", instanceId) - .toAbsolutePath().toString(); - String target = (engine == DatabaseEngine.POSTGRES) - ? "/var/lib/postgresql/data" - : "/var/lib/mysql"; - specBuilder.withBind(hostDataPath, target); + Files.createDirectories(Path.of(hostDataPath)); } catch (IOException e) { - LOG.errorv("Failed to create RDS data directory: {0}", dataPath, e); + LOG.errorv("Failed to create RDS data directory: {0}", hostDataPath, e); } + specBuilder.withBind(hostDataPath, engineDefaultDataPath(engine)); + + } else { + // Default (relative path like ./data): use a per-instance Docker named volume. + // This works on all platforms including Docker Desktop on macOS, where + // bind-mounting paths inside the Floci container is not allowed. + String volumeName = "floci-rds-" + instanceId; + specBuilder.withNamedVolume(volumeName, engineDefaultDataPath(engine)); + if (engine == DatabaseEngine.POSTGRES) { + envVars.add("PGDATA=/var/lib/postgresql/data"); + } + } + } + + private void applyEngineDataPath(ContainerBuilder.Builder specBuilder, DatabaseEngine engine, + List envVars, String instanceId, String dataPath) { + if (engine == DatabaseEngine.POSTGRES) { + envVars.add("PGDATA=" + dataPath); + } else { + // MySQL / MariaDB: use a dedicated per-instance named volume + String volumeName = "floci-rds-" + instanceId; + specBuilder.withNamedVolume(volumeName, engineDefaultDataPath(engine)); + } + } + + private static String engineDefaultDataPath(DatabaseEngine engine) { + return switch (engine) { + case POSTGRES -> "/var/lib/postgresql/data"; + case MYSQL, MARIADB -> "/var/lib/mysql"; + }; + } + + public void removeVolume(String instanceId) { + String volumeName = "floci-rds-" + instanceId; + try { + lifecycleManager.getDockerClient().removeVolumeCmd(volumeName).exec(); + LOG.infov("Removed Docker volume {0} for RDS instance {1}", volumeName, instanceId); + } catch (Exception e) { + LOG.warnv("Could not remove Docker volume {0}: {1}", volumeName, e.getMessage()); } } diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/model/DbCluster.java b/src/main/java/io/github/hectorvent/floci/services/rds/model/DbCluster.java index 6d0b4e3953..91cc5962b6 100644 --- a/src/main/java/io/github/hectorvent/floci/services/rds/model/DbCluster.java +++ b/src/main/java/io/github/hectorvent/floci/services/rds/model/DbCluster.java @@ -26,6 +26,8 @@ public class DbCluster { private Instant createdAt; private int proxyPort; + private String dockerVolumeName; // null in in-memory mode; "floci-rds-" otherwise + // Transient — not persisted private transient String containerId; private transient String containerHost; @@ -104,6 +106,9 @@ public class DbCluster { public int getProxyPort() { return proxyPort; } public void setProxyPort(int proxyPort) { this.proxyPort = proxyPort; } + public String getDockerVolumeName() { return dockerVolumeName; } + public void setDockerVolumeName(String dockerVolumeName) { this.dockerVolumeName = dockerVolumeName; } + public String getContainerId() { return containerId; } public void setContainerId(String containerId) { this.containerId = containerId; } diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/model/DbInstance.java b/src/main/java/io/github/hectorvent/floci/services/rds/model/DbInstance.java index 27b96bb25d..04377be0d0 100644 --- a/src/main/java/io/github/hectorvent/floci/services/rds/model/DbInstance.java +++ b/src/main/java/io/github/hectorvent/floci/services/rds/model/DbInstance.java @@ -25,6 +25,8 @@ public class DbInstance { private Instant createdAt; private int proxyPort; + private String dockerVolumeName; // null in in-memory mode; "floci-rds-" otherwise + // Transient — not persisted; restored on startup by re-launching containers private transient String containerId; private transient String containerHost; @@ -108,6 +110,9 @@ public class DbInstance { public int getProxyPort() { return proxyPort; } public void setProxyPort(int proxyPort) { this.proxyPort = proxyPort; } + public String getDockerVolumeName() { return dockerVolumeName; } + public void setDockerVolumeName(String dockerVolumeName) { this.dockerVolumeName = dockerVolumeName; } + public String getContainerId() { return containerId; } public void setContainerId(String containerId) { this.containerId = containerId; } diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/proxy/MySqlProtocolHandler.java b/src/main/java/io/github/hectorvent/floci/services/rds/proxy/MySqlProtocolHandler.java index 0606bc8519..6668e74267 100644 --- a/src/main/java/io/github/hectorvent/floci/services/rds/proxy/MySqlProtocolHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/rds/proxy/MySqlProtocolHandler.java @@ -65,7 +65,10 @@ public class MySqlProtocolHandler { return; } - // Phase 4: Validate credentials against the backend nonce + // Phase 4: Validate credentials against the backend nonce. + // Master user: validate the scramble locally against the known master password. + // Non-master users: pass through — the backend validates their scramble directly. + // IAM tokens: validate SigV4, then connect to backend as master. byte[] clientPayload = Arrays.copyOfRange(clientResponseRaw, 4, clientResponseRaw.length); String[] parsed = parseHandshakeResponse(clientPayload); String clientUsername = parsed[0]; @@ -74,8 +77,13 @@ public class MySqlProtocolHandler { boolean valid; try { - byte[] expected = scrambleNativePassword(masterPassword, backendNonce); - valid = masterUsername.equals(clientUsername) && Arrays.equals(expected, clientAuthData); + if (masterUsername.equals(clientUsername)) { + byte[] expected = scrambleNativePassword(masterPassword, backendNonce); + valid = Arrays.equals(expected, clientAuthData); + } else { + // Non-master user: defer to backend — it knows their password. + valid = true; + } } catch (Exception e) { LOG.warnv("MySQL auth error for instance: {0}", e.getMessage()); valid = false; diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/proxy/PostgresProtocolHandler.java b/src/main/java/io/github/hectorvent/floci/services/rds/proxy/PostgresProtocolHandler.java index ccb6ab82b5..17e7c52f8b 100644 --- a/src/main/java/io/github/hectorvent/floci/services/rds/proxy/PostgresProtocolHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/rds/proxy/PostgresProtocolHandler.java @@ -68,32 +68,48 @@ public class PostgresProtocolHandler { return; } - // Phase 4: Validate credentials - boolean valid; - if (iamEnabled && clientPassword.contains("X-Amz-Signature")) { - valid = sigV4.validate(clientPassword, clientUsername); - } else { - valid = passwordValidator.validate(clientUsername, clientPassword); + // Phase 4: Validate credentials. + // - IAM tokens: validated locally via SigV4. + // - Master user (plain password): validated at the proxy via passwordValidator, which + // reads from RdsService and therefore reflects modifyDBInstance password changes. + // - Non-master users: pass through — the backend is the authority for their passwords. + boolean isIam = iamEnabled && clientPassword.contains("X-Amz-Signature"); + boolean isMaster = masterUsername.equals(clientUsername); + + if (isIam) { + if (!sigV4.validate(clientPassword, clientUsername)) { + sendErrorResponse(clientOut, "FATAL", "28P01", + "password authentication failed for user \"" + clientUsername + "\""); + clientOut.flush(); + closeQuietly(client); + closeQuietly(backend); + return; + } + } else if (isMaster) { + if (!passwordValidator.validate(clientUsername, clientPassword)) { + sendErrorResponse(clientOut, "FATAL", "28P01", + "password authentication failed for user \"" + clientUsername + "\""); + clientOut.flush(); + closeQuietly(client); + closeQuietly(backend); + return; + } } - if (!valid) { - sendErrorResponse(clientOut, "FATAL", "28P01", - "password authentication failed for user \"" + clientUsername + "\""); - clientOut.flush(); - closeQuietly(client); - closeQuietly(backend); - return; - } - - // Phase 5: Connect to backend PostgreSQL + // Phase 5: Connect to backend PostgreSQL. + // IAM and master: use master credentials — the backend has the original container password + // and is never updated directly, so the proxy always authenticates as master. + // Non-master: forward the client's own credentials so the backend enforces its own ACLs. InputStream backendIn = backend.getInputStream(); OutputStream backendOut = backend.getOutputStream(); String effectiveDbName = (dbName != null && !dbName.isBlank()) ? dbName : "postgres"; - sendStartupToBackend(backendOut, masterUsername, effectiveDbName); + String backendUser = (isIam || isMaster) ? masterUsername : clientUsername; + String backendPass = (isIam || isMaster) ? masterPassword : clientPassword; + sendStartupToBackend(backendOut, backendUser, effectiveDbName); backendOut.flush(); - if (!authenticateWithBackend(backendIn, backendOut, masterUsername, masterPassword)) { + if (!authenticateWithBackend(backendIn, backendOut, backendUser, backendPass)) { sendErrorResponse(clientOut, "FATAL", "08006", "Backend database authentication failed"); clientOut.flush(); diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index b99fd9e8be..f9019e18b1 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -64,6 +64,9 @@ floci: flush-interval-ms: 5000 opensearch: flush-interval-ms: 5000 + rds: + # Override storage mode for RDS (memory = no Docker volumes; hybrid/persistent = named volume per instance) + # mode: memory dns: # Extra hostname suffixes resolved to Floci's container IP by the embedded DNS server. diff --git a/src/test/java/io/github/hectorvent/floci/services/rds/RdsQueryHandlerTest.java b/src/test/java/io/github/hectorvent/floci/services/rds/RdsQueryHandlerTest.java index c2dc5a737f..9718204cfa 100644 --- a/src/test/java/io/github/hectorvent/floci/services/rds/RdsQueryHandlerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/rds/RdsQueryHandlerTest.java @@ -215,6 +215,23 @@ class RdsQueryHandlerTest { assertTrue(((String) response.getEntity()).contains("UnsupportedOperation")); } + // ──────────────────────────── DBSubnetGroup shape ─────────────────────────── + + @Test + void describeDbClusters_dbSubnetGroupIsPlainString() { + DbCluster cluster = makeCluster("mycluster"); + when(service.listDbClusters(null)).thenReturn(List.of(cluster)); + + Response response = handler.handle("DescribeDBClusters", params()); + + String body = (String) response.getEntity(); + // DBCluster.DBSubnetGroup is shape: String in the AWS service model — not a nested struct + assertTrue(body.contains("default"), + "Expected DBSubnetGroup as plain string element"); + assertFalse(body.contains(""), + "Did not expect nested DBSubnetGroupName inside DBCluster"); + } + // ──────────────────────────── Helpers ──────────────────────────── private static MultivaluedMap params() { diff --git a/src/test/java/io/github/hectorvent/floci/services/rds/RdsServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/rds/RdsServiceTest.java index c9968507aa..d444685c5c 100644 --- a/src/test/java/io/github/hectorvent/floci/services/rds/RdsServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/rds/RdsServiceTest.java @@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.rds; import io.github.hectorvent.floci.config.EmulatorConfig; import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.core.common.RegionResolver; +import io.github.hectorvent.floci.core.common.ServiceConfigAccess; import io.github.hectorvent.floci.services.rds.model.DatabaseEngine; import io.github.hectorvent.floci.services.rds.model.DbCluster; import io.github.hectorvent.floci.services.rds.container.RdsContainerHandle; @@ -42,7 +43,9 @@ class RdsServiceTest { when(rdsConfig.proxyBasePort()).thenReturn(7000); when(rdsConfig.proxyMaxPort()).thenReturn(7099); - rdsService = new RdsService(containerManager, proxyManager, regionResolver, config); + ServiceConfigAccess serviceConfigAccess = mock(ServiceConfigAccess.class); + when(serviceConfigAccess.storageMode("rds")).thenReturn("in-memory"); + rdsService = new RdsService(containerManager, proxyManager, regionResolver, config, serviceConfigAccess); when(containerManager.start(any(), any(), any(), any(), any(), any())) .thenReturn(new RdsContainerHandle("cont-id", "id", "localhost", 5432));