barbaria888/GoogleCloudIAMPlaybook
GitHub: barbaria888/GoogleCloudIAMPlaybook
一份 Google Cloud IAM 自定义角色管理的实操指南,涵盖基于 YAML 与 gcloud CLI 的角色生命周期操作及实验性行为发现。
Stars: 0 | Forks: 0
#
Google Cloud IAM Custom Roles 指南
欢迎使用 **IAM Custom Roles** 仓库。本指南将引导您了解如何使用 Google Cloud SDK (`gcloud`) 和 YAML 定义来创建、更新、停用和删除 Custom Cloud IAM 角色。
自定义角色允许您贯彻**最小权限原则**,确保组织中的用户和服务账号仅拥有完成其任务所需的精确权限。
## 🏗️ 仓库结构
所有 YAML 配置文件都组织在 `roles/` 文件夹中:
- 📁 **`roles/`**
- [`role-definition.yaml`](file:///d:/HP/Documents/coursera/GCP/GKE/MultiClusterGKE/custom-iam/roles/role-definition.yaml) - Custom Role Editor 的初始定义。
- [`new-role-definition.yaml`](file:///d:/HP/Documents/coursera/GCP/GKE/MultiClusterGKE/custom-iam/roles/new-role-definition.yaml) - Custom Role Editor 的更新定义(包含 Storage 权限)。
- [`viewer.yaml`](file:///d:/HP/Documents/coursera/GCP/GKE/MultiClusterGKE/custom-iam/roles/viewer.yaml) - 用于更新/恢复 Custom Role Viewer 的 YAML 定义。
- 📁 **`images/`**** - 展示终端命令和控制台状态的屏幕截图。
## 🔑 核心概念
### 所需的管理员角色
要管理自定义角色,您必须拥有 `iam.roles.create` 权限,该权限通常通过以下角色授予:
* **Organization Role Administrator** (`roles/iam.organizationRoleAdmin`)
* **IAM Role Administrator** (`roles/iam.roleAdmin`)
## 🚀 分步演练
### 第 1 步:查询 IAM 环境
在创建自定义角色之前,请检查有哪些可用的权限和角色:
1. **列出项目上所有可测试的权限**:
gcloud iam list-testable-permissions //cloudresourcemanager.googleapis.com/projects/$DEVSHELL_PROJECT_ID
2. **描述预定义角色**以查看其元数据和包含的权限:
gcloud iam roles describe roles/viewer
3. **列出资源上可授予的角色**:
gcloud iam list-grantable-roles //cloudresourcemanager.googleapis.com/projects/$DEVSHELL_PROJECT_ID
### 第 2 步:创建自定义角色
您可以使用 **YAML 定义文件**或 **CLI 参数**来创建自定义角色。
#### 方法 A:使用 YAML 文件
在类似 [`role-definition.yaml`](file:///d:/HP/Documents/coursera/GCP/GKE/MultiClusterGKE/custom-iam/roles/role-definition.yaml) 的文件中定义角色:
```
title: "Role Editor"
description: "Edit access for App Versions"
stage: "ALPHA"
includedPermissions:
- appengine.versions.create
- appengine.versions.delete
```
应用该定义:
```
gcloud iam roles create editor --project $DEVSHELL_PROJECT_ID --file roles/role-definition.yaml
```
#### 方法 B:使用 CLI 参数
直接使用参数创建角色:
```
gcloud iam roles create viewer --project $DEVSHELL_PROJECT_ID \
--title "Role Viewer" \
--description "Custom role description." \
--permissions compute.instances.get,compute.instances.list \
--stage ALPHA
```
### 第 3 步:列出自定义角色
要查看在您的项目中创建的自定义角色:
```
gcloud iam roles list --project $DEVSHELL_PROJECT_ID
```

### 第 4 步:更新自定义角色(使用 Etags 进行并发控制)
#### 方法 A:使用 YAML 文件
1. 获取最新的角色元数据(包含当前的 `etag`):
gcloud iam roles describe editor --project $DEVSHELL_PROJECT_ID
2. 将描述保存到 [`new-role-definition.yaml`](file:///d:/HP/Documents/coursera/GCP/GKE/MultiClusterGKE/custom-iam/roles/new-role-definition.yaml) 中并添加新权限(例如 `storage.buckets.get` 和 `storage.buckets.list`):
description: Edit access for App Versions
etag: BwVxIAbRq_I=
includedPermissions:
- appengine.versions.create
- appengine.versions.delete
- storage.buckets.get
- storage.buckets.list
name: projects/qwiklabs-gcp-04-d6ba91c0de6b/roles/editor
stage: ALPHA
title: Role Editor
3. 更新角色:
gcloud iam roles update editor --project $DEVSHELL_PROJECT_ID --file roles/new-role-definition.yaml
#### 方法 B:使用 CLI 参数
内联添加或移除权限:
```
gcloud iam roles update viewer --project $DEVSHELL_PROJECT_ID \
--add-permissions storage.buckets.get,storage.buckets.list
```

### 第 5 步:停用和删除角色
* **停用角色**(临时停用所有绑定到该角色的策略):
gcloud iam roles update viewer --project $DEVSHELL_PROJECT_ID --stage DISABLED
* **删除角色**(停用角色并标记在 37 天后永久删除):
gcloud iam roles delete viewer --project $DEVSHELL_PROJECT_ID
## 🔍 观察到的行为:通过 YAML 更新恢复
### 1. 意外发现
在 Cloud IAM 中删除自定义角色时,它会进入软删除/停用状态。官方文档记录的恢复方法是:
```
gcloud iam roles undelete viewer --project $DEVSHELL_PROJECT_ID
```
然而,在测试过程中,我们在尝试恢复工作流时观察到了一种替代行为:
1. 角色 `viewer` 被删除,其阶段转换为 `DISABLED`。
2. 准备了一个配置文件 [`viewer.yaml`](file:///d:/HP/Documents/coursera/GCP/GKE/MultiClusterGKE/custom-iam/roles/viewer.yaml),将发布阶段设置为 `GA` 并包含匹配的 `etag`。
3. 没有先运行 `undelete`,而是直接通过以下命令更新了角色:
gcloud iam roles update viewer --project $DEVSHELL_PROJECT_ID --file roles/viewer.yaml
*此更新成功,并将角色的发布阶段转回 `GA`。*
4. 随后执行官方的 undelete 命令时:
gcloud iam roles undelete viewer --project $DEVSHELL_PROJECT_ID
抛出了前置条件错误:
ERROR: (gcloud.iam.roles.undelete) FAILED_PRECONDITION: A role that is not deleted cannot be undeleted

### 2. 分析与验证
为了验证这一发现,我们比较了运行 `update` 命令前后 `gcloud iam roles describe` 的输出。
#### 在 YAML 更新之前(已删除状态)
```
description: Custom role description.
etag: BwZU0S4fPbc=
includedPermissions:
- compute.instances.get
- compute.instances.list
- storage.buckets.get
- storage.buckets.list
name: projects/qwiklabs-gcp-04-d6ba91c0de6b/roles/viewer
stage: DISABLED
title: Role Viewer
```
#### 在 YAML 更新之后(已恢复状态)
```
description: Custom role description.
etag: BwZU0UEBVOg=
includedPermissions:
- compute.instances.get
- compute.instances.list
- storage.buckets.get
- storage.buckets.list
name: projects/qwiklabs-gcp-04-d6ba91c0de6b/roles/viewer
stage: GA
title: Role Viewer
```
如输出所示,YAML 更新成功地将发布阶段从 `DISABLED` 转换回 `GA`,并生成了一个新的 `etag` (`BwZU0UEBVOg=`),从而有效地重新激活了该角色。由于角色现在处于活动状态,随后的 `undelete` 调用返回了 `FAILED_PRECONDITION` 错误。

### 3. 结论与最佳实践
Google Cloud IAM Custom Roles 指南 标签:GCP, IAM, 云计算, 最佳实践, 权限管理, 模型越狱, 规则引擎, 运维