Light, fluffy, and always free - The AWS Local Emulator alternative
fix(rds): grant the MySQL/MariaDB master user RDS-master-equivalent privileges (#2679)
Matan Baruch6c96f47f7350
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.javaindex 41fa160b6d..89bc609caf 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@@ -17,8 +17,11 @@ import io.github.hectorvent.floci.core.common.docker.ContainerSpec;import io.github.hectorvent.floci.services.rds.model.DatabaseEngine;import jakarta.enterprise.context.ApplicationScoped;import jakarta.inject.Inject;+import org.apache.commons.compress.archivers.tar.TarArchiveEntry;+import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;import org.jboss.logging.Logger;+import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.Closeable;import java.io.IOException;@@ -167,6 +170,9 @@ public class RdsContainerManager {cleanupContainerId = containerName;String createdContainerId = lifecycleManager.create(spec);cleanupContainerId = createdContainerId;+ if (needsMasterGrant(engine, masterUsername)) {+ installMasterGrantInitScript(createdContainerId, masterUsername);+ }ContainerInfo info = lifecycleManager.startCreated(createdContainerId, spec);EndpointInfo endpoint = info.getEndpoint(enginePort);@@ -398,13 +404,66 @@ public class RdsContainerManager {"-d", "postgres","-c", postgresIamRoleInitSql()};+ execUntilSuccess(containerName, containerId, cmd, "PostgreSQL IAM role");+ }++ /**+ * The stock mysql/mariadb images grant {@code MYSQL_USER} only {@code ALL} on+ * {@code MYSQL_DATABASE}, so a master user cannot {@code CREATE DATABASE}, {@code CREATE USER}+ * or {@code GRANT} — operations real RDS master users perform routinely. A root master needs+ * nothing (and the images create no separate user for it).+ */+ static boolean needsMasterGrant(DatabaseEngine engine, String masterUsername) {+ return (engine == DatabaseEngine.MYSQL || engine == DatabaseEngine.MARIADB)+ && masterUsername != null && !masterUsername.isBlank() && !"root".equals(masterUsername);+ }++ static String mysqlMasterGrantSql(String masterUsername) {+ // Real RDS grants the master user a near-global privilege list (withholding SUPER, FILE+ // and SHUTDOWN); the emulator grants ALL for simplicity, mirroring how POSTGRES_USER is+ // already a superuser. Floci does not enforce AWS's MasterUsername charset, so the name+ // is escaped rather than trusted — this SQL runs as root inside the container.+ String escaped = masterUsername.replace("\\", "\\\\").replace("'", "\\'");+ return "GRANT ALL PRIVILEGES ON *.* TO '" + escaped + "'@'%' WITH GRANT OPTION;";+ }++ /**+ * Delivered as a {@code /docker-entrypoint-initdb.d} script installed between container create+ * and start, rather than a post-start root exec: the images run init scripts exactly once —+ * against an empty data directory, as root, after creating the master user. A reboot on a+ * reused volume therefore never re-runs it, which matters because the volume's real root+ * password survives a ModifyDBInstance master-password rotation (MYSQL_ROOT_PASSWORD only+ * takes effect on first initialization), so any post-start root exec would fail there.+ */+ private void installMasterGrantInitScript(String containerId, String masterUsername) {+ byte[] sql = mysqlMasterGrantSql(masterUsername).getBytes(StandardCharsets.UTF_8);+ try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {+ try (TarArchiveOutputStream tar = new TarArchiveOutputStream(bos)) {+ TarArchiveEntry entry = new TarArchiveEntry("floci-master-grants.sql");+ entry.setSize(sql.length);+ entry.setMode(0644);+ tar.putArchiveEntry(entry);+ tar.write(sql);+ tar.closeArchiveEntry();+ }+ lifecycleManager.getDockerClient().copyArchiveToContainerCmd(containerId)+ .withRemotePath("/docker-entrypoint-initdb.d")+ .withTarInputStream(new ByteArrayInputStream(bos.toByteArray()))+ .exec();+ } catch (IOException e) {+ throw new IllegalStateException(+ "Failed to install the master-grant init script into container " + containerId, e);+ }+ }++ private void execUntilSuccess(String containerName, String containerId, String[] cmd, String description) {String lastOutput = "";for (int attempt = 1; attempt <= 60; attempt++) {try {ContainerExecResult result = execInContainer(containerId, cmd, 5);lastOutput = result.output();if (result.exitCode() == 0) {- LOG.infov("Initialized PostgreSQL IAM role in RDS container {0}", containerName);+ LOG.infov("Initialized {0} in RDS container {1}", description, containerName);return;}} catch (Exception e) {@@ -414,10 +473,10 @@ public class RdsContainerManager {TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {Thread.currentThread().interrupt();- throw new IllegalStateException("Interrupted initializing PostgreSQL IAM role in " + containerName, e);+ throw new IllegalStateException("Interrupted initializing " + description + " in " + containerName, e);}}- throw new IllegalStateException("Timed out initializing PostgreSQL IAM role in " + containerName + ": " + lastOutput);+ throw new IllegalStateException("Timed out initializing " + description + " in " + containerName + ": " + lastOutput);}private ContainerExecResult execInContainer(String containerId, String[] cmd, int timeoutSeconds) throws Exception {diff --git a/src/test/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManagerTest.java b/src/test/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManagerTest.javaindex a9aa5e02fa..170a312a66 100644--- a/src/test/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManagerTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManagerTest.java@@ -21,6 +21,8 @@ import java.util.concurrent.CompletableFuture;import java.util.concurrent.CountDownLatch;import java.util.concurrent.TimeUnit;+import com.github.dockerjava.api.DockerClient;+import com.github.dockerjava.api.command.CopyArchiveToContainerCmd;import com.github.dockerjava.api.model.Bind;import io.github.hectorvent.floci.services.rds.model.DatabaseEngine;@@ -54,6 +56,91 @@ class RdsContainerManagerTest {assertTrue(sql.contains("CREATE ROLE rds_iam"));}+ @Test+ void mysqlMasterGrantSqlGrantsGlobalPrivilegesWithGrantOption() {+ String sql = RdsContainerManager.mysqlMasterGrantSql("admin");++ assertTrue(sql.contains("GRANT ALL PRIVILEGES ON *.*"));+ assertTrue(sql.contains("'admin'@'%'"));+ assertTrue(sql.contains("WITH GRANT OPTION"));+ }++ @Test+ void mysqlMasterGrantSqlEscapesQuotesAndBackslashes() {+ // Floci does not enforce AWS's MasterUsername charset, so a quote must not be able to+ // break out of the string literal in SQL executed as root.+ assertEquals("GRANT ALL PRIVILEGES ON *.* TO 'we\\'ird\\\\'@'%' WITH GRANT OPTION;",+ RdsContainerManager.mysqlMasterGrantSql("we'ird\\"));+ }++ @Test+ void needsMasterGrantSkipsRootAndPostgres() {+ assertTrue(RdsContainerManager.needsMasterGrant(DatabaseEngine.MYSQL, "admin"));+ assertTrue(RdsContainerManager.needsMasterGrant(DatabaseEngine.MARIADB, "admin"));+ assertFalse(RdsContainerManager.needsMasterGrant(DatabaseEngine.MYSQL, "root"));+ assertFalse(RdsContainerManager.needsMasterGrant(DatabaseEngine.POSTGRES, "admin"));+ assertFalse(RdsContainerManager.needsMasterGrant(DatabaseEngine.MYSQL, null));+ }++ @Test+ void mysqlRootMasterStartDoesNotInstallAnInitScript() {+ EmulatorConfig config = config(tempDir.resolve("host-root"));+ ContainerLifecycleManager lifecycleManager = mock(ContainerLifecycleManager.class);+ stubStarts(lifecycleManager, new ContainerLifecycleManager.ContainerInfo(+ "container-id", Map.of(3306, new ContainerLifecycleManager.EndpointInfo("db1", 3306))));+ ContainerLogStreamer logStreamer = mock(ContainerLogStreamer.class);+ lenient().when(logStreamer.generateLogStreamName(any())).thenReturn("log-stream");++ RdsContainerManager manager = new RdsContainerManager(+ new ContainerBuilder(config, mock(DockerHostResolver.class), mock(EmbeddedDnsServer.class)),+ lifecycleManager, logStreamer, mock(ContainerDetector.class), config,+ new RegionResolver("us-east-1", "000000000000"),+ mock(ServiceConfigAccess.class));++ manager.start("db1", "vol1", DatabaseEngine.MYSQL, "mysql:8.0", "root", "password", "db");++ verify(lifecycleManager, never()).getDockerClient();+ }++ @Test+ void mysqlNonRootMasterStartInstallsGrantInitScriptBeforeStart() throws Exception {+ EmulatorConfig config = config(tempDir.resolve("host-root"));+ ContainerLifecycleManager lifecycleManager = mock(ContainerLifecycleManager.class);+ stubStarts(lifecycleManager, new ContainerLifecycleManager.ContainerInfo(+ "container-id", Map.of(3306, new ContainerLifecycleManager.EndpointInfo("db1", 3306))));+ DockerClient dockerClient = mock(DockerClient.class);+ when(lifecycleManager.getDockerClient()).thenReturn(dockerClient);+ CopyArchiveToContainerCmd copyCmd = mock(CopyArchiveToContainerCmd.class);+ when(dockerClient.copyArchiveToContainerCmd(any())).thenReturn(copyCmd);+ when(copyCmd.withRemotePath(any())).thenReturn(copyCmd);+ when(copyCmd.withTarInputStream(any())).thenReturn(copyCmd);+ ContainerLogStreamer logStreamer = mock(ContainerLogStreamer.class);+ lenient().when(logStreamer.generateLogStreamName(any())).thenReturn("log-stream");++ RdsContainerManager manager = new RdsContainerManager(+ new ContainerBuilder(config, mock(DockerHostResolver.class), mock(EmbeddedDnsServer.class)),+ lifecycleManager, logStreamer, mock(ContainerDetector.class), config,+ new RegionResolver("us-east-1", "000000000000"),+ mock(ServiceConfigAccess.class));++ manager.start("db1", "vol1", DatabaseEngine.MYSQL, "mysql:8.0", "admin", "password", "db");++ verify(copyCmd).withRemotePath("/docker-entrypoint-initdb.d");+ var tarCaptor = org.mockito.ArgumentCaptor.forClass(java.io.InputStream.class);+ verify(copyCmd).withTarInputStream(tarCaptor.capture());+ try (var tar = new org.apache.commons.compress.archivers.tar.TarArchiveInputStream(tarCaptor.getValue())) {+ var entry = tar.getNextEntry();+ assertEquals("floci-master-grants.sql", entry.getName());+ assertEquals(RdsContainerManager.mysqlMasterGrantSql("admin"),+ new String(tar.readAllBytes(), java.nio.charset.StandardCharsets.UTF_8));+ }+ // The script lands on the created container before it starts, so the entrypoint's+ // one-time init phase is the thing that runs it.+ var order = org.mockito.Mockito.inOrder(dockerClient, lifecycleManager);+ order.verify(dockerClient).copyArchiveToContainerCmd("container-id");+ order.verify(lifecycleManager).startCreated(org.mockito.ArgumentMatchers.eq("container-id"), any());+ }+@Testvoid postgres18UsesParentDataMount() {assertEquals("/var/lib/postgresql",