

# Auditing access and member cluster configuration

In a database cluster group for Aurora, each [member cluster](databaseclustergroups-member-clusters.md) keeps its own parameter groups, users, and schema, and you decide how each one is configured. The group reads data on member clusters but doesn't validate it, so you run the checks on this page yourself. Keep records of access and changes, and audit the member clusters on a schedule.

For API calls on the group and its member clusters, see [Monitoring Amazon Aurora API calls in AWS CloudTrail](logging-using-cloudtrail.md).

## Auditing statements
<a name="databaseclustergroups-auditing-statements"></a>

Two audit logs record statements:
+ **The routing layer audit log** records statements and sign-ins at the [routing endpoint](databaseclustergroups-connecting.md), under the router user's name. Turn it on with the `audit` value in the routing endpoint parameter `enable_cloudwatch_logs_exports`; see [Creating, modifying, and deleting a database cluster group for Aurora](databaseclustergroups-group-lifecycle.md). It doesn't record the statements of a [pinned session](databaseclustergroups-sessions.md), except the statement that sets the first pin; use the member cluster's audit log for pinned sessions. It delivers entries to Amazon CloudWatch Logs asynchronously, so they appear after a short delay. For a complete record of the statements that ran, for example for compliance, use the member cluster audit logs below. For its format, see [Logs for database cluster groups for Aurora](databaseclustergroups-logging.md).
+ **Each member cluster's audit log** records the statements that ran on it, both routed (under the member cluster user with the router user's name) and direct. Records name objects by their cluster names. Turn on Advanced Auditing (see [Using Advanced Auditing with an Amazon Aurora MySQL DB cluster](AuroraMySQL.Auditing.md)) with the same values for `server_audit_logging`, `server_audit_events`, `server_audit_incl_users`, and `server_audit_excl_users` in every member cluster's DB cluster parameter group.

To tell routed statements from pinned-session or direct ones on a member cluster, classify sessions by their label on the member cluster: a session that the routing layer opened carries the `_rds_connection_source` connect attribute. Don't rely on the `tid` comment, which pinned-session, router-answered, and direct statements don't carry, and never on host. See [Tracing a session or statement across the group](databaseclustergroups-trace-session.md).

On a schedule, compare each account's grants across every member cluster and against the router users, review who connects to each member cluster directly, and confirm that no application account sets `router_cluster_pin` (see [SET router\_cluster\_pin](databaseclustergroups-sql-set-router-cluster-pin.md)) unless you intend it.

## Recording mapping and router user changes
<a name="databaseclustergroups-auditing-changes"></a>

On Aurora MySQL, the routing layer audit log records each statement that changes a data mapping or a router user, with the router user who ran it. Statements that change a scaleout table or a routing function are in the DDL class. Statements that create, change, or drop a router user, or grant or revoke privileges, are in the DCL class. [Data movement commands](databaseclustergroups-sql-data-movement.md) and default role changes are in no class, so only the `QUERY` event selects them. Statements in a pinned session aren't recorded.

Export the mappings before and after every mapping change, as described in [Changing data mappings](databaseclustergroups-changing-mappings.md). Read `dbclustergroup_sys.router_user_definition` (see [router\_user\_definition](databaseclustergroups-sysviews-router-user-definition.md)) before and after every router user change, and keep both copies. Make mapping changes through a reviewed path, such as a pipeline, and limit who holds `dbclustergroup_metadata_admin`.

### Reviewing router users and their privileges
<a name="databaseclustergroups-auditing-review-users"></a>

Review every router user and its privileges on a fixed schedule. You need the `dbclustergroup_user_admin` privilege.

1. At the routing endpoint, list each router user and its privileges.

   ```sql
   SELECT username, host, authentication_method, account_locked,
          privileges, grantable_privileges
     FROM dbclustergroup_sys.router_user_definition;
   ```

1. In the result, look for:
   + router users that hold `dbclustergroup_super_admin` or `dbclustergroup_user_admin`
   + privileges in `grantable_privileges` that a router user can pass on
   + a host part of `%` on an account that needs access from fewer hosts
   + router users that you can't match to a person or an application

1. Read the statements that give one router user its privileges.

   ```sql
   SHOW GRANTS FOR '{{USER_NAME}}'@'{{HOST}}';
   ```

The table holds no password hashes, and you can't change it with `INSERT`, `UPDATE`, or `DELETE`. To change or remove a router user, see [Maintaining router users](databaseclustergroups-maintaining-router-users.md).

The routing layer error log records most failed sign-ins at the routing endpoint, with the user name and client IP address, and each sign-in to a locked router user. These entries show whether a person or an application still uses a router user that you locked. See [Connection and sign-in errors](databaseclustergroups-errors-connection.md).

## Detecting schema drift
<a name="databaseclustergroups-auditing-drift"></a>

Each member cluster keeps its own table definitions, and the routing layer routes by the data mappings without comparing them or checking the data. Run the following checks after every [rollout](databaseclustergroups-schema-changes.md), before every change to data mappings or scaleout tables, and on a schedule.
+ **Member cluster against member cluster.** On each member cluster, connected directly, read the table, column, index, and constraint definitions of each application database from `information_schema` with the following query. Compare them by role, not by name, because a scaleout member table can have a different cluster name on each member cluster. Name every constraint and index explicitly, because generated names include the cluster table name.
+ **Member cluster against data mappings.** Read `dbclustergroup_sys.data_mappings` (see [data\_mappings](databaseclustergroups-sysviews-data-mappings.md)) at the routing endpoint, and confirm that every mapping row names a database or table that exists on its member cluster, and that each scaleout table's member tables exist. Also look for databases on a member cluster that no mapping routes to.
+ **Foreign keys.** A foreign key holds only on its own member cluster. After a mapping change, a [routing function switch](databaseclustergroups-changing-routing-function.md), or a [move](databaseclustergroups-running-a-move.md), count child rows with no parent on each member cluster.

```sql
SELECT TABLE_NAME, COLUMN_NAME, ORDINAL_POSITION, COLUMN_TYPE, IS_NULLABLE, COLUMN_DEFAULT,
       CHARACTER_SET_NAME, COLLATION_NAME FROM information_schema.COLUMNS
 WHERE TABLE_SCHEMA = '{{CLUSTER_DB}}' ORDER BY TABLE_NAME, ORDINAL_POSITION;
SELECT TABLE_NAME, INDEX_NAME, NON_UNIQUE, COLUMN_NAME, SEQ_IN_INDEX, IS_VISIBLE
  FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = '{{CLUSTER_DB}}'
 ORDER BY TABLE_NAME, INDEX_NAME, SEQ_IN_INDEX;
```

## Auditing member cluster configuration
<a name="databaseclustergroups-auditing-member-cluster-consistency"></a>

Check that every member cluster is configured the same way at the Amazon RDS level and in its engine settings. This audit compares configuration, not data. Collect every member cluster's configuration in the same run, so that a change between reads doesn't show as drift.

1. List the member clusters with `describe-db-cluster-groups`. With routing enabled, cross-check the list with `dbclustergroup_sys.member_cluster_mappings` (see [member\_cluster\_mappings](databaseclustergroups-sysviews-member-cluster-mappings.md)) at the routing endpoint. For the parameters, see `DescribeDBClusterGroups` in the *Amazon RDS API Reference*.

   ```bash
   aws rds describe-db-cluster-groups --region {{REGION}} --db-cluster-group-identifier {{GROUP_IDENTIFIER}} \
     --query 'DBClusterGroups[0].DBClusterGroupMembers'
   ```

1. For each member cluster, collect its cluster, its instances, and its parameter values with `describe-db-clusters`, `describe-db-instances`, and `describe-db-cluster-parameters`, one file per member cluster.

1. On each member cluster, connected directly, read the settings that change statement results and binary logging:

   ```sql
   SELECT VARIABLE_NAME, VARIABLE_VALUE FROM performance_schema.global_variables
    WHERE VARIABLE_NAME IN ('sql_mode', 'time_zone', 'character_set_server', 'collation_server',
                            'lower_case_table_names', 'transaction_isolation', 'explicit_defaults_for_timestamp',
                            'max_connections', 'log_bin', 'binlog_format')
    ORDER BY VARIABLE_NAME;
   ```

1. Compare the member clusters, one column per member cluster: engine version, parameter values (not only parameter group names), backup and maintenance windows, deletion protection, and capacity settings, and for each instance the instance class, Performance Insights, and monitoring settings. Mark each row consistent, expected to differ, or drift. Check the settings that change results, and binary logging, first.

1. Fix one member cluster at a time: point it at the shared parameter group, or change a value in its group, and restart its writer if the parameter needs it. Then rerun step 2 for it.

1. At the routing endpoint, pin a session (see [SET router\_cluster\_pin](databaseclustergroups-sql-set-router-cluster-pin.md)) to the fixed member cluster and run one routed read for each mapping on it.