mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(rds): buffer PostgreSQL login responses for PgBouncer (#6005)
This commit is contained in:
1 parent
2b45288e13
commit
83147f204e
5 files changed
+285
-1
No files matched your search
+122
@@ -0,0 +1,122 @@
|
||||
package io.github.hectorvent.floci.services.rds.proxy;
|
||||
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.Socket;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
class PostgresHandshakeFramingTest {
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void clientLoginResponsesAreBufferedUntilTheCompleteResponseIsReady(boolean rejected) throws Exception {
|
||||
RecordingOutputStream output = new RecordingOutputStream();
|
||||
Socket client = new MemorySocket(startupAndPassword(), output);
|
||||
Socket backend = new MemorySocket(new byte[] {
|
||||
'R', 0, 0, 0, 8, 0, 0, 0, 0,
|
||||
'Z', 0, 0, 0, 5, 'I'
|
||||
}, new RecordingOutputStream());
|
||||
AtomicInteger backendConnections = new AtomicInteger();
|
||||
try (client; backend) {
|
||||
PostgresProtocolHandler.AuthenticatedSession session = PostgresProtocolHandler.authenticate(
|
||||
client, () -> {
|
||||
backendConnections.incrementAndGet();
|
||||
return backend;
|
||||
}, "master", "password", "appdb", false, null, null, null,
|
||||
(user, password) -> rejected ? PasswordValidator.AuthResult.REJECT
|
||||
: PasswordValidator.AuthResult.MASTER_EQUIVALENT, 5000);
|
||||
|
||||
assertArrayEquals(new byte[] {'R', 0, 0, 0, 8, 0, 0, 0, 3}, output.writes.getFirst());
|
||||
assertEquals(2, output.writes.size());
|
||||
assertEquals(2, output.flushes);
|
||||
if (rejected) {
|
||||
assertNull(session);
|
||||
assertEquals(0, backendConnections.get());
|
||||
byte[] error = output.writes.get(1);
|
||||
assertEquals('E', error[0]);
|
||||
assertEquals(error.length - 1, ByteBuffer.wrap(error, 1, 4).getInt());
|
||||
assertArrayEquals(("SFATAL\0C28P01\0Mpassword authentication failed for user \"master\"\0\0")
|
||||
.getBytes(StandardCharsets.UTF_8), Arrays.copyOfRange(error, 5, error.length));
|
||||
} else {
|
||||
assertNotNull(session);
|
||||
assertEquals(1, backendConnections.get());
|
||||
assertArrayEquals(new byte[] {
|
||||
'R', 0, 0, 0, 8, 0, 0, 0, 0,
|
||||
'Z', 0, 0, 0, 5, 'I'
|
||||
}, output.writes.get(1));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static byte[] startupAndPassword() throws IOException {
|
||||
ByteArrayOutputStream input = new ByteArrayOutputStream();
|
||||
DataOutputStream out = new DataOutputStream(input);
|
||||
byte[] parameters = "user\0master\0database\0appdb\0\0".getBytes(StandardCharsets.UTF_8);
|
||||
out.writeInt(8 + parameters.length);
|
||||
out.writeInt(196608);
|
||||
out.write(parameters);
|
||||
byte[] password = "password\0".getBytes(StandardCharsets.UTF_8);
|
||||
out.writeByte('p');
|
||||
out.writeInt(4 + password.length);
|
||||
out.write(password);
|
||||
return input.toByteArray();
|
||||
}
|
||||
|
||||
private static final class RecordingOutputStream extends ByteArrayOutputStream {
|
||||
private final List<byte[]> writes = new ArrayList<>();
|
||||
private int flushes;
|
||||
|
||||
@Override
|
||||
public void write(int value) {
|
||||
writes.add(new byte[] {(byte) value});
|
||||
super.write(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(byte[] bytes, int offset, int length) {
|
||||
writes.add(Arrays.copyOfRange(bytes, offset, offset + length));
|
||||
super.write(bytes, offset, length);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void flush() {
|
||||
flushes++;
|
||||
}
|
||||
}
|
||||
|
||||
private static final class MemorySocket extends Socket {
|
||||
private final InputStream input;
|
||||
private final RecordingOutputStream output;
|
||||
|
||||
private MemorySocket(byte[] input, RecordingOutputStream output) {
|
||||
this.input = new ByteArrayInputStream(input);
|
||||
this.output = output;
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream getInputStream() {
|
||||
return input;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RecordingOutputStream getOutputStream() {
|
||||
return output;
|
||||
}
|
||||
}
|
||||
}
|
||||
+153
@@ -0,0 +1,153 @@
|
||||
package io.github.hectorvent.floci.services.rds.proxy;
|
||||
|
||||
import com.github.dockerjava.api.DockerClient;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerBuilder;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.ContainerInfo;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.EndpointInfo;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerLogStreamer;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerSpec;
|
||||
import io.github.hectorvent.floci.testing.TestImages;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Tag;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Socket;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.Statement;
|
||||
import java.time.Duration;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
|
||||
import static io.github.hectorvent.floci.testing.RdsRequests.rds;
|
||||
import static org.awaitility.Awaitility.await;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assumptions.assumeTrue;
|
||||
|
||||
@QuarkusTest
|
||||
@Tag("docker")
|
||||
class RdsPgBouncerIntegrationTest {
|
||||
|
||||
@Inject DockerClient docker;
|
||||
@Inject ContainerBuilder builder;
|
||||
@Inject ContainerLifecycleManager lifecycle;
|
||||
@Inject ContainerLogStreamer logs;
|
||||
|
||||
@BeforeEach
|
||||
void requireDocker() {
|
||||
try {
|
||||
docker.pingCmd().exec();
|
||||
} catch (Exception e) {
|
||||
assumeTrue(false, "Docker is not available: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"disable", "require"})
|
||||
void freshPgBouncerServerLoginsKeepWorking(String serverTlsMode) throws Exception {
|
||||
String suffix = Long.toString(System.nanoTime(), 36);
|
||||
String dbId = "pgbouncer-" + suffix;
|
||||
int proxyPort = rds("CreateDBInstance")
|
||||
.formParam("DBInstanceIdentifier", dbId)
|
||||
.formParam("Engine", "postgres")
|
||||
.formParam("MasterUsername", "master")
|
||||
.formParam("MasterUserPassword", "Master123!")
|
||||
.formParam("DBName", "appdb")
|
||||
.formParam("AllocatedStorage", "20")
|
||||
.formParam("DBInstanceClass", "db.t3.micro")
|
||||
.when().post("/").then().statusCode(200).extract().xmlPath()
|
||||
.getInt("CreateDBInstanceResponse.CreateDBInstanceResult.DBInstance.Endpoint.Port");
|
||||
ContainerInfo pooler = null;
|
||||
Closeable poolerLogs = null;
|
||||
try {
|
||||
try (Connection connection = DriverManager.getConnection(jdbcUrl("127.0.0.1", proxyPort),
|
||||
credentials("master", "Master123!"));
|
||||
Statement statement = connection.createStatement()) {
|
||||
statement.execute("CREATE ROLE app_user LOGIN PASSWORD 'Application123!'");
|
||||
}
|
||||
ContainerSpec spec = builder.newContainer(TestImages.PGBOUNCER)
|
||||
.withName("floci-test-pgbouncer-" + suffix)
|
||||
.withHostDockerInternalOnLinux()
|
||||
// PgBouncer's DNS-only resolver cannot always resolve Docker's host alias.
|
||||
.withEntrypoint(List.of("/bin/sh", "-ec"))
|
||||
.withCmd("""
|
||||
export DB_HOST="$(getent hosts "$DB_HOST" | awk 'NR == 1 {print $1}')"
|
||||
test -n "$DB_HOST"
|
||||
exec /entrypoint.sh /usr/bin/pgbouncer /etc/pgbouncer/pgbouncer.ini
|
||||
""")
|
||||
.withLoopbackPortBinding(5432, 0)
|
||||
.withEnv("DB_HOST", "host.docker.internal")
|
||||
.withEnv("DB_PORT", String.valueOf(proxyPort))
|
||||
.withEnv("DB_NAME", "appdb")
|
||||
.withEnv("DB_USER", "app_user")
|
||||
.withEnv("DB_PASSWORD", "Application123!")
|
||||
.withEnv("AUTH_TYPE", "scram-sha-256")
|
||||
.withEnv("POOL_MODE", "session")
|
||||
.withEnv("SERVER_LIFETIME", "0")
|
||||
.withEnv("SERVER_LOGIN_RETRY", "1")
|
||||
.withEnv("SERVER_CONNECT_TIMEOUT", "5")
|
||||
.withEnv("SERVER_TLS_SSLMODE", serverTlsMode)
|
||||
.withLogRotation()
|
||||
.build();
|
||||
pooler = lifecycle.createAndStart(spec);
|
||||
poolerLogs = logs.attachConsoleOnly(pooler.containerId(), "pgbouncer:" + suffix);
|
||||
EndpointInfo endpoint = pooler.getEndpoint(5432);
|
||||
await().atMost(Duration.ofSeconds(10)).until(() -> listening(endpoint));
|
||||
|
||||
// server_lifetime=0 closes a pooled server after each client; distinct backend PIDs
|
||||
// prove that every iteration authenticates again rather than reusing a warm session.
|
||||
Set<Integer> backends = new HashSet<>();
|
||||
for (int i = 0; i < 30; i++) {
|
||||
try (Connection connection = DriverManager.getConnection(jdbcUrl(endpoint.host(), endpoint.port()),
|
||||
credentials("app_user", "Application123!"));
|
||||
Statement statement = connection.createStatement();
|
||||
ResultSet result = statement.executeQuery("SELECT current_user, pg_backend_pid()")) {
|
||||
assertTrue(result.next());
|
||||
assertEquals("app_user", result.getString(1));
|
||||
assertTrue(backends.add(result.getInt(2)), "each login must reach a new backend session");
|
||||
}
|
||||
}
|
||||
assertEquals(30, backends.size());
|
||||
} finally {
|
||||
if (pooler != null) {
|
||||
lifecycle.stopAndRemove(pooler.containerId(), poolerLogs);
|
||||
}
|
||||
rds("DeleteDBInstance").formParam("DBInstanceIdentifier", dbId)
|
||||
.formParam("SkipFinalSnapshot", "true").when().post("/").then().statusCode(200);
|
||||
}
|
||||
}
|
||||
|
||||
private static Properties credentials(String user, String password) {
|
||||
Properties properties = new Properties();
|
||||
properties.setProperty("user", user);
|
||||
properties.setProperty("password", password);
|
||||
properties.setProperty("sslmode", "disable");
|
||||
properties.setProperty("connectTimeout", "3");
|
||||
properties.setProperty("socketTimeout", "5");
|
||||
return properties;
|
||||
}
|
||||
|
||||
private static String jdbcUrl(String host, int port) {
|
||||
return "jdbc:postgresql://" + host + ":" + port + "/appdb";
|
||||
}
|
||||
|
||||
private static boolean listening(EndpointInfo endpoint) {
|
||||
try (Socket socket = new Socket()) {
|
||||
socket.connect(new InetSocketAddress(endpoint.host(), endpoint.port()), 200);
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -27,6 +27,8 @@ public final class TestImages {
|
||||
|
||||
public static final String PYTHON_ALPINE = "python:3.12-alpine";
|
||||
|
||||
public static final String PGBOUNCER = "edoburu/pgbouncer:v1.24.0-p1";
|
||||
|
||||
/** The same image AWS publishes at {@code public.ecr.aws/aws-observability/aws-for-fluent-bit:3}. */
|
||||
public static final String FLUENT_BIT = "amazon/aws-for-fluent-bit:3";
|
||||
|
||||
|
||||
Reference in new issue
Block a user