繁体   English   中英

AWS CLI - 如何加密凭证

[英]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://ben11kehoe.medium.com/never-put-aws-temporary-credentials-in-env-vars-or-credentials-files-theres-a-better-way-25ec45b4d73e

这是我能找到的最简单的解决方法。 参考:

https://devblogs.microsoft.com/powershell/secretmanagement-and-secretstore-are-generally-available/

https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-sourcing-external.html

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.secretmanagement/?view=ps-modules

下面的 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 configure 语句并以明文形式存储凭据。
  • 无法强制管理员使用此方法。

像其他 AWS 一样:

  • 它不必要的复杂性。
  • 文档非常详细,但不知何故总是缺少重要信息。
  • 一切都是黑客工作。

可能性:

  • 可以创建一个用户(User1),该用户只能访问秘密管理器中的某个秘密(User2 凭证)。
  • User1 凭据存储在本地 Vault 中。
  • User1 将在调用 credential_process.cmd 期间从 Secret Manager 获取要使用的 User2 凭据
  • Person 永远不会直接获得 User2 凭据。
  • 这将迫使用户使用上述方法。
  • 但是,这个的实现应该在aws configure中,而不是一起hack。 这将允许其他依赖工具在配置完成后才能工作。

这是一个巨大的安全违规。 我尝试在 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM