fix(rds): grant the MySQL/MariaDB master user RDS-master-equivalent privileges (#2679)

* fix(rds): grant the MySQL/MariaDB master user RDS-master-equivalent privileges

The stock mysql/mariadb images grant MYSQL_USER only ALL on MYSQL_DATABASE
plus USAGE, so a non-root master user cannot CREATE DATABASE, CREATE USER
or GRANT. Real RDS master users perform these routinely, and the terraform
and pulumi mysql providers run them right after connecting; against Floci
they failed with ERROR 1044.

After the DB container starts, exec a root GRANT ALL PRIVILEGES ON *.*
WITH GRANT OPTION for the master user, reusing the exec-with-retry
machinery the PostgreSQL rds_iam init already uses (extracted into a
shared execUntilSuccess). Real RDS withholds SUPER, FILE and SHUTDOWN
from its master grant list; the emulator grants ALL for simplicity,
mirroring how POSTGRES_USER is already a superuser. A root master is
skipped: the images create no separate user for it and root already
holds every privilege.

* fix(rds): deliver the master grant as a one-time init script and escape the username

Review follow-ups from #2679:

- Escape single quotes and backslashes in the GRANT statement. Floci does
  not enforce AWS's MasterUsername charset anywhere, so an unescaped name
  could break out of the string literal in SQL executed as root.
- Replace the post-start root exec with a /docker-entrypoint-initdb.d
  script installed between container create and start. The images run init
  scripts exactly once, against an empty data directory, as root, after
  creating the master user, so a reboot on a reused volume never re-runs
  the grant and never needs to re-authenticate as root. This removes the
  reboot-after-password-rotation failure: MYSQL_ROOT_PASSWORD only takes
  effect on first initialization, so the rotated password can never open
  a root session on a reused volume.
- The tar-and-copy shape follows CodeBuildRunner.copySourceToContainer.
This commit is contained in:
Matan Baruch authored and GitHub committed 2026-08-28 17:51:44 -04:00
1 parent be892864db
commit 6c96f47f73
2 files changed
+149 -3

No files matched your search

@@ -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 {
@@ -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());
}
@Test
void postgres18UsesParentDataMount() {
assertEquals("/var/lib/postgresql",