You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AWS IAM信任策略无法识别Cognito用户的Principal Tag问题求助

解决AWS IAM信任策略无法识别Cognito JWT中Principal Tag的问题

核心问题分析

问题根源在于两点:一是Cognito身份池默认不会自动将JWT中的principal_tags传递给STS角色的aws:PrincipalTag;二是当前信任策略的条件逻辑存在语法冲突,导致标签检测失效。

分步解决方案

1. 配置Cognito身份池的标签传播

Cognito不会自动转发JWT里的https://aws.amazon.com/tags声明到STS角色,必须手动配置角色映射:

  • 登录AWS控制台 → 进入Cognito → 目标身份池 → 编辑身份池
  • 找到身份验证提供者下对应的用户池/身份源,进入角色映射设置
  • 开启「使用自定义角色映射」,添加映射规则:
    • 来源:选择https://aws.amazon.com/tags声明中的principal_tags.attribute
    • 目标:设置为PrincipalTag的attribute键
  • 保存配置后,Cognito才会将该标签传递给STS角色

2. 修正信任策略的条件逻辑

你当前的策略把aws:PrincipalTag/attribute和cognito-identity.amazonaws.com:amr放在同一个ForAnyValue:StringLike块中,这会导致逻辑判断异常。修改为分开的条件块:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "Federated": "cognito-identity.amazonaws.com"
            },
            "Action": [
                "sts:AssumeRoleWithWebIdentity",
                "sts:TagSession"
            ],
            "Condition": {
                "StringEquals": {
                    "cognito-identity.amazonaws.com:aud": "<POOL ID>"
                },
                "StringLike": {
                    "aws:PrincipalTag/attribute": "*"
                },
                "ForAnyValue:StringLike": {
                    "cognito-identity.amazonaws.com:amr": "authenticated"
                }
            }
        }
    ]
}
  • cognito-identity.amazonaws.com:amr是数组类型,用ForAnyValue:StringLike匹配其中的authenticated项
  • aws:PrincipalTag/attribute是单个字符串值(STS会自动处理JWT中的数组为单个值或逗号分隔字符串),用StringLike即可

3. 用CloudTrail验证标签传递状态

开启CloudTrail后,搜索AssumeRoleWithWebIdentity操作,查看请求日志中的PrincipalTags字段:

  • 如果该字段不存在attribute键,说明Cognito身份池的映射配置未生效,需重新检查步骤1
  • 如果存在,则说明标签已传递,问题出在信任策略的条件语法,按步骤2调整即可

4. 最小化测试验证

先移除aws:PrincipalTag/attribute的条件,确认角色能正常被Assume;再逐步添加条件,每一步测试授权是否正常,以此定位具体问题点。

内容的提问来源于stack exchange,提问作者Johan Wieffer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 15:07:18