gpt4 book ai didi

amazon-web-services - Terraform 无法承担启用 MFA 的角色

转载 作者:行者123 更新时间:2023-12-04 08:18:07 27 4
gpt4 key购买 nike

我很难让 Terraform 承担另一个需要 MFA 的帐户的 IAM 角色。这是我的设置

AWS 配置

[default]
region = us-west-2
output = json

[profile GEHC-000]
region = us-west-2
output = json

....

[profile GEHC-056]
source_profile = GEHC-000
role_arn = arn:aws:iam::~069:role/hc/hc-master
mfa_serial = arn:aws:iam::~183:mfa/username
external_id = ~069

AWS 凭证
[default]
aws_access_key_id = xxx
aws_secret_access_key = xxx


[GEHC-000]
aws_access_key_id = same as above
aws_secret_access_key = same as above

分配给 IAM 用户的策略

STS 政策
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AssumeRole",
"Effect": "Allow",
"Action": [
"sts:AssumeRole"
],
"Resource": [
"arn:aws:iam::*:role/hc/hc-master"
]
}
]
}

用户政策
{
"Statement": [
{
"Action": [
"iam:*AccessKey*",
"iam:*MFA*",
"iam:*SigningCertificate*",
"iam:UpdateLoginProfile*",
"iam:RemoveUserFromGroup*"
],
"Effect": "Allow",
"Resource": [
"arn:aws:iam::~183:mfa/${aws:username}",
"arn:aws:iam::~183:mfa/*/${aws:username}",
"arn:aws:iam::~183:mfa/*/*/${aws:username}",
"arn:aws:iam::~183:mfa/*/*/*${aws:username}",
"arn:aws:iam::~183:user/${aws:username}",
"arn:aws:iam::~183:user/*/${aws:username}",
"arn:aws:iam::~183:user/*/*/${aws:username}",
"arn:aws:iam::~183:user/*/*/*${aws:username}"
],
"Sid": "Write"
},
{
"Action": [
"iam:*Get*",
"iam:*List*"
],
"Effect": "Allow",
"Resource": [
"*"
],
"Sid": "Read"
},
{
"Action": [
"iam:CreateUser*",
"iam:UpdateUser*",
"iam:AddUserToGroup"
],
"Effect": "Allow",
"Resource": [
"*"
],
"Sid": "CreateUser"
}
],
"Version": "2012-10-17"
}

强制 MFA 政策
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "BlockAnyAccessOtherThanAboveUnlessSignedInWithMFA",
"Effect": "Deny",
"NotAction": "iam:*",
"Resource": "*",
"Condition": {
"BoolIfExists": {
"aws:MultiFactorAuthPresent": "false"
}
}
}
]
}
main.tf
provider "aws" {
profile = "GEHC-056"
shared_credentials_file = "${pathexpand("~/.aws/config")}"
region = "${var.region}"
}

data "aws_iam_policy_document" "test" {
statement {
sid = "TestAssumeRole"
effect = "Allow"

actions = [
"sts:AssumeRole",
]

principals = {
type = "AWS"

identifiers = [
"arn:aws:iam::~183:role/hc-devops",
]
}

sid = "BuUserTrustDocument"
effect = "Allow"

principals = {
type = "Federated"

identifiers = [
"arn:aws:iam::~875:saml-provider/ge-saml-for-aws",
]
}

condition {
test = "StringEquals"
variable = "SAML:aud"
values = ["https://signin.aws.amazon.com/saml"]
}
}
}

resource "aws_iam_role" "test_role" {
name = "test_role"
path = "/"
assume_role_policy = "${data.aws_iam_policy_document.test.json}"
}

获取来电者身份
bash-4.4$ aws --profile GEHC-056 sts get-caller-identity
Enter MFA code for arn:aws:iam::772660252183:mfa/503072343:
{
"UserId": "AROAIWCCLC2BGRPQMJC7U:botocore-session-1537474244",
"Account": "730993910069",
"Arn": "arn:aws:sts::730993910069:assumed-role/hc-master/botocore-session-1537474244"
}

和错误:
bash-4.4$ terraform plan
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.


Error: Error refreshing state: 1 error(s) occurred:

* provider.aws: Error creating AWS session: AssumeRoleTokenProviderNotSetError: assume role with MFA enabled, but AssumeRoleTokenProvider session option not set.

最佳答案

另一种方法是使用 credential_process 为了使用本地脚本生成凭据并将 token 缓存在新配置文件中(我们称之为 tf_temp )
该脚本将:

  • 检查 token 对于配置文件是否仍然有效 tf_temp
  • 如果 token 有效,则使用 aws configure get xxx --profile tf_temp 从现有配置中提取 token
  • 如果token无效,提示使用输入mfa token
  • 使用 aws assume-role --token-code xxxx ... --profile your_profile 生成 session token
  • 设置临时配置文件 token tf_temp使用 aws configure set xxx --profile tf_temp

  • 你将会拥有:
    ~/.aws/凭证
    [prod]
    aws_secret_access_key = redacted
    aws_access_key_id = redacted

    [tf_temp]

    [tf]
    credential_process = sh -c 'mfa.sh arn:aws:iam::{account_id}:role/{role} arn:aws:iam::{account_id}:mfa/{mfa_entry} prod 2> $(tty)'
    mfa.sh
    gist
    将此脚本移入 /bin/mfa.sh/usr/local/bin/mfa.sh :
    #!/bin/sh
    set -e

    role=$1
    mfa_arn=$2
    profile=$3
    temp_profile=tf_temp

    if [ -z $role ]; then echo "no role specified"; exit 1; fi
    if [ -z $mfa_arn ]; then echo "no mfa arn specified"; exit 1; fi
    if [ -z $profile ]; then echo "no profile specified"; exit 1; fi

    resp=$(aws sts get-caller-identity --profile $temp_profile | jq '.UserId')

    if [ ! -z $resp ]; then
    echo '{
    "Version": 1,
    "AccessKeyId": "'"$(aws configure get aws_access_key_id --profile $temp_profile)"'",
    "SecretAccessKey": "'"$(aws configure get aws_secret_access_key --profile $temp_profile)"'",
    "SessionToken": "'"$(aws configure get aws_session_token --profile $temp_profile)"'",
    "Expiration": "'"$(aws configure get expiration --profile $temp_profile)"'"
    }'
    exit 0
    fi
    read -p "Enter MFA token: " mfa_token

    if [ -z $mfa_token ]; then echo "MFA token can't be empty"; exit 1; fi

    data=$(aws sts assume-role --role-arn $role \
    --profile $profile \
    --role-session-name "$(tr -dc A-Za-z0-9 </dev/urandom | head -c 20)" \
    --serial-number $mfa_arn \
    --token-code $mfa_token | jq '.Credentials')

    aws_access_key_id=$(echo $data | jq -r '.AccessKeyId')
    aws_secret_access_key=$(echo $data | jq -r '.SecretAccessKey')
    aws_session_token=$(echo $data | jq -r '.SessionToken')
    expiration=$(echo $data | jq -r '.Expiration')

    aws configure set aws_access_key_id $aws_access_key_id --profile $temp_profile
    aws configure set aws_secret_access_key $aws_secret_access_key --profile $temp_profile
    aws configure set aws_session_token $aws_session_token --profile $temp_profile
    aws configure set expiration $expiration --profile $temp_profile

    echo '{
    "Version": 1,
    "AccessKeyId": "'"$aws_access_key_id"'",
    "SecretAccessKey": "'"$aws_secret_access_key"'",
    "SessionToken": "'"$aws_session_token"'",
    "Expiration": "'"$expiration"'"
    }'
    使用 tf提供者设置中的个人资料。第一次,系统会提示您 mfa token :
    # terraform apply
    Enter MFA token: 428313
    此解决方案适用于 terraform 和/或 terragrunt

    关于amazon-web-services - Terraform 无法承担启用 MFA 的角色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52432717/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com