diff --git a/db-migration/src/main/java/com/idea2strategy/backend/migration/DatabaseAccessPolicy.java b/db-migration/src/main/java/com/idea2strategy/backend/migration/DatabaseAccessPolicy.java index 38d0cb7e..73e49ca8 100644 --- a/db-migration/src/main/java/com/idea2strategy/backend/migration/DatabaseAccessPolicy.java +++ b/db-migration/src/main/java/com/idea2strategy/backend/migration/DatabaseAccessPolicy.java @@ -106,6 +106,47 @@ public final class DatabaseAccessPolicy { private static final Set BACKTEST_BOT_READ_TABLES = Set.of("launch_contract_plans", "bots"); + /** + * The only {@code operations} tables the trading worker touches, with exactly the privileges its + * SQL needs. Everything else in the schema — operator credentials and sessions, the RBAC catalog, + * cases, notifications, batch bookkeeping — stays out of reach of the trading role. + * + *

Derived from every statement in {@code trading-engine} that names the schema, all of them in + * {@code apps/trading-worker}: + *

+ */ + private static final Map> TRADING_OPERATIONS_ACCESS = Map.of( + "outbox_messages", Set.of(Access.READ, Access.INSERT), + "outbox_consumer_receipts", Set.of(Access.READ, Access.INSERT, Access.UPDATE), + "audit_events", Set.of(Access.INSERT)); + + /** + * Column-level reads for a role that must not read the whole table. + * + *

PostgreSQL requires SELECT on every column of an {@code on conflict (...)} arbiter, even for + * {@code do nothing}. Without it the trading worker's audit append fails with + * {@code permission denied for table audit_events}; a table-wide SELECT would hand it every + * operator and account audit row instead. A table-level REVOKE also revokes column privileges, so + * the generated revocations still reset these on every run. + */ + private static final List COLUMN_READS = List.of( + new ColumnRead( + ApplicationRole.TRADING, + new QualifiedTable("operations", "audit_events"), + List.of("idempotency_key"))); + /** Tables {@code backend-batch} appends to outside the schemas it owns. */ private static final Set BATCH_INSERTED_TABLES = Set.of( new QualifiedTable("competition", "room_events"), @@ -244,6 +285,17 @@ public static String runtimeGrantSql(List migrationSql) { .append(quoted(table.table())).append(" TO ").append(roleName).append(";\n"); } } + for (var columnRead : columnReads(role)) { + if (!manifest.tables().contains(columnRead.table())) { + continue; + } + usedSchemas.add(columnRead.table().schema()); + sql.append("GRANT SELECT (") + .append(columnRead.columns().stream().map(DatabaseAccessPolicy::quoted) + .collect(java.util.stream.Collectors.joining(", "))) + .append(") ON TABLE ").append(quoted(columnRead.table().schema())).append(".") + .append(quoted(columnRead.table().table())).append(" TO ").append(roleName).append(";\n"); + } for (var schema : usedSchemas) { sql.append("GRANT USAGE ON SCHEMA ").append(quoted(schema)) .append(" TO ").append(roleName).append(";\n"); @@ -285,6 +337,11 @@ public static String databaseRole(ApplicationRole role) { return ROLE_PREFIX + role.name().toLowerCase(java.util.Locale.ROOT); } + /** The column-level reads granted to a role on top of its table privileges. */ + public static List columnReads(ApplicationRole role) { + return COLUMN_READS.stream().filter(columnRead -> columnRead.role() == role).toList(); + } + private static String sqlPrivilege(Access access) { return switch (access) { case READ -> "SELECT"; @@ -458,12 +515,14 @@ private static boolean allowsPipeline(Access access, String schema, String table } private static boolean allowsTrading(Access access, String schema, String table) { - if (access == Access.READ && Set.of("strategy", "market_data", "bot", "trading", "operations").contains(schema)) { + if ("operations".equals(schema)) { + return TRADING_OPERATIONS_ACCESS.getOrDefault(table, Set.of()).contains(access); + } + if (access == Access.READ && Set.of("strategy", "market_data", "bot", "trading").contains(schema)) { return true; } return "trading".equals(schema) - || ("bot".equals(schema) && TRADING_BOT_TABLES.contains(table)) - || "operations".equals(schema); + || ("bot".equals(schema) && TRADING_BOT_TABLES.contains(table)); } private static boolean ownsBackendTable(String schema, String table) { @@ -491,6 +550,16 @@ public enum Access { public record QualifiedTable(String schema, String table) {} + /** SELECT on {@code columns} of {@code table} for {@code role}, without a table-wide SELECT. */ + public record ColumnRead(ApplicationRole role, QualifiedTable table, List columns) { + public ColumnRead { + columns = List.copyOf(columns); + if (columns.isEmpty()) { + throw new IllegalArgumentException("A column read must name at least one column"); + } + } + } + public static final class OwnershipManifest { private final Map ownership; diff --git a/db-migration/src/test/java/com/idea2strategy/backend/migration/CentralFlywayIntegrationTest.java b/db-migration/src/test/java/com/idea2strategy/backend/migration/CentralFlywayIntegrationTest.java index 3be189c9..5f29e6f1 100644 --- a/db-migration/src/test/java/com/idea2strategy/backend/migration/CentralFlywayIntegrationTest.java +++ b/db-migration/src/test/java/com/idea2strategy/backend/migration/CentralFlywayIntegrationTest.java @@ -123,6 +123,58 @@ void migratesOnceAndHasNoPendingWorkOnTheSecondRun() throws Exception { statement.execute("SET ROLE idea2strategy_pipeline"); statement.execute("RESET ROLE"); + // The statement shapes the trading worker executes against operations, under its own role. + // WHERE false keeps the rows out; PostgreSQL checks every privilege before execution anyway, + // including SELECT on an ON CONFLICT arbiter and UPDATE for FOR UPDATE. + statement.execute("SET ROLE idea2strategy_trading"); + for (var allowed : java.util.List.of( + "INSERT INTO operations.outbox_consumer_receipts (consumer_handler_id, outbox_message_id, " + + "producer_idempotency_key, payload_hash, status, first_received_at, last_received_at) " + + "SELECT 'grant-check', message.id, message.producer_idempotency_key, message.payload_hash, " + + "'PROCESSING', now(), now() FROM operations.outbox_messages message " + + "LEFT JOIN operations.outbox_consumer_receipts existing " + + "ON existing.outbox_message_id = message.id WHERE false " + + "ON CONFLICT (consumer_handler_id, outbox_message_id) DO UPDATE SET " + + "receive_attempt_count = operations.outbox_consumer_receipts.receive_attempt_count + 1 " + + "RETURNING outbox_message_id, claim_token, receive_attempt_count", + "SELECT claim_token FROM operations.outbox_consumer_receipts WHERE false FOR UPDATE", + "UPDATE operations.outbox_consumer_receipts SET failure_code = NULL WHERE false", + "INSERT INTO operations.outbox_messages (id, owner_domain, aggregate_id, aggregate_sequence, " + + "event_type, event_schema_version, payload_document, idempotency_key, created_at) " + + "SELECT gen_random_uuid(), 'trading', gen_random_uuid(), 1, 'GRANT_CHECK', 'v1', " + + "'{}'::jsonb, 'grant-check', now() WHERE false " + + "ON CONFLICT (idempotency_key) DO NOTHING", + "INSERT INTO operations.audit_events (id, actor_type, actor_id, action_type, target_domain, " + + "target_id, reason_code, correlation_id, idempotency_key, occurred_at) " + + "SELECT gen_random_uuid(), 'SYSTEM', gen_random_uuid(), 'GRANT_CHECK', 'trading', " + + "gen_random_uuid(), 'GRANT_CHECK', gen_random_uuid(), 'grant-check', now() WHERE false " + + "ON CONFLICT (idempotency_key) DO NOTHING")) { + statement.execute(allowed); + } + for (var denied : java.util.List.of( + "SELECT * FROM operations.operator_login_credentials LIMIT 0", + "SELECT * FROM operations.operator_sessions LIMIT 0", + "SELECT * FROM operations.operator_accounts LIMIT 0", + "SELECT * FROM operations.roles LIMIT 0", + "SELECT actor_id, response_document FROM operations.audit_events LIMIT 0", + "DELETE FROM operations.audit_events WHERE false", + "DELETE FROM operations.outbox_consumer_receipts WHERE false", + "UPDATE operations.outbox_messages SET published_at = published_at WHERE false", + "INSERT INTO operations.cases (id) SELECT gen_random_uuid() WHERE false")) { + var failure = assertThrows(SQLException.class, () -> statement.execute(denied), denied); + assertEquals("42501", failure.getSQLState(), denied); + statement.execute("ROLLBACK"); + statement.execute("SET ROLE idea2strategy_trading"); + } + try (var columns = statement.executeQuery( + "SELECT has_column_privilege('operations.audit_events', 'idempotency_key', 'SELECT'), " + + "has_column_privilege('operations.audit_events', 'actor_id', 'SELECT')")) { + assertTrue(columns.next()); + assertTrue(columns.getBoolean(1)); + assertFalse(columns.getBoolean(2)); + } + statement.execute("RESET ROLE"); + statement.execute("SET ROLE idea2strategy_backend"); try (var pinPrivileges = statement.executeQuery( "SELECT has_table_privilege('idea2strategy_backend', 'backtest.run_input_pins', 'INSERT'), " diff --git a/db-migration/src/test/java/com/idea2strategy/backend/migration/DatabaseAccessPolicyTest.java b/db-migration/src/test/java/com/idea2strategy/backend/migration/DatabaseAccessPolicyTest.java index 12a83ac8..b85ed13b 100644 --- a/db-migration/src/test/java/com/idea2strategy/backend/migration/DatabaseAccessPolicyTest.java +++ b/db-migration/src/test/java/com/idea2strategy/backend/migration/DatabaseAccessPolicyTest.java @@ -286,8 +286,8 @@ void grantsTheBacktestRoleTheOperationsAccessItsRequestIntakeExecutes() { "operations", "outbox_messages")); - // The intake never deletes a receipt. The other three roles hold DELETE on this table; the - // backtest role must not inherit it just because the rule was widened. + // The intake never deletes a receipt. The backend and batch roles hold DELETE on this table; + // the backtest role must not inherit it just because the rule was widened. assertFalse(DatabaseAccessPolicy.allows( DatabaseAccessPolicy.ApplicationRole.BACKTEST, DatabaseAccessPolicy.Access.DELETE, @@ -311,6 +311,88 @@ void grantsTheBacktestRoleTheOperationsAccessItsRequestIntakeExecutes() { } } + @Test + void limitsTheTradingRoleToTheOperationsTablesItsWorkerExecutes() throws Exception { + // The trading worker claims and settles outbox receipts, reads the outbox, and appends its + // own facts and rejection audits. It used to hold every privilege on every operations table, + // operator credentials and sessions included. + var expected = java.util.Map.of( + "outbox_messages", List.of(DatabaseAccessPolicy.Access.READ, DatabaseAccessPolicy.Access.INSERT), + "outbox_consumer_receipts", List.of( + DatabaseAccessPolicy.Access.READ, + DatabaseAccessPolicy.Access.INSERT, + DatabaseAccessPolicy.Access.UPDATE), + "audit_events", List.of(DatabaseAccessPolicy.Access.INSERT)); + String baseline; + try (var input = getClass().getClassLoader().getResourceAsStream("db/migration/V1__initial_schema.sql")) { + baseline = new String(input.readAllBytes(), StandardCharsets.UTF_8); + } + var operationsTables = DatabaseAccessPolicy.verifyBaselineOwnership(baseline).tables().stream() + .filter(table -> "operations".equals(table.schema())) + .map(DatabaseAccessPolicy.QualifiedTable::table) + .toList(); + assertTrue(operationsTables.containsAll(List.of( + "operator_login_credentials", "operator_sessions", "roles", "audit_events"))); + for (var table : operationsTables) { + for (var access : List.of( + DatabaseAccessPolicy.Access.READ, + DatabaseAccessPolicy.Access.INSERT, + DatabaseAccessPolicy.Access.UPDATE, + DatabaseAccessPolicy.Access.DELETE)) { + assertEquals( + expected.getOrDefault(table, List.of()).contains(access), + DatabaseAccessPolicy.allows( + DatabaseAccessPolicy.ApplicationRole.TRADING, access, "operations", table), + "trading " + access + " on operations." + table); + } + } + + // The audit append's ON CONFLICT arbiter needs SELECT on that one column, not the audit trail. + assertEquals( + List.of(new DatabaseAccessPolicy.ColumnRead( + DatabaseAccessPolicy.ApplicationRole.TRADING, + new DatabaseAccessPolicy.QualifiedTable("operations", "audit_events"), + List.of("idempotency_key"))), + DatabaseAccessPolicy.columnReads(DatabaseAccessPolicy.ApplicationRole.TRADING)); + for (var role : List.of( + DatabaseAccessPolicy.ApplicationRole.BACKEND, + DatabaseAccessPolicy.ApplicationRole.BATCH, + DatabaseAccessPolicy.ApplicationRole.BACKTEST, + DatabaseAccessPolicy.ApplicationRole.PIPELINE)) { + assertEquals(List.of(), DatabaseAccessPolicy.columnReads(role), role.toString()); + } + + // The generated statements are what the database is asked for, so assert them exactly. + var sql = DatabaseAccessPolicy.runtimeGrantSql(List.of(baseline)); + assertEquals( + List.of( + "GRANT INSERT ON TABLE \"operations\".\"audit_events\" TO idea2strategy_trading;", + "GRANT SELECT, INSERT, UPDATE ON TABLE \"operations\".\"outbox_consumer_receipts\" " + + "TO idea2strategy_trading;", + "GRANT SELECT, INSERT ON TABLE \"operations\".\"outbox_messages\" TO idea2strategy_trading;", + "GRANT SELECT (\"idempotency_key\") ON TABLE \"operations\".\"audit_events\" " + + "TO idea2strategy_trading;"), + sql.lines() + .filter(line -> line.startsWith("GRANT ") + && line.contains(" ON TABLE \"operations\".") + && line.endsWith(" TO idea2strategy_trading;")) + .toList()); + assertTrue(sql.contains("GRANT USAGE ON SCHEMA \"operations\" TO idea2strategy_trading;")); + // Every rerun still starts from nothing: a table-level REVOKE also drops column privileges. + assertTrue(sql.contains( + "REVOKE ALL PRIVILEGES ON ALL TABLES IN SCHEMA \"operations\" FROM idea2strategy_trading;")); + } + + @Test + void skipsAColumnReadWhoseTableTheBundleDoesNotDeclare() { + var sql = DatabaseAccessPolicy.runtimeGrantSql(List.of( + "CREATE TABLE operations.outbox_messages (id uuid PRIMARY KEY);")); + + assertTrue(sql.contains( + "GRANT SELECT, INSERT ON TABLE \"operations\".\"outbox_messages\" TO idea2strategy_trading;")); + assertFalse(sql.contains("\"audit_events\"")); + } + @Test void grantsTheBatchRoleTheWritesItsScheduledJobsPerform() { // Derived from the write statements of the six adapters backend-batch imports. Without these