diff --git a/en/grant-permissions-to-remote-storage.md b/en/grant-permissions-to-remote-storage.md index b2979896b..b8515d3e9 100644 --- a/en/grant-permissions-to-remote-storage.md +++ b/en/grant-permissions-to-remote-storage.md @@ -15,11 +15,23 @@ Amazon Web Services (AWS) provides different methods to grant permissions for di - [Grant permissions by associating IAM with Pod](#grant-permissions-by-associating-iam-with-pod): applicable to self-managed Kubernetes clusters. - [Grant permissions by associating IAM with ServiceAccount](#grant-permissions-by-associating-iam-with-serviceaccount): applicable only to AWS EKS clusters. -### Create a bucket-scoped IAM policy +### Create least-privilege IAM policies -Before using any of the AWS authorization methods, create a customer-managed IAM policy that grants access only to the S3 bucket used for backup and restore. Do not attach an account-wide AWS managed S3 policy, because it grants access to every S3 bucket in the account. +Before using any of the AWS authorization methods, create a customer-managed IAM policy for the S3 location used by your `Backup` or `Restore` CR. Do not attach an account-wide AWS managed S3 policy such as `AmazonS3FullAccess`, because it grants access to every S3 bucket in the account. -The following policy includes the minimum S3 permissions required for both backup and restore. Replace `` with the name of your backup bucket, and then attach the policy to the IAM user or role used by TiDB: +Scope the policy according to the `s3` fields in the CR: + +- Use `.spec.s3.bucket` for the bucket ARN. +- If `.spec.s3.prefix` is empty, grant access to the whole bucket. +- If `.spec.s3.prefix` is set, scope object access to `arn:aws:s3::://*`. The read-only restore example also restricts `s3:ListBucket` with an `s3:prefix` condition; backup requires an unconditional `s3:ListBucket` grant, as explained below. + +Replace `` with the bucket name and `` with `.spec.s3.prefix` without leading or trailing slashes. For example, for a prefix of `backup/`, use `backup`. + +For point-in-time recovery (PITR), also grant read access to the snapshot backup location specified by `.spec.pitrFullBackupStorageProvider.s3`. Add separate policy statements if the snapshot and log backups use different buckets or prefixes. + +The following examples show the minimum S3 permissions for a `Backup` CR and for a read-only `Restore` CR. + +#### Bucket-scoped policy for a `Backup` CR ```json { @@ -46,20 +58,104 @@ The following policy includes the minimum S3 permissions required for both backu } ``` -When you use this policy, note the following conditions: +#### Prefix-scoped policy for a `Backup` CR + +This policy restricts object access to the backup prefix but allows listing all object keys in the bucket. BR checks whether `backupmeta` exists before starting a backup. Without an unconditional `s3:ListBucket` grant, S3 can return `403 Forbidden` for a missing object, causing the backup to fail. Use a dedicated backup bucket if other object keys must remain hidden. + +```json +{ + "Version": "2012-10-17", + "Statement": [ + { + "Sid": "ListBackupBucket", + "Effect": "Allow", + "Action": "s3:ListBucket", + "Resource": "arn:aws:s3:::" + }, + { + "Sid": "AccessBackupObjectsInPrefix", + "Effect": "Allow", + "Action": [ + "s3:GetObject", + "s3:PutObject", + "s3:DeleteObject", + "s3:AbortMultipartUpload" + ], + "Resource": "arn:aws:s3::://*" + } + ] +} +``` + +#### Bucket-scoped policy for a read-only `Restore` CR + +```json +{ + "Version": "2012-10-17", + "Statement": [ + { + "Sid": "ListBackupBucket", + "Effect": "Allow", + "Action": "s3:ListBucket", + "Resource": "arn:aws:s3:::" + }, + { + "Sid": "ReadBackupObjects", + "Effect": "Allow", + "Action": "s3:GetObject", + "Resource": "arn:aws:s3:::/*" + } + ] +} +``` + +#### Prefix-scoped policy for a read-only `Restore` CR + +```json +{ + "Version": "2012-10-17", + "Statement": [ + { + "Sid": "ListBackupPrefix", + "Effect": "Allow", + "Action": "s3:ListBucket", + "Resource": "arn:aws:s3:::", + "Condition": { + "StringLike": { + "s3:prefix": "/*" + } + } + }, + { + "Sid": "ReadBackupObjectsInPrefix", + "Effect": "Allow", + "Action": "s3:GetObject", + "Resource": "arn:aws:s3::://*" + } + ] +} +``` + +When you use these policies, note the following conditions: - If Backup and Restore resources use different buckets with the same IAM user or role, add the bucket and object ARNs for every required bucket to the corresponding `Resource` elements. Do not replace them with a wildcard that grants access to unrelated buckets. - For buckets in AWS China Regions or AWS GovCloud (US) Regions, replace the `aws` partition in both ARNs with `aws-cn` or `aws-us-gov`, respectively. - If a Backup resource, or a Restore resource that writes restore metadata to S3, sets `.spec.s3.acl`, add `s3:PutObjectAcl` to the object actions unless another policy already grants it. - If bucket default encryption or a Backup or Restore resource selects a customer-managed AWS KMS key, grant the IAM user or role `kms:GenerateDataKey` and `kms:Decrypt` on the key used to write backup objects or restore metadata. For a read-only restore, grant `kms:Decrypt` on every key that encrypted the backup objects, including an older key if the bucket's default KMS key has changed since the backup was created. Make sure that each KMS key policy also permits these principals to use the required actions, either directly or through IAM policies. -The permissions required for restore depend on whether BR writes restore metadata to S3. If BR writes checkpoint metadata to S3, retain `s3:ListBucket`, `s3:GetObject`, `s3:PutObject`, and `s3:DeleteObject`. Starting from BR v8.5.5, if the target cluster does not contain `mysql.tidb_pitr_id_map` and `--checkpoint-storage` is not specified, PITR writes the ID map to the log backup directory. If that directory is in S3, retain `s3:ListBucket`, `s3:GetObject`, and `s3:PutObject`. If BR does not write restore metadata to S3, you can restrict a read-only restore to `s3:ListBucket` and `s3:GetObject`. For more information, see [Checkpoint Restore](https://docs.pingcap.com/tidb/stable/br-checkpoint-restore) and [Backup storages](https://docs.pingcap.com/tidb/stable/backup-and-restore-storages#authentication). +For snapshot restores with checkpoint restore enabled, BR versions earlier than v8.4.0 store checkpoint data in the backup location. If that location is in S3, the read-only restore policy is insufficient. Starting from BR v8.4.0, snapshot restore checkpoints are stored in the target cluster by default. Starting from BR v8.5.5, you can use `--checkpoint-storage` to specify external checkpoint storage. + +The permissions required for restore depend on whether BR writes restore metadata to S3. If BR writes checkpoint metadata to S3, grant `s3:ListBucket`, `s3:GetObject`, `s3:PutObject`, and `s3:DeleteObject` for the checkpoint storage location. When `--checkpoint-storage` points to a different bucket or prefix, add policy statements for that location instead of adding write permissions to the backup location. Keep `s3:ListBucket` unconditional on the checkpoint bucket so that checks for missing checkpoint objects can succeed. + +Starting from BR v8.5.5, if the target cluster does not contain `mysql.tidb_pitr_id_map` and `--checkpoint-storage` is not specified, PITR writes the ID map to the log backup directory. If that directory is in S3, add `s3:PutObject` for the log backup location and keep `s3:ListBucket` unconditional on that bucket so that BR can check whether the ID map exists. + +If the same IAM user or role is shared by both Backup and Restore resources, you can combine the required actions into a single policy. For more information, see [Checkpoint Restore](https://docs.pingcap.com/tidb/stable/br-checkpoint-restore) and [Backup storages](https://docs.pingcap.com/tidb/stable/backup-and-restore-storages#authentication). ### Grant permissions by AccessKey and SecretKey To grant permissions to Amazon S3 using AccessKey and SecretKey, perform the following steps. For Ceph, MinIO, or another S3-compatible storage service, obtain credentials from that service and grant bucket permissions through its access-control mechanism. AWS IAM users and policies might not be recognized by other storage services. -1. Create an IAM user by following [Create an IAM user in your AWS account](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_users_create.html), and attach the [bucket-scoped IAM policy](#create-a-bucket-scoped-iam-policy). +1. Create an IAM user by following [Create an IAM user in your AWS account](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_users_create.html), and attach the [least-privilege IAM policies](#create-least-privilege-iam-policies). 2. Create an access key by following [Create an access key for yourself (console)](https://docs.aws.amazon.com/IAM/latest/UserGuide/access-key-self-managed.html#Using_CreateAccessKey). After completion, you can obtain the AccessKey and SecretKey. @@ -157,7 +253,7 @@ The method of granting permissions by associating IAM with a Pod is supported by To grant permissions by associating IAM with a Pod, perform the following steps: -1. Follow the [IAM role creation](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_create.html) document to create an IAM role in your AWS account, and attach the [bucket-scoped IAM policy](#create-a-bucket-scoped-iam-policy). +1. Follow the [IAM role creation](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_create.html) document to create an IAM role in your AWS account, and attach the [least-privilege IAM policies](#create-least-privilege-iam-policies). 2. Use the [Overlay](overlay.md) feature to associate the IAM role with the target component (TiKV or TiFlash). The following example shows how to associate the role with a TiKVGroup: @@ -185,7 +281,7 @@ By associating a user's [IAM](https://aws.amazon.com/iam/) role with a [`Service To grant permissions by associating IAM with a ServiceAccount, perform the following steps: -1. Follow the [IAM role creation](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_create.html) document to create an IAM role in your AWS account, and attach the [bucket-scoped IAM policy](#create-a-bucket-scoped-iam-policy). +1. Follow the [IAM role creation](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_create.html) document to create an IAM role in your AWS account, and attach the [least-privilege IAM policies](#create-least-privilege-iam-policies). 2. Follow the instructions in [Create an IAM OIDC provider for your cluster](https://docs.aws.amazon.com/eks/latest/userguide/enable-iam-roles-for-service-accounts.html) to create an IAM OIDC provider for your EKS cluster. diff --git a/zh/grant-permissions-to-remote-storage.md b/zh/grant-permissions-to-remote-storage.md index fdf693e8f..f27c3eea1 100644 --- a/zh/grant-permissions-to-remote-storage.md +++ b/zh/grant-permissions-to-remote-storage.md @@ -15,11 +15,23 @@ summary: 介绍如何授权访问远程存储从而实现 TiDB 集群的备份 - [通过 IAM 绑定 Pod 授权](#通过-iam-绑定-pod-授权):适用于自建 Kubernetes 集群。 - [通过 IAM 绑定 ServiceAccount 授权](#通过-iam-绑定-serviceaccount-授权):只适用于 AWS EKS 集群。 -### 创建存储桶级 IAM 策略 +### 创建最小权限 IAM 策略 -在使用任一 AWS 授权方式之前,请创建一个仅允许访问备份和恢复所用 S3 存储桶的客户托管 IAM 策略。不要附加账号级 AWS 托管 S3 策略,因为此类策略允许访问账号中的所有 S3 存储桶。 +在使用任一 AWS 授权方式之前,请为 `Backup` 或 `Restore` CR 使用的 S3 位置创建客户托管 IAM 策略。不要附加 `AmazonS3FullAccess` 之类的账号级 AWS 托管 S3 策略,因为此类策略允许访问账号中的所有 S3 存储桶。 -以下策略包含备份和恢复所需的最小 S3 权限。请将 `` 替换为备份存储桶的名称,然后将该策略附加到 TiDB 使用的 IAM 用户或角色: +根据 CR 中的 `s3` 字段来限定策略范围: + +- 使用 `.spec.s3.bucket` 构造存储桶 ARN。 +- 如果 `.spec.s3.prefix` 为空,则授予整个存储桶的访问权限。 +- 如果 `.spec.s3.prefix` 已设置,则将对象访问范围限制为 `arn:aws:s3::://*`。只读恢复的示例还为 `s3:ListBucket` 添加了 `s3:prefix` 条件;备份需要不带此条件的 `s3:ListBucket` 权限,原因见下文。 + +请将 `` 替换为存储桶名称,将 `` 替换为 `.spec.s3.prefix` 的值,并去掉开头和结尾的 `/`。例如,前缀为 `backup/` 时,使用 `backup`。 + +对于时间点恢复 (PITR),还需为 `.spec.pitrFullBackupStorageProvider.s3` 指定的快照备份位置授予读取权限。如果快照备份和日志备份使用不同的存储桶或前缀,请分别添加策略语句。 + +以下示例展示了 `Backup` CR 以及只读 `Restore` CR 所需的最小 S3 权限。 + +#### `Backup` CR 的存储桶级策略 ```json { @@ -46,20 +58,104 @@ summary: 介绍如何授权访问远程存储从而实现 TiDB 集群的备份 } ``` -使用此策略时,请注意以下情况: +#### `Backup` CR 的前缀级策略 + +此策略将对象访问限制在备份前缀内,但允许列出存储桶中的所有对象键。BR 在开始备份前会检查 `backupmeta` 是否存在。如果 `s3:ListBucket` 权限带有 `s3:prefix` 条件,S3 可能对不存在的对象返回 `403 Forbidden`,导致备份失败。如果需要隐藏其他对象键,请使用专用的备份存储桶。 + +```json +{ + "Version": "2012-10-17", + "Statement": [ + { + "Sid": "ListBackupBucket", + "Effect": "Allow", + "Action": "s3:ListBucket", + "Resource": "arn:aws:s3:::" + }, + { + "Sid": "AccessBackupObjectsInPrefix", + "Effect": "Allow", + "Action": [ + "s3:GetObject", + "s3:PutObject", + "s3:DeleteObject", + "s3:AbortMultipartUpload" + ], + "Resource": "arn:aws:s3::://*" + } + ] +} +``` + +#### 只读 `Restore` CR 的存储桶级策略 + +```json +{ + "Version": "2012-10-17", + "Statement": [ + { + "Sid": "ListBackupBucket", + "Effect": "Allow", + "Action": "s3:ListBucket", + "Resource": "arn:aws:s3:::" + }, + { + "Sid": "ReadBackupObjects", + "Effect": "Allow", + "Action": "s3:GetObject", + "Resource": "arn:aws:s3:::/*" + } + ] +} +``` + +#### 只读 `Restore` CR 的前缀级策略 + +```json +{ + "Version": "2012-10-17", + "Statement": [ + { + "Sid": "ListBackupPrefix", + "Effect": "Allow", + "Action": "s3:ListBucket", + "Resource": "arn:aws:s3:::", + "Condition": { + "StringLike": { + "s3:prefix": "/*" + } + } + }, + { + "Sid": "ReadBackupObjectsInPrefix", + "Effect": "Allow", + "Action": "s3:GetObject", + "Resource": "arn:aws:s3::://*" + } + ] +} +``` + +使用这些策略时,请注意以下情况: - 如果 Backup 和 Restore 资源通过同一个 IAM 用户或角色访问不同的存储桶,请将每个所需存储桶的存储桶 ARN 和对象 ARN 添加到对应的 `Resource` 元素中。不要将其替换为允许访问无关存储桶的通配符。 - 对于 AWS 中国区域或 AWS GovCloud (US) 区域中的存储桶,请分别将两个 ARN 中的 `aws` 分区替换为 `aws-cn` 或 `aws-us-gov`。 - 如果 Backup 资源或将恢复元数据写入 S3 的 Restore 资源设置了 `.spec.s3.acl`,且没有其他策略授予相应权限,请将 `s3:PutObjectAcl` 添加到对象操作中。 - 如果存储桶的默认加密配置或 Backup/Restore 资源选择了客户托管 AWS KMS 密钥,请为 IAM 用户或角色授予写入备份对象或恢复元数据所用密钥的 `kms:GenerateDataKey` 和 `kms:Decrypt` 权限。对于只读恢复,请为加密备份对象的每个密钥授予 `kms:Decrypt` 权限;如果创建备份后存储桶的默认 KMS 密钥发生了变化,这些密钥也包括之前使用的旧密钥。还需确保每个 KMS 密钥的策略直接允许这些主体执行所需操作,或者允许通过 IAM 策略授予这些权限。 -恢复所需的权限取决于 BR 是否将恢复元数据写入 S3。如果 BR 将检查点元数据写入 S3,请保留 `s3:ListBucket`、`s3:GetObject`、`s3:PutObject` 和 `s3:DeleteObject` 权限。从 BR v8.5.5 开始,如果目标集群中不存在 `mysql.tidb_pitr_id_map` 且未指定 `--checkpoint-storage`,PITR 会将 ID 映射写入日志备份目录。如果该目录位于 S3,请保留 `s3:ListBucket`、`s3:GetObject` 和 `s3:PutObject` 权限。如果 BR 不会将恢复元数据写入 S3,可以将只读恢复的权限限制为 `s3:ListBucket` 和 `s3:GetObject`。有关更多信息,请参阅[检查点恢复](https://docs.pingcap.com/zh/tidb/stable/br-checkpoint-restore)和[备份存储](https://docs.pingcap.com/zh/tidb/stable/backup-and-restore-storages#鉴权)。 +对于启用检查点恢复的快照恢复,v8.4.0 之前的 BR 将检查点数据存储在备份位置。如果该位置位于 S3,仅授予只读恢复策略中的权限是不够的。从 BR v8.4.0 开始,快照恢复检查点默认存储在目标集群中。从 BR v8.5.5 开始,你可以使用 `--checkpoint-storage` 指定外部检查点存储。 + +恢复所需的权限取决于 BR 是否将恢复元数据写入 S3。如果 BR 将检查点元数据写入 S3,请为检查点存储位置授予 `s3:ListBucket`、`s3:GetObject`、`s3:PutObject` 和 `s3:DeleteObject` 权限。如果 `--checkpoint-storage` 指向不同的存储桶或前缀,请为该位置添加策略语句,而不是为备份位置添加写入权限。检查点存储桶的 `s3:ListBucket` 权限不应带有 `s3:prefix` 条件,以便检查不存在的检查点对象。 + +从 BR v8.5.5 开始,如果目标集群中不存在 `mysql.tidb_pitr_id_map` 且未指定 `--checkpoint-storage`,PITR 会将 ID 映射写入日志备份目录。如果该目录位于 S3,请为日志备份位置添加 `s3:PutObject` 权限,并使用不带 `s3:prefix` 条件的 `s3:ListBucket` 权限,以便检查 ID 映射文件是否存在。 + +如果同一个 IAM 用户或角色同时供 Backup 和 Restore 资源使用,你可以将所需操作合并到一个策略中。有关更多信息,请参阅[检查点恢复](https://docs.pingcap.com/zh/tidb/stable/br-checkpoint-restore)和[备份存储](https://docs.pingcap.com/zh/tidb/stable/backup-and-restore-storages#鉴权)。 ### 通过 AccessKey 和 SecretKey 授权 按照以下步骤配置 AccessKey 和 SecretKey,以授权访问 Amazon S3。对于 Ceph、MinIO 或其他兼容 S3 的存储服务,请从对应服务获取凭证,并通过该服务的访问控制机制授予存储桶权限。其他存储服务不一定支持 AWS IAM 用户和策略。 -1. 参考[在 AWS 账户中创建 IAM 用户](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_users_create.html),创建一个 IAM 用户,并附加[存储桶级 IAM 策略](#创建存储桶级-iam-策略)。 +1. 参考[在 AWS 账户中创建 IAM 用户](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_users_create.html),创建一个 IAM 用户,并附加[最小权限 IAM 策略](#创建最小权限-iam-策略)。 2. 参考[为自己创建访问密钥(控制台)](https://docs.aws.amazon.com/IAM/latest/UserGuide/access-key-self-managed.html#Using_CreateAccessKey),为 IAM 用户创建访问密钥 (Access Key)。完成后,你将获得 AccessKey 和 SecretKey。 @@ -157,7 +253,7 @@ summary: 介绍如何授权访问远程存储从而实现 TiDB 集群的备份 通过 IAM 绑定 Pod 授权的步骤如下: -1. 参考 [IAM 角色创建](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_create.html),为你的 AWS 账号创建一个 IAM 角色,并附加[存储桶级 IAM 策略](#创建存储桶级-iam-策略)。 +1. 参考 [IAM 角色创建](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_create.html),为你的 AWS 账号创建一个 IAM 角色,并附加[最小权限 IAM 策略](#创建最小权限-iam-策略)。 2. 通过 [Overlay](overlay.md) 方式为目标组件(TiKV 或 TiFlash)绑定 IAM 角色。以下示例展示如何为 TiKVGroup 添加绑定: @@ -185,7 +281,7 @@ summary: 介绍如何授权访问远程存储从而实现 TiDB 集群的备份 通过 IAM 绑定 ServiceAccount 授权的步骤如下: -1. 参考 [IAM 角色创建](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_create.html),为你的 AWS 账号创建一个 IAM 角色,并附加[存储桶级 IAM 策略](#创建存储桶级-iam-策略)。 +1. 参考 [IAM 角色创建](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_create.html),为你的 AWS 账号创建一个 IAM 角色,并附加[最小权限 IAM 策略](#创建最小权限-iam-策略)。 2. 参考[为集群创建 IAM OIDC 提供商](https://docs.aws.amazon.com/eks/latest/userguide/enable-iam-roles-for-service-accounts.html),为 EKS 集群创建一个 IAM OIDC 提供商。