![](/img/trans.png)
[英]How do I prompt for an MFA key to generate and use credentials for AWS CLI access?
[英]AWS CLI - How do I encrypt the credentials
使用 aws configure 时,凭据以明文形式存储在我的工作站上。 这是一个巨大的安全违规。 我尝试在 aws cli github 上打开一个问题,但它已被立即关闭。 我直接使用 Terraform 和 aws cli,因此需要一个变通方法来支持这一点。
例子:
[MyProfile]
aws_access_key_id = xxxxxxxxxxxxxxx
aws_secret_access_key = yyyyyyyyyyyyyyyyyy
region=us-east-2
output=json
不久前我遇到了这个链接,并认为它非常适合解释您可以尝试解决上述问题的所有不同选项。
这是我能找到的最简单的解决方法。 参考:
https://devblogs.microsoft.com/powershell/secretmanagement-and-secretstore-are-generally-available/
https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-sourcing-external.html
下面的 powershell 创建了一个加密的保管库。
#This will destroy existing AWS vault
#The Vault will be set accessible to the current User with no password.
#When AWS CLI invokes this there is no way to request a password.
Install-Module Microsoft.PowerShell.SecretManagement
Install-Module Microsoft.PowerShell.SecretStore
Set-SecretStoreConfiguration -Authentication None -Scope CurrentUser -Interaction None
Register-SecretVault -Name "AWS" -ModuleName Microsoft.PowerShell.SecretStore -DefaultVault -AllowClobber
Set-Secret -Vault "AWS" -Name "test" -Secret "test"
Get-SecretVault
Write-Host "Vault Created"
这个 powershell 可以创建秘密。 请注意,密钥可能会过期。
$profile = Read-Host -Prompt "Enter AWS Account Number"
$aws_access_key_id = Read-Host -Prompt "Enter AWS access key"
$aws_secret_access_key = Read-Host -Prompt "Enter AWS secret access key"
$secretIn = @{
Version=1;
AccessKeyId= $aws_access_key_id;
SecretAccessKey=$aws_secret_access_key;
SessionToken= $null; #"the AWS session token for temporary credentials";
#Expiration="ISO8601 timestamp when the credentials expire";
}
$secret = ConvertTo-Json -InputObject $secretIn
Set-Secret -Name $profile -Secret $secret
此名为credential_process.cmd 的文件需要位于路径上或 terrform.exe 旁边。
@echo off
REM This file needs to be accessible to the aws cli or programs using it.
REM To support other paths, copy it to C:\Program Files\Amazon\AWSCLIV2
Powershell.exe -Command "Get-Secret -Vault AWS -Name %1 -AsPlainText "
最后在您的 {user}.aws\\credentials 文件中放置以下条目:
[XXXXX-us-east-1]
credential_process = credential_process.cmd "XXXXX"
region=us-east-1
output=json
现在您可以使用以下命令运行 aws cli 命令(或 Terraform):
aws ec2 describe-vpcs --profile XXXXX-us-east-1
缺点:
像其他 AWS 一样:
可能性:
这是一个巨大的安全违规。 我尝试在 aws cli github 上打开一个问题,它被立即关闭。
在 AWS 上运行,您可以使用实例角色(对于 EC2、Lambda 或 ECS)。
在 AWS 之外运行没有更好的选择。 如果有人可以访问主目录,那么它就不再是您的计算机了。 但是 - 凭据也可以作为env 变量或 cli/api 参数传递。
这些可以加密和解密或在使用时请求,但您仍然需要访问解密密钥或服务。
您实际上可以使用aws-vault 之类的东西:它将机密存储在本地钥匙串中,并且基本上创建一个临时 shell,其中凭证作为 env 变量,或者您可以只执行特定命令而不创建整个 shell。
还有另一个类似的工具被保管在一个加密的文件中,并在你想使用它时创建一个临时的 shell 会话
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.