diff --git a/docs/features.md b/docs/features.md index a41cfadeb..51b430c1a 100644 --- a/docs/features.md +++ b/docs/features.md @@ -18,6 +18,8 @@ The following is a short list of the Percona XtraBackup features: * Exports individual tables from a full InnoDB backup +* Connects to the server with [OpenID Connect authentication](openid-connect-authentication.md) instead of a password + Percona XtraBackup automatically uses backup locks, a lightweight alternative to `FLUSH TABLES WITH READ LOCK` available in Percona Server, to copy non-InnoDB data. This operation avoids blocking DML queries that modify InnoDB tables. !!! admonition "See also" diff --git a/docs/openid-connect-authentication.md b/docs/openid-connect-authentication.md new file mode 100644 index 000000000..7210f212c --- /dev/null +++ b/docs/openid-connect-authentication.md @@ -0,0 +1,200 @@ +# OpenID Connect authentication + +Percona XtraBackup 8.4.0-9 adds support for OpenID Connect (OIDC) authentication. With OIDC, the backup account logs in to Percona Server for MySQL with an ID token from an external identity provider (IdP), such as Keycloak, Okta, or Microsoft Entra ID. The backup account needs no password. + +To try OIDC authentication step by step, see [Get started with OpenID Connect authentication](quickstart-openid-connect.md). + +## How OIDC authentication works + +Percona Server for MySQL 8.4.10 and later ship the `auth_openid_connect` server plugin. A database account created `IDENTIFIED WITH auth_openid_connect` logs in with a signed ID token, a JSON Web Token (JWT), instead of a password. + +A backup with OIDC authentication runs as follows: + +1. You get an ID token for the backup account from your IdP and save the token to a file. + +2. You pass the file path to xtrabackup with the [`--authentication-openid-connect-client-id-token-file`](xtrabackup-option-reference.md#authentication-openid-connect-client-id-token-file) option. + +3. xtrabackup loads the `authentication_openid_connect_client` client plugin from the directory set by [`--xtrabackup-plugin-dir`](xtrabackup-option-reference.md#xtrabackup-plugin-dir). + +4. The client plugin reads the token, checks that the connection is secure, and sends the token to the server during the handshake. + +5. The server plugin checks the token signature, issuer, audience, expiry time, and subject. If all checks pass, the server lets xtrabackup in. + +xtrabackup uses the same token for every connection to the server: + +* The main backup connection + +* The metadata lock (MDL) connection + +* The query killer connection + +* The redo log archiving connection + +* The redo log consumer connection + +The `--prepare` and `--copy-back` modes do not connect to the server and do not need a token. + +## Requirements + +To back up a server with OIDC authentication, you need the following: + +* Percona Server for MySQL 8.4.10 or later with the `auth_openid_connect` server plugin installed and configured. For details, see [OpenID Connect authentication in Percona Server for MySQL :octicons-link-external-16:](https://docs.percona.com/percona-server/{{vers}}/openid-connect-authentication.html). + +* A database account `IDENTIFIED WITH auth_openid_connect` with the [privileges that xtrabackup needs](privileges.md#privileges-needed). + +* A valid ID token for that account in a file. The file must contain only the raw JWT: no spaces, no line breaks, no JSON wrapper, and no `Bearer` prefix. The client plugin accepts files up to 10 KB. + +* A secure connection to the server: a Unix socket, or TCP with TLS (`--ssl-mode=REQUIRED`). The client and server plugins refuse a plain TCP connection. + +* The `authentication_openid_connect_client.so` client plugin in the xtrabackup plugin directory. The Percona XtraBackup packages include this file. + +## Client plugin location + +Percona XtraBackup installs the client plugin in the following directory: + +| Package type | Directory | +|----------------|-----------------------------------| +| RPM | `/usr/lib64/xtrabackup/plugin/` | +| DEB | `/usr/lib/xtrabackup/plugin/` | +| Binary tarball | `lib/plugin/` inside the tarball directory | + +With the RPM or DEB package, `--xtrabackup-plugin-dir` points to this directory by default, so you do not need to set the option. + +With a binary tarball, set the option to the plugin directory of the tarball: + +```shell +xtrabackup --backup --user=bkpuser \ + --authentication-openid-connect-client-id-token-file=/root/.oidc/id_token.jwt \ + --xtrabackup-plugin-dir=/opt/percona-xtrabackup-8.4/lib/plugin \ + --target-dir=/data/backups/full +``` + +## Create the backup account + +Create the account on the server and grant the backup privileges. The `identity_provider` value must match an IdP name in the `auth_openid_connect_configuration` system variable. The `user` value must match the `sub` claim of the ID token. Keycloak and many other IdPs set `sub` to an internal user ID, not to the login name. + +```sql +CREATE USER 'bkpuser'@'localhost' + IDENTIFIED WITH 'auth_openid_connect' + AS '{"identity_provider": "my-keycloak", "user": "4c28d537-a635-4b6d-957f-58e3c8860bcc"}'; + +GRANT BACKUP_ADMIN, PROCESS, RELOAD, LOCK TABLES, REPLICATION CLIENT ON *.* TO 'bkpuser'@'localhost'; +GRANT SELECT ON performance_schema.log_status TO 'bkpuser'@'localhost'; +GRANT SELECT ON performance_schema.keyring_component_status TO 'bkpuser'@'localhost'; +GRANT SELECT ON performance_schema.replication_group_members TO 'bkpuser'@'localhost'; +``` + +The server checks the token when xtrabackup connects, not when you run `CREATE USER`. + +## Back up with an ID token + +Pass the token file instead of a password. + +Connect through the Unix socket: + +```shell +xtrabackup --backup --user=bkpuser \ + --socket=/var/lib/mysql/mysql.sock \ + --authentication-openid-connect-client-id-token-file=/root/.oidc/id_token.jwt \ + --target-dir=/data/backups/full +``` + +Connect over TCP with TLS: + +```shell +xtrabackup --backup --user=bkpuser \ + --host=db1.example.com --port=3306 --ssl-mode=REQUIRED \ + --authentication-openid-connect-client-id-token-file=/root/.oidc/id_token.jwt \ + --target-dir=/data/backups/full +``` + +Prepare and restore the backup as usual. See [Prepare a full backup](prepare-full-backup.md) and [Restore a backup](restore-a-backup.md). + +You can also set the option in the `[xtrabackup]` group of your configuration file: + +```ini +[xtrabackup] +user=bkpuser +socket=/var/lib/mysql/mysql.sock +authentication-openid-connect-client-id-token-file=/root/.oidc/id_token.jwt +``` + +## Token file path + +xtrabackup reads the path as follows: + +* xtrabackup uses an absolute path unchanged. + +* A path that starts with `~/` starts from the home directory of the user who runs xtrabackup. + +* A relative path starts from the directory where you started xtrabackup. + +xtrabackup does not expand `~user/`. An empty file name causes an error. + +Use an absolute path in configuration files and scheduled jobs. Restrict access to the token file, for example with `chmod 600`, because anyone who reads the token can log in as the backup account until the token expires. + +## Options that work with the token + +| Option | Behavior with `--authentication-openid-connect-client-id-token-file` | +|--------|------| +| `--password` | You cannot use both options. The ID token replaces the password. xtrabackup stops with an error before the backup starts. xtrabackup also refuses `-p` before asking for a password. `--skip-password` sets no password, so xtrabackup accepts this option. | +| `--xtrabackup-plugin-dir` | Sets the directory with `authentication_openid_connect_client.so`. | +| `--socket`, `--host`, `--ssl-mode` | The token goes only over a Unix socket or a TLS connection. Plain TCP fails. | +| `--history-user`, `--history-password` | Control the credentials for the backup history connection. See [Backup history connection](#backup-history-connection). | + +### Backup history connection + +The [`--history`](store-backup-history.md) option writes a record to the `PERCONA_SCHEMA.xtrabackup_history` table. The history connection has no token option of its own and follows these rules: + +* Without `--history-user` and `--history-password`, the history connection uses the backup credentials, including the ID token. + +* With `--history-user`, the history account must log in with a password set by `--history-password`. + +## Log messages + +Before the first connection, xtrabackup logs the token file and the plugin directory. Each connection that uses the token says so instead of showing a password status: + +```text +Using OpenID Connect ID token file '/root/.oidc/id_token.jwt' for authentication to the server. +Loading the authentication_openid_connect_client plugin from '/usr/lib64/xtrabackup/plugin' (--xtrabackup-plugin-dir). +Connecting to MySQL server host: localhost, user: bkpuser, password: not set (using OpenID Connect ID token), port: not set, socket: /var/lib/mysql/mysql.sock, purpose: backup +``` + +## Token lifetime + +ID tokens are short-lived. Many IdPs issue tokens that expire after a few minutes. xtrabackup does not get or refresh tokens. + +xtrabackup opens most connections at the start of the backup, but the query killer connects later. Follow these recommendations: + +* Get a fresh token right before each backup, for example in the script that starts xtrabackup. + +* Make sure the token lifetime covers the whole backup. + +## Troubleshoot + +The server rejects a bad token with a generic `Access denied` message. The server error log shows the exact reason. + +| Error | Cause and fix | +|-------|---------------| +| `--password and --authentication-openid-connect-client-id-token-file are mutually exclusive: the ID token is the credential.` | Remove `--password` from the command line and from the configuration files. | +| `Cannot load the authentication_openid_connect_client plugin: ` | xtrabackup cannot find the client plugin. Check `--xtrabackup-plugin-dir`. With a binary tarball, set the option to `lib/plugin` in the tarball directory. | +| `Failed to set the ID token file '' for the authentication_openid_connect_client plugin. Check that the file exists and is readable.` | The file does not exist, or the system user who runs xtrabackup cannot read the file. xtrabackup reports this error before contacting the server. | +| `The id token file: does not contain a valid JWT.` | The file holds something other than a raw JWT. Save only the `id_token` value, not the access token or the full JSON response. | +| `Access denied for user ...` | The token expired, the token is not signed by a trusted key, the `sub` claim does not match the account, or the connection is not secure. Check the server error log. For example, `unsecure connection, use TLS, socket or memory` means you need a socket or `--ssl-mode=REQUIRED`. | +| `Access denied` after a server restart or restore | The server lost the `auth_openid_connect_configuration` value because you set the variable with `SET GLOBAL`. Persist the value with `SET PERSIST`, or set the variable in `my.cnf`. | + +For more server-side messages, see [Troubleshoot connection failures in Percona Server for MySQL :octicons-link-external-16:](https://docs.percona.com/percona-server/{{vers}}/openid-connect-authentication.html). + +## Limitations + +* The history connection cannot use a different ID token. A history server that trusts another IdP or audience needs a password account. + +* xtrabackup does not request, cache, or refresh ID tokens. Your script or operator must provide a valid token before each backup. + +!!! admonition "See also" + + * [Get started with OpenID Connect authentication](quickstart-openid-connect.md) + + * [OpenID Connect authentication in Percona Server for MySQL :octicons-link-external-16:](https://docs.percona.com/percona-server/{{vers}}/openid-connect-authentication.html) + + * [Connection and privileges needed](privileges.md) diff --git a/docs/privileges.md b/docs/privileges.md index 27236ecfc..4eda87914 100644 --- a/docs/privileges.md +++ b/docs/privileges.md @@ -60,6 +60,18 @@ alteration, see `mysql --help` for details. (port, socket, host) must be specified in order for xtrabackup to talk to the correct server. +### Connect with OpenID Connect authentication + +If the database account uses OpenID Connect (OIDC) authentication, pass an ID token file instead of a password: + +```bash +xtrabackup --user=bkpuser \ +--authentication-openid-connect-client-id-token-file=/root/.oidc/id_token.jwt \ +--backup --target-dir=/data/bkps/ +``` + +For details, see [OpenID Connect authentication](openid-connect-authentication.md). + ## Privileges needed Once connected to the server, in order to perform a backup you need diff --git a/docs/quickstart-openid-connect.md b/docs/quickstart-openid-connect.md new file mode 100644 index 000000000..568dde975 --- /dev/null +++ b/docs/quickstart-openid-connect.md @@ -0,0 +1,248 @@ +# Get started with OpenID Connect authentication + +This guide shows how to back up Percona Server for MySQL with an account that logs in with an OpenID Connect (OIDC) ID token from Keycloak. You install and configure the server plugin, create a backup account, get an ID token, and run a backup without a password. + +The guide takes about 20 minutes. + +For details about the feature, see [OpenID Connect authentication](openid-connect-authentication.md). + +## Before you begin + +Make sure you have the following: + +* Percona Server for MySQL 8.4.10 or later with `auth_openid_connect.so` in the `plugin_dir` directory. + +* Percona XtraBackup 8.4.0-9 or later, installed from an RPM or DEB package. + +* A running Keycloak server with a realm, an OIDC client, and a user for backups. To set up Keycloak, see [Prepare Keycloak](#prepare-keycloak). + +* `curl` and `jq` on the host where you run xtrabackup. + +* A MySQL administrative account with the `INSERT` privilege on `mysql.plugin`, the `SYSTEM_VARIABLES_ADMIN` privilege, and the `CREATE USER` privilege. + +Run the shell commands in this guide as `root` on the database host, because xtrabackup needs read access to the data directory. + +## Quickstart values + +Replace the following example values with your own: + +| Item | Example value | +|------|---------------| +| Keycloak host | `keycloak.example.com` | +| Keycloak realm | `master` | +| Keycloak client ID | `mysql-oidc` | +| Keycloak user | `backup-operator` | +| `sub` claim of the Keycloak user | `4c28d537-a635-4b6d-957f-58e3c8860bcc` | +| IdP name in MySQL | `my-keycloak` | +| MySQL backup account | `bkpuser@localhost` | +| MySQL socket | `/var/lib/mysql/mysql.sock` | +| Token file | `/root/.oidc/id_token.jwt` | +| Backup directory | `/data/backups/full` | + +To find the `sub` claim, open the user in the Keycloak admin console and copy the user ID. + +## Prepare Keycloak + +If you do not have a Keycloak server yet, install one and create the objects that this guide uses. + +1. Install and start Keycloak. Choose the guide for your platform: Docker, Podman, Kubernetes, OpenShift, or OpenJDK on bare metal. See [Keycloak getting started guides :octicons-link-external-16:](https://www.keycloak.org/guides#getting-started). + +2. Create a realm, or use the `master` realm. See [Keycloak Server Administration Guide :octicons-link-external-16:](https://www.keycloak.org/docs/latest/server_admin/). + +3. Create an OpenID Connect client with the client ID `mysql-oidc`. In the client settings, turn off **Client authentication** to make a public client, and enable **Direct access grants**. + +4. Create the `backup-operator` user and set a password for the user. + +5. Copy the user ID from the user details page. You need this value as the `sub` claim when you create the MySQL account. + +Keycloak serves the endpoints that this guide uses under the realm URL: + +| Endpoint | URL | +|----------|-----| +| Token | `https://keycloak.example.com/realms/master/protocol/openid-connect/token` | +| Signing keys (JWKS) | `https://keycloak.example.com/realms/master/protocol/openid-connect/certs` | + +For the full list of endpoints, see [Keycloak OIDC endpoints :octicons-link-external-16:](https://www.keycloak.org/securing-apps/oidc-layers). + +!!! note + + The Percona Server for MySQL host must reach the Keycloak server over HTTPS to download the signing keys. + +## Install the server plugin + +Connect to the server as the administrative user and install the plugin: + +```sql +INSTALL PLUGIN auth_openid_connect SONAME 'auth_openid_connect.so'; +``` + +Check that the plugin is active: + +```sql +SELECT PLUGIN_NAME, PLUGIN_STATUS +FROM INFORMATION_SCHEMA.PLUGINS +WHERE PLUGIN_NAME = 'auth_openid_connect'; +``` + +??? example "Expected output" + + ```{.text .no-copy} + +---------------------+---------------+ + | PLUGIN_NAME | PLUGIN_STATUS | + +---------------------+---------------+ + | auth_openid_connect | ACTIVE | + +---------------------+---------------+ + ``` + +## Configure trust for Keycloak + +Tell the server which IdP to trust. The server gets the signing keys from the Keycloak JWKS endpoint. The `audiences` value must match the Keycloak client ID, because Keycloak puts the client ID in the `aud` claim of the ID token. + +```sql +SET PERSIST auth_openid_connect_configuration = 'JSON://{ + "my-keycloak": { + "issuer-name": "https://keycloak.example.com/realms/master", + "jwks-url": "https://keycloak.example.com/realms/master/protocol/openid-connect/certs", + "audiences": ["mysql-oidc"] + } +}'; +``` + +`SET PERSIST` keeps the value after a server restart. A value set with `SET GLOBAL` disappears after a restart, and then every OIDC login fails. + +## Create the backup account + +Create an account that logs in with an ID token. The `user` value is the `sub` claim of the Keycloak user: + +```sql +CREATE USER 'bkpuser'@'localhost' + IDENTIFIED WITH 'auth_openid_connect' + AS '{"identity_provider": "my-keycloak", "user": "4c28d537-a635-4b6d-957f-58e3c8860bcc"}'; +``` + +Grant the privileges that xtrabackup needs for a full backup: + +```sql +GRANT BACKUP_ADMIN, PROCESS, RELOAD, LOCK TABLES, REPLICATION CLIENT ON *.* TO 'bkpuser'@'localhost'; +GRANT SELECT ON performance_schema.log_status TO 'bkpuser'@'localhost'; +GRANT SELECT ON performance_schema.keyring_component_status TO 'bkpuser'@'localhost'; +GRANT SELECT ON performance_schema.replication_group_members TO 'bkpuser'@'localhost'; +``` + +For the full list of privileges, see [Connection and privileges needed](privileges.md). + +## Get an ID token from Keycloak + +Create a private directory for the token: + +```shell +mkdir -p -m 700 /root/.oidc +``` + +Request a token and save the `id_token` field to a file. Replace `` with the password of the Keycloak user: + +```shell +umask 077 +curl -sS --fail -X POST \ + https://keycloak.example.com/realms/master/protocol/openid-connect/token \ + -d 'grant_type=password' \ + -d 'client_id=mysql-oidc' \ + -d 'scope=openid' \ + -d 'username=backup-operator' \ + -d 'password=' \ + | jq -er .id_token > /root/.oidc/id_token.jwt + +chmod 600 /root/.oidc/id_token.jwt +``` + +`umask 077` makes the shell create the token file readable only by `root`. `--fail` makes `curl` stop on an HTTP error, and `jq -e` stops with an error when the response has no `id_token` field. + +Check that the file holds a JWT. A JWT has three parts separated by dots: + +```shell +awk -F. '{print NF}' /root/.oidc/id_token.jwt +``` + +??? example "Expected output" + + ```{.text .no-copy} + 3 + ``` + +Keep the following in mind: + +* The Keycloak client must allow the password grant. In the Keycloak admin console, open the client and enable **Direct access grants**. See [Prepare Keycloak](#prepare-keycloak). + +* The plugin needs the ID token, not the access token. + +* The `scope=openid` parameter is required. Without this parameter, Keycloak returns no ID token. + +* This example uses a public client. For a confidential client, also send the `client_secret` parameter. + +* The password grant suits tests and demos. In production, use the authorization code flow with PKCE or the device authorization flow. + +ID tokens expire quickly. Get a fresh token right before each backup. + +## Run the backup + +Run xtrabackup with the token file instead of a password. The RPM and DEB packages already point `--xtrabackup-plugin-dir` to the client plugin, so you do not need to set the option. + +```shell +xtrabackup --backup --user=bkpuser \ + --socket=/var/lib/mysql/mysql.sock \ + --authentication-openid-connect-client-id-token-file=/root/.oidc/id_token.jwt \ + --target-dir=/data/backups/full +``` + +??? example "Expected output" + + ```{.text .no-copy} + ... + [Note] [MY-011825] [Xtrabackup] Using OpenID Connect ID token file '/root/.oidc/id_token.jwt' for authentication to the server. + [Note] [MY-011825] [Xtrabackup] Loading the authentication_openid_connect_client plugin from '/usr/lib64/xtrabackup/plugin' (--xtrabackup-plugin-dir). + [Note] [MY-011825] [Xtrabackup] Connecting to MySQL server host: localhost, user: bkpuser, password: not set (using OpenID Connect ID token), port: not set, socket: /var/lib/mysql/mysql.sock, purpose: backup + ... + [Note] [MY-011825] [Xtrabackup] completed OK! + ``` + +To connect over TCP instead of the socket, add `--host`, `--port`, and `--ssl-mode=REQUIRED`. The plugins refuse a plain TCP connection. + +## Prepare the backup + +The `--prepare` mode does not connect to the server, so you do not need the token: + +```shell +xtrabackup --prepare --target-dir=/data/backups/full +``` + +To restore the backup, see [Restore a backup](restore-a-backup.md). + +## Clean up + +Remove the token file: + +```shell +rm /root/.oidc/id_token.jwt +``` + +If you created the account only for this guide, drop the account: + +```sql +DROP USER 'bkpuser'@'localhost'; +``` + +To remove the server plugin, see [Uninstall the plugin in Percona Server for MySQL :octicons-link-external-16:](https://docs.percona.com/percona-server/{{vers}}/openid-connect-authentication.html). + +## Additional resources + +* [OpenID Connect authentication](openid-connect-authentication.md) + +* [OpenID Connect authentication in Percona Server for MySQL :octicons-link-external-16:](https://docs.percona.com/percona-server/{{vers}}/openid-connect-authentication.html) + +* [Get started with OpenID Connect authentication in Percona Server for MySQL :octicons-link-external-16:](https://docs.percona.com/percona-server/{{vers}}/quickstart-openid-connect.html) + +* [Keycloak getting started guides :octicons-link-external-16:](https://www.keycloak.org/guides#getting-started) + +* [Keycloak Server Administration Guide :octicons-link-external-16:](https://www.keycloak.org/docs/latest/server_admin/) + +* [Keycloak OIDC endpoints :octicons-link-external-16:](https://www.keycloak.org/securing-apps/oidc-layers) diff --git a/docs/xtrabackup-option-reference.md b/docs/xtrabackup-option-reference.md index ce966ad12..3394444ad 100644 --- a/docs/xtrabackup-option-reference.md +++ b/docs/xtrabackup-option-reference.md @@ -68,6 +68,24 @@ Usage: `--apply-log-only` This option causes only the redo stage to be performed when preparing a backup. It is essential for incremental backups. +### authentication-openid-connect-client-id-token-file + +Usage: `--authentication-openid-connect-client-id-token-file=PATH` + +Introduced in Percona XtraBackup 8.4.0-9. + +This option sets the file with the OpenID Connect (OIDC) ID token for an account `IDENTIFIED WITH auth_openid_connect`. xtrabackup sends the token instead of a password on every connection to the server. + +* You cannot combine this option with [`--password`](#password). `--skip-password` is allowed. + +* xtrabackup loads the `authentication_openid_connect_client` plugin from [`--xtrabackup-plugin-dir`](#xtrabackup-plugin-dir). + +* A relative path starts from the directory where you start xtrabackup. A path that starts with `~/` starts from your home directory. + +* The connection must use a Unix socket or TLS. + +For details, see [OpenID Connect authentication](openid-connect-authentication.md). + ### backup Usage: `--backup` @@ -822,6 +840,8 @@ Usage: `--password=PASSWORD` This option accepts a string argument that specifies the password used when connecting to the database. +You cannot combine this option with [`--authentication-openid-connect-client-id-token-file`](#authentication-openid-connect-client-id-token-file). + ### plugin-load Usage: `--plugin-load` @@ -1193,4 +1213,6 @@ This option prints xtrabackup version and exits. Usage: `--xtrabackup-plugin-dir=DIRNAME` -The absolute path to the directory that contains the `keyring` plugin. +The absolute path to the directory that contains the `keyring` plugin and the `authentication_openid_connect_client` plugin. + +The RPM and DEB packages set the default to the xtrabackup plugin directory: `/usr/lib64/xtrabackup/plugin` for RPM and `/usr/lib/xtrabackup/plugin` for DEB. With a binary tarball, set the option to the `lib/plugin` directory of the tarball. diff --git a/mkdocs-base.yml b/mkdocs-base.yml index 3c13dc37b..3fb48df50 100644 --- a/mkdocs-base.yml +++ b/mkdocs-base.yml @@ -209,6 +209,9 @@ nav: - throttling-backups.md - store-backup-history.md - backup-size-reporting.md + - OpenID Connect authentication: + - openid-connect-authentication.md + - quickstart-openid-connect.md - xbstream features: - take-streaming-backup.md - accelerate-backup-process.md