我在调用代入 STS 的角色
方法时遇到错误。它说用户无权对资源xxx
执行sts:阿苏梅罗莱
。
我做了以下工作:
我做错了什么?
组中的策略
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "some-large-id",
"Effect": "Allow",
"Action": [
"sts:*"
],
"Resource": [
"*"
]
}
]
}
策略在角色中
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "another-large-id",
"Effect": "Allow",
"Action": [
"s3:PutObject"
],
"Resource": [
"arn:aws:s3:::my-bucket-name/*"
]
}
]
}
最后像这样打电话
let policy = {
"Version": "2012-10-17",
"Statement": [
{
"Sid": "new-custom-id",
"Effect": "Allow",
"Action": ["s3:PutObject"],
"Resource": ["arn:aws:s3:::my-bucket-name/*"]
}
]
};
let params = {
DurationSeconds: 3600,
ExternalId: 'some-value',
Policy: JSON.stringify(policy),
RoleArn: "arn:aws:iam::NUMBER:role/ROLE-NAME", //Cheked, role is the same that step one
RoleSessionName: this.makeNewSessionId()
};
let sts = new AWS.STS({ apiVersion: '2012-08-10' });
sts.assumeRole(params, (err, data) => {
if(err) console.log(err);
else console.log(data);
});
我遇到了同样的问题。这些步骤我固定如下:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::IAM_USER_ID:user/haipv",//the roleARN need to be granted, use * for all
"Service": "s3.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
示例代码:
import com.amazonaws.AmazonServiceException;
import com.amazonaws.SdkClientException;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.auth.BasicSessionCredentials;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.ObjectListing;
import com.amazonaws.services.securitytoken.AWSSecurityTokenService;
import com.amazonaws.services.securitytoken.AWSSecurityTokenServiceClientBuilder;
import com.amazonaws.services.securitytoken.model.AssumeRoleRequest;
import com.amazonaws.services.securitytoken.model.AssumeRoleResult;
import com.amazonaws.services.securitytoken.model.Credentials;
public class Main {
public static void main(String[] args) {
Regions clientRegion = Regions.AP_SOUTHEAST_1;
String roleARN = "arn:aws:iam::IAM_USER_ID:role/haipvRole"; // the roleARN coppied above
String roleSessionName = "haipv-session";
String bucketName = "haipv.docketName";//file_example_MP4_640_3MG.mp4
String accesskey = "YOURKEY";
String secretkey = "YOUR SECRET KEY";
try {
BasicAWSCredentials credentials = new BasicAWSCredentials(accesskey, secretkey);
// Creating the STS client is part of your trusted code. It has
// the security credentials you use to obtain temporary security credentials.
AWSSecurityTokenService stsClient = AWSSecurityTokenServiceClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(credentials))
.withRegion(clientRegion)
.build();
// Obtain credentials for the IAM role. Note that you cannot assume the role of an AWS root account;
// Amazon S3 will deny access. You must use credentials for an IAM user or an IAM role.
AssumeRoleRequest roleRequest = new AssumeRoleRequest()
.withRoleArn(roleARN)
.withRoleSessionName(roleSessionName);
AssumeRoleResult roleResponse = stsClient.assumeRole(roleRequest);
Credentials sessionCredentials = roleResponse.getCredentials();
// Create a BasicSessionCredentials object that contains the credentials you just retrieved.
BasicSessionCredentials awsCredentials = new BasicSessionCredentials(
sessionCredentials.getAccessKeyId(),
sessionCredentials.getSecretAccessKey(),
sessionCredentials.getSessionToken());
// Provide temporary security credentials so that the Amazon S3 client
// can send authenticated requests to Amazon S3. You create the client
// using the sessionCredentials object.
AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(awsCredentials))
.withRegion(clientRegion)
.build();
// 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);
System.out.println("No. of Objects: " + objects.getObjectSummaries().size());
}
catch(AmazonServiceException e) {
// The call was transmitted successfully, but Amazon S3 couldn't process
// it, so it returned an error response.
e.printStackTrace();
}
catch(SdkClientException e) {
// Amazon S3 couldn't be contacted for a response, or the client
// couldn't parse the response from Amazon S3.
e.printStackTrace();
}
}
}
请参阅此链接中的官方文档
它对我有用。
在您要承担的角色上,例如使用STSJavaV2 API(不是Node),您需要设置信任关系。在信任关系中,指定要信任的用户。例如:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::<AWS Account ID>:user/JohnDoe” //Specify the AWS ARN of your IAM user.
},
"Action": "sts:AssumeRole"
}
]
}
例如,现在您可以运行一个Java程序来调用assumeRole操作。
package com.example.sts;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.sts.StsClient;
import software.amazon.awssdk.services.sts.model.AssumeRoleRequest;
import software.amazon.awssdk.services.sts.model.StsException;
import software.amazon.awssdk.services.sts.model.AssumeRoleResponse;
import software.amazon.awssdk.services.sts.model.Credentials;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Locale;
/**
* To make this code example work, create a Role that you want to assume.
* Then define a Trust Relationship in the AWS Console. YOu can use this as an example:
*
* {
* "Version": "2012-10-17",
* "Statement": [
* {
* "Effect": "Allow",
* "Principal": {
* "AWS": "<Specify the ARN of your IAM user you are using in this code example>"
* },
* "Action": "sts:AssumeRole"
* }
* ]
* }
*
* For more information, see "Editing the Trust Relationship for an Existing Role" in the AWS Directory Service guide.
*/
public class AssumeRole {
public static void main(String[] args) {
String roleArn = "arn:aws:iam::000540000000:role/s3role" ; // args[0];
String roleSessionName = "mysession101"; // args[1];
Region region = Region.US_EAST_1;
StsClient stsClient = StsClient.builder()
.region(region)
.build();
try {
AssumeRoleRequest roleRequest = AssumeRoleRequest.builder()
.roleArn(roleArn)
.roleSessionName(roleSessionName)
.build();
AssumeRoleResponse roleResponse = stsClient.assumeRole(roleRequest);
Credentials myCreds = roleResponse.credentials();
//Display the time when the temp creds expire
Instant exTime = myCreds.expiration();
// Convert the Instant to readable date
DateTimeFormatter formatter =
DateTimeFormatter.ofLocalizedDateTime( FormatStyle.SHORT )
.withLocale( Locale.US)
.withZone( ZoneId.systemDefault() );
formatter.format( exTime );
System.out.println("The temporary credentials expire on " + exTime );
} catch (StsException e) {
System.err.println(e.getMessage());
System.exit(1);
}
}
}
如果不设置信任关系,则此代码不起作用。
缺少一个步骤:在步骤 1 中创建的角色上设置信任关系。无论用户拥有什么权限,如果未设置信任关系,STS 都会拒绝请求。
故障排除IAM角色解释其工作原理。
我想问的主要问题是boto3如何找到IAM的角色。 我正在使用AWSEC2实例测试docker容器。我将IAM角色分配给EC2实例,但没有为Docker映像分配任何AWS访问配置。但当我运行Docker映像时,我可以使用分配给IAM角色的AWS资源。 这就是boto3如何在boto3留档中找到凭据。 将凭据作为boto.client()方法中的参数传递 创建会话对象时将凭据作为参数传递 环境变量
您可通过点击侧边栏角色栏进入角色管理页面。 添加角色 除系统提供角色之外,您可根据企业实际情况自定义角色。点击“添加角色”进行设置:•输入角色名称(必填) •输入角色说明(选填) •勾选角色功能权限(审批、单据为默认权限,不可删除) •勾选角色管理范围(项目或部门) 修改系统角色 主管需要您在部门模块或项目模块设置部门主管或项目主管。具体请详见【通过部门、项目管理员工】
假设出现如下情况: 每个用户可以有一个角色。每个角色都可以访问许多功能。我为每个角色设计了保存和检索访问矩阵的部分。所以我知道哪个角色应该访问哪些功能,它被保存在一个名为roles_features的表中。 我是否必须编写自己的函数来检查该角色是否允许路由。注意:我不想使用文件; 我想知道是否存在任何预定义的,它为每个角色和链接返回优化的访问控制方式,或者我必须实现我的; 正如我所写的,我不想使用
我的意思是我的错误是: ``` 正在检查“contacts”表是否存在{unrecognizedClientException:请求中包含的安全令牌无效。在request.extractorry(/root/work/contacts_api/node_modules/aws-sdk/lib/protocol/json.js:51:27)在request.calllisteners(/root/w
我不太确定如何使用actors访问数据库。在Akka的文档和书籍中,这个主题似乎被省略了。 一种解决方案可以是无状态参与者中的包装DAO。例如,对于数据库中的每个表(或域对象类型或聚合类型),可以创建一个负责所有CRUD操作的参与者。这种方法的一个变体可以是命令和查询的分离。例如,对于每个数据类型,1个命令参与者(用于并发)和10个查询参与者(用于并行性)。 另一种方法可以是创建只表示数据库中一行
我想知道PySpark是否支持使用IAM角色进行S3访问。具体来说,我有一个业务限制,我必须承担AWS角色才能访问给定的存储桶。使用boto时可以这样做(因为它是API的一部分),但我无法找到关于PySpark是否支持开箱即用的明确答案。 理想情况下,我希望能够在本地以独立模式运行时扮演一个角色,并将我的SparkContext指向该s3路径。我发现非IAM呼叫通常会随之而来: 是否存在提供IAM