Aliyun 角色 - Assume Role

建议点击 查看原文 查看最新内容。

原文链接: https://typonotes.com/posts/2025/10/28/aliyun-assume-role/

通过使用 角色 (Assume Role) , 可以不再对 用户/账户 (RAM User) 授权。

而是 RAM User 使用命令 aliyun sts AssumeRole 获取对应角色的权限。 而 RAM User 只需要授权 STS AssumeRole 权限即可。

1. 使用 Assume Role

1.1. 直接配置 profile

这个方法可以直接登录, 并使用 profile 。 不用二次切换。

在使用 aliyun configure 的时候指定 --mode RamRoleArn

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
$ aliyun configure --profile demo-sts-profile-sts --mode RamRoleArn


Configuring profile 'demo-sts-profile-sts' in 'RamRoleArn' authenticate mode...
Access Key Id []:   xxxx
Access Key Secret []:  yyyy
Sts Region []: cn-hangzhou
Ram Role Arn []: zzzzzzzz
Role Session Name []: anything ## 临时ID
External ID []:   ## 配置 External ID
Expired Seconds [900]:
Default Region Id []:
Default Output Format [json]: json (Only support json)
Default Language [zh|en] en:
  1. 之后便可以使用 profile 进行权限管理了。
1
$ aliyun --profile demo-sts-profile-sts oss ls

1.2. 使用 RAM 登录, 并调用 sts 接口

https://help.aliyun.com/zh/ram/developer-reference/sts-cli-reference#c4aa2998b5xu3

  1. 使用 RAM user 登录

这里没有指定 mode, 实际使用默认方式 --mode AK

1
$ aliyun configure --profile demo-sts-user-test
  1. 使用 sts 命令切换角色权限
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
$ aliyun sts AssumeRole \
  --DurationSeconds 3600 \
  --RoleSessionName demo-with-ecs \
  --RoleArn 'acs:ram::00000000:role/demo-sts-role-test'


## output
{
	"AssumedRoleUser": {
		"Arn": "acs:ram::00000000:role/demo-sts-role-test/demo-demo",
		"AssumedRoleId": "30022222:demo-demo"
	},
	"Credentials": {
		"AccessKeyId": "STS.xxxx",
		"AccessKeySecret": "yyyyy",
		"Expiration": "2025-10-28T03:24:44Z",
		"SecurityToken": "zzzzz"
	},
	"RequestId": "58DD8F8C-38F1-589E-89A6-913A5051FE21"
}
  1. Credentials 中的 ID, Secret, Token 设置到环境变量中。
1
2
3
export ALIBABA_CLOUD_ACCESS_KEY_ID=STS.xxxxx
export ALIBABA_CLOUD_ACCESS_KEY_SECRET=yyyy
export ALIBABA_CLOUD_SECURITY_TOKEN=zzzzzz
  1. 使用 aliyun 命令

注意: aliyun cli (<= 3.10) 是 不支持 通过环境变量传递 region 的。 需要在命令上通过 --region cn-hangzhou 指定一个默认 region。

1
2
3
$ aliyun oss ls

$ aliyun --region cn-hangzhou oss ls

2. 使用 External ID

https://help.aliyun.com/zh/ram/use-cases/use-externalid-to-prevent-the-confused-deputy-problem?spm=a2c4g.11186623.0.0.521133afpQSo2Y

External ID 可以有效的防止 混淆代理人攻击

External ID 配置在 角色 / 信任策略

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
{
  "Statement": [
    {
      "Action": "sts:AssumeRole",
      "Effect": "Allow",
      "Principal": {
        "RAM": [
          "acs:ram::00000000:root"
        ]
      },
      "Condition": {
        "StringEquals": {
          "sts:ExternalId": "demo-sts-external-id-ab1234"
        }
      }
    }
  ],
  "Version": "1"
}

添加了 External ID 之后, 在调用 sts 接口时, 传入 External ID 参数即可。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
$ aliyun sts AssumeRole \
  --DurationSeconds 3600 \
  --RoleSessionName demo-with-external-id \
  --RoleArn 'acs:ram::00000000:role/demo-sts-role-test' \
  --ExternalId demo-sts-external-id-ab1234


{
	"AssumedRoleUser": {
		"Arn": "acs:ram::00000000:role/demo-sts-role-test/demo-with-external-id",
		"AssumedRoleId": "30022222:demo-with-external-id"
	},
	"Credentials": {
		"AccessKeyId": "STS.xxxx",
		"AccessKeySecret": "yyyy",
		"Expiration": "2025-10-28T06:33:16Z",
		"SecurityToken": "zzzzz"
	},
	"RequestId": "2C73D3EA-8D0F-56C7-9E46-65F324D7C61B"
}