ByteNess/aws-vault
GitHub: ByteNess/aws-vault
一款将 AWS 凭证安全存储于操作系统密钥库并通过 STS 生成临时凭证的开发辅助工具,有效避免长期凭证明文落盘的风险。
Stars: 286 | Forks: 17
# AWS Vault
[](https://github.com/byteness/aws-vault/releases)
[](https://github.com/byteness/aws-vault/actions)
AWS Vault 是一个用于在开发环境中安全存储和访问 AWS 凭证的工具。
AWS Vault 将 IAM 凭证存储在操作系统的安全密钥库中,然后从中生成临时凭证以暴露给您的 shell 和应用程序。它旨在与 AWS CLI 工具互补,并且能够识别您的 [`~/.aws/config` 中的配置文件和配置](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html#cli-config-files)。
查看[公告博客文章](https://99designs.com.au/tech-blog/blog/2015/10/26/aws-vault/)了解更多详情。
## 安装
您可以通过以下方式安装 AWS Vault:
- 下载[最新版本](https://github.com/byteness/aws-vault/releases/latest)
- 使用 [Homebrew](https://formulae.brew.sh/formula/aws-vault):`brew install aws-vault`
- 在 Windows 上使用 [Chocolatey](https://chocolatey.org/packages/aws-vault):`choco install aws-vault`([仓库](https://github.com/gusztavvargadr/aws-vault-chocolatey) 由 [Gusztáv Varga](https://github.com/gusztavvargadr) 提供)
- 在 [NixOS](https://search.nixos.org/packages?channel=unstable&query=aws-vault) 上(目前仅在 unstable 频道中可用):`nix-env -iA nixos.aws-vault`
## 文档
配置、用法、技巧和窍门可在 [USAGE.md](./USAGE.md) 文件中找到。
## Vaulting 后端
支持的 vaulting 后端包括:
* [macOS Keychain](https://support.apple.com/en-au/guide/keychain-access/welcome/mac)
* [Windows Credential Manager](https://support.microsoft.com/en-au/help/4026814/windows-accessing-credential-manager)
* Secret Service ([Gnome Keyring](https://wiki.gnome.org/Projects/GnomeKeyring)、[KWallet](https://kde.org/applications/system/org.kde.kwalletmanager5))
* [KWallet](https://kde.org/applications/system/org.kde.kwalletmanager5)
* [Pass](https://www.passwordstore.org/)
* [Passage](https://github.com/FiloSottile/passage)
* 加密文件
* [1Password Connect](https://developer.1password.com/docs/connect/)
* [1Password Service Accounts](https://developer.1password.com/docs/service-accounts)
* [1Password Desktop App](https://developer.1password.com/docs/sdks/desktop-app-integrations/)
使用 `--backend` 标志或 `AWS_VAULT_BACKEND` 环境变量进行指定。
## 快速入门
```
# 为 "jonsmith" profile 存储 AWS credentials
$ aws-vault add jonsmith
Enter Access Key Id: ABDCDEFDASDASF
Enter Secret Key: ****************************************
Enter MFA Device ARN (If MFA is not enabled, leave this blank): arn:aws:iam::123456789012:mfa/jonsmith
Added credentials to profile "jonsmith" in vault
# 执行命令 (使用临时 credentials)
$ aws-vault exec jonsmith -- aws s3 ls
bucket_1
bucket_2
# 打开浏览器窗口并登录到 AWS Console
$ aws-vault login jonsmith
# 列出 credentials
$ aws-vault list
Profile Credentials Sessions
======= =========== ========
jonsmith jonsmith -
# 使用临时 credentials 启动 subshell
$ aws-vault exec jonsmith
Starting subshell /bin/zsh, use `exit` to exit the subshell
$ aws s3 ls
bucket_1
bucket_2
```
## 工作原理
`aws-vault` 使用 Amazon 的 STS 服务通过 `GetSessionToken` 或 `AssumeRole` API 调用来生成[临时凭证](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_temp.html)。这些凭证会在短时间内过期,因此降低了凭证泄露的风险。
然后 AWS Vault 通过以下两种方式之一将临时凭证暴露给子进程:
1. **环境变量** 被写入子进程。请注意以下示例中 AWS 凭证是如何输出的:
$ aws-vault exec jonsmith -- env | grep AWS
AWS_VAULT=jonsmith
AWS_DEFAULT_REGION=us-east-1
AWS_REGION=us-east-1
AWS_ACCESS_KEY_ID=%%%
AWS_SECRET_ACCESS_KEY=%%%
AWS_SESSION_TOKEN=%%%
AWS_CREDENTIAL_EXPIRATION=2020-04-16T11:16:27Z
2. **本地元数据服务器** 被启动。这种方法的优点是,任何使用 Amazon SDK 的程序都会根据需要自动刷新凭证,因此会话时间可以尽可能短。
$ aws-vault exec --server jonsmith -- env | grep AWS
AWS_VAULT=jonsmith
AWS_DEFAULT_REGION=us-east-1
AWS_REGION=us-east-1
AWS_CONTAINER_CREDENTIALS_FULL_URI=%%%
AWS_CONTAINER_AUTHORIZATION_TOKEN=%%%
默认情况下使用环境变量,但您可以通过在 `exec` 命令中使用 `--server` 标志来选择使用本地实例元数据服务器。
## 角色和 MFA
[最佳实践](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#delegate-using-roles)是[创建角色以委派权限](https://docs.aws.amazon.com/cli/latest/userguide/cli-roles.html)。为了安全起见,您还应该要求用户提供由多因素身份验证 (MFA) 设备生成的一次性密钥。
首先,您需要在 IAM 中创建用户和角色,以及[设置 MFA 设备](https://docs.aws.amazon.com/IAM/latest/UserGuide/GenerateMFAConfigAccount.html)。然后您可以[设置 IAM 角色以强制执行 MFA](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-role.html#cli-configure-role-mfa)。
这是一个使用角色和 MFA 的配置示例:
```
[default]
region = us-east-1
[profile jonsmith]
mfa_serial = arn:aws:iam::111111111111:mfa/jonsmith
[profile foo-readonly]
source_profile = jonsmith
role_arn = arn:aws:iam::22222222222:role/ReadOnly
[profile foo-admin]
source_profile = jonsmith
role_arn = arn:aws:iam::22222222222:role/Administrator
mfa_serial = arn:aws:iam::111111111111:mfa/jonsmith
[profile bar-role1]
source_profile = jonsmith
role_arn = arn:aws:iam::333333333333:role/Role1
mfa_serial = arn:aws:iam::111111111111:mfa/jonsmith
[profile bar-role2]
source_profile = bar-role1
role_arn = arn:aws:iam::333333333333:role/Role2
mfa_serial = arn:aws:iam::111111111111:mfa/jonsmith
```
以下是您对 aws-vault 的预期行为
| 命令 | 凭证 | 已缓存 | MFA |
|------------------------------------------|-----------------------------|---------------|-----|
| `aws-vault exec jonsmith --no-session` | 长期凭证 | 否 | 否 |
| `aws-vault exec jonsmith` | session-token | session-token | 是 |
| `aws-vault exec foo-readonly` | role | 否 | 否 |
| `aws-vault exec foo-admin` | session-token + role | session-token | 是 |
| `aws-vault exec foo-admin --duration=2h` | role | role | 是 |
| `aws-vault exec bar-role2` | session-token + role + role | session-token | 是 |
| `aws-vault exec bar-role2 --no-session` | role + role | role | 是 |
## 自动登出
自 v7.3+ 起,`aws-vault` 引入了在执行 `aws-vault login ` 登录之前自动尝试先登出的选项。
此行为可以通过使用 `--auto-logout` 或 `-a` 标志来实现!请在 [USAGE.md](./USAGE.md) 文件中阅读更多内容。
## 开发
[macOS 发布版本构建](https://github.com/byteness/aws-vault/releases)经过代码签名,以避免在 Keychain 中出现额外的提示。您可以使用以下命令进行验证:
```
$ codesign --verify --verbose $(which aws-vault)
```
如果您正在自己开发或编译 aws-vault 二进制文件,您可以通过访问 Keychain Access > Certificate Assistant > Create Certificate -> Certificate Type: Code Signing 来[生成自签名证书](https://support.apple.com/en-au/guide/keychain-access/kyca8916/mac)。然后您可以使用以下命令对您的二进制文件进行签名:
```
$ go build .
$ codesign --sign ./aws-vault
```
## 维护者
* [Marko Bevc](https://github.com/mbevc1)
* 完整[贡献者列表](https://github.com/byteness/aws-vault/graphs/contributors)
## 参考和灵感来源
* https://github.com/pda/aws-keychain
* https://docs.aws.amazon.com/IAM/latest/UserGuide/MFAProtectedAPI.html
* https://docs.aws.amazon.com/IAM/latest/UserGuide/IAMBestPractices.html#create-iam-users
* https://github.com/makethunder/awsudo
* https://github.com/AdRoll/hologram
* https://github.com/realestate-com-au/credulous
* https://github.com/dump247/aws-mock-metadata
* https://boto.readthedocs.org/en/latest/boto_config_tut.html
标签:AWS, Chocolatey, DPI, EVTX分析, Homebrew, IAM, Linux Secret Service, MacOS Keychain, NixOS, StruQ, Windows Credential Manager, 临时凭证, 代码分析, 凭证保管库, 凭证管理, 安全存储, 开发环境, 操作系统检测, 日志审计, 身份与访问管理