fix(rds): harden MySQL IAM authentication (#3686)

* fix: harden MySQL RDS IAM authentication

* test(rds): cover IAM token binding axes
This commit is contained in:
Niels Kootstra authored and GitHub committed 2026-09-16 09:08:14 -04:00
1 parent 794a587129
commit fc5380418e
8 files changed
+888 -63

No files matched your search

@@ -39,6 +39,12 @@ import java.util.regex.Pattern;
* the terminal OK/ERR (including any {@code AuthSwitchRequest}/{@code AuthMoreData} round trips)
* is relayed through {@link #relayConnectionPhase} to keep both sides in sync before handing off
* to the plain byte-for-byte {@link #bridge}.
*
* <p>With IAM database authentication enabled, an IAM token offered in cleartext
* ({@code mysql_clear_password}) is validated by the proxy, which then authenticates the session to
* the backend as the master user. Clients normally reveal the token only after the server asks them
* to switch to {@code mysql_clear_password}, so when the backend rejects a non-master login the proxy
* asks for that switch itself (see {@link #authenticateIamUserAfterAuthSwitch}).
*/
public class MySqlProtocolHandler {
@@ -48,13 +54,19 @@ public class MySqlProtocolHandler {
private static final int CLIENT_PLUGIN_AUTH = 0x0008_0000;
private static final int CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA = 0x200000;
private static final int CLIENT_SECURE_CONNECTION = 0x8000;
private static final int CLIENT_CONNECT_WITH_DB = 0x0008;
private static final String MYSQL_NATIVE_PASSWORD = "mysql_native_password";
private static final String CACHING_SHA2_PASSWORD = "caching_sha2_password";
private static final String MYSQL_CLEAR_PASSWORD = "mysql_clear_password";
// First payload byte of the packet types that can appear during the connection phase.
private static final int OK_PACKET_MARKER = 0x00;
private static final int ERR_PACKET_MARKER = 0xFF;
private static final int AUTH_MORE_DATA_MARKER = 0x01;
private static final int AUTH_SWITCH_REQUEST_MARKER = 0xFE;
// Server error code for a failed login ("Access denied for user ...").
private static final int ER_ACCESS_DENIED_ERROR = 1045;
// Second payload byte of an AuthMoreData packet under caching_sha2_password: fast_auth_success
// means the backend is about to send the terminal OK with no further input from the client.
@@ -76,11 +88,38 @@ public class MySqlProtocolHandler {
"IDENTIFIED WITH mysql_native_password AS '" + NO_MATCHING_PASSWORD_HASH + "'";
public static void handleAuth(Socket client, Socket backend,
PostgresProtocolHandler.BackendConnector backendConnector,
String masterUsername, String masterPassword,
boolean iamEnabled, RdsSigV4Validator sigV4,
RdsProxyTlsCertificates tlsCertificates,
PasswordValidator passwordValidator,
int handshakeTimeoutMillis) throws IOException {
handleAuth(client, backend, backendConnector, masterUsername, masterPassword,
iamEnabled, sigV4, tlsCertificates, passwordValidator, handshakeTimeoutMillis,
username -> true, null);
}
public static void handleAuth(Socket client, Socket backend,
PostgresProtocolHandler.BackendConnector backendConnector,
String masterUsername, String masterPassword,
boolean iamEnabled, RdsSigV4Validator sigV4,
RdsProxyTlsCertificates tlsCertificates,
PasswordValidator passwordValidator,
int handshakeTimeoutMillis,
IamUserChecker iamUserChecker) throws IOException {
handleAuth(client, backend, backendConnector, masterUsername, masterPassword, iamEnabled,
sigV4, tlsCertificates, passwordValidator, handshakeTimeoutMillis, iamUserChecker, null);
}
public static void handleAuth(Socket client, Socket backend,
PostgresProtocolHandler.BackendConnector backendConnector,
String masterUsername, String masterPassword,
boolean iamEnabled, RdsSigV4Validator sigV4,
RdsProxyTlsCertificates tlsCertificates,
PasswordValidator passwordValidator,
int handshakeTimeoutMillis,
IamUserChecker iamUserChecker,
RdsMysqlBinding mysqlBinding) throws IOException {
client.setSoTimeout(handshakeTimeoutMillis);
// The backend accepted the TCP connection but may never answer (or answer only
@@ -169,21 +208,34 @@ public class MySqlProtocolHandler {
}
// Phase 4: Validate credentials against the backend nonce.
// IAM token sent up front in cleartext: validate SigV4, then connect to backend as master.
// 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];
byte[] clientAuthData = parsed[1] != null
? parsed[1].getBytes(StandardCharsets.ISO_8859_1) : new byte[0];
ParsedHandshakeResponse parsed = parseHandshakeResponse(
Arrays.copyOfRange(clientResponseRaw, 4, clientResponseRaw.length));
String clientUsername = parsed.username();
boolean masterUser = masterUsername.equals(clientUsername);
boolean tlsEstablished = client instanceof SSLSocket ssl && ssl.getSession().isValid();
boolean iamLogin = iamEnabled && tlsEstablished && MYSQL_CLEAR_PASSWORD.equals(parsed.authPlugin())
&& isIamToken(clearPassword(parsed.authData()));
boolean valid;
try {
if (masterUsername.equals(clientUsername)) {
if (iamEnabled && MYSQL_CLEAR_PASSWORD.equals(parsed.authPlugin())
&& isIamToken(clearPassword(parsed.authData())) && !tlsEstablished) {
valid = false;
} else if (iamLogin) {
valid = sigV4.validate(clearPassword(parsed.authData()), clientUsername, mysqlBinding);
if (valid) {
clientResponseRaw = rewriteHandshakeCredentials(
clientResponseRaw, parsed, masterUsername,
scramblePassword(masterPassword, backendNonce, backendAuthPlugin),
backendAuthPlugin);
}
} else if (masterUser) {
byte[] expected = scramblePassword(
masterPassword, backendNonce, backendAuthPlugin);
valid = Arrays.equals(expected, clientAuthData);
valid = Arrays.equals(expected, parsed.authData());
} else {
// Non-master user: defer to backend — it knows their password.
valid = true;
@@ -194,7 +246,7 @@ public class MySqlProtocolHandler {
}
if (!valid) {
byte[] err = buildErrorPacket(1045,
byte[] err = buildErrorPacket(ER_ACCESS_DENIED_ERROR,
"Access denied for user '" + clientUsername + "'@'localhost' (using password: YES)");
writeMysqlPacket(clientOut, 2, err);
clientOut.flush();
@@ -205,15 +257,32 @@ public class MySqlProtocolHandler {
// Phase 5: Forward client's HandshakeResponse to backend, then relay the rest of the
// connection phase (renumbering if a TLS upgrade shifted the shared sequence counter)
// before handing off to the plain byte-for-byte bridge.
// before handing off to the plain byte-for-byte bridge. With IAM auth enabled, a non-master
// login may be an IAM user whose client only sends its token after an auth switch, so the
// backend's verdict is inspected before it reaches the client.
backendOut.write(clientResponseRaw);
backendOut.flush();
if (sequenceOffset != 0
&& !relayConnectionPhase(clientIn, clientOut, backendIn, backendOut, sequenceOffset)) {
closeQuietly(client);
closeQuietly(backend);
return;
boolean mayBeIamUser = iamEnabled && !masterUser && !iamLogin;
if (sequenceOffset != 0 || mayBeIamUser) {
byte[] verdict = relayConnectionPhase(
clientIn, clientOut, backendIn, backendOut, sequenceOffset);
if (verdict == null) {
closeQuietly(client);
closeQuietly(backend);
return;
}
if (mayBeIamUser && isAccessDenied(verdict)
&& tlsEstablished && iamUserChecker.isIamUser(clientUsername)) {
closeQuietly(backend);
authenticateIamUserAfterAuthSwitch(
client, clientIn, clientOut, verdict[3] & 0xFF, clientResponseRaw, parsed,
backendNonce, backendConnector, masterUsername, masterPassword, sigV4,
handshakeTimeoutMillis, mysqlBinding);
return;
}
clientOut.write(verdict);
clientOut.flush();
}
// Authenticated: clear the handshake deadline so a long-lived idle session is never killed.
@@ -224,29 +293,32 @@ public class MySqlProtocolHandler {
/**
* Relays packets between client and backend for the remainder of the connection phase,
* shifting each sequence number by {@code offset} to correct for a packet the backend never
* saw (see {@link #handleAuth}). Stops once the terminal OK/ERR packet reaches the client —
* at that point both sides independently reset their sequence counters for the next command,
* so the offset no longer applies and {@link #bridge} can take over untouched.
* shifting each sequence number by {@code offset} to correct for packets the backend never
* saw (see {@link #handleAuth}). Stops at the terminal OK/ERR packet and returns it renumbered
* but not yet forwarded, so the caller can act on the verdict. After it both sides
* independently reset their sequence counters for the next command, so the offset no longer
* applies and {@link #bridge} can take over untouched.
*
* @return {@code false} if either side closed the connection mid-exchange
* @return the terminal OK/ERR packet, or {@code null} if either side closed the connection
* mid-exchange
*/
private static boolean relayConnectionPhase(InputStream clientIn, OutputStream clientOut,
InputStream backendIn, OutputStream backendOut,
int offset) throws IOException {
private static byte[] relayConnectionPhase(InputStream clientIn, OutputStream clientOut,
InputStream backendIn, OutputStream backendOut,
int offset) throws IOException {
while (true) {
byte[] backendRaw = readMysqlPacketRaw(backendIn);
if (backendRaw == null || backendRaw.length < 5) {
return false;
return null;
}
backendRaw[3] = (byte) (backendRaw[3] + offset);
clientOut.write(backendRaw);
clientOut.flush();
int marker = backendRaw[4] & 0xFF;
if (marker == OK_PACKET_MARKER || marker == ERR_PACKET_MARKER) {
return true;
return backendRaw;
}
clientOut.write(backendRaw);
clientOut.flush();
if (marker == AUTH_MORE_DATA_MARKER && backendRaw.length > 5
&& (backendRaw[5] & 0xFF) == CACHING_SHA2_FAST_AUTH_SUCCESS) {
// caching_sha2_password fast-auth success: the client sends nothing back — the
@@ -259,7 +331,7 @@ public class MySqlProtocolHandler {
// once more before the backend issues its next verdict.
byte[] clientRaw = readMysqlPacketRaw(clientIn);
if (clientRaw == null || clientRaw.length < 4) {
return false;
return null;
}
clientRaw[3] = (byte) (clientRaw[3] - offset);
backendOut.write(clientRaw);
@@ -267,6 +339,127 @@ public class MySqlProtocolHandler {
}
}
// ── IAM authentication ────────────────────────────────────────────────────
/**
* Standard clients (libmysqlclient, PyMySQL, mysql2, Connector/J) answer the plugin the server
* advertised with a scramble of the IAM token, and send the token itself only when asked to
* switch to {@code mysql_clear_password}, as RDS does for {@code AWSAuthenticationPlugin} users.
* The backend holds those users with a password hash that never matches (see
* {@link #rewriteIamAuthPlugin}), so it rejects the scramble. This turns that rejection into the
* auth switch, validates the token, and authenticates the session as master on a fresh backend
* connection, since the backend drops the one whose login failed.
*
* @param switchSequence the sequence number the client expects the server's next packet at
*/
private static void authenticateIamUserAfterAuthSwitch(
Socket client, InputStream clientIn, OutputStream clientOut, int switchSequence,
byte[] clientResponseRaw, ParsedHandshakeResponse parsed, byte[] nonce,
PostgresProtocolHandler.BackendConnector backendConnector,
String masterUsername, String masterPassword, RdsSigV4Validator sigV4,
int handshakeTimeoutMillis, RdsMysqlBinding mysqlBinding) throws IOException {
if (!(client instanceof SSLSocket ssl) || !ssl.getSession().isValid()) {
closeQuietly(client);
return;
}
writeMysqlPacket(clientOut, switchSequence, buildAuthSwitchRequest(MYSQL_CLEAR_PASSWORD, nonce));
clientOut.flush();
byte[] reply = readMysqlPacketRaw(clientIn);
if (reply == null) {
closeQuietly(client);
return;
}
int nextSequence = (reply[3] & 0xFF) + 1;
String token = clearPassword(Arrays.copyOfRange(reply, 4, reply.length));
boolean valid;
try {
valid = isIamToken(token) && sigV4.validate(token, parsed.username(), mysqlBinding);
} catch (Exception e) {
LOG.warnv("MySQL IAM auth error: {0}", e.getMessage());
valid = false;
}
if (!valid) {
byte[] err = buildErrorPacket(ER_ACCESS_DENIED_ERROR, "Access denied for user '"
+ parsed.username() + "'@'localhost' (using password: YES)");
writeMysqlPacket(clientOut, nextSequence, err);
clientOut.flush();
closeQuietly(client);
return;
}
Socket backend = backendConnector.connect();
try {
backend.setSoTimeout(handshakeTimeoutMillis);
InputStream backendIn = backend.getInputStream();
OutputStream backendOut = backend.getOutputStream();
byte[] handshake = readMysqlPacketRaw(backendIn);
if (handshake == null || handshake.length < 5) {
LOG.warnv("MySQL backend sent no handshake");
closeQuietly(client);
closeQuietly(backend);
return;
}
String authPlugin = extractMysqlAuthPlugin(handshake);
byte[] masterResponse = rewriteHandshakeCredentials(
clientResponseRaw, parsed, masterUsername,
scramblePassword(masterPassword, extractMysqlNonce(handshake), authPlugin),
authPlugin);
// The fresh connection has seen nothing yet: it expects this response at sequence 1 and
// answers at 2, while the client expects that answer at nextSequence.
masterResponse[3] = 1;
backendOut.write(masterResponse);
backendOut.flush();
byte[] verdict = relayConnectionPhase(
clientIn, clientOut, backendIn, backendOut, nextSequence - 2);
if (verdict == null) {
closeQuietly(client);
closeQuietly(backend);
return;
}
clientOut.write(verdict);
clientOut.flush();
client.setSoTimeout(0);
backend.setSoTimeout(0);
} catch (Exception e) {
LOG.warnv("MySQL IAM auth against the backend failed: {0}", e.getMessage());
closeQuietly(client);
closeQuietly(backend);
return;
}
bridge(client, backend);
}
/** mysql_clear_password sends the password NUL-terminated. */
private static String clearPassword(byte[] authData) {
int length = authData.length;
if (length > 0 && authData[length - 1] == 0) {
length--;
}
return new String(authData, 0, length, StandardCharsets.UTF_8);
}
private static boolean isIamToken(String password) {
return password.contains("X-Amz-Signature");
}
private static boolean isAccessDenied(byte[] raw) {
return raw.length >= 7 && (raw[4] & 0xFF) == ERR_PACKET_MARKER
&& ((raw[5] & 0xFF) | ((raw[6] & 0xFF) << 8)) == ER_ACCESS_DENIED_ERROR;
}
/** AuthSwitchRequest: marker, NUL-terminated plugin name, then the plugin's challenge data. */
private static byte[] buildAuthSwitchRequest(String authPlugin, byte[] nonce) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.write(AUTH_SWITCH_REQUEST_MARKER);
baos.writeBytes(authPlugin.getBytes(StandardCharsets.UTF_8));
baos.write(0);
baos.writeBytes(nonce);
baos.write(0);
return baos.toByteArray();
}
// ── TLS upgrade ───────────────────────────────────────────────────────────
/**
@@ -415,7 +608,14 @@ public class MySqlProtocolHandler {
// ── Parse HandshakeResponse41 ─────────────────────────────────────────────
private static String[] parseHandshakeResponse(byte[] data) {
/**
* Parses a HandshakeResponse41 payload: capabilities, max-packet-size, charset, 23 reserved
* bytes, username and auth-response, then (each only when its capability flag is set) the
* database ({@code CLIENT_CONNECT_WITH_DB}), the client plugin name ({@code CLIENT_PLUGIN_AUTH}),
* connection attributes and a zstd compression level. Records where the auth-response and plugin
* name fields sit so {@link #rewriteHandshakeCredentials} can copy everything else verbatim.
*/
private static ParsedHandshakeResponse parseHandshakeResponse(byte[] data) {
int i = 0;
// 4 bytes: capabilities
int caps = (data[i] & 0xFF) | ((data[i + 1] & 0xFF) << 8)
@@ -437,37 +637,120 @@ public class MySqlProtocolHandler {
i++; // skip null
// auth-response
String password = "";
byte[] authData = new byte[0];
if (i < data.length) {
byte[] authData;
if ((caps & CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA) != 0) {
int[] consumed = {0};
long authLen = readLenencInt(data, i, consumed);
i += consumed[0];
authData = new byte[(int) authLen];
if (i + authData.length <= data.length) {
System.arraycopy(data, i, authData, 0, authData.length);
}
authData = Arrays.copyOfRange(data, i, (int) Math.min(data.length, i + authLen));
i += authData.length;
} else if ((caps & CLIENT_SECURE_CONNECTION) != 0) {
int authLen = data[i] & 0xFF;
i++;
authData = new byte[authLen];
if (i + authLen <= data.length) {
System.arraycopy(data, i, authData, 0, authLen);
}
authData = Arrays.copyOfRange(data, i, Math.min(data.length, i + authLen));
i += authData.length;
} else {
int passStart = i;
while (i < data.length && data[i] != 0) {
i++;
}
authData = new byte[i - passStart];
System.arraycopy(data, passStart, authData, 0, authData.length);
authData = Arrays.copyOfRange(data, passStart, i);
i++; // skip null
}
// Preserve raw bytes using ISO-8859-1 so binary scramble data survives
password = new String(authData, StandardCharsets.ISO_8859_1);
}
i = Math.min(i, data.length);
int authEnd = i;
// null-terminated database
if ((caps & CLIENT_CONNECT_WITH_DB) != 0) {
while (i < data.length && data[i] != 0) {
i++;
}
i = Math.min(i + 1, data.length);
}
return new String[]{username, password};
// null-terminated client plugin name
int pluginStart = i;
String authPlugin = null;
if ((caps & CLIENT_PLUGIN_AUTH) != 0) {
while (i < data.length && data[i] != 0) {
i++;
}
if (i > pluginStart) {
authPlugin = new String(data, pluginStart, i - pluginStart, StandardCharsets.UTF_8);
}
i = Math.min(i + 1, data.length);
}
return new ParsedHandshakeResponse(
caps, username, authData, authPlugin, authEnd, pluginStart, i);
}
/**
* @param authEnd payload offset just past the auth-response field
* @param pluginStart payload offset of the client plugin name field (where it would be if absent)
* @param pluginEnd payload offset just past the client plugin name field
*/
private record ParsedHandshakeResponse(int capabilities, String username, byte[] authData,
String authPlugin, int authEnd, int pluginStart,
int pluginEnd) {}
/**
* Rebuilds a HandshakeResponse41 with other credentials and client plugin name, copying the
* database, connection attributes and any trailing bytes verbatim and in place.
*/
private static byte[] rewriteHandshakeCredentials(byte[] raw, ParsedHandshakeResponse parsed,
String username, byte[] authData,
String authPlugin) {
int capabilities = parsed.capabilities();
ByteArrayOutputStream rewritten = new ByteArrayOutputStream(raw.length);
// Capabilities, max-packet-size, charset and reserved bytes: the prefix an SSLRequest carries.
rewritten.write(raw, 4, SSL_REQUEST_PAYLOAD_LENGTH);
rewritten.writeBytes(username.getBytes(StandardCharsets.UTF_8));
rewritten.write(0);
if ((capabilities & CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA) != 0) {
writeLenencInt(rewritten, authData.length);
rewritten.writeBytes(authData);
} else if ((capabilities & CLIENT_SECURE_CONNECTION) != 0) {
rewritten.write(authData.length);
rewritten.writeBytes(authData);
} else {
rewritten.writeBytes(authData);
rewritten.write(0);
}
// The database, if any, sits between the auth-response and the plugin name.
rewritten.write(raw, 4 + parsed.authEnd(), parsed.pluginStart() - parsed.authEnd());
if ((capabilities & CLIENT_PLUGIN_AUTH) != 0) {
rewritten.writeBytes(authPlugin.getBytes(StandardCharsets.UTF_8));
rewritten.write(0);
}
// Connection attributes and zstd compression level.
rewritten.write(raw, 4 + parsed.pluginEnd(), raw.length - 4 - parsed.pluginEnd());
byte[] result = new byte[4 + rewritten.size()];
int length = rewritten.size();
result[0] = (byte) (length & 0xFF);
result[1] = (byte) ((length >> 8) & 0xFF);
result[2] = (byte) ((length >> 16) & 0xFF);
result[3] = raw[3];
System.arraycopy(rewritten.toByteArray(), 0, result, 4, length);
return result;
}
private static void writeLenencInt(ByteArrayOutputStream out, int value) {
if (value < 0xFB) {
out.write(value);
} else if (value <= 0xFFFF) {
out.write(0xFC);
out.write(value & 0xFF);
out.write((value >> 8) & 0xFF);
} else {
out.write(0xFD);
out.write(value & 0xFF);
out.write((value >> 8) & 0xFF);
out.write((value >> 16) & 0xFF);
}
}
private static long readLenencInt(byte[] data, int offset, int[] consumed) {
@@ -560,6 +843,9 @@ public class MySqlProtocolHandler {
return null;
}
int length = b0 | (b1 << 8) | (b2 << 16);
if (length > MAX_PACKET_PAYLOAD) {
throw new IOException("MySQL packet exceeds protocol limit");
}
byte[] raw = new byte[4 + length];
raw[0] = (byte) b0;
raw[1] = (byte) b1;
@@ -585,6 +871,11 @@ public class MySqlProtocolHandler {
out.write(payload);
}
@FunctionalInterface
public interface IamUserChecker {
boolean isIamUser(String username);
}
private static byte[] buildErrorPacket(int errorCode, String message) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.write(0xFF); // ERR marker
@@ -27,6 +27,8 @@ public class RdsAuthProxy {
private final String dbName;
private final DatabaseEngine engine;
private final RdsSigV4Validator sigV4;
private final RdsMysqlBinding mysqlBinding;
private final MySqlProtocolHandler.IamUserChecker iamUserChecker;
private final RdsProxyTlsCertificates tlsCertificates;
private final MasterPasswordCheck passwordValidator;
private final int handshakeTimeoutMillis;
@@ -43,6 +45,32 @@ public class RdsAuthProxy {
MasterPasswordCheck passwordValidator,
int handshakeTimeoutMillis, int backendConnectTimeoutMillis,
int maxConnections) {
this(instanceId, backendHost, backendPort, engine, iamEnabled, masterUsername, masterPassword,
dbName, sigV4, tlsCertificates, passwordValidator, handshakeTimeoutMillis,
backendConnectTimeoutMillis, maxConnections, null, username -> false);
}
public RdsAuthProxy(String instanceId, String backendHost, int backendPort,
DatabaseEngine engine, boolean iamEnabled,
String masterUsername, String masterPassword, String dbName,
RdsSigV4Validator sigV4, RdsProxyTlsCertificates tlsCertificates,
MasterPasswordCheck passwordValidator,
int handshakeTimeoutMillis, int backendConnectTimeoutMillis,
int maxConnections, RdsMysqlBinding mysqlBinding) {
this(instanceId, backendHost, backendPort, engine, iamEnabled, masterUsername, masterPassword,
dbName, sigV4, tlsCertificates, passwordValidator, handshakeTimeoutMillis,
backendConnectTimeoutMillis, maxConnections, mysqlBinding,
username -> mysqlBinding != null);
}
public RdsAuthProxy(String instanceId, String backendHost, int backendPort,
DatabaseEngine engine, boolean iamEnabled,
String masterUsername, String masterPassword, String dbName,
RdsSigV4Validator sigV4, RdsProxyTlsCertificates tlsCertificates,
MasterPasswordCheck passwordValidator,
int handshakeTimeoutMillis, int backendConnectTimeoutMillis,
int maxConnections, RdsMysqlBinding mysqlBinding,
MySqlProtocolHandler.IamUserChecker iamUserChecker) {
this.instanceId = instanceId;
this.backendHost = backendHost;
this.backendPort = backendPort;
@@ -52,6 +80,8 @@ public class RdsAuthProxy {
this.masterPassword = masterPassword;
this.dbName = dbName;
this.sigV4 = sigV4;
this.mysqlBinding = mysqlBinding;
this.iamUserChecker = iamUserChecker;
this.tlsCertificates = tlsCertificates;
this.passwordValidator = passwordValidator;
this.handshakeTimeoutMillis = handshakeTimeoutMillis;
@@ -135,15 +165,16 @@ public class RdsAuthProxy {
: PasswordValidator.AuthResult.REJECT;
};
PostgresProtocolHandler.BackendConnector connector = () -> {
Socket backendSocket = new Socket();
backendSocket.connect(new InetSocketAddress(backendHost, backendPort),
backendConnectTimeoutMillis);
backendSocket.setTcpNoDelay(true);
return backendSocket;
};
switch (engine) {
case POSTGRES -> {
PostgresProtocolHandler.BackendConnector connector = () -> {
Socket backendSocket = new Socket();
backendSocket.connect(new InetSocketAddress(backendHost, backendPort),
backendConnectTimeoutMillis);
backendSocket.setTcpNoDelay(true);
return backendSocket;
};
session = PostgresProtocolHandler.authenticate(
client, connector, masterUsername, masterPassword, dbName,
iamEnabled, sigV4, tlsCertificates, authAdapter,
@@ -153,14 +184,12 @@ public class RdsAuthProxy {
}
}
case MYSQL, MARIADB -> {
backend = new Socket();
backend.connect(new InetSocketAddress(backendHost, backendPort),
backendConnectTimeoutMillis);
backend.setTcpNoDelay(true);
backend = connector.connect();
MySqlProtocolHandler.handleAuth(
client, backend, masterUsername, masterPassword,
client, backend, connector, masterUsername, masterPassword,
iamEnabled, sigV4, tlsCertificates, authAdapter,
handshakeTimeoutMillis);
handshakeTimeoutMillis,
iamUserChecker, mysqlBinding);
}
}
} catch (Exception e) {
@@ -0,0 +1,5 @@
package io.github.hectorvent.floci.services.rds.proxy;
/** The published identity used when validating a MySQL RDS IAM token. */
public record RdsMysqlBinding(String advertisedHost, int publishedPort, String region) {
}
@@ -35,13 +35,24 @@ public class RdsProxyManager {
String advertisedHost,
String masterUsername, String masterPassword, String dbName,
RdsAuthProxy.MasterPasswordCheck passwordValidator) {
startProxy(instanceId, engine, iamEnabled, proxyPort, backendHost, backendPort,
advertisedHost, masterUsername, masterPassword, dbName, passwordValidator,
new RdsMysqlBinding(advertisedHost, proxyPort, regionFromRelayKey(instanceId)));
}
public synchronized void startProxy(String instanceId, DatabaseEngine engine, boolean iamEnabled,
int proxyPort, String backendHost, int backendPort,
String advertisedHost,
String masterUsername, String masterPassword, String dbName,
RdsAuthProxy.MasterPasswordCheck passwordValidator,
RdsMysqlBinding mysqlBinding) {
tlsCertificates.ensureHost(advertisedHost);
EmulatorConfig.RdsServiceConfig rdsConfig = config.services().rds();
RdsAuthProxy proxy = new RdsAuthProxy(
instanceId, backendHost, backendPort, engine, iamEnabled,
masterUsername, masterPassword, dbName, sigV4Validator, tlsCertificates, passwordValidator,
rdsConfig.proxyHandshakeTimeoutMillis(), rdsConfig.proxyBackendConnectTimeoutMillis(),
rdsConfig.proxyMaxConnections());
rdsConfig.proxyMaxConnections(), mysqlBinding);
try {
proxy.start(proxyPort);
} catch (IOException e) {
@@ -80,6 +91,17 @@ public class RdsProxyManager {
}
}
private String regionFromRelayKey(String relayKey) {
int arnStart = relayKey.indexOf("arn:");
if (arnStart >= 0) {
String[] parts = relayKey.substring(arnStart).split(":", 6);
if (parts.length > 3 && !parts[3].isBlank()) {
return parts[3];
}
}
return config.defaultRegion();
}
public synchronized void updateMasterPassword(String instanceId, String newPassword) {
RdsAuthProxy proxy = proxies.get(instanceId);
if (proxy != null) {
@@ -7,6 +7,8 @@ import jakarta.inject.Inject;
import org.jboss.logging.Logger;
import java.net.URI;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
/**
* Validates RDS IAM auth tokens (SigV4 presigned URLs).
@@ -40,6 +42,10 @@ public class RdsSigV4Validator {
* @return true if the token signature is valid, the DBUser matches, and the token is not expired
*/
public boolean validate(String token, String clientUsername) {
return validate(token, clientUsername, null);
}
public boolean validate(String token, String clientUsername, RdsMysqlBinding binding) {
try {
URI uri = URI.create("http://" + token);
String host = uri.getHost();
@@ -54,6 +60,12 @@ public class RdsSigV4Validator {
// RDS tokens sign host:port in the canonical host header
String authority = (port > 0) ? host + ":" + port : host;
if (binding != null && (!binding.advertisedHost().equalsIgnoreCase(host)
|| binding.publishedPort() != port
|| !credentialMatches(rawQuery, binding.region()))) {
return false;
}
return requestValidator.validate(rawQuery, authority, "DBUser", true, clientUsername, "RDS IAM token");
} catch (Exception e) {
LOG.debugv("RDS IAM token validation error: {0}", e.getMessage());
@@ -61,6 +73,18 @@ public class RdsSigV4Validator {
}
}
private static boolean credentialMatches(String rawQuery, String region) {
for (String pair : rawQuery.split("&")) {
int eq = pair.indexOf('=');
if (eq >= 0 && "X-Amz-Credential".equals(pair.substring(0, eq))) {
String[] parts = URLDecoder.decode(pair.substring(eq + 1), StandardCharsets.UTF_8)
.split("/");
return parts.length >= 5 && region.equals(parts[2]) && "rds-db".equals(parts[3]);
}
}
return false;
}
/**
* Kept here, not only in SigV4RequestValidator, because RdsSigV4ValidatorTest reflects
* on this exact declared method to verify log-injection protection at this class's own
@@ -3,8 +3,11 @@ package io.github.hectorvent.floci.services.rds.proxy;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.services.acm.CertificateGenerator;
import io.github.hectorvent.floci.testutil.IamServiceTestHelper;
import io.github.hectorvent.floci.testutil.SigV4TokenTestHelper;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;
@@ -15,17 +18,24 @@ import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UncheckedIOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.security.MessageDigest;
import java.security.cert.X509Certificate;
import java.time.Instant;
import java.util.Arrays;
import java.util.HexFormat;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -36,6 +46,25 @@ class MySqlProtocolHandlerTest {
private static final int CLIENT_SECURE_CONNECTION = 0x8000;
private static final int CLIENT_SSL = 0x0800;
private static final int CLIENT_PLUGIN_AUTH = 0x0008_0000;
private static final String MYSQL_NATIVE_PASSWORD = "mysql_native_password";
private static final String MYSQL_CLEAR_PASSWORD = "mysql_clear_password";
private static final int CLIENT_CONNECT_WITH_DB = 0x0008;
private static final int CLIENT_CONNECT_ATTRS = 0x0010_0000;
private static final int CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA = 0x0020_0000;
private static final int CLIENT_ZSTD_COMPRESSION_ALGORITHM = 0x0400_0000;
/** What libmysqlclient 8 sends: every optional field after the auth-response is present. */
private static final int FULL_RESPONSE_CAPABILITIES = CLIENT_PROTOCOL_41 | CLIENT_SECURE_CONNECTION
| CLIENT_CONNECT_WITH_DB | CLIENT_PLUGIN_AUTH | CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA
| CLIENT_CONNECT_ATTRS | CLIENT_ZSTD_COMPRESSION_ALGORITHM;
private static final int ER_ACCESS_DENIED = 1045;
private static final byte[] OK_PAYLOAD = {0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00};
private static final byte[] MASTER_BACKEND_NONCE =
"second-backend-nonce".getBytes(StandardCharsets.US_ASCII);
/** Fails a login that opens a second backend connection; only an IAM auth switch may. */
private static final PostgresProtocolHandler.BackendConnector NO_RECONNECT = () -> {
throw new IOException("unexpected second backend connection");
};
@TempDir
Path tempDir;
@@ -64,7 +93,7 @@ class MySqlProtocolHandlerTest {
Thread authThread = Thread.ofVirtual().start(() -> {
try {
MySqlProtocolHandler.handleAuth(
proxyClient, backend, "admin", "secret",
proxyClient, backend, NO_RECONNECT, "admin", "secret",
false, testSigV4Validator(), testTlsCertificates(),
(user, pass) -> PasswordValidator.AuthResult.MASTER_EQUIVALENT, 5000);
} catch (IOException e) {
@@ -126,7 +155,7 @@ class MySqlProtocolHandlerTest {
Thread authThread = Thread.ofVirtual().start(() -> {
try {
MySqlProtocolHandler.handleAuth(
proxyClient, backend, "admin", "secret",
proxyClient, backend, NO_RECONNECT, "admin", "secret",
false, testSigV4Validator(), testTlsCertificates(),
(user, pass) -> PasswordValidator.AuthResult.MASTER_EQUIVALENT, 5000);
} catch (IOException e) {
@@ -182,7 +211,7 @@ class MySqlProtocolHandlerTest {
Thread authThread = Thread.ofVirtual().start(() -> {
try {
MySqlProtocolHandler.handleAuth(
proxyClient, backend, "admin", "secret",
proxyClient, backend, NO_RECONNECT, "admin", "secret",
false, testSigV4Validator(), tlsCertificates,
(user, pass) -> PasswordValidator.AuthResult.MASTER_EQUIVALENT, 5000);
} catch (IOException e) {
@@ -280,7 +309,7 @@ class MySqlProtocolHandlerTest {
Thread authThread = Thread.ofVirtual().start(() -> {
try {
MySqlProtocolHandler.handleAuth(
proxyClient, backend, "admin", "secret",
proxyClient, backend, NO_RECONNECT, "admin", "secret",
false, testSigV4Validator(), tlsCertificates,
(user, pass) -> PasswordValidator.AuthResult.MASTER_EQUIVALENT, 5000);
} catch (IOException e) {
@@ -341,7 +370,7 @@ class MySqlProtocolHandlerTest {
Thread authThread = Thread.ofVirtual().start(() -> {
try {
MySqlProtocolHandler.handleAuth(
proxyClient, backend, "admin", "secret",
proxyClient, backend, NO_RECONNECT, "admin", "secret",
false, testSigV4Validator(), testTlsCertificates(),
(user, pass) -> PasswordValidator.AuthResult.MASTER_EQUIVALENT, 200);
} catch (IOException e) {
@@ -363,6 +392,303 @@ class MySqlProtocolHandlerTest {
}
}
// ── RDS IAM authentication ───────────────────────────────────────────────
@Test
void switchesIamLoginToClearPasswordAndAuthenticatesBackendAsMaster() throws Exception {
AuthSwitchLogin login = authSwitchLogin("app", rdsToken("app", Instant.now()), true, true);
assertNotNull(login.authSwitch(), "an access-denied backend verdict must become an auth switch");
assertEquals(3, login.authSwitch()[3] & 0xFF);
assertEquals(MYSQL_CLEAR_PASSWORD, authSwitchPluginName(login.authSwitch()));
assertOk(login.clientVerdict(), 5);
assertEquals(1, login.reconnects());
// The master leg keeps the client's database, attributes and zstd level, in wire order.
assertArrayEquals(
fullHandshakeResponse41("admin", scrambleNativePassword("secret", MASTER_BACKEND_NONCE),
MYSQL_NATIVE_PASSWORD, FULL_RESPONSE_CAPABILITIES, 1),
login.masterResponse());
}
@Test
void iamAuthSwitchKeepsSequenceNumbersInStepAfterTlsUpgrade() throws Exception {
AuthSwitchLogin login = authSwitchLogin("app", rdsToken("app", Instant.now()), true, true);
// SSLRequest(1) and HandshakeResponse(2) leave the client one packet ahead of the backend.
assertNotNull(login.authSwitch());
assertEquals(3, login.authSwitch()[3] & 0xFF);
assertOk(login.clientVerdict(), 5);
assertArrayEquals(
fullHandshakeResponse41("admin", scrambleNativePassword("secret", MASTER_BACKEND_NONCE),
MYSQL_NATIVE_PASSWORD, FULL_RESPONSE_CAPABILITIES, 1),
login.masterResponse(),
"the fresh backend connection expects the response at sequence 1 without CLIENT_SSL");
}
@Test
void rejectsIamTokenWithTamperedSignature() throws Exception {
String token = rdsToken("app", Instant.now());
String tampered = token.substring(0, token.length() - 1) + (token.endsWith("0") ? "1" : "0");
assertIamLoginRejected(authSwitchLogin("app", tampered, true, false));
}
@Test
void rejectsExpiredIamToken() throws Exception {
String expired = rdsToken("app", Instant.now().minusSeconds(3600));
assertIamLoginRejected(authSwitchLogin("app", expired, true, false));
}
@Test
void rejectsIamTokenIssuedForAnotherDbUser() throws Exception {
String otherUser = rdsToken("reporting", Instant.now());
assertIamLoginRejected(authSwitchLogin("app", otherUser, true, false));
}
@Test
void iamDisabledRelaysBackendRejectionWithoutAuthSwitch() throws Exception {
AuthSwitchLogin login = authSwitchLogin("app", rdsToken("app", Instant.now()), false, false);
assertNull(login.authSwitch(), "the proxy must not ask for an IAM token when IAM auth is disabled");
assertAccessDenied(login.clientVerdict(), 2);
assertEquals(0, login.reconnects());
}
@Test
void validatesIamTokenSentUpFrontAfterTheDatabaseAndAuthenticatesBackendAsMaster() throws Exception {
byte[] response = fullHandshakeResponse41("app", nulTerminated(rdsToken("app", Instant.now())),
MYSQL_CLEAR_PASSWORD, FULL_RESPONSE_CAPABILITIES, 1);
DirectLogin login = directLogin(response, true);
assertAccessDenied(login.clientVerdict(), 2);
assertNull(login.backendResponse(), "clear-password IAM must require TLS");
}
@Test
void iamDisabledForwardsClearPasswordLoginToBackendUnmodified() throws Exception {
byte[] response = fullHandshakeResponse41("app", nulTerminated(rdsToken("app", Instant.now())),
MYSQL_CLEAR_PASSWORD, FULL_RESPONSE_CAPABILITIES, 1);
DirectLogin login = directLogin(response, false);
assertOk(login.clientVerdict(), 2);
assertArrayEquals(response, login.backendResponse(),
"without IAM auth the backend, not the proxy, decides on a non-master login");
}
@ParameterizedTest
@ValueSource(booleans = {false, true})
void forwardsNonMasterNativePasswordLoginUnmodified(boolean iamEnabled) throws Exception {
byte[] response = fullHandshakeResponse41("app", scrambleNativePassword("app-password", fixedNonce()),
MYSQL_NATIVE_PASSWORD, FULL_RESPONSE_CAPABILITIES, 1);
DirectLogin login = directLogin(response, iamEnabled);
assertOk(login.clientVerdict(), 2);
assertArrayEquals(response, login.backendResponse());
}
@Test
void forwardsMasterPasswordLoginUnmodifiedWhenIamEnabled() throws Exception {
byte[] response = fullHandshakeResponse41("admin", scrambleNativePassword("secret", fixedNonce()),
MYSQL_NATIVE_PASSWORD, FULL_RESPONSE_CAPABILITIES, 1);
DirectLogin login = directLogin(response, true);
assertOk(login.clientVerdict(), 2);
assertArrayEquals(response, login.backendResponse());
}
@Test
void rejectsWrongMasterPasswordWithoutAuthSwitchWhenIamEnabled() throws Exception {
byte[] response = fullHandshakeResponse41("admin", scrambleNativePassword("wrong", fixedNonce()),
MYSQL_NATIVE_PASSWORD, FULL_RESPONSE_CAPABILITIES, 1);
DirectLogin login = directLogin(response, true);
assertAccessDenied(login.clientVerdict(), 2);
assertNull(login.backendResponse(), "a rejected master login must never reach the backend");
}
private static String rdsToken(String dbUser, Instant issuedAt) throws Exception {
return SigV4TokenTestHelper.createRdsToken("mydb.abc123.us-east-1.rds.amazonaws.com", 3306,
dbUser, "AKIATEST", "secret", issuedAt, 900);
}
private static void assertIamLoginRejected(AuthSwitchLogin login) {
assertNull(login.authSwitch(), "invalid or non-TLS IAM must not receive an auth switch");
assertAccessDenied(login.clientVerdict(), 2);
assertEquals(0, login.reconnects(), "an invalid token must never reach the backend as master");
assertNull(login.masterResponse());
}
private static void assertOk(byte[] packet, int sequence) {
assertNotNull(packet);
assertEquals(0x00, packet[4] & 0xFF, "expected an OK packet");
assertEquals(sequence, packet[3] & 0xFF);
}
private static void assertAccessDenied(byte[] packet, int sequence) {
assertNotNull(packet);
assertEquals(0xFF, packet[4] & 0xFF, "expected an ERR packet");
assertEquals(ER_ACCESS_DENIED, (packet[5] & 0xFF) | ((packet[6] & 0xFF) << 8));
assertEquals(sequence, packet[3] & 0xFF);
}
private record DirectLogin(byte[] clientVerdict, byte[] backendResponse) {}
/** One client HandshakeResponse against a single backend connection that accepts it. */
private DirectLogin directLogin(byte[] clientResponse, boolean iamEnabled) throws Exception {
AtomicReference<byte[]> backendResponse = new AtomicReference<>();
try (ServerSocket backendServer = new ServerSocket(0);
ServerSocket clientServer = new ServerSocket(0);
Socket ourClient = new Socket("localhost", clientServer.getLocalPort());
Socket proxyClient = clientServer.accept();
Socket backend = new Socket("localhost", backendServer.getLocalPort())) {
ourClient.setSoTimeout(5_000);
Thread backendThread = startBackend(backendServer, fixedNonce(), OK_PAYLOAD, backendResponse);
Thread authThread = startProxy(proxyClient, backend, NO_RECONNECT, iamEnabled,
testTlsCertificates());
InputStream clientIn = ourClient.getInputStream();
OutputStream clientOut = ourClient.getOutputStream();
assertNotNull(readMysqlPacketRaw(clientIn));
clientOut.write(clientResponse);
clientOut.flush();
byte[] verdict = readMysqlPacketRaw(clientIn);
ourClient.close();
proxyClient.close();
joinAll(authThread, backendThread);
return new DirectLogin(verdict, backendResponse.get());
}
}
private record AuthSwitchLogin(byte[] authSwitch, byte[] clientVerdict, byte[] masterResponse,
int reconnects) {}
/**
* Drives a cleartext-enabled client the way libmysqlclient, PyMySQL, mysql2 and Connector/J log in
* with an IAM token: the HandshakeResponse answers the advertised mysql_native_password with a
* scramble of the token, and the token itself is only sent if the server switches to
* mysql_clear_password. The first backend connection rejects that scramble — as the real backend
* does for an IAM user, whose password hash never matches — and a second one, if the proxy opens
* it, accepts whatever it receives.
*/
private AuthSwitchLogin authSwitchLogin(String username, String token, boolean iamEnabled, boolean tls)
throws Exception {
AtomicReference<byte[]> rejectedResponse = new AtomicReference<>();
AtomicReference<byte[]> masterResponse = new AtomicReference<>();
AtomicReference<Thread> masterBackendThread = new AtomicReference<>();
AtomicInteger reconnects = new AtomicInteger();
RdsProxyTlsCertificates tlsCertificates = testTlsCertificates();
if (tls) {
tlsCertificates.ensureHost("172.17.0.8");
}
try (ServerSocket backendServer = new ServerSocket(0);
ServerSocket masterBackendServer = new ServerSocket(0);
ServerSocket clientServer = new ServerSocket(0);
Socket ourClient = new Socket("localhost", clientServer.getLocalPort());
Socket proxyClient = clientServer.accept();
Socket backend = new Socket("localhost", backendServer.getLocalPort())) {
ourClient.setSoTimeout(5_000);
PostgresProtocolHandler.BackendConnector connector = () -> {
reconnects.incrementAndGet();
masterBackendThread.set(startBackend(
masterBackendServer, MASTER_BACKEND_NONCE, OK_PAYLOAD, masterResponse));
return new Socket("localhost", masterBackendServer.getLocalPort());
};
Thread backendThread = startBackend(
backendServer, fixedNonce(), accessDeniedPayload(), rejectedResponse);
Thread authThread = startProxy(proxyClient, backend, connector, iamEnabled, tlsCertificates);
InputStream clientIn = ourClient.getInputStream();
OutputStream clientOut = ourClient.getOutputStream();
assertNotNull(readMysqlPacketRaw(clientIn));
int capabilities = FULL_RESPONSE_CAPABILITIES;
int sequence = 1;
if (tls) {
clientOut.write(buildSslRequest());
clientOut.flush();
SSLSocket sslClient = trustedClientSocket(ourClient);
sslClient.startHandshake();
clientIn = sslClient.getInputStream();
clientOut = sslClient.getOutputStream();
capabilities |= CLIENT_SSL;
sequence = 2;
}
clientOut.write(fullHandshakeResponse41(username, scrambleNativePassword(token, fixedNonce()),
MYSQL_NATIVE_PASSWORD, capabilities, sequence));
clientOut.flush();
byte[] reply = readMysqlPacketRaw(clientIn);
byte[] authSwitch = null;
if (reply != null && (reply[4] & 0xFF) == 0xFE) {
authSwitch = reply;
clientOut.write(wrapPacket((reply[3] & 0xFF) + 1, nulTerminated(token)));
clientOut.flush();
reply = readMysqlPacketRaw(clientIn);
}
ourClient.close();
proxyClient.close();
joinAll(authThread, backendThread);
if (masterBackendThread.get() != null) {
joinAll(masterBackendThread.get());
}
return new AuthSwitchLogin(authSwitch, reply, masterResponse.get(), reconnects.get());
}
}
/**
* Backend that sends its handshake, records the HandshakeResponse it receives (null if the proxy
* never forwards one), answers it with {@code verdict} at sequence 2, then closes.
*/
private static Thread startBackend(ServerSocket server, byte[] nonce, byte[] verdict,
AtomicReference<byte[]> response) {
return Thread.ofVirtual().start(() -> {
try (Socket socket = server.accept()) {
OutputStream out = socket.getOutputStream();
out.write(buildHandshakeV10(nonce, MYSQL_NATIVE_PASSWORD));
out.flush();
byte[] raw = readMysqlPacketRaw(socket.getInputStream());
response.set(raw);
if (raw != null) {
writeMysqlPacket(out, 2, verdict);
out.flush();
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
});
}
private static Thread startProxy(Socket proxyClient, Socket backend,
PostgresProtocolHandler.BackendConnector connector,
boolean iamEnabled, RdsProxyTlsCertificates tlsCertificates) {
return Thread.ofVirtual().start(() -> {
try {
MySqlProtocolHandler.handleAuth(
proxyClient, backend, connector, "admin", "secret",
iamEnabled, testSigV4Validator(), tlsCertificates,
(user, pass) -> PasswordValidator.AuthResult.PASSTHROUGH, 5000);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
});
}
private static void joinAll(Thread... threads) throws InterruptedException {
for (Thread thread : threads) {
thread.join(5_000);
assertFalse(thread.isAlive(), "thread did not terminate");
}
}
private RdsProxyTlsCertificates testTlsCertificates() {
EmulatorConfig.StorageConfig storage = mock(EmulatorConfig.StorageConfig.class);
when(storage.persistentPath()).thenReturn(tempDir.toString());
@@ -489,6 +815,66 @@ class MySqlProtocolHandlerTest {
return wrapPacket(sequence, payload.toByteArray());
}
/**
* A HandshakeResponse41 shaped like libmysqlclient 8's: a length-encoded auth-response followed by
* the database, client plugin name, connection attributes and zstd compression level, in the
* order the protocol defines them.
*/
private static byte[] fullHandshakeResponse41(String username, byte[] authData, String plugin,
int capabilities, int sequence) {
ByteArrayOutputStream payload = new ByteArrayOutputStream();
writeInt32LE(payload, capabilities);
writeInt32LE(payload, 0x0100_0000); // max packet size
payload.write(0xFF); // charset utf8mb4_0900_ai_ci
payload.writeBytes(new byte[23]); // reserved
payload.writeBytes(nulTerminated(username));
writeLenenc(payload, authData.length);
payload.writeBytes(authData);
payload.writeBytes(nulTerminated("appdb"));
payload.writeBytes(nulTerminated(plugin));
ByteArrayOutputStream attributes = new ByteArrayOutputStream();
for (String entry : new String[]{"_client_name", "libmysql", "program_name", "mysql"}) {
byte[] bytes = entry.getBytes(StandardCharsets.UTF_8);
writeLenenc(attributes, bytes.length);
attributes.writeBytes(bytes);
}
writeLenenc(payload, attributes.size());
payload.writeBytes(attributes.toByteArray());
payload.write(3); // zstd compression level
return wrapPacket(sequence, payload.toByteArray());
}
private static void writeLenenc(ByteArrayOutputStream out, int value) {
if (value < 0xFB) {
out.write(value);
} else {
out.write(0xFC);
writeInt16LE(out, value);
}
}
private static byte[] nulTerminated(String value) {
byte[] bytes = value.getBytes(StandardCharsets.UTF_8);
return Arrays.copyOf(bytes, bytes.length + 1);
}
private static byte[] accessDeniedPayload() {
ByteArrayOutputStream payload = new ByteArrayOutputStream();
payload.write(0xFF);
writeInt16LE(payload, ER_ACCESS_DENIED);
payload.writeBytes("#28000Access denied for user 'app'@'172.17.0.1' (using password: YES)"
.getBytes(StandardCharsets.UTF_8));
return payload.toByteArray();
}
private static String authSwitchPluginName(byte[] raw) {
int end = 5;
while (end < raw.length && raw[end] != 0) {
end++;
}
return new String(raw, 5, end - 5, StandardCharsets.UTF_8);
}
private static String extractUsername(byte[] payload) {
int i = 4 + 4 + 1 + 23; // caps + max-packet-size + charset + reserved
int start = i;
@@ -191,6 +191,56 @@ class RdsSigV4ValidatorTest {
"Token signed for 'admin' must be rejected when client connects as 'attacker'");
}
@Test
void validateBindsTokenToPublishedMysqlEndpointAndRegion() throws Exception {
IamService iamService = IamServiceTestHelper.iamServiceWithAccessKey("AKIDRDS", "secret-rds");
RdsSigV4Validator validator = new RdsSigV4Validator(iamService);
String token = SigV4TokenTestHelper.createRdsToken(
"db.example.local", 3307, "admin", "AKIDRDS", "secret-rds",
Instant.now().minusSeconds(60), 900);
assertTrue(validator.validate(token, "admin",
new RdsMysqlBinding("db.example.local", 3307, "us-east-1")));
assertFalse(validator.validate(token, "admin",
new RdsMysqlBinding("db.example.local", 3306, "us-east-1")));
}
@Test
void validateRejectsTokenSignedForAnotherHost() throws Exception {
IamService iamService = IamServiceTestHelper.iamServiceWithAccessKey("AKIDRDS", "secret-rds");
RdsSigV4Validator validator = new RdsSigV4Validator(iamService);
String token = SigV4TokenTestHelper.createRdsToken(
"db.example.local", 3307, "admin", "AKIDRDS", "secret-rds",
Instant.now().minusSeconds(60), 900);
assertFalse(validator.validate(token, "admin",
new RdsMysqlBinding("other.example.local", 3307, "us-east-1")));
}
@Test
void validateRejectsTokenSignedForAnotherRegion() throws Exception {
IamService iamService = IamServiceTestHelper.iamServiceWithAccessKey("AKIDRDS", "secret-rds");
RdsSigV4Validator validator = new RdsSigV4Validator(iamService);
String token = SigV4TokenTestHelper.createRdsToken(
"db.example.local", 3307, "admin", "AKIDRDS", "secret-rds",
Instant.now().minusSeconds(60), 900);
assertFalse(validator.validate(token, "admin",
new RdsMysqlBinding("db.example.local", 3307, "eu-west-1")));
}
@Test
void validateRejectsTokenSignedForAnotherService() throws Exception {
IamService iamService = IamServiceTestHelper.iamServiceWithAccessKey("AKIDRDS", "secret-rds");
RdsSigV4Validator validator = new RdsSigV4Validator(iamService);
String token = SigV4TokenTestHelper.createRdsTokenWithScope(
"db.example.local", 3307, "admin", "AKIDRDS", "secret-rds",
"us-east-1", "s3", Instant.now().minusSeconds(60), 900);
assertFalse(validator.validate(token, "admin",
new RdsMysqlBinding("db.example.local", 3307, "us-east-1")));
}
@Test
void validateAcceptsTokenWhenClientUsernameIsNull() throws Exception {
IamService iamService = IamServiceTestHelper.iamServiceWithAccessKey("AKIDRDS", "secret-rds");
@@ -78,6 +78,24 @@ public final class SigV4TokenTestHelper {
return createRdsToken(host, port, dbUser, accessKeyId, secretKey, timestamp, expiresSeconds, null);
}
public static String createRdsTokenWithScope(
String host,
int port,
String dbUser,
String accessKeyId,
String secretKey,
String region,
String service,
Instant timestamp,
int expiresSeconds
) throws Exception {
Map<String, String> params = new LinkedHashMap<>();
params.put("Action", "connect");
params.put("DBUser", dbUser);
return signToken(host, port, accessKeyId, secretKey, region, service, timestamp, expiresSeconds,
params, Map.of("host", host + ":" + port));
}
public static String createRdsToken(
String host,
int port,