Light, fluffy, and always free - The AWS Local Emulator alternative
fix(rds): apply PostgreSQL startup parameters to IAM sessions as the token's role (#5304)
David Wooldridge91c15be74a04
diff --git a/docs/services/rds.md b/docs/services/rds.mdindex 8dd8066fa8..07199d4706 100644--- a/docs/services/rds.md+++ b/docs/services/rds.md@@ -669,7 +669,7 @@ On PostgreSQL, the token names a database role (`DBUser`) and the session runs aUnderneath, the proxy reaches the container as the master user and hands the session over to the token's role, so an IAM session that talks its way back to the master role, via `RESET SESSION AUTHORIZATION` and its variants, is terminated with `FATAL: permission denied to set session authorization` rather than being allowed to regain superuser. `SET ROLE` is untouched: PostgreSQL still permission-checks it against the token's role, exactly as on RDS. One difference from RDS: the proxy learns of the switch from PostgreSQL's own report, so when several statements are batched into a single query after the switch, their results are returned before the session is closed.-The parameters a PostgreSQL client sends when it connects (`options` such as `-c search_path=app`, `application_name`, and any other run-time parameter) apply to the session as they do on RDS, with one exception: an IAM session for a role other than the master starts without them. The proxy would have to apply them while the session is still the master's, and a `-c role=<master>` among them would outlive the handover. Set them with `SET` once connected instead.+The parameters a PostgreSQL client sends when it connects (`options` such as `-c search_path=app`, `application_name`, and any other run-time parameter) apply to the session as they do on RDS. An IAM session for a role other than the master gets them once the session has been handed over to the token's role, applied with `set_config()` so PostgreSQL checks each one against that role's privileges, and a parameter PostgreSQL refuses fails the login with PostgreSQL's own error, as it would at startup. A `session_authorization` naming any other role is refused with `permission denied to set session authorization` before anything is applied, since PostgreSQL would authorize it against the master the proxy connected as. Because those sessions have already started, three things differ from RDS: `options` may carry only `-c name=value` and `--name=value` switches, a parameter that can only be set at connection start is refused with `cannot be set after connection start`, and `replication` connections are refused. And because the values are set with `set_config()` rather than at startup, `RESET` (and `RESET ALL`) takes such a parameter back to the server default, not to the value the client connected with: after `-c search_path=app`, `RESET search_path` gives `"$user", public` where RDS keeps `app`.On MySQL, `AWSAuthenticationPlugin` is proprietary to RDS and ships in no public MySQL build, so`CREATE USER ... IDENTIFIED WITH AWSAuthenticationPlugin AS 'RDS'` would fail against the containerdiff --git a/src/main/java/io/github/hectorvent/floci/services/rds/proxy/PostgresProtocolHandler.java b/src/main/java/io/github/hectorvent/floci/services/rds/proxy/PostgresProtocolHandler.javaindex fa465eb559..a1ad3edf49 100644--- a/src/main/java/io/github/hectorvent/floci/services/rds/proxy/PostgresProtocolHandler.java+++ b/src/main/java/io/github/hectorvent/floci/services/rds/proxy/PostgresProtocolHandler.java@@ -23,6 +23,7 @@ import java.util.Base64;import java.util.HashMap;import java.util.LinkedHashMap;import java.util.List;+import java.util.Locale;import java.util.Map;import java.util.concurrent.atomic.AtomicBoolean;@@ -37,7 +38,8 @@ import java.util.concurrent.atomic.AtomicBoolean;* <li>Validate (IAM SigV4 or plain password)* <li>Connect to backend with MD5 or SCRAM-SHA-256 auth* <li>Buffer backend messages until ReadyForQuery- * <li>For IAM logins, hand the session over to the role named in the token+ * <li>For IAM logins, hand the session over to the role named in the token, then apply the+ * client's startup parameters as that role* <li>Send AuthOK + buffered messages to client, then bridge, guarding an IAM* session against being handed back to the master role* </ol>@@ -105,7 +107,8 @@ public class PostgresProtocolHandler {/*** Authenticates the client. With {@code forwardStartupParameters}, the client's other* StartupMessage parameters ({@code options}, {@code application_name} and any run-time- * parameter) open the backend session too, so they apply as they would on the server itself.+ * parameter) apply as they would on the server itself: they open the backend session, or,+ * for an IAM login handed over to the token's role, are set once the role owns the session.*/public static AuthenticatedSession authenticate(Socket client, BackendConnector backendConnector,String masterUsername, String masterPassword, String dbName,@@ -202,7 +205,8 @@ public class PostgresProtocolHandler {// An IAM login for any role but the master runs on a session opened as the master and// handed over below, so PostgreSQL would apply the client's parameters with the// master's privileges: a "-c role=<master>" would outlive the handover and RESET ROLE- // would regain the master. Those logins send only user and database.+ // would regain the master. Those logins open with only user and database, and their+ // parameters are applied once the session belongs to the role (Phase 5c).Map<String, String> clientParameters = forwardStartupParameters && !(isIam && !isMaster)? startup.parameters() : Map.of();sendStartupToBackend(backendOut, backendUser, effectiveDbName, clientParameters);@@ -253,6 +257,50 @@ public class PostgresProtocolHandler {}bufferedMessages = applyParameterStatusUpdates(bufferedMessages, roleSwitch);iamRole = clientUsername;++ // Phase 5c: Apply the client's startup parameters now that the session belongs to+ // the role, so PostgreSQL checks each one against the role's privileges, as it+ // does when the role logs in directly. A parameter PostgreSQL refuses fails the+ // login with PostgreSQL's own reason, as it would at startup. One check cannot be+ // left to PostgreSQL: it authorizes session_authorization against the user the+ // backend connection authenticated as, the superuser master, so a change of it is+ // refused here before anything is applied, and the outcome is checked again below.+ if (forwardStartupParameters) {+ List<SessionSetting> settings;+ try {+ settings = sessionSettings(startup.parameters());+ refuseSessionAuthorizationChange(settings, clientUsername);+ } catch (StartupParameterException e) {+ sendErrorResponse(clientOut, "FATAL", e.sqlState(), e.getMessage());+ clientOut.flush();+ closeQuietly(client);+ closeQuietly(backend);+ return null;+ }+ if (!settings.isEmpty()) {+ List<byte[]> applied = applySessionSettings(backendIn, backendOut, settings);+ if (endsWithErrorResponse(applied)) {+ byte[] refusal = applied.get(applied.size() - 1);+ sendErrorResponse(clientOut, "FATAL", errorField(refusal, 'C', "42601"),+ errorField(refusal, 'M', "invalid startup parameter"));+ clientOut.flush();+ closeQuietly(client);+ closeQuietly(backend);+ return null;+ }+ if (reportsAnotherSessionAuthorization(applied, iamRole)) {+ LOG.warnv("RDS IAM session for role {0} changed its session authorization "+ + "through startup parameters; refusing the login", iamRole);+ sendErrorResponse(clientOut, "FATAL", "42501",+ "permission denied to set session authorization");+ clientOut.flush();+ closeQuietly(client);+ closeQuietly(backend);+ return null;+ }+ bufferedMessages = applyParameterStatusUpdates(bufferedMessages, applied);+ }+ }}// Phase 6: Send AuthenticationOK to client, forward buffered messages, then bridge@@ -737,6 +785,232 @@ public class PostgresProtocolHandler {return "\"" + identifier.replace("\"", "\"\"") + "\"";}+ /** One run-time parameter a client asked for at startup, by name. */+ record SessionSetting(String name, String value) {}++ /**+ * A startup parameter PostgreSQL would refuse while parsing the StartupMessage, before any+ * setting is applied.+ */+ static final class StartupParameterException extends IOException {+ private final String sqlState;++ StartupParameterException(String sqlState, String message) {+ super(message);+ this.sqlState = sqlState;+ }++ String sqlState() {+ return sqlState;+ }+ }++ /**+ * The run-time parameters a StartupMessage asks for, in the order PostgreSQL applies them:+ * the {@code -c} switches in {@code options} first, then every other parameter as sent.+ * {@code user}, {@code database} and {@code _pq_.} protocol extensions are not parameters.+ */+ static List<SessionSetting> sessionSettings(Map<String, String> startupParameters)+ throws StartupParameterException {+ List<SessionSetting> settings = new ArrayList<>();+ String options = startupParameters.get("options");+ if (options != null) {+ settings.addAll(parseOptions(options));+ }+ for (Map.Entry<String, String> parameter : startupParameters.entrySet()) {+ String name = parameter.getKey();+ if ("user".equals(name) || "database".equals(name) || "options".equals(name)+ || name.startsWith("_pq_.")) {+ continue;+ }+ if ("replication".equals(name)) {+ String value = parameter.getValue();+ Boolean replication = "database".equals(value) ? Boolean.TRUE : parseBool(value);+ if (replication == null) {+ throw new StartupParameterException("22023",+ "invalid value for parameter \"replication\": \"" + value + "\"");+ }+ if (replication) {+ throw new StartupParameterException("0A000",+ "replication connections are not supported for IAM sessions through the proxy");+ }+ continue;+ }+ settings.add(new SessionSetting(name, parameter.getValue()));+ }+ return settings;+ }++ /**+ * A boolean as PostgreSQL's {@code parse_bool} reads it: any case-insensitive prefix of+ * {@code true}, {@code false}, {@code yes} or {@code no}, at least two letters of {@code on}+ * or {@code off}, or exactly {@code 1} or {@code 0}. {@code null} for anything else,+ * including the ambiguous {@code o}.+ */+ static Boolean parseBool(String value) {+ if (value.isEmpty()) {+ return null;+ }+ String lower = value.toLowerCase(Locale.ROOT);+ if ("true".startsWith(lower) || "yes".startsWith(lower)) {+ return Boolean.TRUE;+ }+ if ("false".startsWith(lower) || "no".startsWith(lower)) {+ return Boolean.FALSE;+ }+ if (lower.length() >= 2 && "on".startsWith(lower)) {+ return Boolean.TRUE;+ }+ if (lower.length() >= 2 && "off".startsWith(lower)) {+ return Boolean.FALSE;+ }+ if ("1".equals(lower)) {+ return Boolean.TRUE;+ }+ if ("0".equals(lower)) {+ return Boolean.FALSE;+ }+ return null;+ }++ /**+ * Refuses a {@code session_authorization} setting naming anyone but {@code role}. PostgreSQL+ * would authorize it against the superuser the backend connection authenticated as, not the+ * token's role, so it would hand the session back to the master.+ */+ static void refuseSessionAuthorizationChange(List<SessionSetting> settings, String role)+ throws StartupParameterException {+ for (SessionSetting setting : settings) {+ if ("session_authorization".equalsIgnoreCase(setting.name()) && !role.equals(setting.value())) {+ throw new StartupParameterException("42501", "permission denied to set session authorization");+ }+ }+ }++ /** Whether {@code messages} report a {@code session_authorization} other than {@code role}. */+ private static boolean reportsAnotherSessionAuthorization(List<byte[]> messages, String role) {+ for (byte[] message : messages) {+ if (!"session_authorization".equals(parameterStatusName(message))) {+ continue;+ }+ String[] status = parameterStatus(message, 5);+ if (status != null && !role.equals(status[1])) {+ return true;+ }+ }+ return false;+ }++ /**+ * The settings an {@code options} startup parameter carries: {@code -c name=value},+ * {@code -cname=value} or {@code --name=value}, with arguments split on unescaped ASCII+ * whitespace and a backslash taking the next character literally, as PostgreSQL's+ * {@code pg_split_opts} does; a backslash that ends the string is dropped. A dash in a name stands for an underscore. The proxy cannot apply PostgreSQL's other+ * server switches to a session that has already started, so it refuses them.+ */+ static List<SessionSetting> parseOptions(String options) throws StartupParameterException {+ List<String> arguments = splitOptions(options);+ List<SessionSetting> settings = new ArrayList<>();+ for (int i = 0; i < arguments.size(); i++) {+ String argument = arguments.get(i);+ String assignment;+ String switchName;+ if (argument.startsWith("--")) {+ assignment = argument.substring(2);+ switchName = "--";+ } else if ("-c".equals(argument)) {+ if (i + 1 >= arguments.size()) {+ throw new StartupParameterException("42601",+ "invalid command-line argument for server process: -c");+ }+ assignment = arguments.get(++i);+ switchName = "-c ";+ } else if (argument.startsWith("-c")) {+ assignment = argument.substring(2);+ switchName = "-c ";+ } else {+ throw new StartupParameterException("42601",+ "invalid command-line argument for server process: " + argument);+ }+ int equals = assignment.indexOf('=');+ if (equals <= 0) {+ throw new StartupParameterException("42601",+ switchName + assignment + " requires a value");+ }+ settings.add(new SessionSetting(assignment.substring(0, equals).replace('-', '_'),+ assignment.substring(equals + 1)));+ }+ return settings;+ }++ private static List<String> splitOptions(String options) {+ List<String> arguments = new ArrayList<>();+ int i = 0;+ while (i < options.length()) {+ while (i < options.length() && isAsciiSpace(options.charAt(i))) {+ i++;+ }+ if (i >= options.length()) {+ break;+ }+ StringBuilder argument = new StringBuilder();+ while (i < options.length() && !isAsciiSpace(options.charAt(i))) {+ if (options.charAt(i) == '\\') {+ i++;+ if (i >= options.length()) {+ break;+ }+ }+ argument.append(options.charAt(i));+ i++;+ }+ arguments.add(argument.toString());+ }+ return arguments;+ }++ /** The characters C's {@code isspace} accepts in the C locale. */+ private static boolean isAsciiSpace(char c) {+ return c == ' ' || c == '\t' || c == '\n' || c == '\u000B' || c == '\f' || c == '\r';+ }++ /**+ * Applies {@code settings} to the session in one statement, in order, with+ * {@code set_config(name, value, false)}: what a startup parameter does, checked against the+ * privileges of the role the session now belongs to.+ *+ * @return the backend messages the statement produced, up to ReadyForQuery or ErrorResponse+ */+ private static List<byte[]> applySessionSettings(InputStream in, OutputStream out,+ List<SessionSetting> settings) throws IOException {+ sendMessage(out, 'Q', setConfigStatement(settings).getBytes(StandardCharsets.UTF_8), new byte[]{0});+ out.flush();+ return readUntilReadyForQuery(in);+ }++ static String setConfigStatement(List<SessionSetting> settings) {+ StringBuilder sql = new StringBuilder("SELECT ");+ for (int i = 0; i < settings.size(); i++) {+ if (i > 0) {+ sql.append(", ");+ }+ sql.append("pg_catalog.set_config(")+ .append(quoteLiteral(settings.get(i).name()))+ .append(", ")+ .append(quoteLiteral(settings.get(i).value()))+ .append(", false)");+ }+ return sql.toString();+ }++ /**+ * An escape-string literal, so it reads the same whatever standard_conforming_strings says.+ * Apostrophes are doubled rather than backslash-escaped, which backslash_quote can forbid.+ */+ static String quoteLiteral(String value) {+ return "E'" + value.replace("\\", "\\\\").replace("'", "''") + "'";+ }+/*** Folds the ParameterStatus messages a statement produced into the buffered startup messages,* so the client's view of parameters such as {@code session_authorization} and@@ -813,6 +1087,11 @@ public class PostgresProtocolHandler {/** Human-readable 'M' field of an ErrorResponse, or {@code fallback} when it carries none. */static String errorMessage(byte[] errorResponse, String fallback) {+ return errorField(errorResponse, 'M', fallback);+ }++ /** The {@code field} of an ErrorResponse, such as 'C' for SQLSTATE, or {@code fallback}. */+ static String errorField(byte[] errorResponse, char field, String fallback) {int i = 5; // skip type byte and Int32 lengthwhile (i < errorResponse.length && errorResponse[i] != 0) {char fieldType = (char) errorResponse[i];@@ -821,7 +1100,7 @@ public class PostgresProtocolHandler {while (i < errorResponse.length && errorResponse[i] != 0) {i++;}- if (fieldType == 'M') {+ if (fieldType == field) {return new String(errorResponse, start, i - start, StandardCharsets.UTF_8);}i++; // skip the field's null terminatordiff --git a/src/test/java/io/github/hectorvent/floci/services/rds/proxy/PostgresProtocolHandlerTest.java b/src/test/java/io/github/hectorvent/floci/services/rds/proxy/PostgresProtocolHandlerTest.javaindex 5368aa356e..2b2f6512d7 100644--- a/src/test/java/io/github/hectorvent/floci/services/rds/proxy/PostgresProtocolHandlerTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/rds/proxy/PostgresProtocolHandlerTest.java@@ -634,12 +634,67 @@ class PostgresProtocolHandlerTest {DataOutputStream clientOut = new DataOutputStream(ourClient.getOutputStream());DataInputStream clientIn = new DataInputStream(ourClient.getInputStream());- // Applied while the backend session is still the master's, this would outlive the- // handover and let RESET ROLE regain the master.+ writeStartup(clientOut, "approle", "postgres");+ readCleartextPasswordChallenge(clientIn);+ writePassword(clientOut, rdsToken("approle"));+ readAuthenticationOk(clientIn);++ Map<String, String> params = readParametersUntilReadyForQuery(clientIn);+ assertEquals("approle", params.get("session_authorization"));+ assertEquals("off", params.get("is_superuser"));++ ourClient.close();+ proxyClient.close();+ authThread.join(5_000);+ backendThread.join(5_000);+ assertEquals(false, authThread.isAlive(), "authThread did not terminate");+ assertEquals(false, backendThread.isAlive(), "backendThread did not terminate");+ }++ assertEquals("dbadmin", backendStartup.get().get("user"), "backend connection is still opened as master");+ assertEquals("SET SESSION AUTHORIZATION \"approle\"", backendQuery.get());+ }+ }++ @Test+ void iamSessionAppliesClientStartupParametersAsTheTokenRole() throws Exception {+ AtomicReference<Map<String, String>> backendStartup = new AtomicReference<>();+ AtomicReference<String> settingsQuery = new AtomicReference<>();++ try (ServerSocket backendServer = new ServerSocket(0);+ ServerSocket clientServer = new ServerSocket(0)) {++ int backendPort = backendServer.getLocalPort();+ Thread backendThread = Thread.ofVirtual().start(() -> {+ try {+ mockBackendRoleSwitch(backendServer, backendStartup, new AtomicReference<>(),+ "approle", true, (in, out) -> {+ settingsQuery.set(readSimpleQuery(in));+ writeParameterStatus(out, "application_name", "svc");+ writeCommandComplete(out, "SELECT 1");+ writeReadyForQuery(out);+ });+ } catch (IOException e) {+ throw new RuntimeException(e);+ }+ });++ Socket proxyClient;+ try (Socket ourClient = new Socket("localhost", clientServer.getLocalPort())) {+ ourClient.setSoTimeout(5_000);+ proxyClient = clientServer.accept();+ Socket backend = new Socket("localhost", backendPort);++ Thread authThread = startIamAuth(proxyClient, backend);++ DataOutputStream clientOut = new DataOutputStream(ourClient.getOutputStream());+ DataInputStream clientIn = new DataInputStream(ourClient.getInputStream());+Map<String, String> clientStartup = new LinkedHashMap<>();clientStartup.put("user", "approle");clientStartup.put("database", "postgres");- clientStartup.put("options", "-c role=dbadmin");+ clientStartup.put("application_name", "svc");+ clientStartup.put("options", "-c search_path=app --statement-timeout=5s");writeStartup(clientOut, clientStartup);readCleartextPasswordChallenge(clientIn);writePassword(clientOut, rdsToken("approle"));@@ -647,7 +702,7 @@ class PostgresProtocolHandlerTest {Map<String, String> params = readParametersUntilReadyForQuery(clientIn);assertEquals("approle", params.get("session_authorization"));- assertEquals("off", params.get("is_superuser"));+ assertEquals("svc", params.get("application_name"));ourClient.close();proxyClient.close();@@ -657,13 +712,285 @@ class PostgresProtocolHandlerTest {assertEquals(false, backendThread.isAlive(), "backendThread did not terminate");}- assertEquals("dbadmin", backendStartup.get().get("user"), "backend connection is still opened as master");assertEquals(List.of("user", "database"), List.copyOf(backendStartup.get().keySet()),"client startup parameters are not applied to the master session");- assertEquals("SET SESSION AUTHORIZATION \"approle\"", backendQuery.get());+ assertEquals("SELECT pg_catalog.set_config(E'search_path', E'app', false), "+ + "pg_catalog.set_config(E'statement_timeout', E'5s', false), "+ + "pg_catalog.set_config(E'application_name', E'svc', false)",+ settingsQuery.get());}}+ @Test+ void iamSessionIsRefusedWhenPostgresRefusesAStartupParameterForTheTokenRole() throws Exception {+ try (ServerSocket backendServer = new ServerSocket(0);+ ServerSocket clientServer = new ServerSocket(0)) {++ int backendPort = backendServer.getLocalPort();+ Thread backendThread = Thread.ofVirtual().start(() -> {+ try {+ mockBackendRoleSwitch(backendServer, new AtomicReference<>(), new AtomicReference<>(),+ "approle", true, (in, out) -> {+ readSimpleQuery(in);+ // What PostgreSQL answers when a role that is not a member of+ // the master asks to act as it.+ writeErrorResponse(out, "ERROR", "42501",+ "permission denied to set role \"dbadmin\"");+ writeReadyForQuery(out);+ });+ } catch (IOException e) {+ throw new RuntimeException(e);+ }+ });++ Socket proxyClient;+ try (Socket ourClient = new Socket("localhost", clientServer.getLocalPort())) {+ ourClient.setSoTimeout(5_000);+ proxyClient = clientServer.accept();+ Socket backend = new Socket("localhost", backendPort);++ Thread authThread = startIamAuth(proxyClient, backend);++ DataOutputStream clientOut = new DataOutputStream(ourClient.getOutputStream());+ DataInputStream clientIn = new DataInputStream(ourClient.getInputStream());++ Map<String, String> clientStartup = new LinkedHashMap<>();+ clientStartup.put("user", "approle");+ clientStartup.put("database", "postgres");+ clientStartup.put("options", "-c role=dbadmin");+ writeStartup(clientOut, clientStartup);+ readCleartextPasswordChallenge(clientIn);+ writePassword(clientOut, rdsToken("approle"));++ Map<Character, String> error = readErrorResponse(clientIn);+ assertEquals("FATAL", error.get('S'));+ assertEquals("42501", error.get('C'));+ assertEquals("permission denied to set role \"dbadmin\"", error.get('M'));+ assertEquals(-1, clientIn.read(), "the login must fail, not continue without the parameter");++ authThread.join(5_000);+ backendThread.join(5_000);+ assertEquals(false, authThread.isAlive(), "authThread did not terminate");+ assertEquals(false, backendThread.isAlive(), "backendThread did not terminate");+ }+ }+ }++ @Test+ void iamSessionIsRefusedBeforeAnyParameterIsAppliedWhenOneWouldRestoreTheMaster() throws Exception {+ AtomicReference<Integer> afterHandover = new AtomicReference<>();++ try (ServerSocket backendServer = new ServerSocket(0);+ ServerSocket clientServer = new ServerSocket(0)) {++ int backendPort = backendServer.getLocalPort();+ Thread backendThread = Thread.ofVirtual().start(() -> {+ try {+ mockBackendRoleSwitch(backendServer, new AtomicReference<>(), new AtomicReference<>(),+ "approle", true, (in, out) -> afterHandover.set(in.read()));+ } catch (IOException e) {+ throw new RuntimeException(e);+ }+ });++ Socket proxyClient;+ try (Socket ourClient = new Socket("localhost", clientServer.getLocalPort())) {+ ourClient.setSoTimeout(5_000);+ proxyClient = clientServer.accept();+ Socket backend = new Socket("localhost", backendPort);++ Thread authThread = startIamAuth(proxyClient, backend);++ DataOutputStream clientOut = new DataOutputStream(ourClient.getOutputStream());+ DataInputStream clientIn = new DataInputStream(ourClient.getInputStream());++ Map<String, String> clientStartup = new LinkedHashMap<>();+ clientStartup.put("user", "approle");+ clientStartup.put("database", "postgres");+ clientStartup.put("options", "-c session_replication_role=replica -c session_authorization=dbadmin");+ writeStartup(clientOut, clientStartup);+ readCleartextPasswordChallenge(clientIn);+ writePassword(clientOut, rdsToken("approle"));++ Map<Character, String> error = readErrorResponse(clientIn);+ assertEquals("FATAL", error.get('S'));+ assertEquals("42501", error.get('C'));+ assertEquals("permission denied to set session authorization", error.get('M'));+ assertEquals(-1, clientIn.read(), "the login must fail");++ authThread.join(5_000);+ backendThread.join(5_000);+ assertEquals(false, authThread.isAlive(), "authThread did not terminate");+ assertEquals(false, backendThread.isAlive(), "backendThread did not terminate");+ }+ }+ assertEquals(-1, afterHandover.get(), "no setting may reach the backend once one is refused");+ }++ @Test+ void iamSessionIsRefusedWhenPostgresReportsAnotherSessionRoleAfterTheParameters() throws Exception {+ try (ServerSocket backendServer = new ServerSocket(0);+ ServerSocket clientServer = new ServerSocket(0)) {++ int backendPort = backendServer.getLocalPort();+ Thread backendThread = Thread.ofVirtual().start(() -> {+ try {+ mockBackendRoleSwitch(backendServer, new AtomicReference<>(), new AtomicReference<>(),+ "approle", true, (in, out) -> {+ readSimpleQuery(in);+ writeParameterStatus(out, "session_authorization", "dbadmin");+ writeParameterStatus(out, "is_superuser", "on");+ writeCommandComplete(out, "SELECT 1");+ writeReadyForQuery(out);+ });+ } catch (IOException e) {+ throw new RuntimeException(e);+ }+ });++ Socket proxyClient;+ try (Socket ourClient = new Socket("localhost", clientServer.getLocalPort())) {+ ourClient.setSoTimeout(5_000);+ proxyClient = clientServer.accept();+ Socket backend = new Socket("localhost", backendPort);++ Thread authThread = startIamAuth(proxyClient, backend);++ DataOutputStream clientOut = new DataOutputStream(ourClient.getOutputStream());+ DataInputStream clientIn = new DataInputStream(ourClient.getInputStream());++ Map<String, String> clientStartup = new LinkedHashMap<>();+ clientStartup.put("user", "approle");+ clientStartup.put("database", "postgres");+ clientStartup.put("application_name", "svc");+ writeStartup(clientOut, clientStartup);+ readCleartextPasswordChallenge(clientIn);+ writePassword(clientOut, rdsToken("approle"));++ Map<Character, String> error = readErrorResponse(clientIn);+ assertEquals("FATAL", error.get('S'));+ assertEquals("42501", error.get('C'));+ assertEquals(-1, clientIn.read(), "the session must not be handed over as the master");++ authThread.join(5_000);+ backendThread.join(5_000);+ assertEquals(false, authThread.isAlive(), "authThread did not terminate");+ assertEquals(false, backendThread.isAlive(), "backendThread did not terminate");+ }+ }+ }++ @Test+ void sessionSettingsApplyOptionsFirstThenTheOtherParametersInOrder() throws Exception {+ Map<String, String> startup = new LinkedHashMap<>();+ startup.put("user", "approle");+ startup.put("application_name", "svc");+ startup.put("database", "postgres");+ startup.put("_pq_.test_extension", "on");+ startup.put("DateStyle", "ISO");+ startup.put("replication", "off");+ startup.put("options", "-c search_path=app");++ assertEquals(List.of(+ new PostgresProtocolHandler.SessionSetting("search_path", "app"),+ new PostgresProtocolHandler.SessionSetting("application_name", "svc"),+ new PostgresProtocolHandler.SessionSetting("DateStyle", "ISO")),+ PostgresProtocolHandler.sessionSettings(startup));+ }++ @ParameterizedTest+ @ValueSource(strings = {"f", "FALSE", "n", "no", "of", "OFF", "0"})+ void sessionSettingsTreatEveryFalseReplicationValueAsAnOrdinaryConnection(String value) throws Exception {+ assertEquals(List.of(), PostgresProtocolHandler.sessionSettings(+ Map.of("user", "approle", "replication", value)));+ }++ @ParameterizedTest+ @CsvSource({"database,0A000", "true,0A000", "t,0A000", "on,0A000", "1,0A000", "o,22023", "maybe,22023"})+ void sessionSettingsRefuseAReplicationConnectionOrAValuePostgresCannotRead(String value, String sqlState) {+ PostgresProtocolHandler.StartupParameterException e = assertThrows(+ PostgresProtocolHandler.StartupParameterException.class,+ () -> PostgresProtocolHandler.sessionSettings(Map.of("user", "approle", "replication", value)));+ assertEquals(sqlState, e.sqlState());+ }++ @ParameterizedTest+ @CsvSource({"t,true", "TRUE,true", "y,true", "yes,true", "on,true", "1,true",+ "f,false", "fal,false", "n,false", "of,false", "off,false", "0,false"})+ void parseBoolReadsWhatPostgresReads(String value, boolean expected) {+ assertEquals(Boolean.valueOf(expected), PostgresProtocolHandler.parseBool(value));+ }++ @ParameterizedTest+ @ValueSource(strings = {"", "o", "truex", "10", "nope"})+ void parseBoolRefusesWhatPostgresRefuses(String value) {+ assertNull(PostgresProtocolHandler.parseBool(value));+ }++ @Test+ void sessionAuthorizationNamingAnotherRoleIsRefusedWhateverItsSpelling() throws Exception {+ for (Map<String, String> startup : List.of(+ Map.of("options", "-c session_authorization=dbadmin"),+ Map.of("options", "--session-authorization=dbadmin"),+ Map.of("Session_Authorization", "dbadmin"))) {+ List<PostgresProtocolHandler.SessionSetting> settings = PostgresProtocolHandler.sessionSettings(startup);+ PostgresProtocolHandler.StartupParameterException e = assertThrows(+ PostgresProtocolHandler.StartupParameterException.class,+ () -> PostgresProtocolHandler.refuseSessionAuthorizationChange(settings, "approle"),+ startup.toString());+ assertEquals("42501", e.sqlState());+ assertEquals("permission denied to set session authorization", e.getMessage());+ }+ }++ @Test+ void sessionAuthorizationNamingTheTokenRoleIsAllowed() throws Exception {+ PostgresProtocolHandler.refuseSessionAuthorizationChange(+ PostgresProtocolHandler.sessionSettings(Map.of("options", "-c session_authorization=approle")),+ "approle");+ }++ @Test+ void parseOptionsReadsEverySwitchFormPostgresAccepts() throws Exception {+ assertEquals(List.of(+ new PostgresProtocolHandler.SessionSetting("search_path", "app, public"),+ new PostgresProtocolHandler.SessionSetting("work_mem", "64MB"),+ new PostgresProtocolHandler.SessionSetting("statement_timeout", "5s"),+ new PostgresProtocolHandler.SessionSetting("application_name", "a\\b")),+ PostgresProtocolHandler.parseOptions(+ " -c search_path=app,\\ public -cwork_mem=64MB\t--statement-timeout=5s -c application_name=a\\\\b "));+ }++ @Test+ void parseOptionsSplitsOnlyOnAsciiWhitespaceAndDropsATrailingBackslash() throws Exception {+ assertEquals(List.of(+ new PostgresProtocolHandler.SessionSetting("application_name", "a\u00A0b"),+ new PostgresProtocolHandler.SessionSetting("search_path", "app")),+ PostgresProtocolHandler.parseOptions("-c application_name=a\u00A0b\u000B-c search_path=app\\"));+ }++ @ParameterizedTest+ @CsvSource(delimiter = '|', value = {+ "-B 128|invalid command-line argument for server process: -B",+ "-c|invalid command-line argument for server process: -c",+ "-c work_mem|-c work_mem requires a value",+ "--work_mem|--work_mem requires a value"+ })+ void parseOptionsRefusesWhatPostgresRefuses(String options, String message) {+ PostgresProtocolHandler.StartupParameterException e = assertThrows(+ PostgresProtocolHandler.StartupParameterException.class,+ () -> PostgresProtocolHandler.parseOptions(options));+ assertEquals("42601", e.sqlState());+ assertEquals(message, e.getMessage());+ }++ @Test+ void setConfigStatementQuotesNamesAndValuesAsEscapeStrings() {+ assertEquals("SELECT pg_catalog.set_config(E'application_name', E'it''s a \\\\ path', false)",+ PostgresProtocolHandler.setConfigStatement(List.of(+ new PostgresProtocolHandler.SessionSetting("application_name", "it's a \\ path"))));+ }+@Testvoid iamSessionIsRejectedWhenTheTokenRoleDoesNotExist() throws Exception {AtomicReference<Map<String, String>> backendStartup = new AtomicReference<>();diff --git a/src/test/java/io/github/hectorvent/floci/services/rds/proxy/RdsPostgresStartupParametersIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/rds/proxy/RdsPostgresStartupParametersIntegrationTest.javaindex 154c911756..39d4b95f6f 100644--- a/src/test/java/io/github/hectorvent/floci/services/rds/proxy/RdsPostgresStartupParametersIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/rds/proxy/RdsPostgresStartupParametersIntegrationTest.java@@ -1,6 +1,7 @@package io.github.hectorvent.floci.services.rds.proxy;import com.github.dockerjava.api.DockerClient;+import io.github.hectorvent.floci.testutil.SigV4TokenTestHelper;import io.quarkus.test.junit.QuarkusTest;import io.restassured.specification.RequestSpecification;import jakarta.inject.Inject;@@ -13,18 +14,21 @@ import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;+import java.time.Instant;import java.util.Properties;import static io.restassured.RestAssured.given;import static io.restassured.http.ContentType.URLENC;import static org.hamcrest.MatcherAssert.assertThat;import static org.hamcrest.Matchers.equalTo;+import static org.junit.jupiter.api.Assertions.assertThrows;import static org.junit.jupiter.api.Assumptions.assumeTrue;/*** The parameters a client puts in its PostgreSQL StartupMessage reach the instance through the* proxy, as they reach an RDS for PostgreSQL endpoint: {@code options} and- * {@code application_name} here.+ * {@code application_name} here, for a password login and for an IAM login whose session the+ * proxy hands over to the token's role.*/@QuarkusTest@Tag("docker")@@ -82,6 +86,104 @@ class RdsPostgresStartupParametersIntegrationTest {}}+ @Test+ void clientStartupParametersApplyToAnIamSessionAsTheTokenRole() throws Exception {+ String dbId = "startup-params-iam-" + Long.toString(System.nanoTime(), 36);+ int port = createIamInstance(dbId);+ try {+ asMaster(port,+ "CREATE ROLE app_user LOGIN",+ "CREATE SCHEMA floci_opts AUTHORIZATION app_user",+ // A database that refuses \' in string literals must still accept the parameters.+ "ALTER DATABASE appdb SET backslash_quote = off");++ Properties iam = iamLogin(port, "app_user");+ iam.setProperty("options", "-c search_path=floci_opts -c work_mem=64MB");+ iam.setProperty("ApplicationName", "floci's startup params");++ try (Connection connection = DriverManager.getConnection(jdbcUrl(port), iam)) {+ assertThat(show(connection, "search_path"), equalTo("floci_opts"));+ assertThat(show(connection, "work_mem"), equalTo("64MB"));+ assertThat(show(connection, "application_name"), equalTo("floci's startup params"));+ assertThat(show(connection, "session_authorization"), equalTo("app_user"));+ }+ } finally {+ deleteInstance(dbId);+ }+ }++ @Test+ void anIamSessionCannotTakeTheMasterIdentityThroughStartupParameters() throws Exception {+ String dbId = "startup-params-iam-escalation-" + Long.toString(System.nanoTime(), 36);+ int port = createIamInstance(dbId);+ try {+ asMaster(port, "CREATE ROLE app_user LOGIN");++ for (String options : new String[] {+ "-c session_authorization=" + MASTER_USER,+ "-c session_replication_role=replica --Session-Authorization=" + MASTER_USER,+ "-c role=" + MASTER_USER}) {+ Properties iam = iamLogin(port, "app_user");+ iam.setProperty("options", options);+ SQLException refused = assertThrows(SQLException.class,+ () -> DriverManager.getConnection(jdbcUrl(port), iam).close(), options);+ assertThat(options, refused.getSQLState(), equalTo("42501"));+ }+ } finally {+ deleteInstance(dbId);+ }+ }++ /** Creates a PostgreSQL instance with IAM database authentication and returns its proxy port. */+ private static int createIamInstance(String dbId) {+ return rds("CreateDBInstance")+ .formParam("DBInstanceIdentifier", dbId)+ .formParam("Engine", "postgres")+ .formParam("MasterUsername", MASTER_USER)+ .formParam("MasterUserPassword", MASTER_PASSWORD)+ .formParam("DBName", "appdb")+ .formParam("AllocatedStorage", "20")+ .formParam("DBInstanceClass", "db.t3.micro")+ .formParam("EnableIAMDatabaseAuthentication", "true")+ .when().post("/").then().statusCode(200)+ .extract().xmlPath()+ .getInt("CreateDBInstanceResponse.CreateDBInstanceResult.DBInstance.Endpoint.Port");+ }++ private static void deleteInstance(String dbId) {+ rds("DeleteDBInstance")+ .formParam("DBInstanceIdentifier", dbId)+ .formParam("SkipFinalSnapshot", "true")+ .when().post("/");+ }++ /** Runs {@code statements} in order as the master user. */+ private static void asMaster(int port, String... statements) throws SQLException {+ Properties master = new Properties();+ master.setProperty("user", MASTER_USER);+ master.setProperty("password", MASTER_PASSWORD);+ master.setProperty("sslmode", "disable");+ try (Connection connection = DriverManager.getConnection(jdbcUrl(port), master);+ Statement statement = connection.createStatement()) {+ for (String sql : statements) {+ statement.execute(sql);+ }+ }+ }++ private static String jdbcUrl(int port) {+ return "jdbc:postgresql://localhost:" + port + "/appdb";+ }++ private static Properties iamLogin(int port, String user) throws Exception {+ Properties iam = new Properties();+ iam.setProperty("user", user);+ iam.setProperty("password", SigV4TokenTestHelper.createRdsToken("localhost", port, user,+ "test", "test", Instant.now(), 900));+ iam.setProperty("sslmode", "disable");+ return iam;+ }+private static String show(Connection connection, String parameter) throws SQLException {try (Statement statement = connection.createStatement();ResultSet rows = statement.executeQuery("SHOW " + parameter)) {