用于使用AWS cliv2和SSO执行命令的包装器
aws2-wrap的Python项目详细描述
aws2包装
这是一个简单的脚本,它可以使AWS单点登录凭证更容易地与不理解AWS概要文件中的sso
条目的工具一起使用。在
该脚本提供以下功能:
- 使用检索到的AWS SSO凭据运行指定的命令。在
- 正在导出AWS SSO凭据。在
- 在AWS配置文件中支持
credential_process
- 支持在AWS概要文件中承担角色
请注意,该脚本名为aws2-wrap
,以表明它可以与AWS cliv2一起工作,即使CLI工具不再被称为aws2
。在
使用pip
安装
https://pypi.org/project/aws2-wrap
pip install aws2-wrap==1.1.2
使用AWS SSO凭据运行命令
aws2-wrap [--profile <awsprofilename>] [--exec] <command>
请注意,如果使用的是--exec
,并且<command>
包含空格,则必须用双引号将其括起来。在
您还可以通过AWS_profile指定要使用的概要文件,然后允许后续工具和命令使用相同的概要文件。在
示例:
aws2-wrap --profile MySSOProfile terraform plan
aws2-wrap --profile MySSOProfile --exec "terraform plan"
AWS_PROFILE=MySSOProfile aws2-wrap terraform plan
在$AWS_CONFIG_文件和$AWS_SHARED_CREDENTIALS_文件中生成临时配置文件
有一些实用程序可以更好地处理配置文件而不是环境变量。例如,如果您需要一次访问多个配置文件。在
aws2-wrap --generate --profile $AWS_PROFILE --credentialsfile $AWS_SHARED_CREDENTIALS_FILE --configfile $AWS_CONFIG_FILE --outprofile $DESTINATION_PROFILE
导出凭据
在某些情况下,设置适当的环境变量更容易/更好,以便任何aws
命令都可以重用它们。在
由于脚本无法在调用shell进程中直接设置环境变量,因此需要使用以下语法:
eval "$(aws2-wrap [--profile <awsprofilename>] --export)"
例如:
eval "$(aws2-wrap --profile MySSOProfile --export)"
通过AWS SSO担任角色
您的.aws/config
文件可以如下所示:
[default] sso_start_url = xxxxxxxxxxxx sso_region = us-west-2 sso_account_id = xxxxxxxxxxxx sso_role_name = SSORoleName [profile account1] role_arn = arn:aws:iam::xxxxxxxxxxxx:role/role-to-be-assumed source_profile = default region = ap-northeast-1
允许您运行:
aws2-wrap --profile account1 <command>
并且<command>
将在role-to-be-assumed
下运行。在
通过.aws/config使用凭据
如果您使用的工具可以处理普通的AWS凭据,但不了解新的AWS SSO凭据,那么另一个选项是向调用aws2-wrap
脚本的.aws/config
添加一个配置文件。在
例如,将以下块添加到.aws/config
:
然后,在身份验证之后,您可以通过指定“Wrapped”概要文件来运行任何使用AWS凭据的命令:
aws sso login --profile <awsprofilename> export AWS_PROFILE=Wrapped export AWS_SDK_LOAD_CONFIG=1 terraform plan
请注意,由于配置文件是通过AWS_PROFILE
指定的,所以有时有必要(如上所示)设置AWS_SDK_LOAD_CONFIG
,以便获得像{
学分
感谢@flyinprogrammer、@abeluck、@topu、@bigheel、@krabbit、@jscook2345、@hieki、@blazdivjak、@fukushun1994和@johann8384的贡献。在
- 项目
标签: