【发布时间】:2018-06-05 07:40:47
【问题描述】:
我是 AWS 新手。我想为 aws 调用生成临时凭证。为此,我使用来自Making Requests Using IAM User Temporary Credentials - AWS SDK for Java的示例
我经过的地方
String clientRegion = "<specific region>";
String roleARN = "<ARN from role>";
String roleSessionName = "Just random string"; //<-- maybe I should pass specific SessionName?
String bucketName = "<specific bucket name>";
当尝试扮演角色时
stsClient.assumeRole(roleRequest);
得到一个错误
com.amazonaws.services.securitytoken.model.AWSSecurityTokenServiceException: 用户:arn:aws:iam:::user/ 无权执行:
sts:AssumeRole on resource: arn:aws:iam::<ID>:role/<ROLE_NAME> (Service: AWSSecurityTokenService; Status Code: 403; Error Code:拒绝访问;请求 ID:)
我有一个认知角色。 我认为角色信任关系设置存在问题。 它看起来像这样:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::<iam user ID>:user/<USER_NAME>",
"Federated": "cognito-identity.amazonaws.com"
},
"Action": "sts:AssumeRoleWithWebIdentity",
"Condition": {
"StringEquals": {
"cognito-identity.amazonaws.com:aud": "<user pool ID>"
},
"ForAnyValue:StringLike": {
"cognito-identity.amazonaws.com:amr": "authenticated"
}
}
}
]
}
和用户政策(此用户政策也附加到此角色):
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "<sidId1>",
"Effect": "Allow",
"Action": [
"s3:PutObject",
"s3:PutObjectAcl"
],
"Resource": [
"arn:aws:s3:::<path>*"
]
},
{
"Sid": "sidId2",
"Effect": "Allow",
"Action": [
"sts:AssumeRole",
"sts:AssumeRoleWithWebIdentity"
],
"Resource": [
"arn:aws:iam::<ID>:role/<ROLE_NAME>"
]
}
]
}
用户政策有两个警告:
UPD 我换了角色信任关系,只删除Condition:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Federated": "cognito-identity.amazonaws.com",
"AWS": "arn:aws:iam::<ID>:user/<USER>"
},
"Action": [
"sts:AssumeRole",
"sts:AssumeRoleWithWebIdentity"
]
}
]
}
现在拒绝访问错误发生在另一行代码:
// Verify that assuming the role worked and the permissions are set correctly
// by getting a set of object keys from the bucket.
ObjectListing objects = s3Client.listObjects(bucketName);
收到错误响应:com.amazonaws.services.s3.model.AmazonS3Exception:访问被拒绝(服务:Amazon S3;状态代码:403;错误代码:AccessDenied;请求 ID:),S3 扩展请求 ID:
【问题讨论】:
-
您的策略中似乎缺少
s3:ListBucket操作。此操作适用于存储桶资源。此外,您应该在最新更新中删除上面发布的政策中的帐户 ID(出于安全原因)。
标签: amazon-web-services aws-cognito aws-java-sdk aws-sts