我正在使用 AWS Cognito 作为用户 AuthN 和 AuthZ。
roleA 附加到身份池,条件为 amr=authenticated(经过身份验证的用户)。 roleA还包含s3:ListAllMyBuckets的权限策略。
角色A - 信任关系:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Federated": "cognito-identity.amazonaws.com"
},
"Action": "sts:AssumeRoleWithWebIdentity",
"Condition": {
"StringEquals": {
"cognito-identity.amazonaws.com:aud": "eu-central-1:XXXX" --> identityPoolId
},
"ForAllValues:StringLike": {
"cognito-identity.amazonaws.com:groups": "Admin"
},
"ForAnyValue:StringLike": {
"cognito-identity.amazonaws.com:amr": "authenticated"
}
}
}
]
}
角色A - 权限策略:
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "s3:ListAllMyBuckets",
"Resource": "*",
"Effect": "Allow"
}
]
}
roleB 附加到用户池中的“Admin”组。 roleB 不包含 s3:ListAllMyBuckets 的此权限。
角色B - 信任关系:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Federated": "cognito-identity.amazonaws.com"
},
"Action": "sts:AssumeRoleWithWebIdentity",
"Condition": {
"StringEquals": {
"cognito-identity.amazonaws.com:aud": "eu-central-1:XXXX" --> identityPoolId
},
"ForAnyValue:StringLike": {
"cognito-identity.amazonaws.com:amr": "authenticated"
}
}
}
]
}
组“Admin”中的 userX 通过用户池进行身份验证,然后使用 GetCredentialsForIdentityCommand 将 jwtIdToken 与凭据交换。 身份池颁发的凭证不允许 userX 列出 S3 中的存储桶。
我进行了更改,将 s3:ListAllMyBuckets 的权限策略附加到角色 B,并从角色 A 中删除此权限。 “Admin”组中的同一 userX 通过用户池进行身份验证,然后使用GetCredentialsForIdentityCommand 交换 jwtIdToken 与凭证。 身份池颁发的凭证允许 userX 列出 S3 中的存储桶。
您能否帮助我理解这种行为以及 Cognito 是否将凭据分配给该角色?身份池颁发的凭证似乎仅与角色B绑定,我该如何控制?
GetCredentialsForIdentityCommand是否返回附加到身份池或用户池中的组(管理员)的角色的凭据?
以下是将 jwtIdToken 与凭证交换的代码:
const identityId = (
await client.send(
new GetIdCommand({
IdentityPoolId: identityPoolId,
Logins: {
[cognitoIdentityPool]: jwtIdToken,
},
})
)
).IdentityId;
console.log('identityId', identityId);
const credentials = (
await client.send(
new GetCredentialsForIdentityCommand({
IdentityId: identityId,
Logins: {
[cognitoIdentityPool]: jwtIdToken,
},
})
)
).Credentials;
roleMappings 是一个可选字段。 如果存在,Cognito Identity 会优先考虑此处的设置,而不是角色字段定义的设置。 在上面的代码中,它被称为 AWS 控制台中的“在令牌中选择具有 Preferred_role 声明的角色”。
ambiguouslyRoleResolution 设置确定如果 roleMappings 结果处于可以承担多个角色的不明确情况下要承担的角色。 将其设置为“AuthenticatedRole”意味着在这种情况下,将使用角色字段中的“authentiated”下指定的角色。