

# Maintaining router users

After you create router users and understand routing layer privileges, use the following procedures to change and remove router users, rotate their passwords, and keep them consistent across the member clusters of a database cluster group for Aurora.

## Changing, renaming, and removing users
<a name="databaseclustergroups-router-users-change"></a>

You change, rename, lock, and drop router users at the routing endpoint with the engine's own user statements. Router user changes don't end open sessions. Change both accounts yourself, with the same name on every member cluster:
+ Renaming a router user doesn't rename the member cluster users. The renamed router user gets the data privileges of the member cluster users with the new name. Rename them on every member cluster before you route traffic under the new name.
+ To remove a user, drop it at the routing endpoint and on every member cluster. Dropping only the router user leaves a direct login to the member clusters. Dropping only the member cluster users leaves a login at the routing endpoint whose statements fail.

A router user change applies to new connections on every routing layer node within a few seconds. Existing sessions keep their sign-in. To end them, see the procedure that follows.

`ALTER USER`, `DROP USER`, `RENAME USER`, `SET PASSWORD`, `ACCOUNT LOCK`, `ACCOUNT UNLOCK`, and `REQUIRE SSL` work at the routing endpoint as in MySQL. A change applies without `FLUSH PRIVILEGES`, which the routing layer doesn't have.

Each account management statement, including `CREATE USER`, `GRANT`, and `REVOKE`, commits on its own, so a `ROLLBACK` can't undo it. Inside an open transaction, the routing layer refuses the statement with error 1179 and writes nothing. A transaction is open after `BEGIN` or `START TRANSACTION`, and after a statement that runs with `autocommit = 0`. Commit or roll back first, and then run the statement.

To remove access, lock the router user, end its open connections, and then lock or drop its member cluster users. These steps need `dbclustergroup_user_admin` and `dbclustergroup_connection_admin`. To end another user's connection that is running a statement, your member cluster user also needs `CONNECTION_ADMIN` on that member cluster.

**To remove access for a router user**

1. At the routing endpoint, list each host part of the user name, and lock each one.

1. At the routing endpoint, list the user's open connections.

1. At the routing endpoint, end each connection from the preceding step.

1. At the routing endpoint, repeat steps 2 and 3 until the list is empty.

1. On every member cluster (direct), lock or drop the member cluster user, because it can still connect to its member cluster's endpoint.

The statements for steps 1 to 3 are as follows.

```sql
-- Step 1
SELECT username, host FROM dbclustergroup_sys.router_user_definition WHERE username = '{{USER}}';
ALTER USER '{{USER}}'@'{{HOST}}' ACCOUNT LOCK;
-- Step 2
SELECT id, user, host, db, command, time FROM dbclustergroup_sys.router_processlist WHERE user = '{{USER}}';
-- Step 3
KILL CONNECTION {{ID}};
```

Error `1094` means that the connection is already gone, so there's nothing to do. Error `1095` means that you don't own the session and lack `dbclustergroup_connection_admin`, or that another user's session is running a statement and your member cluster user lacks `CONNECTION_ADMIN` on that member cluster. Error `1227`, `Access denied: user no longer exists`, means that your own router user was dropped, or dropped and created again, after you signed in. Sign in again as a current router user. Run `KILL` again only after error `1105`.

On Aurora MySQL, lock the router user first, so that it can't sign in again. Then, from any connection to the routing endpoint, list the router user's connections in `router_processlist`. To see another router user's connections, you need `dbclustergroup_process_admin`. End each connection with `KILL CONNECTION` and its ID. The routing layer sends the request to the node that holds the connection. Error `1094` means that no routing layer node holds the connection. If `KILL` returns error `1105`, run it again. If the list returns a warning that some routing layer nodes didn't respond, list the connections again. Repeat until the list is empty. To end another router user's connection, you need `dbclustergroup_connection_admin`.

## Rotating passwords
<a name="databaseclustergroups-router-users-rotate"></a>

To rotate an application router user's password without downtime, use a dual password. We recommend that you rotate the administrative router user's password on a fixed schedule.

**To rotate a router user's password**

1. At the routing endpoint, add the new password and keep the current one. Both passwords sign in.

1. In each client application or secret store, update the password to the new one.

1. At the routing endpoint, discard the old password. A sign-in with it is then refused.

The statements for steps 1 and 3 are as follows.

**Step 1.** At the routing endpoint, add the new password.

```sql
ALTER USER '{{APP_USER}}'@'{{HOST}}' IDENTIFIED BY '{{NEW_PASSWORD}}' RETAIN CURRENT PASSWORD;
```

**Step 3.** At the routing endpoint, discard the old password. A sign-in with it then returns `ERROR 1045`.

```sql
ALTER USER '{{APP_USER}}'@'{{HOST}}' DISCARD OLD PASSWORD;
```

To rotate the administrative router user's password, use the Amazon RDS API.

**To rotate the administrative router user's password**

1. On the host where you run the AWS CLI, put the new password in a JSON file that only you can read: `{"DBClusterGroupIdentifier": "{{GROUP_IDENTIFIER}}", "AdminUserPassword": "{{NEW_PASSWORD}}", "ApplyImmediately": true}`.

1. With the AWS CLI, call `ModifyDBClusterGroup` (`aws rds modify-db-cluster-group`) with the file.

   ```bash
   aws rds modify-db-cluster-group --region {{REGION}} --cli-input-json file://rotate-admin-password.json
   ```

1. On the same host, update the stored secret and delete the file.

1. At the routing endpoint, sign in with the new password to confirm the change.

You can't change the admin user's name after Amazon RDS creates it. A `ModifyDBClusterGroup` call with a different `AdminUsername` fails, and the existing admin user doesn't change. If the group has no admin user, specify `AdminUsername` and `AdminUserPassword` together to create one. Routing must be on after the call.

## Keeping users consistent across member clusters
<a name="databaseclustergroups-router-users-consistent"></a>

Every router user needs its member cluster user, with the same grants, on every member cluster. Check this after every user change. A data move doesn't create users, so create them on the target member cluster before the cutover.

**To keep users consistent when you add a member cluster**

1. Before you add the cluster, at the routing endpoint, list the router users.

1. On the new cluster and on an existing member cluster (direct), compare the accounts and grants.

1. On the new cluster (direct), create each missing member cluster user, as in step 1 of [Creating a router user](databaseclustergroups-router-users.md#databaseclustergroups-router-users-create).

1. With the AWS CLI, add the cluster, as described in [Adding and removing member clusters](databaseclustergroups-member-clusters.md).

1. At the routing endpoint, after the first data mapping that sends traffic to the new member cluster, run one routed read as each application router user.

The statements for steps 1 and 2 are as follows.

**Step 1.** At the routing endpoint, as a router user with `dbclustergroup_user_admin`, list the router users.

```sql
SELECT username, host FROM dbclustergroup_sys.router_user_definition;
```

**Step 2.** On each cluster's cluster endpoint (direct), list the accounts and grants. Leave out the cluster's own accounts, such as `rdsadmin`, `mysql.*`, and the `rds_` roles.

```sql
SELECT user, host FROM mysql.user ORDER BY user, host;
SHOW GRANTS FOR '{{APP_USER}}'@'%';
```

For a scheduled audit of users and settings across member clusters, see [Auditing access and member cluster configuration](databaseclustergroups-auditing.md).