rigup/ephemeral-iam
GitHub: rigup/ephemeral-iam
一个用于在GCP环境中临时获取服务账号身份执行高特权操作的命令行工具,通过本地代理和短期OAuth令牌实现安全的权限提升。
Stars: 29 | Forks: 4
# ephemeral-iam
一个利用 service account token 生成功能的 CLI 工具,允许用户临时以 service account 身份验证 `gcloud` 命令。该工具的预期用例是限制用户在其 GCP organization 中默认获得的权限,同时仍允许他们完成需要提升权限的管理任务。
## 常见问题
#### 为什么不直接使用 `--impersonate-service-account`?
相比于 `--impersonate-service-account`,您选择使用 `ephemeral-iam` 有几个原因。以下是一些值得注意的点:
- 使用 `assume-privileges` 命令,您可以启动一个特权会话并以 service account 身份运行命令,而无需每次都提供 `--impersonate-service-account` 标志
- 使用 `ephemeral-iam`,您可以通过使用 `request_reason` 请求属性向审计日志添加字段来增强审计日志记录。例如,您可以配置一个警报,当生成 service account token 但未提供 `request_reason` 字段时触发。
- `ephemeral-iam` 强制执行会话时长限制,将用户每次模拟 service account 的时间限制为 10 分钟,之后需要生成新的 OAuth token。
- 该工具提供了一些 QoL(生活质量)功能,例如能够列出您可以模拟的 service account 以及查询您在 GCP 资源上的权限
- 当您运行 `gcloud container clusters get-credentials CLUSTER --impersonate-service-account SA_EMAIL` 时,会生成一个新的 `kubeconfig` 条目并持久保存在您的文件系统中。然而,`ephemeral-iam` 不会将特权 `kubeconfig` 条目持久化到文件系统中,以增加安全性。
## 概念概述
本节解释了运行 `eiam assume-privileges` 命令时发生的基本过程。
ephemeral-iam 使用 `projects.serviceAccounts.generateAccessToken` 方法为 service account 生成 OAuth 2.0 token,然后在随后的 API 调用中使用这些 token。当用户运行 `assume-privileges` 命令时,`eiam` 会调用该方法为指定的 service account 生成一个 10 分钟后过期的 OAuth 2.0 token。
如果 token 生成成功,`eiam` 随后会在用户的 localhost 上启动一个 HTTPS proxy。为了处理 HTTPS 流量,会为 proxy 生成一个自签名 TLS 证书并存储以备将来使用。
接下来,活动的 `gcloud` 配置会被更新,将所有 API 调用转发通过本地 proxy。
**更新后的配置字段示例:**
```
[core]
custom_ca_certs_file: [/path/to/eiam/config_dir/server.pem]
[proxy]
address: [127.0.0.1]
port: [8084]
type: [http]
```
在特权会话期间(直到 token 过期或用户手动停止它),所有使用 `gcloud` 发出的 API 调用都将被 proxy 拦截,proxy 会将 `Authorization` 标头替换为生成的 OAuth 2.0 token,以作为 service account 对请求进行授权。
对于 `kubectl` 命令,会生成一个临时的 `kubeconfig`,`KUBECONFIG` 环境变量被设置为该临时 `kubeconfig` 的路径,接着调用 `gcloud container clusters get-credentials` 生成一个带有 GCP Auth Provider 的 context,然后将 OAuth 2.0 token 写入该 context 的 token 缓存字段。有关为何采用此方式 的更多信息,请参阅 [Issue #49](https://github.com/rigup/ephemeral-iam/issues/49)。
会话结束后,`eiam` 会优雅地关闭 proxy 服务器,将用户的 `gcloud` 配置恢复到原始状态,并删除临时的 `kubeconfig`。
## 安装
关于如何安装 `eiam` 二进制文件的说明可以在 [INSTALL.md](docs/INSTALL.md) 中找到。
## 快速入门
### 帮助命令
根 `eiam` 调用及其每个子命令都有自己的帮助命令。这些命令可用于收集有关命令的更多信息,并探索接受的参数和标志。
顶层 `--help`
```
$ eiam --help
╭────────────────────────────────────────────────────────────╮
│ │
│ ephemeral-iam │
│ ────────────────────────────────────────────────────── │
│ A CLI tool for temporarily escalating GCP IAM privileges │
│ to perform high privilege tasks. │
│ │
│ https://github.com/rigup/ephemeral-iam │
│ │
╰────────────────────────────────────────────────────────────╯
╭────────────────────── Example usage ───────────────────────╮
│ │
│ Start privileged session │
│ ────────────────────────────────────────────────────── │
│ $ eiam assume-privileges \ │
│ -s example-svc@my-project.iam.gserviceaccount.com \ │
│ --reason "Emergency security patch (JIRA-1234)" │
│ │
│ │
│ │
│ Run gcloud command │
│ ────────────────────────────────────────────────────── │
│ $ eiam gcloud compute instances list --format=json \ │
│ -s example@my-project.iam.gserviceaccount.com \ │
│ -R "Reason" │
│ │
╰────────────────────────────────────────────────────────────╯
Please report any bugs or feature requests by opening a new
issue at https://github.com/rigup/ephemeral-iam/issues
Usage:
eiam [command]
Available Commands:
assume-privileges Configure gcloud to make API calls as the provided service account [alias: priv]
cloud_sql_proxy Run cloud_sql_proxy with the permissions of the specified service account
config Manage configuration values
default-service-accounts Configure default service accounts to use in other commands [alias: default-sa]
gcloud Run a gcloud command with the permissions of the specified service account
help Help about any command
kubectl Run a kubectl command with the permissions of the specified service account
list-service-accounts List service accounts that can be impersonated [alias: list]
plugins Manage ephemeral-iam plugins
query-permissions Query current permissions on a GCP resource
version Print the installed ephemeral-iam version
Flags:
-f, --format string Set the output of the current command (default "text")
-h, --help help for eiam
-y, --yes Assume 'yes' to all prompts
Use "eiam [command] --help" for more information about a command.
```
子命令 `--help`
```
$ eiam priv --help
The "assume-privileges" command fetches short-lived credentials for the provided service Account
and configures gcloud to proxy its traffic through an auth proxy. This auth proxy sets the
authorization header to the OAuth2 token generated for the provided service account. Once
the credentials have expired, the auth proxy is shut down and the gcloud config is restored.
The reason flag is used to add additional metadata to audit logs. The provided reason will
be in 'protoPayload.requestMetadata.requestAttributes.reason'.
Usage:
eiam assume-privileges [flags]
Aliases:
assume-privileges, priv
Examples:
eiam assume-privileges \
--service-account-email example@my-project.iam.gserviceaccount.com \
--reason "Emergency security patch (JIRA-1234)"
Flags:
-h, --help help for assume-privileges
-p, --project string The GCP project. Inherits from the active gcloud config by default (default "my-project")
-R, --reason string A detailed rationale for assuming higher permissions
-s, --service-account-email string The email address for the service account. Defaults to the configured default account for the current project
Global Flags:
-f, --format string Set the output of the current command (default "text")
-y, --yes Assume 'yes' to all prompts
```
### 教程
为了更好地熟悉 `ephemeral-iam` 及其工作原理,您可以遵循 [文档中提供的教程](docs/tutorial)。
标签:CLI 工具, DNS解析, EVTX分析, EVTX分析, gcloud, GCP, Go, Golang, Google Cloud, IAM, JSONLines, Ruby工具, 临时凭证, 协议分析, 安全编程, 开源项目, 日志审计, 服务账号, 权限提升, 模拟 impersonation, 特权访问管理, 身份与访问管理, 零信任