Light, fluffy, and always free - The AWS Local Emulator alternative
feat: deliver Redshift audit logs to CloudWatch Logs (#6432)
Vũ Thế Huyb15bc708faea
diff --git a/docs/services/redshift-data.md b/docs/services/redshift-data.mdindex e1233f11a7..c193c28a5e 100644--- a/docs/services/redshift-data.md+++ b/docs/services/redshift-data.md@@ -61,6 +61,13 @@ connection and its failure rolls it back, including any transaction opened by eacalls on that session. Transaction-control modifiers beyond optional `WORK` or `TRANSACTION`are rejected before execution. These cross-call transaction details have not been verified against live AWS.+Batch audit records track successful user changes through transaction boundaries and savepoints.+`ROLLBACK TO` discards changes after the named savepoint, while `RELEASE` keeps them pending until+commit. A full rollback discards pending changes; changes committed earlier in the batch retain+their audit records if a later statement fails. Batch entries support transaction-control commands+and multiple SQL statements. Audit state advances after each successful command, while an entry's+result metadata preserves its first command's result and its original SQL text.+An omitted keep-alive on a reuse request preserves the previous idle timeout. A supplied valuereplaces it after successful admission, and the idle deadline starts when the query completes.Sending 0 with an existing `SessionId` closes the connection after that query. Omitting bothdiff --git a/docs/services/redshift.md b/docs/services/redshift.mdindex 383213e261..1c91b874f9 100644--- a/docs/services/redshift.md+++ b/docs/services/redshift.md@@ -58,13 +58,17 @@ For running SQL without a PostgreSQL wire connection (the way Lambda and Step Fu| `DescribeOrderableClusterOptions` | Return the static node types and cluster types, optionally filtered by `NodeType` or `ClusterVersion` || `ModifyClusterIamRoles` | Add or remove associated IAM roles and set `DefaultIamRoleArn`; COPY and UNLOAD see the change on new connections || `DescribeLoggingStatus` | Return a cluster's stored audit-logging configuration |-| `EnableLogging` | Store audit-logging configuration (S3 bucket, CloudWatch, or S3 table via `LogDestinationType`); no logs are delivered |+| `EnableLogging` | Store audit-logging configuration and deliver the selected connection, user, and user-activity audit records to CloudWatch Logs for provisioned clusters || `DisableLogging` | Clear a cluster's audit-logging configuration || `RebootCluster` | Restart a cluster's container || `GetClusterCredentials` | Issue a short-lived DbUser / DbPassword pair the auth proxy and Data API accept for a non-master user || `GetClusterCredentialsWithIAM` | Issue short-lived credentials with the DbUser derived from the caller's IAM identity |<!-- floci:actions:end -->+CloudWatch audit events use the AWS log groups `/aws/redshift/cluster/<clusterIdentifier>/<logType>` and CSV log records. `connectionlog` and `userlog` follow the logging export selection. `useractivitylog` additionally requires `enable_user_activity_logging=true` in the cluster parameter group, which is false by default.++Audit delivery covers PostgreSQL wire connections and Data API connections, including batch statements and retained sessions. Records identify the requested database user and database, even when temporary credentials use a master connection internally. User changes are published after commit and discarded on rollback, including rollback to a savepoint on the wire path. SQL password literals are redacted, including literals separated from `PASSWORD` by SQL comments.+### Final snapshots on cluster deletion`DeleteCluster` defaults `SkipFinalClusterSnapshot` to `false`, so adiff --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 efcdb33bae..dd99205ac7 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@@ -31,6 +31,7 @@ import java.util.Locale;import java.util.Map;import java.util.Set;import java.util.concurrent.atomic.AtomicBoolean;+import java.util.function.BiConsumer;/*** Handles the PostgreSQL wire protocol auth intercept.@@ -140,6 +141,19 @@ public class PostgresProtocolHandler {int handshakeTimeoutMillis, boolean forwardStartupParameters,boolean databaseIamAuthentication) throws IOException {+ return authenticate(client, backendConnector, masterUsername, masterPassword, dbName, iamEnabled, sigV4,+ binding, tlsCertificates, passwordValidator, handshakeTimeoutMillis, forwardStartupParameters,+ databaseIamAuthentication, (user, database) -> { });+ }++ public static AuthenticatedSession authenticate(Socket client, BackendConnector backendConnector,+ String masterUsername, String masterPassword, String dbName,+ boolean iamEnabled, RdsSigV4Validator sigV4,+ RdsProxyBinding binding, RdsProxyTlsCertificates tlsCertificates,+ PasswordValidator passwordValidator, int handshakeTimeoutMillis,+ boolean forwardStartupParameters, boolean databaseIamAuthentication,+ BiConsumer<String, String> startupObserver) throws IOException {+client.setSoTimeout(handshakeTimeoutMillis);// Phase 1: Read client startup message (possibly preceded by SSL request)@@ -151,6 +165,7 @@ public class PostgresProtocolHandler {}client = startup.socket();String clientUsername = startup.username();+ startupObserver.accept(clientUsername, resolveEffectiveDbName(startup.database(), dbName));InputStream clientIn = client.getInputStream();// PgBouncer rejects partial login packets; keep the challenge together until flush().// https://github.com/pgbouncer/pgbouncer/blob/pgbouncer_1_24_0/src/server.c#L116-L119diff --git a/src/main/java/io/github/hectorvent/floci/services/redshift/RedshiftAuditLogPublisher.java b/src/main/java/io/github/hectorvent/floci/services/redshift/RedshiftAuditLogPublisher.javanew file mode 100644index 0000000000..f212761d47--- /dev/null+++ b/src/main/java/io/github/hectorvent/floci/services/redshift/RedshiftAuditLogPublisher.java@@ -0,0 +1,245 @@+package io.github.hectorvent.floci.services.redshift;++import io.github.hectorvent.floci.core.common.AwsException;+import io.github.hectorvent.floci.services.cloudwatch.logs.CloudWatchLogsService;+import io.github.hectorvent.floci.services.redshift.model.Cluster;+import jakarta.enterprise.context.ApplicationScoped;+import jakarta.inject.Inject;+import org.jboss.logging.Logger;++import java.time.Clock;+import java.time.Instant;+import java.util.LinkedHashMap;+import java.util.List;+import java.util.Map;+import java.util.Set;+import java.util.concurrent.ConcurrentHashMap;+import java.util.stream.Collectors;++@ApplicationScoped+public class RedshiftAuditLogPublisher {++ private static final Logger LOG = Logger.getLogger(RedshiftAuditLogPublisher.class);+ private static final Set<String> LOG_TYPES = Set.of("connectionlog", "userlog", "useractivitylog");+ private static final Map<String, List<String>> LOG_COLUMNS = Map.of(+ "connectionlog", List.of("event", "recordtime", "remotehost", "remoteport", "pid", "dbname",+ "username", "authmethod", "duration", "sslversion", "sslcipher", "mtu", "sslcompression",+ "sslexpansion", "iamauthguid", "application_name", "os_version", "driver_version",+ "plugin_name", "protocol_version", "sessionid", "compression"),+ "userlog", List.of("userid", "username", "oldusername", "action", "usecreatedb", "usesuper",+ "usecatupd", "valuntil", "pid", "xid", "recordtime"),+ "useractivitylog", List.of("recordtime", "db", "user", "pid", "userid", "xid", "query"));++ private final CloudWatchLogsService logs;+ private final Clock clock;+ private final Map<String, Registration> registrations = new ConcurrentHashMap<>();++ private record Registration(String region, String identifier, boolean enabled, String destination,+ List<String> exports, boolean activityLoggingEnabled) { }++ @Inject+ public RedshiftAuditLogPublisher(CloudWatchLogsService logs) {+ this(logs, Clock.systemUTC());+ }++ RedshiftAuditLogPublisher(CloudWatchLogsService logs, Clock clock) {+ this.logs = logs;+ this.clock = clock;+ }++ public void register(String accountId, String region, Cluster cluster) {+ registrations.put(registryKey(accountId, cluster.getClusterIdentifier()), new Registration(region,+ cluster.getClusterIdentifier(), cluster.isLoggingEnabled(), cluster.getLoggingDestinationType(),+ cluster.getLoggingExports() == null ? null : List.copyOf(cluster.getLoggingExports()),+ cluster.isUserActivityLoggingEnabled()));+ }++ public void unregister(String accountId, String clusterIdentifier) {+ registrations.remove(registryKey(accountId, clusterIdentifier));+ }++ public void publishRegistered(String accountId, String clusterIdentifier, String logType,+ Map<String, Object> record) {+ Registration registration = registrations.get(registryKey(accountId, clusterIdentifier));+ if (registration == null) {+ return;+ }+ Cluster cluster = registeredCluster(registration);+ publish(cluster, accountId, registration.region(), logType, record);+ }++ public void publishRegisteredActivity(String accountId, String clusterIdentifier, String database,+ String username, String sql) {+ Registration registration = registrations.get(registryKey(accountId, clusterIdentifier));+ if (registration == null) {+ return;+ }+ Cluster cluster = registeredCluster(registration);+ publishActivity(cluster, accountId, registration.region(), database, username, sql);+ }++ public void publishRegisteredUserChange(String accountId, String clusterIdentifier, String database,+ String username, String action, String oldUsername) {+ Registration registration = registrations.get(registryKey(accountId, clusterIdentifier));+ if (registration == null) {+ return;+ }+ Cluster cluster = registeredCluster(registration);+ publishUserChange(cluster, accountId, registration.region(), database, username, action, oldUsername);+ }++ private static Cluster registeredCluster(Registration registration) {+ Cluster cluster = new Cluster();+ cluster.setClusterIdentifier(registration.identifier());+ cluster.setLoggingEnabled(registration.enabled());+ cluster.setLoggingDestinationType(registration.destination());+ cluster.setLoggingExports(registration.exports());+ cluster.setUserActivityLoggingEnabled(registration.activityLoggingEnabled());+ return cluster;+ }++ private static String registryKey(String accountId, String clusterIdentifier) {+ return accountId + ":" + clusterIdentifier;+ }++ public void publishActivity(Cluster cluster, String accountId, String region, String database, String username,+ String sql) {+ Map<String, Object> record = new LinkedHashMap<>();+ record.put("recordtime", Instant.ofEpochMilli(clock.millis()).toString());+ record.put("db", database);+ record.put("user", username);+ record.put("query", "LOG: " + sanitizeSql(sql));+ publish(cluster, accountId, region, "useractivitylog", record);+ }++ public void publishConnection(Cluster cluster, String accountId, String region, String database, String username,+ String event, String remoteAddress, String error) {+ publish(cluster, accountId, region, "connectionlog",+ connectionRecord(database, username, event, remoteAddress, null, error));+ }++ public void publishRegisteredConnection(String accountId, String clusterIdentifier, String database, String username,+ String event, String remoteAddress, int remotePort) {+ publishRegistered(accountId, clusterIdentifier, "connectionlog",+ connectionRecord(database, username, event, remoteAddress, remotePort, null));+ }++ private Map<String, Object> connectionRecord(String database, String username, String event, String remoteAddress,+ Integer remotePort, String error) {+ Map<String, Object> record = new LinkedHashMap<>();+ record.put("recordtime", Instant.ofEpochMilli(clock.millis()).toString());+ record.put("dbname", database);+ record.put("username", username);+ record.put("event", event);+ record.put("remotehost", remoteAddress);+ record.put("authmethod", "password");+ record.put("remoteport", remotePort);+ record.put("pid", "");+ record.put("duration", "");+ record.put("sslversion", "");+ record.put("sslcipher", "");+ record.put("mtu", "");+ record.put("sslcompression", "");+ record.put("sslexpansion", "");+ record.put("iamauthguid", "");+ record.put("application_name", "");+ record.put("os_version", "");+ record.put("driver_version", "");+ record.put("plugin_name", "");+ record.put("protocol_version", "");+ record.put("sessionid", "");+ record.put("compression", "");+ if (error != null) {+ record.put("event", "authentication failure");+ }+ return record;+ }++ public void publishUserChange(Cluster cluster, String accountId, String region, String database,+ String username, String action, String oldUsername) {+ Map<String, Object> record = new LinkedHashMap<>();+ record.put("recordtime", Instant.ofEpochMilli(clock.millis()).toString());+ record.put("userid", "");+ record.put("db", database);+ record.put("username", username);+ record.put("oldusername", oldUsername == null ? "" : oldUsername);+ record.put("action", action);+ record.put("usecreatedb", "");+ record.put("usesuper", "");+ record.put("usecatupd", "");+ record.put("valuntil", "");+ record.put("pid", "");+ record.put("xid", "");+ publish(cluster, accountId, region, "userlog", record);+ }++ public void publish(Cluster cluster, String accountId, String region, String logType,+ Map<String, Object> record) {+ if (cluster == null || !cluster.isLoggingEnabled()+ || !"cloudwatch".equalsIgnoreCase(cluster.getLoggingDestinationType())+ || "useractivitylog".equals(logType) && !cluster.isUserActivityLoggingEnabled()+ || !LOG_TYPES.contains(logType) || !exports(cluster.getLoggingExports()).contains(logType)) {+ return;+ }++ String group = "/aws/redshift/cluster/" + cluster.getClusterIdentifier() + "/" + logType;+ String stream = cluster.getClusterIdentifier();+ try {+ createIfMissing(accountId, region, group, stream);+ Map<String, Object> event = Map.of(+ "timestamp", clock.millis(),+ "message", csvLine(logType, record));+ logs.putLogEventsForAccount(accountId, group, stream, List.of(event), region);+ } catch (RuntimeException exception) {+ LOG.warnv(exception, "Failed to publish Redshift {0} audit event for cluster {1}",+ logType, cluster.getClusterIdentifier());+ }+ }++ private void createIfMissing(String accountId, String region, String group, String stream) {+ try {+ logs.createLogGroupForAccount(accountId, group, null, Map.of(), region);+ } catch (RuntimeException exception) {+ if (!alreadyExists(exception)) {+ throw exception;+ }+ }+ try {+ logs.createLogStreamForAccount(accountId, group, stream, region);+ } catch (RuntimeException exception) {+ if (!alreadyExists(exception)) {+ throw exception;+ }+ }+ }++ private static Set<String> exports(List<String> configured) {+ return configured == null ? LOG_TYPES : Set.copyOf(configured);+ }++ private static boolean alreadyExists(RuntimeException exception) {+ return exception instanceof AwsException awsException+ && "ResourceAlreadyExistsException".equals(awsException.getErrorCode());+ }++ private static String csvLine(String logType, Map<String, Object> record) {+ return LOG_COLUMNS.get(logType).stream()+ .map(column -> csvValue(record.get(column)))+ .collect(Collectors.joining(","));+ }++ private static String csvValue(Object value) {+ if (value == null) {+ return "";+ }+ String text = value.toString();+ if (text.indexOf(',') >= 0 || text.indexOf('"') >= 0 || text.indexOf('\n') >= 0 || text.indexOf('\r') >= 0) {+ return "\"" + text.replace("\"", "\"\"") + "\"";+ }+ return text;+ }++ static String sanitizeSql(String sql) {+ return RedshiftAuditSql.sanitize(sql);+ }+}diff --git a/src/main/java/io/github/hectorvent/floci/services/redshift/RedshiftAuditSql.java b/src/main/java/io/github/hectorvent/floci/services/redshift/RedshiftAuditSql.javanew file mode 100644index 0000000000..e12eab1f98--- /dev/null+++ b/src/main/java/io/github/hectorvent/floci/services/redshift/RedshiftAuditSql.java@@ -0,0 +1,335 @@+package io.github.hectorvent.floci.services.redshift;++import java.util.ArrayDeque;+import java.util.ArrayList;+import java.util.Comparator;+import java.util.List;+import java.util.Locale;++/** SQL inspection for audit records, preserving quoted text and skipping nested comments. */+public final class RedshiftAuditSql {+ private RedshiftAuditSql() { }++ public record UserChange(String username, String action, String oldUsername) { }++ public record Savepoint(String operation, String name) { }++ public static final class UserChanges {+ private final List<UserChange> pending = new ArrayList<>();+ private final List<UserChange> committed = new ArrayList<>();+ private final ArrayDeque<Checkpoint> savepoints = new ArrayDeque<>();++ private record Checkpoint(String name, int count) { }++ public void complete(String sql) {+ List<Token> tokens = tokens(sql);+ String command = !tokens.isEmpty() && tokens.getFirst().kind() == Kind.WORD+ ? tokens.getFirst().text() : "";+ complete(sql, command);+ }++ public void complete(String sql, String commandTag) {+ Savepoint savepoint = savepoint(sql);+ if (savepoint != null) {+ completeSavepoint(savepoint);+ return;+ }+ if ("COMMIT".equalsIgnoreCase(commandTag) || "END".equalsIgnoreCase(commandTag)) {+ commit();+ } else if ("ROLLBACK".equalsIgnoreCase(commandTag) || "ABORT".equalsIgnoreCase(commandTag)) {+ rollback();+ } else {+ pending.addAll(userChanges(sql));+ }+ }++ public void commit() {+ committed.addAll(pending);+ rollback();+ }++ public void rollback() {+ pending.clear();+ savepoints.clear();+ }++ public List<UserChange> drainCommitted() {+ List<UserChange> changes = List.copyOf(committed);+ committed.clear();+ return changes;+ }++ private void completeSavepoint(Savepoint savepoint) {+ if ("SAVEPOINT".equals(savepoint.operation())) {+ savepoints.addLast(new Checkpoint(savepoint.name(), pending.size()));+ return;+ }+ while (!savepoints.isEmpty()) {+ Checkpoint checkpoint = savepoints.peekLast();+ if (checkpoint.name().equals(savepoint.name())) {+ if ("ROLLBACK".equals(savepoint.operation())) {+ pending.subList(checkpoint.count(), pending.size()).clear();+ } else {+ savepoints.removeLast();+ }+ return;+ }+ savepoints.removeLast();+ }+ }+ }++ private enum Kind { WORD, IDENTIFIER, STRING, SYMBOL }++ private record Token(String text, int start, int end, Kind kind) {+ boolean keyword(String keyword) {+ return kind == Kind.WORD && keyword.equalsIgnoreCase(text);+ }++ String identifier() {+ return kind == Kind.IDENTIFIER+ ? text.substring(1, text.length() - 1).replace("\"\"", "\"") : text.toLowerCase(Locale.ROOT);+ }+ }++ private record Redaction(int start, int end) { }++ public static String sanitize(String sql) {+ List<Redaction> redactions = new ArrayList<>();+ collectPasswordRedactions(sql, tokens(sql), redactions);+ collectPasswordRedactions(sql, tokens(sql, true), redactions);+ redactions.sort(Comparator.comparingInt(Redaction::start));+ StringBuilder sanitized = new StringBuilder(sql.length());+ int copied = 0;+ for (Redaction redaction : redactions) {+ if (redaction.start() >= copied) {+ sanitized.append(sql, copied, redaction.start()).append("'[REDACTED]'");+ }+ copied = Math.max(copied, redaction.end());+ }+ return sanitized.append(sql, copied, sql.length()).toString();+ }++ private static void collectPasswordRedactions(String sql, List<Token> tokens, List<Redaction> redactions) {+ for (int i = 0; i + 1 < tokens.size(); i++) {+ if (!tokens.get(i).keyword("PASSWORD")) {+ continue;+ }+ Token value = tokens.get(++i);+ if (value.kind() == Kind.SYMBOL) {+ continue;+ }+ int end = value.end();+ boolean uncertainBoundary = ambiguousPasswordString(value);+ // PostgreSQL concatenates string literals separated by a newline, including comments.+ while (value.kind() == Kind.STRING && i + 1 < tokens.size() && tokens.get(i + 1).kind() == Kind.STRING) {+ Token continuation = tokens.get(++i);+ end = continuation.end();+ uncertainBoundary |= ambiguousPasswordString(continuation);+ }+ // A session may disable standard_conforming_strings, making the remaining password boundary uncertain.+ redactions.add(new Redaction(value.start(), uncertainBoundary ? sql.length() : end));+ }+ }++ private static boolean ambiguousPasswordString(Token token) {+ String text = token.text();+ return token.kind() == Kind.STRING && text.indexOf('\\') >= 0+ && (text.startsWith("'") || text.regionMatches(true, 0, "N'", 0, 2));+ }++ public static List<String> statements(String sql) {+ List<String> statements = new ArrayList<>();+ int start = 0;+ boolean hasContent = false;+ for (Token token : tokens(sql)) {+ if (token.kind() == Kind.SYMBOL && ";".equals(token.text())) {+ if (hasContent) {+ statements.add(sql.substring(start, token.start()));+ }+ start = token.end();+ hasContent = false;+ } else {+ hasContent = true;+ }+ }+ if (hasContent) {+ statements.add(sql.substring(start));+ }+ return statements;+ }++ public static boolean isCommit(String sql) {+ List<Token> tokens = tokens(sql);+ return !tokens.isEmpty() && (tokens.getFirst().keyword("COMMIT") || tokens.getFirst().keyword("END"));+ }++ public static List<UserChange> userChanges(String sql) {+ List<Token> tokens = tokens(sql);+ if (tokens.size() < 3 || !tokens.get(1).keyword("USER")) {+ return List.of();+ }+ Token command = tokens.getFirst();+ String action = command.keyword("CREATE") ? "Create"+ : command.keyword("ALTER") ? "Alter" : command.keyword("DROP") ? "Drop" : null;+ if (action == null) {+ return List.of();+ }+ int name = 2;+ if (tokens.get(name).keyword("IF")) {+ name++;+ if (name < tokens.size() && tokens.get(name).keyword("NOT")) {+ name++;+ }+ if (name >= tokens.size() || !tokens.get(name++).keyword("EXISTS")) {+ return List.of();+ }+ }+ if (name >= tokens.size()) {+ return List.of();+ }+ String username = tokens.get(name).identifier();+ if ("Alter".equals(action) && name + 3 < tokens.size()+ && tokens.get(name + 1).keyword("RENAME") && tokens.get(name + 2).keyword("TO")) {+ return List.of(new UserChange(tokens.get(name + 3).identifier(), "Rename", username));+ }+ List<UserChange> changes = new ArrayList<>();+ changes.add(new UserChange(username, action, null));+ if ("Drop".equals(action)) {+ for (int i = name + 1; i + 1 < tokens.size() && ",".equals(tokens.get(i).text()); i += 2) {+ changes.add(new UserChange(tokens.get(i + 1).identifier(), action, null));+ }+ }+ return changes;+ }++ public static Savepoint savepoint(String sql) {+ List<Token> tokens = tokens(sql);+ if (tokens.size() < 2) {+ return null;+ }+ int name = 1;+ String operation;+ if (tokens.getFirst().keyword("SAVEPOINT")) {+ operation = "SAVEPOINT";+ } else if (tokens.getFirst().keyword("RELEASE")) {+ operation = "RELEASE";+ if (tokens.get(name).keyword("SAVEPOINT")) {+ name++;+ }+ } else if (tokens.getFirst().keyword("ROLLBACK")) {+ operation = "ROLLBACK";+ if (tokens.get(name).keyword("WORK") || tokens.get(name).keyword("TRANSACTION")) {+ name++;+ }+ if (name >= tokens.size() || !tokens.get(name++).keyword("TO")) {+ return null;+ }+ if (name < tokens.size() && tokens.get(name).keyword("SAVEPOINT")) {+ name++;+ }+ } else {+ return null;+ }+ return name < tokens.size() ? new Savepoint(operation, tokens.get(name).identifier()) : null;+ }++ private static List<Token> tokens(String sql) {+ return tokens(sql, false);+ }++ private static List<Token> tokens(String sql, boolean ordinaryStringEscapes) {+ List<Token> tokens = new ArrayList<>();+ int i = 0;+ while (i < sql.length()) {+ char c = sql.charAt(i);+ if (Character.isWhitespace(c)) {+ i++;+ } else if (sql.startsWith("--", i)) {+ while (i < sql.length() && sql.charAt(i) != '\n' && sql.charAt(i) != '\r') {+ i++;+ }+ } else if (sql.startsWith("/*", i)) {+ i = skipComment(sql, i);+ } else {+ int start = i;+ Kind kind;+ if ("eEnNbBxX".indexOf(c) >= 0 && i + 1 < sql.length() && sql.charAt(i + 1) == '\'') {+ i = skipQuoted(sql, i + 1, '\'', c == 'e' || c == 'E'+ || ordinaryStringEscapes && (c == 'n' || c == 'N'));+ kind = Kind.STRING;+ } else if ((c == 'u' || c == 'U') && sql.startsWith("&'", i + 1)) {+ i = skipQuoted(sql, i + 2, '\'', false);+ kind = Kind.STRING;+ } else if (c == '\'' || c == '"') {+ i = skipQuoted(sql, i, c, c == '\'' && ordinaryStringEscapes);+ kind = c == '\'' ? Kind.STRING : Kind.IDENTIFIER;+ } else if (c == '$' && dollarDelimiter(sql, i) != null) {+ String delimiter = dollarDelimiter(sql, i);+ int end = sql.indexOf(delimiter, i + delimiter.length());+ i = end < 0 ? sql.length() : end + delimiter.length();+ kind = Kind.STRING;+ } else if (Character.isLetterOrDigit(c) || c == '_') {+ i++;+ while (i < sql.length() && (Character.isLetterOrDigit(sql.charAt(i))+ || sql.charAt(i) == '_' || sql.charAt(i) == '$')) {+ i++;+ }+ kind = Kind.WORD;+ } else {+ i++;+ kind = Kind.SYMBOL;+ }+ tokens.add(new Token(sql.substring(start, i), start, i, kind));+ }+ }+ return tokens;+ }++ private static int skipComment(String sql, int start) {+ int depth = 1;+ int i = start + 2;+ while (i < sql.length() && depth > 0) {+ if (sql.startsWith("/*", i)) {+ depth++;+ i += 2;+ } else if (sql.startsWith("*/", i)) {+ depth--;+ i += 2;+ } else {+ i++;+ }+ }+ return i;+ }++ private static int skipQuoted(String sql, int start, char quote, boolean escapes) {+ int i = start + 1;+ while (i < sql.length()) {+ char c = sql.charAt(i++);+ if (escapes && c == '\\' && i < sql.length()) {+ i++;+ } else if (c == quote) {+ if (i >= sql.length() || sql.charAt(i) != quote) {+ return i;+ }+ i++;+ }+ }+ return i;+ }++ private static String dollarDelimiter(String sql, int start) {+ int i = start + 1;+ if (i < sql.length() && sql.charAt(i) == '$') {+ return "$$";+ }+ if (i >= sql.length() || !(Character.isLetter(sql.charAt(i)) || sql.charAt(i) == '_')) {+ return null;+ }+ while (i < sql.length() && (Character.isLetterOrDigit(sql.charAt(i)) || sql.charAt(i) == '_')) {+ i++;+ }+ return i < sql.length() && sql.charAt(i) == '$' ? sql.substring(start, i + 1) : null;+ }+}diff --git a/src/main/java/io/github/hectorvent/floci/services/redshift/RedshiftService.java b/src/main/java/io/github/hectorvent/floci/services/redshift/RedshiftService.javaindex 42995a1448..2222dbf03a 100644--- a/src/main/java/io/github/hectorvent/floci/services/redshift/RedshiftService.java+++ b/src/main/java/io/github/hectorvent/floci/services/redshift/RedshiftService.java@@ -91,6 +91,7 @@ public class RedshiftService {private final SecretsManagerService secretsManagerService;private final ObjectMapper objectMapper;private final DynamoDbStreamReader streamReader;+ private final RedshiftAuditLogPublisher auditLogPublisher;// Proxy ports currently handed out, so allocateProxyPort never double-assigns within this JVM.private final Set<Integer> usedPorts = ConcurrentHashMap.newKeySet();private final ConcurrentHashMap<ResourceKey, ResourceLock> resourceLocks = new ConcurrentHashMap<>();@@ -133,7 +134,7 @@ public class RedshiftService {RedshiftProxyManager proxyManager, DockerHostResolver dockerHostResolver,RedshiftCredentialBroker credentialBroker,SecretsManagerService secretsManagerService, ObjectMapper objectMapper,- DynamoDbStreamReader streamReader) {+ DynamoDbStreamReader streamReader, RedshiftAuditLogPublisher auditLogPublisher) {this.clusters = storageFactory.create("redshift", "redshift-clusters.json", new TypeReference<Map<String, Cluster>>() {});this.snapshots = storageFactory.create("redshift", "redshift-snapshots.json", new TypeReference<Map<String, Snapshot>>() {});this.parameterGroups = storageFactory.create("redshift", "redshift-parameter-groups.json", new TypeReference<Map<String, ClusterParameterGroup>>() {});@@ -150,6 +151,16 @@ public class RedshiftService {this.secretsManagerService = secretsManagerService;this.objectMapper = objectMapper;this.streamReader = streamReader;+ this.auditLogPublisher = auditLogPublisher;+ }++ RedshiftService(StorageFactory storageFactory, RedshiftContainerManager containerManager,+ EmulatorConfig config, RegionResolver regionResolver, RedshiftProxyManager proxyManager,+ DockerHostResolver dockerHostResolver, RedshiftCredentialBroker credentialBroker,+ SecretsManagerService secretsManagerService, ObjectMapper objectMapper,+ DynamoDbStreamReader streamReader) {+ this(storageFactory, containerManager, config, regionResolver, proxyManager, dockerHostResolver,+ credentialBroker, secretsManagerService, objectMapper, streamReader, null);}RedshiftService(StorageFactory storageFactory, RedshiftContainerManager containerManager,@@ -168,6 +179,7 @@ public class RedshiftService {.toList();for (AccountAwareStorageBackend.AccountEntry<Cluster> entry : availableClusters) {Cluster cluster = entry.value();+ registerAuditLogging(entry.accountId(), cluster);if (containerManager.getContainer(entry.accountId(), cluster.getClusterIdentifier()).isPresent()) {continue;}@@ -263,6 +275,7 @@ public class RedshiftService {cluster.setDefaultIamRoleArn(defaultRole);// A group named at create time is applied afterwards via modifyCluster.cluster.setClusterParameterGroupName(DEFAULT_PARAMETER_GROUP_NAME);+ cluster.setUserActivityLoggingEnabled(userActivityLoggingEnabled(DEFAULT_PARAMETER_GROUP_NAME));cluster.setClusterStatus("creating");clusters.put(identifier, cluster);clusters.flush();@@ -737,6 +750,9 @@ public class RedshiftService {proxyManager.forgetSpectrumRuntime(relayKey(clusters.accountId(), identifier));clusters.delete(identifier);clusters.flush();+ if (auditLogPublisher != null) {+ auditLogPublisher.unregister(clusters.accountId(), identifier);+ }// Invalidate any GetClusterCredentials passwords so a cluster later recreated with this// identifier does not accept them as master-equivalent.credentialBroker.revokeCluster(clusters.accountId(), identifier);@@ -794,6 +810,7 @@ public class RedshiftService {}if (clusterParameterGroupName != null && !clusterParameterGroupName.isBlank()) {cluster.setClusterParameterGroupName(clusterParameterGroupName);+ cluster.setUserActivityLoggingEnabled(userActivityLoggingEnabled(clusterParameterGroupName));}if (vpcSecurityGroupIds != null && !vpcSecurityGroupIds.isEmpty()) {cluster.setVpcSecurityGroupIds(vpcSecurityGroupIds);@@ -804,6 +821,7 @@ public class RedshiftService {clusters.put(clusterIdentifier, cluster);clusters.flush();+ registerAuditLogging(clusters.accountId(), cluster);return cluster;}@@ -841,6 +859,7 @@ public class RedshiftService {proxyManager.updateIamRoles(relayKey(clusters.accountId(), clusterIdentifier), cluster.getIamRoleArns(), defaultRole);clusters.put(clusterIdentifier, cluster);clusters.flush();+ registerAuditLogging(clusters.accountId(), cluster);return cluster;}@@ -931,12 +950,14 @@ public class RedshiftService {cluster.setLoggingEnabled(true);cluster.setLoggingBucketName(bucketName);cluster.setLoggingDestinationType(logDestinationType);+ cluster.setLoggingRegion(regionResolver.getRegion());cluster.setLoggingExports(logExports == null || logExports.isEmpty() ? null : List.copyOf(logExports));cluster.setLoggingS3KeyPrefix(s3KeyPrefix);cluster.setLoggingS3TableKmsKeyId(s3TableKmsKeyId);cluster.setLoggingS3TableGranularity(s3TableGranularity);clusters.put(clusterIdentifier, cluster);clusters.flush();+ registerAuditLogging(clusters.accountId(), cluster);return cluster;}@@ -951,11 +972,13 @@ public class RedshiftService {cluster.setLoggingBucketName(null);cluster.setLoggingS3KeyPrefix(null);cluster.setLoggingDestinationType(null);+ cluster.setLoggingRegion(null);cluster.setLoggingExports(null);cluster.setLoggingS3TableKmsKeyId(null);cluster.setLoggingS3TableGranularity(null);clusters.put(clusterIdentifier, cluster);clusters.flush();+ registerAuditLogging(clusters.accountId(), cluster);return cluster;}@@ -1305,6 +1328,7 @@ public class RedshiftService {cluster.setMasterUsername(username);cluster.setMasterPassword(password);cluster.setClusterParameterGroupName(DEFAULT_PARAMETER_GROUP_NAME);+ cluster.setUserActivityLoggingEnabled(userActivityLoggingEnabled(DEFAULT_PARAMETER_GROUP_NAME));cluster.setClusterStatus("creating");storeCluster(cluster);@@ -1431,9 +1455,25 @@ public class RedshiftService {group.setParameters(current);parameterGroups.put(parameterGroupName, group);parameterGroups.flush();+ refreshClustersForActivityParameter(parameterGroupName, group);return group;}+ private boolean userActivityLoggingEnabled(String parameterGroupName) {+ ClusterParameterGroup group = parameterGroups.get(parameterGroupName)+ .or(() -> DEFAULT_PARAMETER_GROUP_NAME.equals(parameterGroupName)+ ? Optional.of(defaultParameterGroup()) : Optional.empty())+ .orElse(null);+ return group != null && userActivityLoggingEnabled(group);+ }++ private static boolean userActivityLoggingEnabled(ClusterParameterGroup group) {+ return group.getParameters().stream()+ .filter(parameter -> "enable_user_activity_logging".equalsIgnoreCase(parameter.getParameterName()))+ .map(Parameter::getParameterValue)+ .anyMatch(value -> Boolean.parseBoolean(value));+ }+public synchronized void resetClusterParameterGroup(String parameterGroupName, boolean resetAllParameters,List<String> parameterNames) {requireMutableParameterGroup(parameterGroupName);@@ -1475,6 +1515,27 @@ public class RedshiftService {group.setParameters(reset);parameterGroups.put(parameterGroupName, group);parameterGroups.flush();+ refreshClustersForActivityParameter(parameterGroupName, group);+ }++ private void refreshClustersForActivityParameter(String parameterGroupName, ClusterParameterGroup group) {+ boolean enabled = userActivityLoggingEnabled(group);+ String accountId = clusters.accountId();+ List<Cluster> affected = clusters.scan(key -> true).stream()+ .filter(cluster -> parameterGroupName.equals(cluster.getClusterParameterGroupName())).toList();+ for (Cluster cluster : affected) {+ cluster.setUserActivityLoggingEnabled(enabled);+ clusters.putForAccount(accountId, cluster.getClusterIdentifier(), cluster);+ registerAuditLogging(accountId, cluster);+ }+ clusters.flush();+ }++ private void registerAuditLogging(String accountId, Cluster cluster) {+ if (auditLogPublisher != null) {+ String region = cluster.getLoggingRegion() != null ? cluster.getLoggingRegion() : regionResolver.getRegion();+ auditLogPublisher.register(accountId, region, cluster);+ }}private void requireMutableParameterGroup(String name) {diff --git a/src/main/java/io/github/hectorvent/floci/services/redshift/model/Cluster.java b/src/main/java/io/github/hectorvent/floci/services/redshift/model/Cluster.javaindex 55862c130c..427e56fee4 100644--- a/src/main/java/io/github/hectorvent/floci/services/redshift/model/Cluster.java+++ b/src/main/java/io/github/hectorvent/floci/services/redshift/model/Cluster.java@@ -50,7 +50,7 @@ public class Cluster {public String getDefaultIamRoleArn() { return defaultIamRoleArn; }public void setDefaultIamRoleArn(String defaultIamRoleArn) { this.defaultIamRoleArn = defaultIamRoleArn; }- // Audit logging config as set by EnableLogging; no log delivery is emulated.+ // Audit logging configuration set by EnableLogging.private boolean loggingEnabled = false;private String loggingBucketName;private String loggingS3KeyPrefix;@@ -63,14 +63,22 @@ public class Cluster {public void setLoggingS3KeyPrefix(String loggingS3KeyPrefix) { this.loggingS3KeyPrefix = loggingS3KeyPrefix; }private String loggingDestinationType;+ private String loggingRegion;private List<String> loggingExports;+ private boolean userActivityLoggingEnabled;private String loggingS3TableKmsKeyId;private String loggingS3TableGranularity;public String getLoggingDestinationType() { return loggingDestinationType; }public void setLoggingDestinationType(String loggingDestinationType) { this.loggingDestinationType = loggingDestinationType; }+ public String getLoggingRegion() { return loggingRegion; }+ public void setLoggingRegion(String loggingRegion) { this.loggingRegion = loggingRegion; }public List<String> getLoggingExports() { return loggingExports; }public void setLoggingExports(List<String> loggingExports) { this.loggingExports = loggingExports; }+ public boolean isUserActivityLoggingEnabled() { return userActivityLoggingEnabled; }+ public void setUserActivityLoggingEnabled(boolean userActivityLoggingEnabled) {+ this.userActivityLoggingEnabled = userActivityLoggingEnabled;+ }public String getLoggingS3TableKmsKeyId() { return loggingS3TableKmsKeyId; }public void setLoggingS3TableKmsKeyId(String loggingS3TableKmsKeyId) { this.loggingS3TableKmsKeyId = loggingS3TableKmsKeyId; }public String getLoggingS3TableGranularity() { return loggingS3TableGranularity; }diff --git a/src/main/java/io/github/hectorvent/floci/services/redshift/model/ClusterParameterGroup.java b/src/main/java/io/github/hectorvent/floci/services/redshift/model/ClusterParameterGroup.javaindex 7d67e06a43..b7eebdea88 100644--- a/src/main/java/io/github/hectorvent/floci/services/redshift/model/ClusterParameterGroup.java+++ b/src/main/java/io/github/hectorvent/floci/services/redshift/model/ClusterParameterGroup.java@@ -55,6 +55,7 @@ public class ClusterParameterGroup {public static List<Parameter> defaultParameters() {List<Parameter> defaults = new ArrayList<>(List.of(new Parameter("max_cursor_result_set_size", "0", "Maximum cursor result set size", "integer"),+ new Parameter("enable_user_activity_logging", "false", "Enable user activity audit logging", "boolean"),new Parameter("wlm_json_configuration", "{}", "WLM configuration", "string")));defaults.forEach(parameter -> parameter.setSource("engine-default"));diff --git a/src/main/java/io/github/hectorvent/floci/services/redshift/proxy/BackendResponseCoordinator.java b/src/main/java/io/github/hectorvent/floci/services/redshift/proxy/BackendResponseCoordinator.javaindex c89d3903c8..403b2b1e37 100644--- a/src/main/java/io/github/hectorvent/floci/services/redshift/proxy/BackendResponseCoordinator.java+++ b/src/main/java/io/github/hectorvent/floci/services/redshift/proxy/BackendResponseCoordinator.java@@ -7,6 +7,7 @@ import java.util.Map;import java.util.concurrent.TimeUnit;import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.ReentrantLock;+import java.util.function.BiConsumer;final class BackendResponseCoordinator {@@ -25,6 +26,10 @@ final class BackendResponseCoordinator {}Ticket register(Operation operation, ExtendedQuerySession.Mutation mutation) {+ return register(operation, mutation, null);+ }++ Ticket register(Operation operation, ExtendedQuerySession.Mutation mutation, BiConsumer<Character, byte[]> response) {lock.lock();try {Ticket ticket = new Ticket(nextSequence++, operation);@@ -36,7 +41,7 @@ final class BackendResponseCoordinator {resolvedGates.put(ticket.sequence(), GateResult.SKIPPED_AFTER_ERROR);}} else {- pending.addLast(new PendingOperation(ticket, mutation));+ pending.addLast(new PendingOperation(ticket, mutation, response));}changed.signalAll();return ticket;@@ -124,6 +129,9 @@ final class BackendResponseCoordinator {}Operation operation = head.ticket().operation();+ if (head.response() != null) {+ head.response().accept(type, body);+ }if (type == 'E') {if (operation != Operation.SIMPLE_QUERY) {pending.removeFirst();@@ -316,6 +324,7 @@ final class BackendResponseCoordinator {record Ticket(long sequence, Operation operation) {}- private record PendingOperation(Ticket ticket, ExtendedQuerySession.Mutation mutation) {+ private record PendingOperation(Ticket ticket, ExtendedQuerySession.Mutation mutation,+ BiConsumer<Character, byte[]> response) {}}diff --git a/src/main/java/io/github/hectorvent/floci/services/redshift/proxy/ExtendedQuerySession.java b/src/main/java/io/github/hectorvent/floci/services/redshift/proxy/ExtendedQuerySession.javaindex 1e03c751af..31ff4cc306 100644--- a/src/main/java/io/github/hectorvent/floci/services/redshift/proxy/ExtendedQuerySession.java+++ b/src/main/java/io/github/hectorvent/floci/services/redshift/proxy/ExtendedQuerySession.java@@ -11,25 +11,29 @@ import java.util.Set;final class ExtendedQuerySession {- private final Map<String, CopyStatementParser.S3Statement> statements = new LinkedHashMap<>();+ private final Map<String, PreparedSql> statements = new LinkedHashMap<>();private final Map<String, String> portals = new LinkedHashMap<>();private final List<JournalEntry> journal = new ArrayList<>();private long nextMutationId = 1;synchronized Mutation stageParse(String statementName, CopyStatementParser.S3Statement statement) {- Map<String, CopyStatementParser.S3Statement> statementsBefore = new LinkedHashMap<>(statements);+ return stageParse(statementName, statement, null);+ }++ synchronized Mutation stageParse(String statementName, CopyStatementParser.S3Statement statement, String sql) {+ Map<String, PreparedSql> statementsBefore = new LinkedHashMap<>(statements);Map<String, String> portalsBefore = new LinkedHashMap<>(portals);- if (statement == null) {+ if (statement == null && sql == null) {statements.remove(statementName);} else {- statements.put(statementName, statement);+ statements.put(statementName, new PreparedSql(statement, sql));}portals.entrySet().removeIf(entry -> entry.getValue().equals(statementName));return record(statementsBefore, portalsBefore);}synchronized Mutation stageBind(String portalName, String statementName) {- Map<String, CopyStatementParser.S3Statement> statementsBefore = new LinkedHashMap<>(statements);+ Map<String, PreparedSql> statementsBefore = new LinkedHashMap<>(statements);Map<String, String> portalsBefore = new LinkedHashMap<>(portals);if (statements.containsKey(statementName)) {portals.put(portalName, statementName);@@ -44,7 +48,7 @@ final class ExtendedQuerySession {throw new IllegalArgumentException("Close target type must be S or P");}- Map<String, CopyStatementParser.S3Statement> statementsBefore = new LinkedHashMap<>(statements);+ Map<String, PreparedSql> statementsBefore = new LinkedHashMap<>(statements);Map<String, String> portalsBefore = new LinkedHashMap<>(portals);if (targetType == 'S') {statements.remove(name);@@ -118,12 +122,16 @@ final class ExtendedQuerySession {}synchronized Optional<CopyStatementParser.S3Statement> statement(String statementName) {- return Optional.ofNullable(statements.get(statementName));+ return Optional.ofNullable(statements.get(statementName)).map(PreparedSql::statement);}synchronized Optional<CopyStatementParser.S3Statement> portal(String portalName) {String statementName = portals.get(portalName);- return Optional.ofNullable(statementName).map(statements::get);+ return Optional.ofNullable(statementName).map(statements::get).map(PreparedSql::statement);+ }++ synchronized Optional<String> portalSql(String portalName) {+ return Optional.ofNullable(portals.get(portalName)).map(statements::get).map(PreparedSql::sql);}synchronized void clearPortals() {@@ -160,7 +168,7 @@ final class ExtendedQuerySession {* entry's "before" or the live maps, keeps it a fixed historical fact: those two proxies go* stale once an earlier {@link #rejectFrom} has already run its own cascading portal cleanup.*/- private Mutation record(Map<String, CopyStatementParser.S3Statement> statementsBefore,+ private Mutation record(Map<String, PreparedSql> statementsBefore,Map<String, String> portalsBefore) {Mutation mutation = new Mutation(nextMutationId++);journal.add(new JournalEntry(@@ -181,11 +189,13 @@ final class ExtendedQuerySession {record Mutation(long id) {}+ private record PreparedSql(CopyStatementParser.S3Statement statement, String sql) { }+private record JournalEntry(Mutation mutation,- Map<String, CopyStatementParser.S3Statement> statementsBefore,+ Map<String, PreparedSql> statementsBefore,Map<String, String> portalsBefore,- Map<String, CopyStatementParser.S3Statement> statementsAfter,+ Map<String, PreparedSql> statementsAfter,Map<String, String> portalsAfter) {}}diff --git a/src/main/java/io/github/hectorvent/floci/services/redshift/proxy/RedshiftAuthProxy.java b/src/main/java/io/github/hectorvent/floci/services/redshift/proxy/RedshiftAuthProxy.javaindex faffad3fb8..a0c82f824b 100644--- a/src/main/java/io/github/hectorvent/floci/services/redshift/proxy/RedshiftAuthProxy.java+++ b/src/main/java/io/github/hectorvent/floci/services/redshift/proxy/RedshiftAuthProxy.java@@ -5,6 +5,7 @@ import io.github.hectorvent.floci.services.rds.proxy.PasswordValidator;import io.github.hectorvent.floci.services.rds.proxy.PostgresProtocolHandler;import io.github.hectorvent.floci.services.rds.proxy.RdsProxyTlsCertificates;import io.github.hectorvent.floci.services.rds.proxy.RdsSigV4Validator;+import io.github.hectorvent.floci.services.redshift.RedshiftAuditLogPublisher;import io.github.hectorvent.floci.services.redshift.spectrum.SpectrumInterceptor;import io.github.hectorvent.floci.services.s3.S3Service;import org.jboss.logging.Logger;@@ -40,6 +41,7 @@ public class RedshiftAuthProxy {private final S3Service s3Service;private final IamService iamService;private final SpectrumInterceptor spectrumInterceptor;+ private final RedshiftAuditLogPublisher auditLogPublisher;private final String clusterAccountId;private volatile RoleConfiguration roleConfiguration;@@ -91,6 +93,21 @@ public class RedshiftAuthProxy {List<String> iamRoleArns,int handshakeTimeoutMillis, int backendConnectTimeoutMillis,int maxConnections, SpectrumInterceptor spectrumInterceptor) {+ this(clusterKey, backendHost, backendPort, masterUsername, masterPassword, dbName, sigV4,+ tlsCertificates, passwordValidator, s3Service, iamService, clusterAccountId, iamRoleArns,+ handshakeTimeoutMillis, backendConnectTimeoutMillis, maxConnections, spectrumInterceptor, null);+ }++ public RedshiftAuthProxy(String clusterKey, String backendHost, int backendPort,+ String masterUsername, String masterPassword, String dbName,+ RdsSigV4Validator sigV4, RdsProxyTlsCertificates tlsCertificates,+ PasswordValidator passwordValidator,+ S3Service s3Service, IamService iamService,+ String clusterAccountId,+ List<String> iamRoleArns,+ int handshakeTimeoutMillis, int backendConnectTimeoutMillis,+ int maxConnections, SpectrumInterceptor spectrumInterceptor,+ RedshiftAuditLogPublisher auditLogPublisher) {this.clusterKey = clusterKey;this.backendHost = backendHost;this.backendPort = backendPort;@@ -103,6 +120,7 @@ public class RedshiftAuthProxy {this.s3Service = s3Service;this.iamService = iamService;this.spectrumInterceptor = spectrumInterceptor;+ this.auditLogPublisher = auditLogPublisher;this.clusterAccountId = clusterAccountId;this.roleConfiguration = new RoleConfiguration(iamRoleArns, null);this.handshakeTimeoutMillis = handshakeTimeoutMillis;@@ -217,12 +235,19 @@ public class RedshiftAuthProxy {private void handleConnection(Socket client) {PostgresProtocolHandler.AuthenticatedSession session = null;+ LoginAttempt login = new LoginAttempt();try {client.setTcpNoDelay(true);PostgresProtocolHandler.BackendConnector connector = () -> {Socket backendSocket = new Socket();- backendSocket.connect(new InetSocketAddress(backendHost, backendPort),- backendConnectTimeoutMillis);+ try {+ backendSocket.connect(new InetSocketAddress(backendHost, backendPort),+ backendConnectTimeoutMillis);+ } catch (IOException failure) {+ login.backendUnavailable = true;+ closeQuietly(backendSocket);+ throw failure;+ }backendSocket.setTcpNoDelay(true);return backendSocket;};@@ -231,17 +256,25 @@ public class RedshiftAuthProxy {session = PostgresProtocolHandler.authenticate(client, connector, masterUsername, masterPassword, dbName,false, sigV4, null, tlsCertificates, passwordValidator,- handshakeTimeoutMillis);+ handshakeTimeoutMillis, false, true, (user, database) -> {+ login.username = user;+ login.database = database;+ });if (session != null) {+ publishConnection(client, login, "authenticated");// Redshift-only DDL (DISTKEY/SORTKEY/ENCODE/...) is rewritten for the plain// PostgreSQL backend on the way through; every other message is relayed verbatim.RoleConfiguration roles = roleConfiguration;new RedshiftInterceptingBridge(session.client(), session.backend(), s3Service, iamService,- clusterAccountId, roles.iamRoleArns(), spectrumInterceptor, clusterKey, dbName,- roles.defaultIamRoleArn()).run();+ clusterAccountId, roles.iamRoleArns(), spectrumInterceptor, clusterKey, login.database,+ roles.defaultIamRoleArn(), auditLogPublisher, login.username).run();+ publishConnection(client, login, "disconnection");+ } else {+ publishConnection(client, login, login.backendUnavailable ? "connection_error" : "authentication failure");}} catch (Exception e) {LOG.debugv("Redshift connection error for cluster {0}: {1}", clusterKey, e.getMessage());+ publishConnection(client, login, "connection_error");} finally {// authenticate's success path hands both sockets to the bridge, which closes both// itself; every other path (early return on a bare probe, auth failure, thrown@@ -256,6 +289,22 @@ public class RedshiftAuthProxy {}}+ private static final class LoginAttempt {+ private String username;+ private String database;+ private boolean backendUnavailable;+ }++ private void publishConnection(Socket client, LoginAttempt login, String event) {+ if (auditLogPublisher == null || login.username == null) {+ return;+ }+ String remoteAddress = client.getInetAddress() == null ? null : client.getInetAddress().getHostAddress();+ String clusterIdentifier = clusterKey.substring(clusterKey.indexOf(':') + 1);+ auditLogPublisher.publishRegisteredConnection(clusterAccountId, clusterIdentifier, login.database, login.username,+ event, remoteAddress, client.getPort());+ }+private static void closeQuietly(Socket s) {if (s == null) {return;diff --git a/src/main/java/io/github/hectorvent/floci/services/redshift/proxy/RedshiftInterceptingBridge.java b/src/main/java/io/github/hectorvent/floci/services/redshift/proxy/RedshiftInterceptingBridge.javaindex e623412779..b22fbc5335 100644--- a/src/main/java/io/github/hectorvent/floci/services/redshift/proxy/RedshiftInterceptingBridge.java+++ b/src/main/java/io/github/hectorvent/floci/services/redshift/proxy/RedshiftInterceptingBridge.java@@ -1,6 +1,10 @@package io.github.hectorvent.floci.services.redshift.proxy;import io.github.hectorvent.floci.services.iam.IamService;+import io.github.hectorvent.floci.services.redshift.RedshiftAuditLogPublisher;+import io.github.hectorvent.floci.services.redshift.RedshiftAuditSql;+import io.github.hectorvent.floci.services.redshift.RedshiftAuditSql.UserChange;+import io.github.hectorvent.floci.services.redshift.RedshiftAuditSql.UserChanges;import io.github.hectorvent.floci.services.redshift.spectrum.PostgresExtendedBackendSession;import io.github.hectorvent.floci.services.redshift.spectrum.SpectrumColumn;import io.github.hectorvent.floci.services.redshift.spectrum.SpectrumInterceptor;@@ -18,6 +22,7 @@ import java.io.OutputStream;import java.net.Socket;import java.net.SocketTimeoutException;import java.nio.charset.StandardCharsets;+import java.util.ArrayDeque;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;@@ -26,6 +31,7 @@ import java.util.Map;import java.util.concurrent.TimeUnit;import java.util.concurrent.locks.LockSupport;import java.util.concurrent.locks.ReentrantLock;+import java.util.function.BiConsumer;/*** Replaces the transparent {@code bridge()} for Redshift connections so SQL in Simple and Extended@@ -65,6 +71,9 @@ public class RedshiftInterceptingBridge {private final SpectrumInterceptor spectrumInterceptor;private final String clusterKey;private final String databaseName;+ private final RedshiftAuditLogPublisher auditLogPublisher;+ private final String databaseUser;+ private final UserChanges auditUserChanges = new UserChanges();private final PostgresExtendedBackendSession relationalBackend;private final Map<String, PostgresWireDecoder.ParseMessage> relationalStatements = new HashMap<>();private final ExtendedQuerySession session = new ExtendedQuerySession();@@ -78,6 +87,7 @@ public class RedshiftInterceptingBridge {private volatile boolean pumpFinished = false;private boolean extendedSpectrumError;private boolean rolledBackInCycle;+ private boolean auditCycleFailed;public RedshiftInterceptingBridge(Socket client, Socket backend, S3Service s3Service, IamService iamService) {this(client, backend, s3Service, iamService, null);@@ -105,6 +115,23 @@ public class RedshiftInterceptingBridge {String clusterAccountId, List<String> iamRoleArns,SpectrumInterceptor spectrumInterceptor, String clusterKey, String databaseName,String defaultIamRoleArn) {+ this(client, backend, s3Service, iamService, clusterAccountId, iamRoleArns, spectrumInterceptor,+ clusterKey, databaseName, defaultIamRoleArn, null, null);+ }++ public RedshiftInterceptingBridge(Socket client, Socket backend, S3Service s3Service, IamService iamService,+ String clusterAccountId, List<String> iamRoleArns,+ SpectrumInterceptor spectrumInterceptor, String clusterKey, String databaseName,+ String defaultIamRoleArn, RedshiftAuditLogPublisher auditLogPublisher) {+ this(client, backend, s3Service, iamService, clusterAccountId, iamRoleArns, spectrumInterceptor,+ clusterKey, databaseName, defaultIamRoleArn, auditLogPublisher, null);+ }++ public RedshiftInterceptingBridge(Socket client, Socket backend, S3Service s3Service, IamService iamService,+ String clusterAccountId, List<String> iamRoleArns,+ SpectrumInterceptor spectrumInterceptor, String clusterKey, String databaseName,+ String defaultIamRoleArn, RedshiftAuditLogPublisher auditLogPublisher,+ String databaseUser) {this.client = client;this.backend = backend;this.s3Service = s3Service;@@ -115,6 +142,8 @@ public class RedshiftInterceptingBridge {this.spectrumInterceptor = spectrumInterceptor;this.clusterKey = clusterKey;this.databaseName = databaseName;+ this.auditLogPublisher = auditLogPublisher;+ this.databaseUser = databaseUser;this.relationalBackend = new PostgresExtendedBackendSession(backend);}@@ -209,6 +238,7 @@ public class RedshiftInterceptingBridge {private void handleSimpleQuery(PostgresWireDecoder.FrontendMessage message, OutputStream backendOut)throws IOException {String sql = message.getSql();+ publishActivity(sql);if (spectrumInterceptor != null) {SpectrumInterceptor.Decision[] decision = new SpectrumInterceptor.Decision[1];boolean intercepted;@@ -268,7 +298,7 @@ public class RedshiftInterceptingBridge {} catch (RuntimeException e) {LOG.warnv("RedshiftSqlInterceptor failed, forwarding original query: {0}", e.getMessage());}- coordinator.register(BackendResponseCoordinator.Operation.SIMPLE_QUERY, null);+ coordinator.register(BackendResponseCoordinator.Operation.SIMPLE_QUERY, null, auditResponses(sql));write(backendOut, packet);}@@ -288,6 +318,7 @@ public class RedshiftInterceptingBridge {|| plan instanceof SpectrumInterceptor.Plan.ExternalDdl) {awaitPriorBackendResponses();spectrumStatements.put(parse.statementName(), plan);+ session.confirm(session.stageParse(parse.statementName(), null, parse.sql()));write(client.getOutputStream(), backendFrame('1', EMPTY_BODY));return;}@@ -319,7 +350,7 @@ public class RedshiftInterceptingBridge {}CopyStatementParser.S3Statement statement = parse.parameterTypeOids().isEmpty()? parseS3Statement(parse.sql()) : null;- ExtendedQuerySession.Mutation mutation = session.stageParse(parse.statementName(), statement);+ ExtendedQuerySession.Mutation mutation = session.stageParse(parse.statementName(), statement, parse.sql());coordinator.register(BackendResponseCoordinator.Operation.PARSE, mutation);String rewritten = parse.sql();@@ -364,6 +395,7 @@ public class RedshiftInterceptingBridge {if (spectrumPlan != null) {awaitPriorBackendResponses();spectrumPortals.put(bind.portalName(), spectrumPlan);+ session.confirm(session.stageBind(bind.portalName(), bind.statementName()));write(client.getOutputStream(), backendFrame('2', EMPTY_BODY));return;}@@ -442,6 +474,10 @@ public class RedshiftInterceptingBridge {private void handleExecute(PostgresWireDecoder decoder, PostgresWireDecoder.FrontendMessage message,OutputStream backendOut) throws IOException {PostgresWireDecoder.ExecuteMessage execute = decoder.decodeExecute(message);+ String auditSql = session.portalSql(execute.portalName()).orElse(null);+ if (auditSql != null) {+ publishActivity(auditSql);+ }SpectrumInterceptor.Plan spectrumPlan = spectrumPortals.get(execute.portalName());if (spectrumPlan != null) {BackendResponseCoordinator.Ticket ticket = coordinator.register(@@ -450,7 +486,8 @@ public class RedshiftInterceptingBridge {return;}BackendResponseCoordinator.Ticket ticket = coordinator.register(- BackendResponseCoordinator.Operation.EXECUTE, null);+ BackendResponseCoordinator.Operation.EXECUTE, null,+ auditSql == null ? null : auditResponses(auditSql));CopyStatementParser.S3Statement statement = session.portal(execute.portalName()).orElse(null);if (statement == null) {write(backendOut, message.toPacketBytes());@@ -543,6 +580,7 @@ public class RedshiftInterceptingBridge {// (they are synthesized locally, see handleParse/handleBind), so the backend has never// heard of this name either: forwarding Close would draw a spurious "does not exist" error.awaitPriorBackendResponses();+ session.confirm(session.stageClose(close.targetType(), close.name()));write(client.getOutputStream(), backendFrame('3', EMPTY_BODY));return;}@@ -825,9 +863,13 @@ public class RedshiftInterceptingBridge {if (type == 'E') {relationalBackend.onBackendError();}+ if (type == 'E') {+ auditCycleFailed = true;+ }coordinator.onBackendFrame(type, body);if (type == 'Z') {relationalBackend.onSync();+ completeUserChanges(body);}});try {@@ -865,6 +907,54 @@ public class RedshiftInterceptingBridge {}}+ private void publishActivity(String sql) {+ if (auditLogPublisher == null) {+ return;+ }+ auditLogPublisher.publishRegisteredActivity(clusterAccountId, clusterIdentifier(), databaseName,+ databaseUser, sql);+ }++ private BiConsumer<Character, byte[]> auditResponses(String sql) {+ ArrayDeque<String> commands = new ArrayDeque<>(RedshiftAuditSql.statements(sql));+ return (type, body) -> {+ if (type != 'C' || auditLogPublisher == null) {+ return;+ }+ String command = commands.pollFirst();+ if (command == null) {+ return;+ }+ String tag = new String(body, StandardCharsets.UTF_8).replace("\0", "");+ auditUserChanges.complete(command, tag);+ publishCompletedUserChanges();+ };+ }++ private void completeUserChanges(byte[] readyStatus) {+ if (readyStatus.length > 0 && readyStatus[0] == 'I') {+ if (auditCycleFailed) {+ auditUserChanges.rollback();+ } else {+ auditUserChanges.commit();+ }+ publishCompletedUserChanges();+ }+ auditCycleFailed = false;+ }++ private void publishCompletedUserChanges() {+ for (UserChange change : auditUserChanges.drainCommitted()) {+ auditLogPublisher.publishRegisteredUserChange(clusterAccountId, clusterIdentifier(), databaseName,+ change.username(), change.action(), change.oldUsername());+ }+ }++ private String clusterIdentifier() {+ int separator = clusterKey.indexOf(':');+ return separator < 0 ? clusterKey : clusterKey.substring(separator + 1);+ }+private void closeQuietly(Socket socket, String which) {try {if (socket != null && !socket.isClosed()) {diff --git a/src/main/java/io/github/hectorvent/floci/services/redshift/proxy/RedshiftProxyManager.java b/src/main/java/io/github/hectorvent/floci/services/redshift/proxy/RedshiftProxyManager.javaindex ed9ffb2bff..8de5830628 100644--- a/src/main/java/io/github/hectorvent/floci/services/redshift/proxy/RedshiftProxyManager.java+++ b/src/main/java/io/github/hectorvent/floci/services/redshift/proxy/RedshiftProxyManager.java@@ -5,6 +5,7 @@ import io.github.hectorvent.floci.services.iam.IamService;import io.github.hectorvent.floci.services.rds.proxy.PasswordValidator;import io.github.hectorvent.floci.services.rds.proxy.RdsProxyTlsCertificates;import io.github.hectorvent.floci.services.rds.proxy.RdsSigV4Validator;+import io.github.hectorvent.floci.services.redshift.RedshiftAuditLogPublisher;import io.github.hectorvent.floci.services.redshift.spectrum.SpectrumInterceptor;import io.github.hectorvent.floci.services.s3.S3Service;import io.quarkus.runtime.ShutdownEvent;@@ -33,6 +34,7 @@ public class RedshiftProxyManager {private final IamService iamService;private final SpectrumInterceptor spectrumInterceptor;private final EmulatorConfig config;+ private final RedshiftAuditLogPublisher auditLogPublisher;private final ConcurrentHashMap<String, RedshiftAuthProxy> proxies = new ConcurrentHashMap<>();/*** Proxies whose listener could not be closed during a failed start or replace. The@@ -47,16 +49,24 @@ public class RedshiftProxyManager {this(sigV4Validator, tlsCertificates, s3Service, iamService, config, null);}- @Injectpublic RedshiftProxyManager(RdsSigV4Validator sigV4Validator, RdsProxyTlsCertificates tlsCertificates,S3Service s3Service, IamService iamService, EmulatorConfig config,SpectrumInterceptor spectrumInterceptor) {+ this(sigV4Validator, tlsCertificates, s3Service, iamService, config, spectrumInterceptor, null);+ }++ @Inject+ public RedshiftProxyManager(RdsSigV4Validator sigV4Validator, RdsProxyTlsCertificates tlsCertificates,+ S3Service s3Service, IamService iamService, EmulatorConfig config,+ SpectrumInterceptor spectrumInterceptor,+ RedshiftAuditLogPublisher auditLogPublisher) {this.sigV4Validator = sigV4Validator;this.tlsCertificates = tlsCertificates;this.s3Service = s3Service;this.iamService = iamService;this.spectrumInterceptor = spectrumInterceptor;this.config = config;+ this.auditLogPublisher = auditLogPublisher;}public synchronized void startProxy(String relayKey, int proxyPort,@@ -94,7 +104,7 @@ public class RedshiftProxyManager {clusterAccountId,iamRoleArns,redshiftConfig.proxyHandshakeTimeoutMillis(), redshiftConfig.proxyBackendConnectTimeoutMillis(),- redshiftConfig.proxyMaxConnections(), spectrumInterceptor);+ redshiftConfig.proxyMaxConnections(), spectrumInterceptor, auditLogPublisher);proxy.updateIamRoles(iamRoleArns, defaultIamRoleArn);try {proxy.start(proxyPort);diff --git a/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataResourceResolver.java b/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataResourceResolver.javaindex 7cc0bbe8e2..22e11cb538 100644--- a/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataResourceResolver.java+++ b/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataResourceResolver.java@@ -54,7 +54,7 @@ class RedshiftDataResourceResolver {if (hasText(request, "SecretArn")) {return resolveViaSecret(request, region, database);}- return resolveViaDbUser(request, database);+ return resolveViaDbUser(request, region, database);}/**@@ -117,21 +117,23 @@ class RedshiftDataResourceResolver {return target(clusterArn(clusterId, region), cluster, database, creds.username(), creds.password());}- private DatabaseTarget resolveViaDbUser(JsonNode request, String database) {+ private DatabaseTarget resolveViaDbUser(JsonNode request, String region, String database) {String clusterId = requiredText(request, "ClusterIdentifier");String dbUser = requiredText(request, "DbUser");Cluster cluster = cluster(clusterId);if (dbUser.equals(cluster.getMasterUsername())) {- return target(clusterArn(clusterId, null), cluster, database, dbUser, cluster.getMasterPassword());+ return target(clusterArn(clusterId, region), cluster, database, dbUser, cluster.getMasterPassword());}boolean liveCredential = credentialBroker.resolve(regionResolver.getAccountId(), clusterId, dbUser).isPresent();if (liveCredential) {// The minted DbUser is nominal: connect as the cluster master.- return target(clusterArn(clusterId, null), cluster, database,+ DatabaseTarget backend = target(clusterArn(clusterId, region), cluster, database,cluster.getMasterUsername(), cluster.getMasterPassword());+ return new DatabaseTarget(backend.arn(), backend.host(), backend.port(), backend.database(), backend.user(),+ backend.password(), backend.spectrum(), cluster, dbUser);}throw validation("DbUser " + dbUser + " has no active credentials; call GetClusterCredentials first.");}@@ -153,7 +155,7 @@ class RedshiftDataResourceResolver {String accountId = AwsArnUtils.parse(arn).accountId();SpectrumSession spectrum = new SpectrumSession(accountId, accountId + ":" + cluster.getClusterIdentifier(),database, cluster.getIamRoleArns(), false);- return new DatabaseTarget(arn, host, port, database, user, password, spectrum);+ return new DatabaseTarget(arn, host, port, database, user, password, spectrum, cluster);}private String clusterArn(String clusterId, String region) {@@ -184,9 +186,18 @@ class RedshiftDataResourceResolver {}record DatabaseTarget(String arn, String host, int port, String database, String user, String password,- SpectrumSession spectrum) {+ SpectrumSession spectrum, Cluster cluster, String auditUser) {+ DatabaseTarget(String arn, String host, int port, String database, String user, String password,+ SpectrumSession spectrum, Cluster cluster) {+ this(arn, host, port, database, user, password, spectrum, cluster, user);+ }DatabaseTarget(String arn, String host, int port, String database, String user, String password) {- this(arn, host, port, database, user, password, null);+ this(arn, host, port, database, user, password, null, null);+ }++ DatabaseTarget(String arn, String host, int port, String database, String user, String password,+ SpectrumSession spectrum) {+ this(arn, host, port, database, user, password, spectrum, null);}}diff --git a/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataService.java b/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataService.javaindex e47580b5ba..6a619ac924 100644--- a/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataService.java+++ b/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataService.java@@ -11,6 +11,10 @@ import io.github.hectorvent.floci.core.common.Resettable;import io.github.hectorvent.floci.core.common.SqlParameterParser.ParsedSql;import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;import io.github.hectorvent.floci.services.iam.IamService;+import io.github.hectorvent.floci.services.redshift.RedshiftAuditLogPublisher;+import io.github.hectorvent.floci.services.redshift.RedshiftAuditSql;+import io.github.hectorvent.floci.services.redshift.RedshiftAuditSql.UserChange;+import io.github.hectorvent.floci.services.redshift.RedshiftAuditSql.UserChanges;import io.github.hectorvent.floci.services.redshift.spectrum.BackendSql;import io.github.hectorvent.floci.services.redshift.spectrum.SpectrumQueryPreparation;import io.github.hectorvent.floci.services.redshift.spectrum.SpectrumReadException;@@ -65,6 +69,7 @@ public class RedshiftDataService implements Resettable {private final SpectrumQueryPreparation preparation;private final RedshiftDataExecutor executor;private final RedshiftDataEventPublisher publisher;+ private final RedshiftAuditLogPublisher auditLogPublisher;private final RequestContext requestContext;private final IamService iamService;private final RedshiftDataIdempotencyStore idempotency;@@ -76,7 +81,16 @@ public class RedshiftDataService implements Resettable {RedshiftDataExecutor executor, RedshiftDataEventPublisher publisher) {this(resolver, connectionFactory, store, objectMapper, preparation, executor, publisher, null,null, new RedshiftDataIdempotencyStore(AccountAwareStorageBackend.inMemory("000000000000"),- Clock.systemUTC()));+ Clock.systemUTC()), new RedshiftDataSessions(), null);+ }++ RedshiftDataService(RedshiftDataResourceResolver resolver, RedshiftDataConnectionFactory connectionFactory,+ RedshiftDataStatementStore store, ObjectMapper objectMapper, SpectrumQueryPreparation preparation,+ RedshiftDataExecutor executor, RedshiftDataEventPublisher publisher,+ RequestContext requestContext, IamService iamService, RedshiftDataIdempotencyStore idempotency,+ RedshiftDataSessions sessions) {+ this(resolver, connectionFactory, store, objectMapper, preparation, executor, publisher, requestContext,+ iamService, idempotency, sessions, null);}RedshiftDataService(RedshiftDataResourceResolver resolver, RedshiftDataConnectionFactory connectionFactory,@@ -84,7 +98,7 @@ public class RedshiftDataService implements Resettable {RedshiftDataExecutor executor, RedshiftDataEventPublisher publisher,RequestContext requestContext, IamService iamService, RedshiftDataIdempotencyStore idempotency) {this(resolver, connectionFactory, store, objectMapper, preparation, executor, publisher,- requestContext, iamService, idempotency, new RedshiftDataSessions());+ requestContext, iamService, idempotency, new RedshiftDataSessions(), null);}@Inject@@ -92,7 +106,7 @@ public class RedshiftDataService implements Resettable {RedshiftDataStatementStore store, ObjectMapper objectMapper, SpectrumQueryPreparation preparation,RedshiftDataExecutor executor, RedshiftDataEventPublisher publisher,RequestContext requestContext, IamService iamService, RedshiftDataIdempotencyStore idempotency,- RedshiftDataSessions sessions) {+ RedshiftDataSessions sessions, RedshiftAuditLogPublisher auditLogPublisher) {this.resolver = resolver;this.connectionFactory = connectionFactory;this.store = store;@@ -100,6 +114,7 @@ public class RedshiftDataService implements Resettable {this.preparation = preparation;this.executor = executor;this.publisher = publisher;+ this.auditLogPublisher = auditLogPublisher;this.requestContext = requestContext;this.iamService = iamService;this.idempotency = idempotency;@@ -406,14 +421,41 @@ public class RedshiftDataService implements Resettable {private ConnectionUse connection(RedshiftDataResourceResolver.DatabaseTarget target,RedshiftDataSessions.Reservation session) throws SQLException {- return new ConnectionUse(session == null ? connectionFactory.open(target) : session.connection(connectionFactory), session);+ Runnable disconnected = () -> publishConnection(target, "disconnection", null);+ try {+ Connection connection;+ if (session == null) {+ connection = connectionFactory.open(target);+ publishConnection(target, "authenticated", null);+ } else {+ connection = session.connection(connectionFactory,+ () -> publishConnection(target, "authenticated", null), disconnected);+ }+ return new ConnectionUse(connection, session, disconnected);+ } catch (SQLException failure) {+ publishConnection(target, "authentication failure", failure.getSQLState());+ throw failure;+ }}- private record ConnectionUse(Connection connection, RedshiftDataSessions.Reservation session) implements AutoCloseable {+ private void publishConnection(RedshiftDataResourceResolver.DatabaseTarget target, String event, String error) {+ if (auditLogPublisher != null && target.cluster() != null) {+ AwsArnUtils.Arn arn = AwsArnUtils.parse(target.arn());+ auditLogPublisher.publishConnection(target.cluster(), arn.accountId(), arn.region(), target.database(),+ target.auditUser(), event, null, error);+ }+ }++ private record ConnectionUse(Connection connection, RedshiftDataSessions.Reservation session,+ Runnable disconnected) implements AutoCloseable {@Overridepublic void close() throws SQLException {if (session == null) {- connection.close();+ try {+ connection.close();+ } finally {+ disconnected.run();+ }return;}try {@@ -443,7 +485,8 @@ public class RedshiftDataService implements Resettable {Connection connection = use.connection();execution.track(connection);try {- if (session != null && transactionControl(stored, sql, connection, session, execution)) {+ publishActivity(stored, target, sql);+ if (session != null && transactionControl(stored, target, sql, connection, session, execution)) {return;}if (session != null && session.transactionFailed()) {@@ -458,6 +501,7 @@ public class RedshiftDataService implements Resettable {}execution.commit(() -> runOnConnection(stored, connection, sql, parameters,target.spectrum(), execution, true));+ successfulUserChange(stored, target, sql, session);} else {runOnConnection(stored, connection, sql, parameters, target.spectrum(), execution, false);execution.commit(() -> {@@ -465,6 +509,7 @@ public class RedshiftDataService implements Resettable {connection.commit();}});+ successfulUserChange(stored, target, sql, session);}} catch (SQLException failure) {if (session != null && failure.getSQLState() != null && failure.getSQLState().startsWith("08")) {@@ -490,7 +535,36 @@ public class RedshiftDataService implements Resettable {stored.updatedAt = Instant.now();}- private boolean transactionControl(RedshiftDataStatementStore.StoredStatement stored, String sql,+ private void publishActivity(RedshiftDataStatementStore.StoredStatement stored,+ RedshiftDataResourceResolver.DatabaseTarget target, String sql) {+ if (auditLogPublisher != null && target.cluster() != null) {+ auditLogPublisher.publishActivity(target.cluster(), stored.accountId, stored.region,+ target.database(), target.auditUser(), sql);+ }+ }++ private void successfulUserChange(RedshiftDataStatementStore.StoredStatement stored,+ RedshiftDataResourceResolver.DatabaseTarget target, String sql,+ RedshiftDataSessions.Reservation session) {+ if (session != null && session.inTransaction()) {+ session.completeUserChanges(sql);+ } else {+ publishUserChanges(stored, target, RedshiftAuditSql.userChanges(sql));+ }+ }++ private void publishUserChanges(RedshiftDataStatementStore.StoredStatement stored,+ RedshiftDataResourceResolver.DatabaseTarget target, List<UserChange> changes) {+ if (auditLogPublisher != null && target.cluster() != null) {+ for (UserChange change : changes) {+ auditLogPublisher.publishUserChange(target.cluster(), stored.accountId, stored.region,+ target.database(), change.username(), change.action(), change.oldUsername());+ }+ }+ }++ private boolean transactionControl(RedshiftDataStatementStore.StoredStatement stored,+ RedshiftDataResourceResolver.DatabaseTarget target, String sql,Connection connection, RedshiftDataSessions.Reservation session,RedshiftDataExecution execution) throws SQLException {String normalized = TRANSACTION_COMMENTS.matcher(sql).replaceAll(" ").trim();@@ -512,6 +586,7 @@ public class RedshiftDataService implements Resettable {session.transaction(true);}case "COMMIT", "END" -> {+ boolean rolledBack = session.transactionFailed();if (!connection.getAutoCommit()) {if (session.transactionFailed()) {connection.rollback();@@ -519,6 +594,10 @@ public class RedshiftDataService implements Resettable {connection.commit();}}+ List<UserChange> changes = session.takeUserChanges();+ if (!rolledBack) {+ publishUserChanges(stored, target, changes);+ }session.transaction(false);connection.setAutoCommit(true);}@@ -526,6 +605,7 @@ public class RedshiftDataService implements Resettable {if (!connection.getAutoCommit()) {connection.rollback();}+ session.takeUserChanges();session.transaction(false);connection.setAutoCommit(true);}@@ -827,6 +907,7 @@ public class RedshiftDataService implements Resettable {Connection connection = use.connection();execution.track(connection);BackendSql batchBackend = new RedshiftDataBackendSql(connection, execution);+ UserChanges userChanges = session == null ? new UserChanges() : session.takeUserChangeState();boolean committed = false;try {connection.setAutoCommit(false);@@ -847,7 +928,8 @@ public class RedshiftDataService implements Resettable {sub.updatedAt = now;try {execution.checkCancellation();- runOnConnection(sub, connection, sqls.get(n), parameters, target.spectrum(), execution, false);+ publishActivity(parent, target, sqls.get(n));+ runBatchEntry(sub, connection, sqls.get(n), parameters, target.spectrum(), execution, userChanges);} catch (SQLException e) {sub.status = RedshiftDataStatementStore.Status.FAILED;sub.error = e.getMessage();@@ -862,6 +944,9 @@ public class RedshiftDataService implements Resettable {throw e;}connection.rollback();+ if (session != null) {+ session.takeUserChanges();+ }failed = true;break;}@@ -876,9 +961,17 @@ public class RedshiftDataService implements Resettable {if (!failed) {execution.commit(connection::commit);committed = true;+ userChanges.commit();parent.status = RedshiftDataStatementStore.Status.FINISHED;}} finally {+ if (!committed) {+ userChanges.rollback();+ }+ publishUserChanges(parent, target, userChanges.drainCommitted());+ if (!committed && session != null) {+ session.takeUserChanges();+ }if (preparation != null) {if (committed) {preparation.finishCycle(batchBackend, true);@@ -907,6 +1000,34 @@ public class RedshiftDataService implements Resettable {parent.updatedAt = Instant.now();}+ private void runBatchEntry(RedshiftDataStatementStore.StoredStatement sub, Connection connection, String sql,+ Map<String, String> parameters, SpectrumSession context,+ RedshiftDataExecution execution, UserChanges userChanges)+ throws SQLException {+ List<String> commands = RedshiftAuditSql.statements(sql);+ if (commands.isEmpty()) {+ runOnConnection(sub, connection, sql, parameters, context, execution, false);+ return;+ }+ long duration = 0;+ for (int i = 0; i < commands.size(); i++) {+ // JDBC exposes the first result of an entry; later commands still affect transaction audit state.+ RedshiftDataStatementStore.StoredStatement result = i == 0 ? sub : new RedshiftDataStatementStore.StoredStatement();+ try {+ String command = commands.get(i);+ if (RedshiftAuditSql.isCommit(command)) {+ execution.commit(() -> runOnConnection(result, connection, command, parameters, context, execution, false));+ } else {+ runOnConnection(result, connection, command, parameters, context, execution, false);+ }+ userChanges.complete(command);+ } finally {+ duration += result.durationNanos;+ sub.durationNanos = duration;+ }+ }+ }+// ── ListStatements ─────────────────────────────────────────────────────public ObjectNode listStatements(JsonNode request) {diff --git a/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataSessions.java b/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataSessions.javaindex 2466343f5e..efd129ca0a 100644--- a/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataSessions.java+++ b/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataSessions.java@@ -3,6 +3,8 @@ package io.github.hectorvent.floci.services.redshiftdata;import com.fasterxml.jackson.databind.JsonNode;import com.fasterxml.jackson.databind.node.ObjectNode;import io.github.hectorvent.floci.core.common.AwsException;+import io.github.hectorvent.floci.services.redshift.RedshiftAuditSql.UserChange;+import io.github.hectorvent.floci.services.redshift.RedshiftAuditSql.UserChanges;import jakarta.annotation.PostConstruct;import jakarta.annotation.PreDestroy;import jakarta.enterprise.context.ApplicationScoped;@@ -177,14 +179,16 @@ class RedshiftDataSessions implements AutoCloseable {}session.closed = true;sessions.remove(session.id, session);- Retirement retirement = new Retirement(session.id, session.connection, cancel ? session.execution : null);+ Retirement retirement = new Retirement(session.id, session.connection, cancel ? session.execution : null,+ session.connectionClosed);session.active = null;session.execution = null;session.connection = null;+ session.userChanges.rollback();return retirement;}- private record Retirement(String id, Connection connection, RedshiftDataExecution execution) { }+ private record Retirement(String id, Connection connection, RedshiftDataExecution execution, Runnable connectionClosed) { }private static void cleanUp(Retirement retirement) {if (retirement == null) {@@ -196,7 +200,13 @@ class RedshiftDataSessions implements AutoCloseable {retirement.execution().cancel();}} finally {- closeConnection(retirement.connection(), retirement.id());+ try {+ closeConnection(retirement.connection(), retirement.id());+ } finally {+ if (retirement.connection() != null && retirement.connectionClosed() != null) {+ retirement.connectionClosed().run();+ }+ }}};if (retirement.execution() == null) {@@ -277,6 +287,8 @@ class RedshiftDataSessions implements AutoCloseable {private Connection connection;private RedshiftDataExecution execution;private Reservation active;+ private Runnable connectionClosed;+ private UserChanges userChanges = new UserChanges();private Session(Scope owner, RedshiftDataResourceResolver.DatabaseTarget target, JsonNode request, Instant createdAt) {this.owner = owner;@@ -338,6 +350,10 @@ class RedshiftDataSessions implements AutoCloseable {}Connection connection(RedshiftDataConnectionFactory factory) throws SQLException {+ return connection(factory, () -> { }, () -> { });+ }++ Connection connection(RedshiftDataConnectionFactory factory, Runnable openedAudit, Runnable closedAudit) throws SQLException {synchronized (session) {if (session.closed) {throw new SQLException("Session has expired", "08003");@@ -347,13 +363,19 @@ class RedshiftDataSessions implements AutoCloseable {}}Connection opened = factory.open(session.target);+ openedAudit.run();synchronized (session) {if (!session.closed && !expired(session)) {session.connection = opened;+ session.connectionClosed = closedAudit;return opened;}}- closeConnection(opened, session.id);+ try {+ closeConnection(opened, session.id);+ } finally {+ closedAudit.run();+ }throw new SQLException("Session has expired", "08003");}@@ -376,6 +398,28 @@ class RedshiftDataSessions implements AutoCloseable {session.transactionFailed = true;}+ void completeUserChanges(String sql) {+ synchronized (session) {+ if (!session.closed) {+ session.userChanges.complete(sql);+ }+ }+ }++ List<UserChange> takeUserChanges() {+ UserChanges changes = takeUserChangeState();+ changes.commit();+ return changes.drainCommitted();+ }++ UserChanges takeUserChangeState() {+ synchronized (session) {+ UserChanges changes = session.userChanges;+ session.userChanges = new UserChanges();+ return changes;+ }+ }+void abort() {release(false);}diff --git a/src/test/java/io/github/hectorvent/floci/services/redshift/RedshiftAuditLogPublisherTest.java b/src/test/java/io/github/hectorvent/floci/services/redshift/RedshiftAuditLogPublisherTest.javanew file mode 100644index 0000000000..51c565eac7--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/redshift/RedshiftAuditLogPublisherTest.java@@ -0,0 +1,170 @@+package io.github.hectorvent.floci.services.redshift;++import io.github.hectorvent.floci.services.cloudwatch.logs.CloudWatchLogsService;+import io.github.hectorvent.floci.services.redshift.model.Cluster;+import org.junit.jupiter.api.Test;+import org.junit.jupiter.params.ParameterizedTest;+import org.junit.jupiter.params.provider.ValueSource;+import org.mockito.ArgumentCaptor;++import java.time.Clock;+import java.time.Instant;+import java.time.ZoneOffset;+import java.util.List;+import java.util.Map;++import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;+import static org.junit.jupiter.api.Assertions.assertEquals;+import static org.junit.jupiter.api.Assertions.assertFalse;+import static org.mockito.ArgumentMatchers.anyList;+import static org.mockito.ArgumentMatchers.anyMap;+import static org.mockito.ArgumentMatchers.anyString;+import static org.mockito.ArgumentMatchers.eq;+import static org.mockito.ArgumentMatchers.isNull;+import static org.mockito.Mockito.doThrow;+import static org.mockito.Mockito.mock;+import static org.mockito.Mockito.never;+import static org.mockito.Mockito.verify;++class RedshiftAuditLogPublisherTest {++ @Test+ void createsAwsNamedResourcesAndWritesSelectedExport() {+ CloudWatchLogsService logs = mock(CloudWatchLogsService.class);+ RedshiftAuditLogPublisher publisher = new RedshiftAuditLogPublisher(logs,+ Clock.fixed(Instant.ofEpochMilli(1234), ZoneOffset.UTC));+ Cluster cluster = enabledCluster(List.of("useractivitylog"));+ cluster.setUserActivityLoggingEnabled(true);++ publisher.publishActivity(cluster, "111111111111", "us-east-1", "dev", "admin", "select 1,2");++ verify(logs).createLogGroupForAccount("111111111111",+ "/aws/redshift/cluster/analytics/useractivitylog", null, Map.of(), "us-east-1");+ verify(logs).createLogStreamForAccount("111111111111",+ "/aws/redshift/cluster/analytics/useractivitylog", "analytics", "us-east-1");+ ArgumentCaptor<List<Map<String, Object>>> events = ArgumentCaptor.forClass(List.class);+ verify(logs).putLogEventsForAccount(eq("111111111111"),+ eq("/aws/redshift/cluster/analytics/useractivitylog"), eq("analytics"),+ events.capture(), eq("us-east-1"));+ assertEquals(1234L, events.getValue().get(0).get("timestamp"));+ assertEquals("1970-01-01T00:00:01.234Z,dev,admin,,,,\"LOG: select 1,2\"",+ events.getValue().get(0).get("message"));+ }++ @Test+ void skipsUnselectedExportsAndNonCloudWatchDestinations() {+ CloudWatchLogsService logs = mock(CloudWatchLogsService.class);+ RedshiftAuditLogPublisher publisher = new RedshiftAuditLogPublisher(logs, Clock.systemUTC());+ Cluster cluster = enabledCluster(List.of("connectionlog"));++ publisher.publish(cluster, "111111111111", "us-east-1", "userlog", Map.of("action", "Create"));+ cluster.setLoggingDestinationType("s3");+ publisher.publish(cluster, "111111111111", "us-east-1", "connectionlog", Map.of("event", "connect"));++ verify(logs, never()).putLogEventsForAccount(anyString(), anyString(), anyString(), anyList(), anyString());+ }++ @Test+ void userActivityExportRequiresTheParameterGroupFlag() {+ CloudWatchLogsService logs = mock(CloudWatchLogsService.class);+ RedshiftAuditLogPublisher publisher = new RedshiftAuditLogPublisher(logs, Clock.systemUTC());+ Cluster cluster = enabledCluster(List.of("useractivitylog"));++ publisher.publishActivity(cluster, "111111111111", "us-east-1", "dev", "admin", "SELECT 1");++ verify(logs, never()).putLogEventsForAccount(anyString(), anyString(), anyString(), anyList(), anyString());+ }++ @Test+ void registeredConnectionsUseTheSharedCsvColumnLayout() {+ CloudWatchLogsService logs = mock(CloudWatchLogsService.class);+ RedshiftAuditLogPublisher publisher = new RedshiftAuditLogPublisher(logs,+ Clock.fixed(Instant.ofEpochMilli(1234), ZoneOffset.UTC));+ publisher.register("111111111111", "us-west-2", enabledCluster(List.of("connectionlog")));++ publisher.publishRegisteredConnection("111111111111", "analytics", "reports", "analyst", "authenticated",+ "192.0.2.1", 54321);++ ArgumentCaptor<List<Map<String, Object>>> events = ArgumentCaptor.forClass(List.class);+ verify(logs).putLogEventsForAccount(eq("111111111111"),+ eq("/aws/redshift/cluster/analytics/connectionlog"), eq("analytics"), events.capture(), eq("us-west-2"));+ String[] columns = events.getValue().getFirst().get("message").toString().split(",", -1);+ assertEquals(22, columns.length);+ assertEquals("authenticated", columns[0]);+ assertEquals("1970-01-01T00:00:01.234Z", columns[1]);+ assertEquals("192.0.2.1", columns[2]);+ assertEquals("54321", columns[3]);+ assertEquals("reports", columns[5]);+ assertEquals("analyst", columns[6]);+ assertEquals("password", columns[7]);+ }++ @Test+ void cloudWatchFailureDoesNotEscapePublisher() {+ CloudWatchLogsService logs = mock(CloudWatchLogsService.class);+ doThrow(new IllegalStateException("logs unavailable"))+ .when(logs).createLogGroupForAccount(anyString(), anyString(), isNull(), anyMap(), anyString());+ RedshiftAuditLogPublisher publisher = new RedshiftAuditLogPublisher(logs, Clock.systemUTC());++ assertDoesNotThrow(() -> publisher.publish(enabledCluster(List.of("userlog")),+ "111111111111", "us-east-1", "userlog", Map.of("action", "Create")));+ }++ @Test+ void redactsSqlPasswordLiteralsFromActivityRecords() {+ assertEquals("ALTER USER alice PASSWORD '[REDACTED]'", RedshiftAuditLogPublisher.sanitizeSql(+ "ALTER USER alice PASSWORD 'top-secret'"));+ }++ @ParameterizedTest+ @ValueSource(strings = {+ "ALTER USER alice PASSWORD /* rotation */ 'secret'",+ "ALTER USER alice PASSWORD -- rotation\n 'secret'",+ "ALTER USER alice PASSWORD /* outer /* nested */ comment */ 'secret'",+ "ALTER USER alice PASSWORD E'secret\\'suffix'",+ "ALTER USER alice PASSWORD $tag$secret$tag$",+ "ALTER USER alice PASSWORD 'first'\n 'secret'",+ "ALTER USER alice PASSWORD U&'secret'",+ "ALTER USER alice PASSWORD N'secret'"+ })+ void redactsCompletePasswordsAfterSqlComments(String sql) {+ String sanitized = RedshiftAuditLogPublisher.sanitizeSql(sql);+ assertFalse(sanitized.contains("secret"), sanitized);+ assertFalse(sanitized.contains("suffix"), sanitized);+ }++ @ParameterizedTest+ @ValueSource(strings = {+ "ALTER USER alice PASSWORD 'prefix\\'suffix'",+ "SET standard_conforming_strings=off; ALTER USER alice PASSWORD 'prefix\\'suffix'",+ "ALTER USER alice PASSWORD N'prefix\\'suffix'",+ "ALTER USER alice PASSWORD 'first'\n 'prefix\\'suffix'",+ "ALTER USER alice PASSWORD 'prefix\\'suffix'; ALTER USER bob PASSWORD 'anothersecret'",+ "SELECT 'path\\'tail'; ALTER USER alice PASSWORD 'anothersecret'",+ "SELECT 'path\\'; ALTER USER alice PASSWORD 'anothersecret'"+ })+ void redactsPasswordsWithUncertainBackslashBoundaries(String sql) {+ String sanitized = RedshiftAuditLogPublisher.sanitizeSql(sql);+ assertFalse(sanitized.contains("prefix"), sanitized);+ assertFalse(sanitized.contains("suffix"), sanitized);+ assertFalse(sanitized.contains("first"), sanitized);+ assertFalse(sanitized.contains("anothersecret"), sanitized);+ }++ @Test+ void explicitEscapeStringsRetainFollowingSqlAndOtherBackslashStringsRemainVisible() {+ assertEquals("ALTER USER alice PASSWORD '[REDACTED]'; SELECT 1", RedshiftAuditLogPublisher.sanitizeSql(+ "ALTER USER alice PASSWORD E'prefix\\'suffix'; SELECT 1"));+ String sql = "SELECT 'C:\\reports\\daily'";+ assertEquals(sql, RedshiftAuditLogPublisher.sanitizeSql(sql));+ }++ private static Cluster enabledCluster(List<String> exports) {+ Cluster cluster = new Cluster();+ cluster.setClusterIdentifier("analytics");+ cluster.setLoggingEnabled(true);+ cluster.setLoggingDestinationType("cloudwatch");+ cluster.setLoggingExports(exports);+ return cluster;+ }+}diff --git a/src/test/java/io/github/hectorvent/floci/services/redshift/RedshiftServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/redshift/RedshiftServiceTest.javaindex b59d06864d..d50d7450d3 100644--- a/src/test/java/io/github/hectorvent/floci/services/redshift/RedshiftServiceTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/redshift/RedshiftServiceTest.java@@ -8,6 +8,7 @@ import io.github.hectorvent.floci.core.common.RegionResolver;import io.github.hectorvent.floci.core.common.docker.DockerHostResolver;import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;import io.github.hectorvent.floci.core.storage.StorageFactory;+import io.github.hectorvent.floci.services.cloudwatch.logs.CloudWatchLogsService;import io.github.hectorvent.floci.services.dynamodb.backend.DynamoDbApiStreamReader;import io.github.hectorvent.floci.services.dynamodb.backend.DynamoDbOperations;import io.github.hectorvent.floci.services.dynamodb.backend.DynamoDbStreamReader.CheckpointLifetime;@@ -68,6 +69,72 @@ import static org.mockito.Mockito.*;class RedshiftServiceTest {+ @ParameterizedTest+ @ValueSource(booleans = {false, true})+ void loggingDestinationRegionSurvivesStorageAndRestart(boolean refresh) throws Exception {+ Cluster cluster = new Cluster();+ cluster.setClusterIdentifier("warehouse");+ cluster.setClusterStatus("available");+ when(clusterBackend.get("warehouse")).thenReturn(Optional.of(cluster));+ ObjectMapper mapper = new ObjectMapper();+ EmulatorConfig config = mock(EmulatorConfig.class);+ RedshiftService original = new RedshiftService(sf, cm, config,+ new RegionResolver("us-west-2", "111111111111"), proxyManager, dockerHostResolver,+ credentialBroker, secretsManagerService, mapper, null, null);+ original.enableLogging("warehouse", null, null, "cloudwatch", List.of("connectionlog"));++ Cluster restored = mapper.readValue(mapper.writeValueAsBytes(cluster), Cluster.class);+ when(clusterBackend.get("warehouse")).thenReturn(Optional.of(restored));+ when(clusterBackend.scanAllAccountEntries(any())).thenReturn(List.of(+ new AccountAwareStorageBackend.AccountEntry<>("111111111111", "warehouse", restored)));+ when(cm.getContainer("111111111111", "warehouse")).thenReturn(Optional.of(+ new RedshiftContainerHandle("container", "warehouse", "localhost", 5432)));+ CloudWatchLogsService logs = mock(CloudWatchLogsService.class);+ RedshiftAuditLogPublisher publisher = new RedshiftAuditLogPublisher(logs);+ RedshiftService restarted = new RedshiftService(sf, cm, config, regionResolver,+ proxyManager, dockerHostResolver, credentialBroker, secretsManagerService, mapper, null, publisher);++ restarted.onStart(null);+ if (refresh) {+ restarted.modifyClusterIamRoles("warehouse", List.of(), List.of());+ }+ publisher.publishRegisteredConnection("111111111111", "warehouse", "dev", "alice",+ "authenticated", "192.0.2.1", 54321);++ verify(logs).putLogEventsForAccount(eq("111111111111"),+ eq("/aws/redshift/cluster/warehouse/connectionlog"), eq("warehouse"), anyList(), eq("us-west-2"));+ verify(logs, never()).createLogGroupForAccount(anyString(), anyString(), any(), anyMap(), eq("us-east-1"));+ }++ @ParameterizedTest+ @ValueSource(booleans = {false, true})+ void activityParameterUpdatesStayWithinTheCallingAccount(boolean reset) {+ ClusterParameterGroup group = new ClusterParameterGroup("shared", "redshift-1.0", "audit");+ when(parameterGroupBackend.get("shared")).thenReturn(Optional.of(group));+ Cluster owned = new Cluster();+ owned.setClusterIdentifier("owned");+ owned.setClusterParameterGroupName("shared");+ owned.setUserActivityLoggingEnabled(reset);+ Cluster foreign = new Cluster();+ foreign.setClusterIdentifier("foreign");+ foreign.setClusterParameterGroupName("shared");+ foreign.setUserActivityLoggingEnabled(reset);+ when(clusterBackend.scanAllAccountEntries(any())).thenReturn(List.of(+ new AccountAwareStorageBackend.AccountEntry<>("111111111111", "owned", owned),+ new AccountAwareStorageBackend.AccountEntry<>("222222222222", "foreign", foreign)));+ when(clusterBackend.scan(any())).thenReturn(List.of(owned));++ if (reset) {+ service.resetClusterParameterGroup("shared", true, List.of());+ } else {+ service.modifyClusterParameterGroup("shared", List.of(new Parameter("enable_user_activity_logging", "true")));+ }++ assertEquals(!reset, owned.isUserActivityLoggingEnabled());+ assertEquals(reset, foreign.isUserActivityLoggingEnabled());+ verify(clusterBackend, never()).putForAccount(eq("222222222222"), anyString(), any());+ }+@Testvoid recoveryProxyFailureDoesNotDestructivelyStopAnAdoptedContainer() {Cluster cluster = new Cluster();@@ -1942,8 +2009,9 @@ class RedshiftServiceTest {assertEquals("0", parameters.get(0).getParameterValue());assertEquals("engine-default", parameters.get(0).getSource());assertEquals("integer", parameters.get(0).getDataType());- assertEquals("custom", parameters.get(1).getParameterValue());- assertEquals("user", parameters.get(1).getSource());+ assertEquals("false", parameters.get(1).getParameterValue());+ assertEquals("custom", parameters.get(2).getParameterValue());+ assertEquals("user", parameters.get(2).getSource());verify(parameterGroupBackend, times(2)).flush();}@@ -1958,9 +2026,10 @@ class RedshiftServiceTest {service.resetClusterParameterGroup("reset-pg", true, List.of());List<Parameter> parameters = service.describeClusterParameters("reset-pg");- assertEquals(2, parameters.size());+ assertEquals(3, parameters.size());assertEquals("0", parameters.get(0).getParameterValue());- assertEquals("{}", parameters.get(1).getParameterValue());+ assertEquals("false", parameters.get(1).getParameterValue());+ assertEquals("{}", parameters.get(2).getParameterValue());assertTrue(parameters.stream().allMatch(parameter -> "engine-default".equals(parameter.getSource())));assertEquals("analytics", group.getTags().get("owner"));assertEquals("description", group.getDescription());diff --git a/src/test/java/io/github/hectorvent/floci/services/redshift/proxy/RedshiftAuthProxyTest.java b/src/test/java/io/github/hectorvent/floci/services/redshift/proxy/RedshiftAuthProxyTest.javaindex 35537ff78c..8d7c669528 100644--- a/src/test/java/io/github/hectorvent/floci/services/redshift/proxy/RedshiftAuthProxyTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/redshift/proxy/RedshiftAuthProxyTest.java@@ -1,11 +1,13 @@package io.github.hectorvent.floci.services.redshift.proxy;import io.github.hectorvent.floci.config.FlociCertificateAuthority;+import io.github.hectorvent.floci.core.common.PostgresWire;import io.github.hectorvent.floci.services.acm.CertificateGenerator;import io.github.hectorvent.floci.services.iam.IamService;import io.github.hectorvent.floci.services.rds.proxy.PasswordValidator;import io.github.hectorvent.floci.services.rds.proxy.RdsProxyTlsCertificates;import io.github.hectorvent.floci.services.rds.proxy.RdsSigV4Validator;+import io.github.hectorvent.floci.services.redshift.RedshiftAuditLogPublisher;import io.github.hectorvent.floci.services.s3.S3Service;import io.github.hectorvent.floci.testutil.FreePorts;import org.junit.jupiter.api.AfterEach;@@ -22,6 +24,8 @@ import java.net.Socket;import java.net.SocketTimeoutException;import java.nio.charset.StandardCharsets;import java.nio.file.Path;+import java.util.List;+import java.util.Map;import java.util.concurrent.CountDownLatch;import java.util.concurrent.Semaphore;import java.util.concurrent.TimeUnit;@@ -30,10 +34,69 @@ import java.util.concurrent.atomic.AtomicReference;import static org.junit.jupiter.api.Assertions.assertEquals;import static org.junit.jupiter.api.Assertions.assertThrows;import static org.junit.jupiter.api.Assertions.assertTrue;-import static org.mockito.Mockito.mock;+import static org.mockito.ArgumentMatchers.*;+import static org.mockito.Mockito.*;class RedshiftAuthProxyTest {+ @Test+ void failedLoginAuditsTheRequestedUserAndDatabase() throws Exception {+ RedshiftAuditLogPublisher audit = mock(RedshiftAuditLogPublisher.class);+ int port = FreePorts.anyInterfacePort();+ proxy = new RedshiftAuthProxy("111111111111:c1", "localhost", 1, "admin", "password", "dev",+ mock(RdsSigV4Validator.class), realTls(), (user, password) -> PasswordValidator.AuthResult.REJECT,+ mock(S3Service.class), mock(IamService.class), "111111111111", List.of(), 5000, 5000, 10, null, audit);+ proxy.start(port);+ try (Socket client = new Socket("localhost", port)) {+ client.setSoTimeout(5000);+ client.getOutputStream().write(PostgresWire.startupMessage(Map.of("user", "analyst", "database", "analytics")));+ client.getOutputStream().flush();+ assertEquals(9, client.getInputStream().readNBytes(9).length);+ PostgresWire.write(client.getOutputStream(), 'p', "bad\0".getBytes(StandardCharsets.UTF_8));+ client.getOutputStream().flush();+ assertEquals('E', client.getInputStream().read());+ verify(audit, timeout(2000)).publishRegisteredConnection(eq("111111111111"), eq("c1"), eq("analytics"),+ eq("analyst"), eq("authentication failure"), anyString(), anyInt());+ }+ }++ @Test+ void successfulLoginAndActivityUseTheRequestedIdentity() throws Exception {+ fakeBackend = new ServerSocket(0);+ Thread.ofVirtual().start(() -> {+ try (Socket backend = fakeBackend.accept()) {+ PostgresWire.readStartup(backend.getInputStream(), 10000, "startup");+ PostgresWire.write(backend.getOutputStream(), 'R', new byte[4]);+ PostgresWire.write(backend.getOutputStream(), 'Z', new byte[]{'I'});+ backend.getOutputStream().flush();+ PostgresWire.read(backend.getInputStream(), 10000, "query");+ } catch (IOException expected) {+ // Closing the test sockets ends the fake server.+ }+ });+ RedshiftAuditLogPublisher audit = mock(RedshiftAuditLogPublisher.class);+ int port = FreePorts.anyInterfacePort();+ proxy = new RedshiftAuthProxy("111111111111:c1", "localhost", fakeBackend.getLocalPort(), "admin", "password", "dev",+ mock(RdsSigV4Validator.class), realTls(), (user, password) -> PasswordValidator.AuthResult.MASTER_EQUIVALENT,+ mock(S3Service.class), mock(IamService.class), "111111111111", List.of(), 5000, 5000, 10, null, audit);+ proxy.start(port);+ try (Socket client = new Socket("localhost", port)) {+ client.setSoTimeout(5000);+ client.getOutputStream().write(PostgresWire.startupMessage(Map.of("user", "analyst", "database", "analytics")));+ client.getOutputStream().flush();+ assertEquals(9, client.getInputStream().readNBytes(9).length);+ PostgresWire.write(client.getOutputStream(), 'p', "password\0".getBytes(StandardCharsets.UTF_8));+ client.getOutputStream().flush();+ assertEquals('R', PostgresWire.read(client.getInputStream(), 10000, "authenticated").type());+ assertEquals('Z', PostgresWire.read(client.getInputStream(), 10000, "ready").type());+ PostgresWire.write(client.getOutputStream(), 'Q', "SELECT 1\0".getBytes(StandardCharsets.UTF_8));+ client.getOutputStream().flush();+ verify(audit, timeout(2000)).publishRegisteredActivity("111111111111", "c1", "analytics", "analyst", "SELECT 1");+ verify(audit, timeout(2000)).publishRegisteredConnection(eq("111111111111"), eq("c1"), eq("analytics"),+ eq("analyst"), eq("authenticated"), anyString(), anyInt());+ }+ }+@TempDirPath tempDir;diff --git a/src/test/java/io/github/hectorvent/floci/services/redshift/proxy/RedshiftInterceptingBridgeTest.java b/src/test/java/io/github/hectorvent/floci/services/redshift/proxy/RedshiftInterceptingBridgeTest.javaindex e460830b19..e416c1a7b5 100644--- a/src/test/java/io/github/hectorvent/floci/services/redshift/proxy/RedshiftInterceptingBridgeTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/redshift/proxy/RedshiftInterceptingBridgeTest.java@@ -1,6 +1,7 @@package io.github.hectorvent.floci.services.redshift.proxy;import io.github.hectorvent.floci.services.iam.IamService;+import io.github.hectorvent.floci.services.redshift.RedshiftAuditLogPublisher;import io.github.hectorvent.floci.services.redshift.spectrum.SpectrumInterceptor;import io.github.hectorvent.floci.services.redshift.spectrum.SpectrumQueryPreparation;import io.github.hectorvent.floci.services.redshift.spectrum.SpectrumSession;@@ -29,6 +30,7 @@ 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.assertTrue;+import static org.mockito.Mockito.*;class RedshiftInterceptingBridgeTest {@@ -61,6 +63,10 @@ class RedshiftInterceptingBridgeTest {}private void startBridge(SpectrumInterceptor spectrumInterceptor) throws IOException {+ startBridge(spectrumInterceptor, null);+ }++ private void startBridge(SpectrumInterceptor spectrumInterceptor, RedshiftAuditLogPublisher audit) throws IOException {clientListener = new ServerSocket(0);testClientEnd = new Socket("localhost", clientListener.getLocalPort());bridgeClientEnd = clientListener.accept();@@ -72,10 +78,145 @@ class RedshiftInterceptingBridgeTest {s3Stub = Mockito.mock(S3Service.class);RedshiftInterceptingBridge bridge = new RedshiftInterceptingBridge(bridgeClientEnd, bridgeBackendEnd, s3Stub, Mockito.mock(IamService.class), "000000000000", List.of(),- spectrumInterceptor, "000000000000:c", "dev");+ spectrumInterceptor, "000000000000:c", "dev", null, audit, "analyst");bridgeThread = Thread.ofVirtual().name("bridge-under-test").start(bridge::run);}+ @Test+ @Timeout(10)+ void multiCommandQueryLogsEveryCommittedUserChange() throws Exception {+ RedshiftAuditLogPublisher audit = mock(RedshiftAuditLogPublisher.class);+ startBridge(null, audit);+ sendAuditQuery("SELECT ';'; /* migration */ CREATE USER alice; ALTER USER alice RENAME TO bob",+ "SELECT 1", "CREATE ROLE", "ALTER ROLE");+ verify(audit, timeout(2000)).publishRegisteredUserChange("000000000000", "c", "dev", "alice", "Create", null);+ verify(audit, timeout(2000)).publishRegisteredUserChange("000000000000", "c", "dev", "bob", "Rename", "alice");+ }++ @Test+ @Timeout(10)+ void emptyAndFailedQueriesDoNotMislabelLaterUserChanges() throws Exception {+ RedshiftAuditLogPublisher audit = mock(RedshiftAuditLogPublisher.class);+ startBridge(null, audit);+ testClientEnd.getOutputStream().write(PostgresWireDecoder.encodeQuery("; -- empty"));+ nextForwarded();+ testBackendEnd.getOutputStream().write(concat(frame('I', new byte[0]), frame('Z', new byte[]{'I'})));+ testBackendEnd.getOutputStream().flush();+ sendAuditQuery("SELECT broken; CREATE USER skipped", new String[0]);+ sendAuditQuery("CREATE USER actual", "CREATE ROLE");+ verify(audit, timeout(2000)).publishRegisteredUserChange("000000000000", "c", "dev", "actual", "Create", null);+ verify(audit, never()).publishRegisteredUserChange(anyString(), anyString(), anyString(), eq("skipped"), anyString(), any());+ }++ @Test+ @Timeout(10)+ void implicitTransactionFailureDiscardsEarlierSuccessfulUserChanges() throws Exception {+ RedshiftAuditLogPublisher audit = mock(RedshiftAuditLogPublisher.class);+ startBridge(null, audit);+ testClientEnd.getOutputStream().write(PostgresWireDecoder.encodeQuery("CREATE USER rolledback; SELECT broken"));+ nextForwarded();+ testBackendEnd.getOutputStream().write(concat(frame('C', cString("CREATE ROLE")),+ frame('E', cString("failed")), frame('Z', new byte[]{'I'})));+ testBackendEnd.getOutputStream().flush();+ sendAuditQuery("CREATE USER actual", "CREATE ROLE");+ verify(audit, timeout(2000)).publishRegisteredUserChange("000000000000", "c", "dev", "actual", "Create", null);+ verify(audit, never()).publishRegisteredUserChange(anyString(), anyString(), anyString(), eq("rolledback"), anyString(), any());+ }++ @Test+ @Timeout(10)+ void rollbackInsideAMultiCommandQueryDoesNotPublishDiscardedChanges() throws Exception {+ RedshiftAuditLogPublisher audit = mock(RedshiftAuditLogPublisher.class);+ startBridge(null, audit);+ sendAuditQuery("BEGIN; CREATE USER rolledback; ROLLBACK; CREATE USER actual",+ "BEGIN", "CREATE ROLE", "ROLLBACK", "CREATE ROLE");+ verify(audit, timeout(2000)).publishRegisteredUserChange("000000000000", "c", "dev", "actual", "Create", null);+ verify(audit, never()).publishRegisteredUserChange(anyString(), anyString(), anyString(), eq("rolledback"), anyString(), any());+ }++ @Test+ @Timeout(10)+ void rollbackToSavepointPreservesOnlyEarlierUserChanges() throws Exception {+ RedshiftAuditLogPublisher audit = mock(RedshiftAuditLogPublisher.class);+ startBridge(null, audit);+ sendAuditQuery("BEGIN; CREATE USER kept; SAVEPOINT s; CREATE USER rolledback; ROLLBACK TO s; COMMIT",+ "BEGIN", "CREATE ROLE", "SAVEPOINT", "CREATE ROLE", "ROLLBACK", "COMMIT");+ verify(audit, timeout(2000)).publishRegisteredUserChange("000000000000", "c", "dev", "kept", "Create", null);+ verify(audit, never()).publishRegisteredUserChange(anyString(), anyString(), anyString(), eq("rolledback"), anyString(), any());+ }++ @Test+ @Timeout(10)+ void suspendedPortalsDoNotConsumeLaterUserChangeCompletions() throws Exception {+ RedshiftAuditLogPublisher audit = mock(RedshiftAuditLogPublisher.class);+ startBridge(null, audit);+ ByteArrayOutputStream pipeline = new ByteArrayOutputStream();+ PostgresWireDecoder.ParseMessage parse = new PostgresWireDecoder.ParseMessage("select", "SELECT 1", List.of());+ pipeline.write(PostgresWireDecoder.encodeParse(parse, parse.sql()));+ pipeline.write(frame('B', concat(cString("portal"), cString("select"), new byte[6])));+ pipeline.write(frame('E', concat(cString("portal"), new byte[]{0, 0, 0, 1})));+ pipeline.write(frame('E', concat(cString("portal"), new byte[4])));+ pipeline.write(frame('S', new byte[0]));+ testClientEnd.getOutputStream().write(pipeline.toByteArray());+ testClientEnd.getOutputStream().flush();+ for (int i = 0; i < 5; i++) {+ nextForwarded();+ }+ testBackendEnd.getOutputStream().write(concat(frame('1', new byte[0]), frame('2', new byte[0]),+ frame('s', new byte[0]), frame('C', cString("SELECT 1")), frame('Z', new byte[]{'I'})));+ testBackendEnd.getOutputStream().flush();+ sendAuditQuery("CREATE USER actual", "CREATE ROLE");+ verify(audit, timeout(2000)).publishRegisteredUserChange("000000000000", "c", "dev", "actual", "Create", null);+ }++ @Test+ @Timeout(10)+ void rejectedNamedParseDoesNotReplaceTheSqlUsedForUserAudit() throws Exception {+ RedshiftAuditLogPublisher audit = mock(RedshiftAuditLogPublisher.class);+ startBridge(null, audit);+ PostgresWireDecoder.ParseMessage original = new PostgresWireDecoder.ParseMessage("s", "SELECT 1", List.of());+ testClientEnd.getOutputStream().write(concat(PostgresWireDecoder.encodeParse(original, original.sql()), frame('S', new byte[0])));+ nextForwarded();+ nextForwarded();+ testBackendEnd.getOutputStream().write(concat(frame('1', new byte[0]), frame('Z', new byte[]{'I'})));+ testBackendEnd.getOutputStream().flush();+ PostgresWireDecoder replies = new PostgresWireDecoder(testClientEnd.getInputStream());+ assertEquals('1', replies.nextMessage().type());+ assertEquals('Z', replies.nextMessage().type());+ PostgresWireDecoder.ParseMessage rejected = new PostgresWireDecoder.ParseMessage("s", "CREATE USER phantom", List.of());+ testClientEnd.getOutputStream().write(concat(PostgresWireDecoder.encodeParse(rejected, rejected.sql()), frame('S', new byte[0])));+ nextForwarded();+ nextForwarded();+ testBackendEnd.getOutputStream().write(concat(frame('E', cString("duplicate name")), frame('Z', new byte[]{'I'})));+ testBackendEnd.getOutputStream().flush();+ assertEquals('E', replies.nextMessage().type());+ assertEquals('Z', replies.nextMessage().type());+ testClientEnd.getOutputStream().write(concat(frame('B', concat(cString("p"), cString("s"), new byte[6])),+ frame('E', concat(cString("p"), new byte[4])), frame('S', new byte[0])));+ for (int i = 0; i < 3; i++) {+ nextForwarded();+ }+ testBackendEnd.getOutputStream().write(concat(frame('2', new byte[0]), frame('C', cString("SELECT 1")), frame('Z', new byte[]{'I'})));+ testBackendEnd.getOutputStream().flush();+ sendAuditQuery("CREATE USER actual", "CREATE ROLE");+ verify(audit, timeout(2000)).publishRegisteredUserChange("000000000000", "c", "dev", "actual", "Create", null);+ verify(audit, never()).publishRegisteredUserChange(anyString(), anyString(), anyString(), eq("phantom"), anyString(), any());+ }++ private void sendAuditQuery(String sql, String... tags) throws IOException {+ testClientEnd.getOutputStream().write(PostgresWireDecoder.encodeQuery(sql));+ testClientEnd.getOutputStream().flush();+ nextForwarded();+ for (String tag : tags) {+ testBackendEnd.getOutputStream().write(frame('C', cString(tag)));+ }+ if (tags.length == 0) {+ testBackendEnd.getOutputStream().write(frame('E', cString("failed")));+ }+ testBackendEnd.getOutputStream().write(frame('Z', new byte[]{'I'}));+ testBackendEnd.getOutputStream().flush();+ }+@AfterEachvoid tearDown() {for (Socket s : new Socket[]{testClientEnd, bridgeClientEnd, bridgeBackendEnd, testBackendEnd}) {diff --git a/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataApiIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataApiIntegrationTest.javaindex 1cab126483..279b5cbd4a 100644--- a/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataApiIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataApiIntegrationTest.java@@ -1,6 +1,7 @@package io.github.hectorvent.floci.services.redshiftdata;import io.github.hectorvent.floci.services.redshift.RedshiftService;+import io.github.hectorvent.floci.services.redshift.model.Parameter;import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;import io.quarkus.test.common.http.TestHTTPResource;import io.quarkus.test.junit.QuarkusTest;@@ -16,6 +17,8 @@ import org.junit.jupiter.api.TestInstance;import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;import software.amazon.awssdk.regions.Region;+import software.amazon.awssdk.services.cloudwatchlogs.CloudWatchLogsClient;+import software.amazon.awssdk.services.cloudwatchlogs.model.GetLogEventsRequest;import software.amazon.awssdk.services.redshiftdata.RedshiftDataClient;import software.amazon.awssdk.services.redshiftdata.model.ExecuteStatementResponse;import software.amazon.awssdk.services.redshiftdata.model.ResourceNotFoundException;@@ -23,11 +26,13 @@ import software.amazon.awssdk.services.redshiftdata.model.ResourceNotFoundExceptimport java.net.URI;import java.time.Duration;import java.util.List;+import java.util.UUID;import static org.hamcrest.Matchers.containsString;import static org.hamcrest.Matchers.hasItem;import static org.hamcrest.Matchers.is;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.assertThrows;import static org.junit.jupiter.api.Assertions.assertTrue;@@ -128,6 +133,65 @@ class RedshiftDataApiIntegrationTest {}}+ @Test+ void sdkExecuteStatementPublishesUserActivityToCloudWatchLogs() {+ String parameterGroup = "it-rs-audit-" + clusterId;+ redshift.createClusterParameterGroup(parameterGroup, "redshift-1.0", "Audit logging integration test");+ redshift.modifyClusterParameterGroup(parameterGroup,+ List.of(new Parameter("enable_user_activity_logging", "true")));+ redshift.modifyCluster(clusterId, null, null, null, parameterGroup, null);+ redshift.enableLogging(clusterId, null, null, "cloudwatch", List.of("useractivitylog"));+ String group = "/aws/redshift/cluster/" + clusterId + "/useractivitylog";++ try (RedshiftDataClient data = RedshiftDataClient.builder().endpointOverride(endpoint).region(Region.US_EAST_1)+ .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("test", "test"))).build();+ CloudWatchLogsClient logs = CloudWatchLogsClient.builder().endpointOverride(endpoint).region(Region.US_EAST_1)+ .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("test", "test"))).build()) {+ String statementId = data.executeStatement(request -> request.clusterIdentifier(clusterId)+ .dbUser("admin").database("dev").sql("SELECT 741")).id();+ awaitFinished(statementId);++ Awaitility.await().atMost(Duration.ofSeconds(5)).pollInterval(Duration.ofMillis(100)).until(() ->+ logs.getLogEvents(GetLogEventsRequest.builder().logGroupName(group).logStreamName(clusterId).build())+ .events().stream().anyMatch(event -> event.message().contains("LOG: SELECT 741")));+ } finally {+ redshift.disableLogging(clusterId);+ redshift.modifyCluster(clusterId, null, null, null,+ "default.redshift-1.0", null);+ redshift.deleteClusterParameterGroup(parameterGroup);+ }+ }++ @Test+ void sdkBatchSavepointRollbackOmitsTheRolledBackUserFromCloudWatchLogs() {+ String suffix = UUID.randomUUID().toString().replace("-", "");+ String kept = "audit_kept_" + suffix;+ String discarded = "audit_discarded_" + suffix;+ String group = "/aws/redshift/cluster/" + clusterId + "/userlog";+ redshift.enableLogging(clusterId, null, null, "cloudwatch", List.of("userlog"));+ try (RedshiftDataClient data = RedshiftDataClient.builder().endpointOverride(endpoint).region(Region.US_EAST_1)+ .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("test", "test"))).build();+ CloudWatchLogsClient logs = CloudWatchLogsClient.builder().endpointOverride(endpoint).region(Region.US_EAST_1)+ .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("test", "test"))).build()) {+ String batch = data.batchExecuteStatement(request -> request.clusterIdentifier(clusterId)+ .dbUser("admin").database("dev").sqls("CREATE USER " + kept + " PASSWORD 'Secret123'",+ "SAVEPOINT s; CREATE USER " + discarded + " PASSWORD 'Secret123'; ROLLBACK TO s")).id();+ awaitFinished(batch);+ String query = data.executeStatement(request -> request.clusterIdentifier(clusterId).dbUser("admin")+ .database("dev").sql("SELECT COUNT(*) FROM pg_user WHERE usename = '" + discarded + "'")).id();+ awaitFinished(query);+ assertEquals(0L, data.getStatementResult(request -> request.id(query)).records().getFirst().getFirst().longValue());+ List<String> messages = logs.getLogEvents(GetLogEventsRequest.builder().logGroupName(group)+ .logStreamName(clusterId).build()).events().stream().map(event -> event.message()).toList();+ assertTrue(messages.stream().anyMatch(message -> message.contains("," + kept + ",,Create,")));+ assertFalse(messages.stream().anyMatch(message -> message.contains("," + discarded + ",")));+ } finally {+ redshift.disableLogging(clusterId);+ executeAndWait("DROP USER IF EXISTS " + kept);+ executeAndWait("DROP USER IF EXISTS " + discarded);+ }+ }+@Testvoid vacuumPreservesAutoCommitExecution() {executeAndWait("VACUUM");diff --git a/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataAuditTest.java b/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataAuditTest.javanew file mode 100644index 0000000000..d8a4f6eba7--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataAuditTest.java@@ -0,0 +1,384 @@+package io.github.hectorvent.floci.services.redshiftdata;++import com.fasterxml.jackson.databind.ObjectMapper;+import com.fasterxml.jackson.databind.node.ArrayNode;+import com.fasterxml.jackson.databind.node.ObjectNode;+import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;+import io.github.hectorvent.floci.services.cloudwatch.logs.CloudWatchLogsService;+import io.github.hectorvent.floci.services.redshift.RedshiftAuditLogPublisher;+import io.github.hectorvent.floci.services.redshift.model.Cluster;+import org.awaitility.Awaitility;+import org.h2.Driver;+import org.junit.jupiter.api.Test;+import org.junit.jupiter.params.ParameterizedTest;+import org.junit.jupiter.params.provider.CsvSource;+import org.junit.jupiter.params.provider.ValueSource;++import java.sql.Connection;+import java.sql.DriverManager;+import java.sql.PreparedStatement;+import java.sql.SQLException;+import java.time.Clock;+import java.time.Duration;+import java.util.List;+import java.util.Map;+import java.util.UUID;+import java.util.concurrent.CopyOnWriteArrayList;+import java.util.concurrent.CountDownLatch;+import java.util.concurrent.TimeUnit;++import static org.junit.jupiter.api.Assertions.*;+import static org.mockito.ArgumentMatchers.*;+import static org.mockito.Mockito.*;++class RedshiftDataAuditTest {++ @ParameterizedTest+ @CsvSource({"COMMIT,false", "END,false", "COMMIT,true", "END,true",+ "/* outer /* inner */ comment */ COMMIT WORK,true"})+ void explicitBatchCommitRejectsCancellationDuringAndAfterCommit(String command, boolean sameEntry) throws Exception {+ try (Fixture fixture = new Fixture()) {+ Connection connection = fixture.mockSuccessfulBackend();+ PreparedStatement create = mock(PreparedStatement.class);+ PreparedStatement commit = mock(PreparedStatement.class);+ PreparedStatement tail = mock(PreparedStatement.class);+ CountDownLatch committing = new CountDownLatch(1);+ CountDownLatch releaseCommit = new CountDownLatch(1);+ CountDownLatch runningTail = new CountDownLatch(1);+ CountDownLatch releaseTail = new CountDownLatch(1);+ when(connection.prepareStatement(anyString())).thenAnswer(invocation -> {+ String sql = invocation.getArgument(0);+ return sql.contains("CREATE USER") ? create : sql.contains("SELECT") ? tail : commit;+ });+ when(commit.execute()).thenAnswer(invocation -> {+ committing.countDown();+ assertTrue(releaseCommit.await(5, TimeUnit.SECONDS));+ return false;+ });+ when(tail.execute()).thenAnswer(invocation -> {+ runningTail.countDown();+ assertTrue(releaseTail.await(5, TimeUnit.SECONDS));+ return false;+ });+ ObjectNode request = fixture.request("unused");+ request.remove("WaitTimeSeconds");+ ArrayNode sqls = request.putArray("Sqls").add("CREATE USER alice");+ if (sameEntry) {+ sqls.add(command + "; SELECT 1");+ } else {+ sqls.add(command).add("SELECT 1");+ }+ String id = fixture.service.batchExecuteStatement(request, "us-east-1").path("Id").asText();+ ObjectNode identifier = fixture.mapper.createObjectNode().put("Id", id);+ try {+ assertTrue(committing.await(5, TimeUnit.SECONDS));+ assertFalse(fixture.service.cancelStatement(identifier).path("Status").asBoolean(),+ "cancellation must be rejected while an explicit commit is running");+ releaseCommit.countDown();+ assertTrue(runningTail.await(5, TimeUnit.SECONDS));+ assertFalse(fixture.service.cancelStatement(identifier).path("Status").asBoolean(),+ "cancellation must be rejected after an explicit commit succeeds");+ } finally {+ releaseCommit.countDown();+ releaseTail.countDown();+ }+ Awaitility.await().atMost(Duration.ofSeconds(5)).untilAsserted(() ->+ assertEquals("FINISHED", fixture.service.describeStatement(identifier).path("Status").asText()));+ assertEquals(List.of("alice"), fixture.loggedUsers());+ }+ }++ @Test+ void commentedUserDdlProducesUserAndActivityRecords() throws Exception {+ try (Fixture fixture = new Fixture()) {+ fixture.execute("/* migration */ CREATE /* change */ USER alice PASSWORD 'secret'");+ assertTrue(fixture.records("userlog").stream().anyMatch(line -> line.contains(",alice,,Create,")),+ fixture.events.toString());+ assertTrue(fixture.records("useractivitylog").stream().allMatch(line -> !line.contains("secret")));+ }+ }++ @Test+ void batchPublishesAttemptedSqlAndUserChangesOnlyAfterCommit() throws Exception {+ try (Fixture fixture = new Fixture()) {+ fixture.batch("CREATE USER alice PASSWORD 'secret'", "SELECT 1");+ assertEquals(2, fixture.records("useractivitylog").size());+ assertEquals(1, fixture.records("userlog").size());+ fixture.events.clear();++ fixture.batch("CREATE USER bob PASSWORD 'secret'", "SELECT * FROM missing", "SELECT 3");+ assertEquals(2, fixture.records("useractivitylog").size());+ assertTrue(fixture.records("userlog").isEmpty(), fixture.events.toString());+ }+ }++ @Test+ void batchSavepointRollbackDoesNotLogDiscardedUsers() throws Exception {+ try (Fixture fixture = new Fixture()) {+ fixture.mockSuccessfulBackend();+ fixture.assertFinished(fixture.batch("SAVEPOINT s", "CREATE USER bob PASSWORD 'secret'", "ROLLBACK TO s"));+ assertTrue(fixture.records("userlog").isEmpty());+ assertEquals(3, fixture.records("useractivitylog").size());+ }+ }++ @ParameterizedTest+ @ValueSource(strings = {+ "ROLLBACK TO s", "ROLLBACK WORK TO SAVEPOINT s", "-- comment\r rOlLbAcK To s"+ })+ void batchSavepointRollbackPreservesEarlierChanges(String rollback) throws Exception {+ try (Fixture fixture = new Fixture()) {+ fixture.mockSuccessfulBackend();+ fixture.assertFinished(fixture.batch("CREATE USER kept", "SAVEPOINT s", "CREATE USER discarded", rollback));+ assertEquals(List.of("kept"), fixture.loggedUsers());+ }+ }++ @Test+ void batchTracksNestedRepeatedAndReleasedSavepointsInsideOneEntry() throws Exception {+ try (Fixture fixture = new Fixture()) {+ Connection connection = fixture.mockSuccessfulBackend();+ String sql = "; CREATE USER kept; SAVEPOINT s; CREATE USER discarded; SAVEPOINT s; CREATE USER nested; "+ + "ROLLBACK TO s; RELEASE SAVEPOINT s; ROLLBACK TO s; "+ + "CREATE USER repeated; ROLLBACK TO s; CREATE USER after_rollback";+ ObjectNode response = fixture.batch(sql);+ fixture.assertFinished(response);+ assertEquals(List.of("kept", "after_rollback"), fixture.loggedUsers());+ verify(connection).prepareStatement(" CREATE USER kept");+ assertEquals(sql, fixture.service.describeStatement(fixture.mapper.createObjectNode()+ .put("Id", response.path("Id").asText())).path("SubStatements").get(0).path("QueryString").asText());+ }+ }++ @Test+ void releasingSavepointRetainsChangesForTheFinalCommit() throws Exception {+ try (Fixture fixture = new Fixture()) {+ fixture.mockSuccessfulBackend();+ fixture.assertFinished(fixture.batch("SAVEPOINT s", "CREATE USER kept", "RELEASE s"));+ assertEquals(List.of("kept"), fixture.loggedUsers());+ }+ }++ @ParameterizedTest+ @ValueSource(strings = {"ROLLBACK", "ABORT"})+ void batchRollbackDiscardsOnlyChangesSinceTheLastCommit(String rollback) throws Exception {+ try (Fixture fixture = new Fixture()) {+ fixture.mockSuccessfulBackend();+ fixture.assertFinished(fixture.batch("CREATE USER committed", "COMMIT", "CREATE USER discarded",+ rollback, "CREATE USER final_commit"));+ assertEquals(List.of("committed", "final_commit"), fixture.loggedUsers());+ }+ }++ @Test+ void failedBatchStillLogsChangesCommittedByAnEarlierStatement() throws Exception {+ try (Fixture fixture = new Fixture()) {+ Connection connection = fixture.mockSuccessfulBackend();+ when(connection.prepareStatement("SELECT broken")).thenThrow(new SQLException("broken", "42P01"));+ ObjectNode response = fixture.batch("CREATE USER committed", "END", "CREATE USER discarded", "SELECT broken");+ assertEquals("FAILED", fixture.service.describeStatement(fixture.mapper.createObjectNode()+ .put("Id", response.path("Id").asText())).path("Status").asText());+ assertEquals(List.of("committed"), fixture.loggedUsers());+ }+ }++ @Test+ void commitBeforeAnErrorInsideTheSameEntryRetainsCommittedAuditRecords() throws Exception {+ try (Fixture fixture = new Fixture()) {+ Connection connection = fixture.mockSuccessfulBackend();+ when(connection.prepareStatement(" SELECT broken")).thenThrow(new SQLException("broken", "42P01"));+ ObjectNode response = fixture.batch("CREATE USER committed", "COMMIT; SELECT broken");+ assertEquals("FAILED", fixture.service.describeStatement(fixture.mapper.createObjectNode()+ .put("Id", response.path("Id").asText())).path("Status").asText());+ assertEquals(List.of("committed"), fixture.loggedUsers());+ }+ }++ @Test+ void batchSavepointRollbackRetainsPendingChangesFromTheSession() throws Exception {+ try (Fixture fixture = new Fixture()) {+ fixture.mockSuccessfulBackend();+ String session = fixture.service.executeStatement(fixture.request("BEGIN")+ .put("SessionKeepAliveSeconds", 60), "us-east-1").path("SessionId").asText();+ fixture.reuse(session, "CREATE USER kept");+ ObjectNode request = fixture.mapper.createObjectNode().put("SessionId", session).put("WaitTimeSeconds", 5);+ request.putArray("Sqls").add("SAVEPOINT s").add("CREATE USER discarded").add("ROLLBACK TO s");+ fixture.assertFinished(fixture.service.batchExecuteStatement(request, "us-east-1"));+ assertEquals(List.of("kept"), fixture.loggedUsers());+ }+ }++ @Test+ void batchRollbackCanUseSavepointCreatedByAnEarlierSessionStatement() throws Exception {+ try (Fixture fixture = new Fixture()) {+ fixture.mockSuccessfulBackend();+ String session = fixture.service.executeStatement(fixture.request("BEGIN")+ .put("SessionKeepAliveSeconds", 60), "us-east-1").path("SessionId").asText();+ fixture.reuse(session, "CREATE USER kept");+ fixture.reuse(session, "SAVEPOINT s");+ fixture.reuse(session, "CREATE USER discarded");+ ObjectNode request = fixture.mapper.createObjectNode().put("SessionId", session).put("WaitTimeSeconds", 5);+ request.putArray("Sqls").add("ROLLBACK TO s");+ fixture.assertFinished(fixture.service.batchExecuteStatement(request, "us-east-1"));+ assertEquals(List.of("kept"), fixture.loggedUsers());+ }+ }++ @Test+ void transactionKeywordsInsideCommentsAndLiteralsDoNotRejectBatch() throws Exception {+ try (Fixture fixture = new Fixture()) {+ ObjectNode response = fixture.batch("/* ROLLBACK */ SELECT 'SAVEPOINT s; COMMIT'", "SELECT 2");+ ObjectNode description = fixture.service.describeStatement(fixture.mapper.createObjectNode()+ .put("Id", response.path("Id").asText()));+ assertEquals("FINISHED", description.path("Status").asText(), description.toString());+ assertEquals(2, fixture.records("useractivitylog").size());+ }+ }++ @Test+ void retainedSessionPublishesUserChangesOnCommitAndDiscardsThemOnRollback() throws Exception {+ try (Fixture fixture = new Fixture()) {+ String session = fixture.service.executeStatement(fixture.request("BEGIN")+ .put("SessionKeepAliveSeconds", 60), "us-east-1").path("SessionId").asText();+ fixture.reuse(session, "CREATE USER alice PASSWORD 'secret'");+ assertTrue(fixture.records("userlog").isEmpty());+ fixture.reuse(session, "COMMIT");+ assertEquals(1, fixture.records("userlog").size(), fixture.events.toString());+ fixture.events.clear();+ fixture.reuse(session, "BEGIN");+ fixture.reuse(session, "CREATE USER bob PASSWORD 'secret'");+ fixture.reuse(session, "ROLLBACK");+ assertTrue(fixture.records("userlog").isEmpty());+ fixture.reuse(session, "BEGIN");+ fixture.reuse(session, "CREATE USER carol PASSWORD 'secret'");+ fixture.reuse(session, "SELECT * FROM missing");+ fixture.reuse(session, "COMMIT");+ assertTrue(fixture.records("userlog").isEmpty());+ }+ }++ @Test+ void retainedConnectionLogsAuthenticationOnceAndDisconnectionWhenRetired() throws Exception {+ try (Fixture fixture = new Fixture()) {+ String session = fixture.service.executeStatement(fixture.request("SELECT 1")+ .put("SessionKeepAliveSeconds", 60), "us-east-1").path("SessionId").asText();+ fixture.reuse(session, "SELECT 2");+ assertEquals(1, fixture.records("connectionlog").size(), fixture.events.toString());+ assertTrue(fixture.records("connectionlog").getFirst().startsWith("authenticated,"));+ fixture.service.clear();+ assertEquals(2, fixture.records("connectionlog").size());+ assertTrue(fixture.records("connectionlog").getLast().startsWith("disconnection,"));+ }+ }++ @Test+ void failedBackendAuthenticationProducesAConnectionRecord() throws Exception {+ try (Fixture fixture = new Fixture()) {+ when(fixture.factory.open(any())).thenThrow(new SQLException("bad password", "28P01"));+ fixture.execute("SELECT 1");+ assertEquals(1, fixture.records("connectionlog").size());+ assertTrue(fixture.records("connectionlog").getFirst().startsWith("authentication failure,"));+ }+ }++ @Test+ void activityAndConnectionRecordsUseRequestedIdentityInsteadOfBackendCredentials() throws Exception {+ try (Fixture fixture = new Fixture()) {+ fixture.execute("SELECT 1");+ assertTrue(fixture.records("useractivitylog").getFirst().contains(",analytics,analyst,"));+ assertTrue(fixture.records("connectionlog").stream().allMatch(line -> line.contains(",analytics,analyst,")));+ }+ }++ private record Event(String group, String message) { }++ private static final class Fixture implements AutoCloseable {+ private final ObjectMapper mapper = new ObjectMapper();+ private final List<Event> events = new CopyOnWriteArrayList<>();+ private final RedshiftDataConnectionFactory factory = mock(RedshiftDataConnectionFactory.class);+ private final RedshiftDataExecutor executor = new RedshiftDataExecutor(2, 8, 1);+ private final RedshiftDataService service;+ private final Connection keeper;++ private Fixture() throws Exception {+ Driver.load();+ String url = "jdbc:h2:mem:" + UUID.randomUUID();+ keeper = DriverManager.getConnection(url);+ when(factory.open(any())).thenAnswer(invocation -> DriverManager.getConnection(url));+ Cluster cluster = new Cluster();+ cluster.setClusterIdentifier("warehouse");+ cluster.setLoggingEnabled(true);+ cluster.setLoggingDestinationType("cloudwatch");+ cluster.setUserActivityLoggingEnabled(true);+ RedshiftDataResourceResolver resolver = mock(RedshiftDataResourceResolver.class);+ when(resolver.resolve(any(), any())).thenReturn(new RedshiftDataResourceResolver.DatabaseTarget(+ "arn:aws:redshift:us-east-1:000000000000:cluster:warehouse", "localhost", 5439,+ "analytics", "admin", "password", null, cluster, "analyst"));+ CloudWatchLogsService logs = mock(CloudWatchLogsService.class);+ doAnswer(invocation -> {+ List<Map<String, Object>> records = invocation.getArgument(3);+ for (Map<String, Object> record : records) {+ events.add(new Event(invocation.getArgument(1), record.get("message").toString()));+ }+ return null;+ }).when(logs).putLogEventsForAccount(anyString(), anyString(), anyString(), anyList(), anyString());+ service = new RedshiftDataService(resolver, factory, new RedshiftDataStatementStore(24, Clock.systemUTC()),+ mapper, null, executor, null, null, null,+ new RedshiftDataIdempotencyStore(AccountAwareStorageBackend.inMemory("000000000000"), Clock.systemUTC()),+ new RedshiftDataSessions(), new RedshiftAuditLogPublisher(logs));+ }++ private List<String> records(String type) {+ return events.stream().filter(event -> event.group().endsWith("/" + type)).map(Event::message).toList();+ }++ private List<String> loggedUsers() {+ return records("userlog").stream().map(line -> line.split(",", -1)[1]).toList();+ }++ private Connection mockSuccessfulBackend() throws SQLException {+ // H2 user DDL implicitly commits; model successful PostgreSQL responses for transaction audit tests.+ Connection connection = mock(Connection.class);+ PreparedStatement statement = mock(PreparedStatement.class);+ when(connection.prepareStatement(anyString())).thenReturn(statement);+ when(factory.open(any())).thenReturn(connection);+ return connection;+ }++ private void assertFinished(ObjectNode response) {+ ObjectNode described = service.describeStatement(mapper.createObjectNode().put("Id", response.path("Id").asText()));+ assertEquals("FINISHED", described.path("Status").asText(), described.toString());+ }++ private ObjectNode request(String sql) {+ return mapper.createObjectNode().put("Sql", sql).put("WaitTimeSeconds", 5)+ .put("ClusterIdentifier", "warehouse").put("Database", "analytics").put("DbUser", "analyst");+ }++ private void execute(String sql) {+ service.executeStatement(request(sql), "us-east-1");+ }++ private void reuse(String session, String sql) {+ service.executeStatement(mapper.createObjectNode().put("Sql", sql).put("SessionId", session)+ .put("WaitTimeSeconds", 5), "us-east-1");+ }++ private ObjectNode batch(String... sqls) {+ ObjectNode request = request("unused");+ request.remove("Sql");+ ArrayNode statements = request.putArray("Sqls");+ for (String sql : sqls) {+ statements.add(sql);+ }+ return service.batchExecuteStatement(request, "us-east-1");+ }++ @Override+ public void close() throws SQLException {+ service.clear();+ executor.close();+ keeper.close();+ }+ }+}diff --git a/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataResourceResolverTest.java b/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataResourceResolverTest.javaindex 1f45f0424d..0095332862 100644--- a/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataResourceResolverTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataResourceResolverTest.java@@ -82,6 +82,17 @@ class RedshiftDataResourceResolverTest {assertEquals("arn:aws:redshift:us-east-1:" + ACCOUNT + ":cluster:wh", target.arn());}+ @Test+ void dbUserTargetUsesTheRequestRegionForAuditDelivery() {+ RedshiftService redshift = mock(RedshiftService.class);+ when(redshift.describeClusters("wh")).thenReturn(List.of(cluster()));+ ObjectNode request = mapper.createObjectNode().put("ClusterIdentifier", "wh")+ .put("Database", "dev").put("DbUser", "admin");+ RedshiftDataResourceResolver.DatabaseTarget target =+ resolver(redshift, mock(SecretsManagerService.class)).resolve(request, "us-west-2");+ assertEquals("arn:aws:redshift:us-west-2:acc:cluster:wh", target.arn());+ }+private static RedshiftServerlessService.WorkgroupTarget workgroupTarget() {return new RedshiftServerlessService.WorkgroupTarget("arn:aws:redshift-serverless:us-east-1:" + ACCOUNT + ":workgroup/wg-id", "wg-1",@@ -211,6 +222,7 @@ class RedshiftDataResourceResolverTest {resolver(redshift, mock(SecretsManagerService.class)).resolve(req, REGION);assertEquals("admin", target.user());+ assertEquals("analyst", target.auditUser());assertEquals("Secret123", target.password());}diff --git a/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataServiceTest.javaindex d3ac1ba166..1124976cae 100644--- a/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataServiceTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataServiceTest.java@@ -310,6 +310,24 @@ class RedshiftDataServiceTest {assertEquals(parameters, service.listStatements(om.createObjectNode()).path("Statements").get(0).path("QueryParameters"));}+ @Test+ void batchParametersBindAcrossCommandsAndSavepointsInOneEntry() {+ service.executeStatement(req("CREATE TABLE batch_savepoint (id INT)"), REGION);+ ObjectNode request = batchReq("INSERT INTO batch_savepoint VALUES (:id); SAVEPOINT s; "+ + "INSERT INTO batch_savepoint VALUES (:discarded); ROLLBACK TO SAVEPOINT s; COMMIT",+ "SELECT id FROM batch_savepoint WHERE id = :id");+ request.putArray("Parameters").addObject().put("name", "id").put("value", "42");+ request.withArray("Parameters").addObject().put("name", "discarded").put("value", "99");++ String id = service.batchExecuteStatement(request, REGION).path("Id").asText();++ ObjectNode description = service.describeStatement(idOf(id));+ assertEquals("FINISHED", description.path("Status").asText(), description.toString());+ assertEquals(42, service.getStatementResult(idOf(id)).path("Records").get(0).get(0).path("longValue").asInt());+ String count = service.executeStatement(req("SELECT COUNT(*) FROM batch_savepoint"), REGION).path("Id").asText();+ assertEquals(1, service.getStatementResult(idOf(count)).path("Records").get(0).get(0).path("longValue").asInt());+ }+@Testvoid batchParametersRollBackEarlierWritesWhenALaterStatementFails() {service.executeStatement(req("CREATE TABLE batch_rollback(id integer)"), REGION);diff --git a/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataSessionIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataSessionIntegrationTest.javaindex b8482af0f9..7fa9151040 100644--- a/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataSessionIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataSessionIntegrationTest.java@@ -171,6 +171,27 @@ class RedshiftDataSessionIntegrationTest {assertThrows(ResourceNotFoundException.class, () -> data.executeStatement(r -> r.waitTimeSeconds(30).sessionId(created.sessionId()).sql("SELECT 3")));}+ @Test+ void sdkBatchAcceptsSavepointsAndMultipleStatementsInAnEntry() {+ ExecuteStatementResponse created = data.executeStatement(r -> r.waitTimeSeconds(30).clusterIdentifier("warehouse")+ .dbUser("admin").database("dev").sessionKeepAliveSeconds(60)+ .sql("CREATE LOCAL TEMPORARY TABLE batch_changes(id integer)"));+ finished(created.id());+ String batch = data.batchExecuteStatement(r -> r.waitTimeSeconds(30).sessionId(created.sessionId())+ .sqls("SAVEPOINT s", "INSERT INTO batch_changes VALUES (1); ROLLBACK TO SAVEPOINT s",+ "INSERT INTO batch_changes VALUES (2)")).id();+ finished(batch);+ String query = data.executeStatement(r -> r.waitTimeSeconds(30).sessionId(created.sessionId())+ .sql("SELECT id FROM batch_changes")).id();+ finished(query);+ assertEquals(1L, data.getStatementResult(r -> r.id(query)).totalNumRows());+ assertEquals(2L, data.getStatementResult(r -> r.id(query)).records().getFirst().getFirst().longValue());+ String multiResult = data.batchExecuteStatement(r -> r.waitTimeSeconds(30).sessionId(created.sessionId())+ .sqls("SELECT 11; SELECT 22")).id();+ finished(multiResult);+ assertEquals(11L, data.getStatementResult(r -> r.id(multiResult)).records().getFirst().getFirst().longValue());+ }+private RedshiftDataClient client(String key, Region region) {return RedshiftDataClient.builder().endpointOverride(endpoint).region(region).credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(key, "test"))).build();