如何要求用户使用 scp 设置其 role_session_name

发布于 2025-01-16 06:23:04 字数 947 浏览 0 评论 0原文

使用 scp,我希望在运行 terraform 模板时向在我的组织帐户中承担角色的用户要求 role_session_name。 role_session_name 值需要等于其 iam username。 我已在我的组织中附加了以下 scp

    {
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Deny",
      "Action": "sts:AssumeRole",
      "Resource": "*",
      "Condition": {
        "StringNotLike": {
          "sts:RoleSessionName": [
            "${aws:username}"
          ]
        }
      }
    }
  ]
}

~/.aws/config 文件内容下方,

[profile my_profile]
region = us-west-3
role_arn = arn:aws:iam:ACOUNT_ID:role/role_name
output = json

在 terraform 模板的 provider 部分下面

provider "aws" {
  shared_credentials = "~/.aws/credentials"
  region  = "eu-west-3"
  profile = "my_profile"
}

未指定 role_session_name = my_aws_user_name ` 在配置文件中,我能够运行模板而不会被 scp 阻止。

请问如何实现这一点?

谢谢

Using scp, I would like to require role_session_name to users who assume roles in my organization accounts when running terraform template. The role_session_name value need to be equals to their iam username.
I have attached below scp in my organization

    {
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Deny",
      "Action": "sts:AssumeRole",
      "Resource": "*",
      "Condition": {
        "StringNotLike": {
          "sts:RoleSessionName": [
            "${aws:username}"
          ]
        }
      }
    }
  ]
}

Below the ~/.aws/config file content

[profile my_profile]
region = us-west-3
role_arn = arn:aws:iam:ACOUNT_ID:role/role_name
output = json

below provider section of terraform template

provider "aws" {
  shared_credentials = "~/.aws/credentials"
  region  = "eu-west-3"
  profile = "my_profile"
}

Without specifying role_session_name = my_aws_user_name` inside the config file, I am able to run the template without being blocked by the scp.

How to achieve this please ?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

人生戏 2025-01-23 06:23:04

编辑

我终于设置了一个AWS组织来测试。您现在拥有的 SCP 运行良好。角色位于帐户 A 中。SCP 附加到帐户 B:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Deny",
      "Action": "sts:AssumeRole",
      "Resource": "*",
      "Condition": {
        "StringNotLike": {
          "sts:RoleSessionName": [
            "${aws:username}"
          ]
        }
      }
    }
  ]
}

使用帐户 B 中的用户,我尝试使用随机会话名称在帐户 A 中承担角色。访问被拒绝。

>aws sts assume-role --profile accountB --role-arn arn:aws:iam::<account-A>:role/<rolename> --role-session-name abc

An error occurred (AccessDenied) when calling the AssumeRole operation: User: arn:aws:iam::<account-B>:user/<username> is not authorized to perform: sts:AssumeRole on resource: arn:aws:iam::<account-A>:role/<rolename>

但是当我使用与我的用户名相同的会话名称时,我就可以了。

>aws sts assume-role --profile accountB --role-arn arn:aws:iam::<account-A>:role/<rolename> --role-session-name username

{
    "Credentials": {
        "AccessKeyId": "xxx",
        "SecretAccessKey": "xxx",
        "SessionToken": "xxx",
        "Expiration": "2022-03-23T10:31:52Z"
    },
    "AssumedRoleUser": {
        "AssumedRoleId": "xxx:username",
        "Arn": "arn:aws:sts::xxx:assumed-role/xxx/yyy"
    }
}

EDIT

I finally setup an AWS organization to test. The SCP as you now have is working fine. Role is in account A. SCP attached to account B:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Deny",
      "Action": "sts:AssumeRole",
      "Resource": "*",
      "Condition": {
        "StringNotLike": {
          "sts:RoleSessionName": [
            "${aws:username}"
          ]
        }
      }
    }
  ]
}

Using a user in account B, I tried to assume a Role in Account A using a random session name. Got access denied.

>aws sts assume-role --profile accountB --role-arn arn:aws:iam::<account-A>:role/<rolename> --role-session-name abc

An error occurred (AccessDenied) when calling the AssumeRole operation: User: arn:aws:iam::<account-B>:user/<username> is not authorized to perform: sts:AssumeRole on resource: arn:aws:iam::<account-A>:role/<rolename>

But when I use a session name that is same as my username, I am able to.

>aws sts assume-role --profile accountB --role-arn arn:aws:iam::<account-A>:role/<rolename> --role-session-name username

{
    "Credentials": {
        "AccessKeyId": "xxx",
        "SecretAccessKey": "xxx",
        "SessionToken": "xxx",
        "Expiration": "2022-03-23T10:31:52Z"
    },
    "AssumedRoleUser": {
        "AssumedRoleId": "xxx:username",
        "Arn": "arn:aws:sts::xxx:assumed-role/xxx/yyy"
    }
}
在你怀里撒娇 2025-01-23 06:23:04

调用 AssumeRole 操作时发生错误 (AccessDenied):用户:arn:aws:iam:::user/ 无权对资源执行:sts:AssumeRole:arn:aws:iam:::role/T
此问题是您的个人资料帐户没有其他帐户的权限附加策略,因此我授予 IAM 角色的完全访问权限,然后运行正常。

An error occurred (AccessDenied) when calling the AssumeRole operation: User: arn:aws:iam:::user/ is not authorized to perform: sts:AssumeRole on resource: arn:aws:iam:::role/T
this issue is your profile account doesnt has permission attach policy for other account, so I give the IAM full access of role then run fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文