mirror of
https://github.com/floci-io/floci.git
synced 2026-11-25 14:57:54 -05:00
fix(rds): use MySQL 8.4 auth options (#3001)
* fix(rds): use MySQL 8.4 auth options * fix(rds): reject unsupported MySQL images * fix(rds): validate caching SHA-2 auth
This commit is contained in:
1 parent
ffe8a92635
commit
e26e9a68fd
4 files changed
+248
-13
No files matched your search
+59
-8
@@ -30,6 +30,7 @@ import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
@@ -157,10 +158,10 @@ public class RdsContainerManager {
|
||||
specBuilder, storageResourceId, exactVolumeName, engine, image);
|
||||
|
||||
// Add engine-specific command
|
||||
List<String> cmd = buildContainerCmd(engine);
|
||||
if (!cmd.isEmpty()) {
|
||||
specBuilder.withCmd(cmd);
|
||||
}
|
||||
List<String> cmd = buildContainerCmd(engine, image);
|
||||
if (!cmd.isEmpty()) {
|
||||
specBuilder.withCmd(cmd);
|
||||
}
|
||||
|
||||
ContainerSpec spec = specBuilder.build();
|
||||
|
||||
@@ -667,12 +668,62 @@ public class RdsContainerManager {
|
||||
return envs;
|
||||
}
|
||||
|
||||
private List<String> buildContainerCmd(DatabaseEngine engine) {
|
||||
// Configure MySQL to use mysql_native_password so the proxy can authenticate
|
||||
// without needing caching_sha2_password RSA key exchange
|
||||
static List<String> buildContainerCmd(DatabaseEngine engine, String image) {
|
||||
// Keep the backend handshake on mysql_native_password so the transparent proxy can
|
||||
// validate the client scramble. MySQL 8.4 removed default-authentication-plugin and
|
||||
// requires the native plugin to be enabled separately before selecting it as the default.
|
||||
// Indeterminate tags and MySQL 9+ use the server default, which the proxy also supports.
|
||||
return switch (engine) {
|
||||
case MYSQL -> List.of("--default-authentication-plugin=mysql_native_password");
|
||||
case MYSQL -> {
|
||||
Optional<MySqlVersion> version = mysqlImageVersion(image);
|
||||
if (version.isEmpty() || version.get().major() >= 9) {
|
||||
yield List.of();
|
||||
}
|
||||
yield version.get().major() == 8 && version.get().minor() >= 4
|
||||
? List.of(
|
||||
"--mysql-native-password=ON",
|
||||
"--authentication-policy=mysql_native_password")
|
||||
: List.of("--default-authentication-plugin=mysql_native_password");
|
||||
}
|
||||
case POSTGRES, MARIADB -> List.of();
|
||||
};
|
||||
}
|
||||
|
||||
private static Optional<MySqlVersion> mysqlImageVersion(String image) {
|
||||
if (image == null || image.isBlank()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
String reference = image;
|
||||
int digestSeparator = reference.indexOf('@');
|
||||
if (digestSeparator >= 0) {
|
||||
reference = reference.substring(0, digestSeparator);
|
||||
}
|
||||
int slashSeparator = reference.lastIndexOf('/');
|
||||
int tagSeparator = reference.lastIndexOf(':');
|
||||
if (tagSeparator < slashSeparator || tagSeparator == reference.length() - 1) {
|
||||
return Optional.empty();
|
||||
}
|
||||
String tag = reference.substring(tagSeparator + 1);
|
||||
int dot = tag.indexOf('.');
|
||||
if (dot <= 0 || dot == tag.length() - 1) {
|
||||
return Optional.empty();
|
||||
}
|
||||
int minorEnd = dot + 1;
|
||||
while (minorEnd < tag.length() && Character.isDigit(tag.charAt(minorEnd))) {
|
||||
minorEnd++;
|
||||
}
|
||||
if (minorEnd == dot + 1) {
|
||||
return Optional.empty();
|
||||
}
|
||||
try {
|
||||
int major = Integer.parseInt(tag.substring(0, dot));
|
||||
int minor = Integer.parseInt(tag.substring(dot + 1, minorEnd));
|
||||
return Optional.of(new MySqlVersion(major, minor));
|
||||
} catch (NumberFormatException e) {
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
private record MySqlVersion(int major, int minor) {
|
||||
}
|
||||
}
|
||||
+73
-1
@@ -43,8 +43,11 @@ public class MySqlProtocolHandler {
|
||||
private static final Logger LOG = Logger.getLogger(MySqlProtocolHandler.class);
|
||||
|
||||
private static final int CLIENT_SSL = 0x0800;
|
||||
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 String MYSQL_NATIVE_PASSWORD = "mysql_native_password";
|
||||
private static final String CACHING_SHA2_PASSWORD = "caching_sha2_password";
|
||||
|
||||
// First payload byte of the packet types that can appear during the connection phase.
|
||||
private static final int OK_PACKET_MARKER = 0x00;
|
||||
@@ -78,6 +81,7 @@ public class MySqlProtocolHandler {
|
||||
|
||||
// Extract the backend's nonce for our credential validation
|
||||
byte[] backendNonce = extractMysqlNonce(backendHandshakeRaw);
|
||||
String backendAuthPlugin = extractMysqlAuthPlugin(backendHandshakeRaw);
|
||||
|
||||
// The proxy terminates TLS itself, so advertise CLIENT_SSL to the client even if the
|
||||
// (plaintext) backend didn't.
|
||||
@@ -153,7 +157,8 @@ public class MySqlProtocolHandler {
|
||||
boolean valid;
|
||||
try {
|
||||
if (masterUsername.equals(clientUsername)) {
|
||||
byte[] expected = scrambleNativePassword(masterPassword, backendNonce);
|
||||
byte[] expected = scramblePassword(
|
||||
masterPassword, backendNonce, backendAuthPlugin);
|
||||
valid = Arrays.equals(expected, clientAuthData);
|
||||
} else {
|
||||
// Non-master user: defer to backend — it knows their password.
|
||||
@@ -345,6 +350,42 @@ public class MySqlProtocolHandler {
|
||||
return nonce;
|
||||
}
|
||||
|
||||
private static String extractMysqlAuthPlugin(byte[] raw) {
|
||||
int i = 4 + 1;
|
||||
while (i < raw.length && raw[i] != 0) {
|
||||
i++;
|
||||
}
|
||||
i++;
|
||||
i += 4 + 8 + 1;
|
||||
if (i + 2 > raw.length) {
|
||||
return MYSQL_NATIVE_PASSWORD;
|
||||
}
|
||||
int capabilities = (raw[i] & 0xFF) | ((raw[i + 1] & 0xFF) << 8);
|
||||
i += 2;
|
||||
if (i + 5 > raw.length) {
|
||||
return MYSQL_NATIVE_PASSWORD;
|
||||
}
|
||||
i += 1 + 2;
|
||||
capabilities |= ((raw[i] & 0xFF) | ((raw[i + 1] & 0xFF) << 8)) << 16;
|
||||
i += 2;
|
||||
int authDataLen = raw[i] & 0xFF;
|
||||
i += 1 + 10;
|
||||
if ((capabilities & CLIENT_PLUGIN_AUTH) == 0) {
|
||||
return MYSQL_NATIVE_PASSWORD;
|
||||
}
|
||||
i += Math.max(13, authDataLen - 8);
|
||||
if (i >= raw.length) {
|
||||
return MYSQL_NATIVE_PASSWORD;
|
||||
}
|
||||
int pluginStart = i;
|
||||
while (i < raw.length && raw[i] != 0) {
|
||||
i++;
|
||||
}
|
||||
return i == pluginStart
|
||||
? MYSQL_NATIVE_PASSWORD
|
||||
: new String(raw, pluginStart, i - pluginStart, StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
// ── Parse HandshakeResponse41 ─────────────────────────────────────────────
|
||||
|
||||
private static String[] parseHandshakeResponse(byte[] data) {
|
||||
@@ -427,6 +468,16 @@ public class MySqlProtocolHandler {
|
||||
|
||||
// ── Scramble ──────────────────────────────────────────────────────────────
|
||||
|
||||
private static byte[] scramblePassword(String password, byte[] nonce, String authPlugin)
|
||||
throws Exception {
|
||||
return switch (authPlugin) {
|
||||
case CACHING_SHA2_PASSWORD -> scrambleCachingSha2Password(password, nonce);
|
||||
case MYSQL_NATIVE_PASSWORD -> scrambleNativePassword(password, nonce);
|
||||
default -> throw new IllegalArgumentException(
|
||||
"Unsupported MySQL authentication plugin: " + authPlugin);
|
||||
};
|
||||
}
|
||||
|
||||
private static byte[] scrambleNativePassword(String password, byte[] nonce) throws Exception {
|
||||
if (password == null || password.isEmpty()) {
|
||||
return new byte[0];
|
||||
@@ -447,6 +498,27 @@ public class MySqlProtocolHandler {
|
||||
return result;
|
||||
}
|
||||
|
||||
private static byte[] scrambleCachingSha2Password(String password, byte[] nonce)
|
||||
throws Exception {
|
||||
if (password == null || password.isEmpty()) {
|
||||
return new byte[0];
|
||||
}
|
||||
MessageDigest sha256 = MessageDigest.getInstance("SHA-256");
|
||||
byte[] hash1 = sha256.digest(password.getBytes(StandardCharsets.UTF_8));
|
||||
sha256.reset();
|
||||
byte[] hash2 = sha256.digest(hash1);
|
||||
sha256.reset();
|
||||
sha256.update(hash2);
|
||||
sha256.update(nonce);
|
||||
byte[] hash3 = sha256.digest();
|
||||
|
||||
byte[] result = new byte[32];
|
||||
for (int i = 0; i < result.length; i++) {
|
||||
result[i] = (byte) (hash1[i] ^ hash3[i]);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// ── MySQL packet helpers ──────────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user