gpt4 book ai didi

java - aws sdk assumeRole 没有生效

转载 作者:行者123 更新时间:2023-12-01 21:36:18 26 4
gpt4 key购买 nike

我已经运行了以下代码段。我对 aws 的调用工作正常,但现在我不得不转换角色,它遇到了问题,使它生效,因为它似乎我仍然停留在原来的角色。

    public void awsAssumeRoleUsingEnvironmentVariable(Regions region, String roleARN, String roleSessionName) throws Exception {
AWSSecurityTokenService stsClient = AWSSecurityTokenServiceClientBuilder.standard()
.withCredentials(new EnvironmentVariableCredentialsProvider())
.withRegion(region)
.build();

GetCallerIdentityRequest request = new GetCallerIdentityRequest();
GetCallerIdentityResult response = stsClient.getCallerIdentity(request);

System.out.println("CURRENT ROLE ASSUMED IS: " + response.toString());

request = new GetCallerIdentityRequest();

System.out.println("EXECUTING ASSUME ROLE");
AssumeRoleRequest roleRequest = new AssumeRoleRequest()
.withRoleArn(roleARN)
.withRoleSessionName(roleSessionName);

AssumeRoleResult roleResponse = stsClient.assumeRole(roleRequest);

Credentials sessionCredentials = roleResponse.getCredentials();

response = stsClient.getCallerIdentity(request);

System.out.println("CURRENT ROLE ASSUMED IS: " + response.toString());
}

getCallerIdentity 每次都返回相同的角色

编辑:只是试图解决这个问题,这绝对是我通过尝试使用使用 AWSCLI 返回的凭证进行编码的方式的问题。当我在运行我的应用程序时生成的 sessionCredentials 变量上执行 System.out.println(),然后使用以下命令手动导出返回的 key ...

export AWS_ACCESS_KEY_ID=RoleAccessKeyID
export AWS_SECRET_ACCESS_KEY=RoleSecretKey
export AWS_SESSION_TOKEN=RoleSessionToken

接着是一个..

aws sts 获取调用者身份

返回了正确的角色,因此我的 java 代码 assumeRole 似乎可以正常工作并获得凭据,但我没有正确设置客户端,所以它没有使用它刚刚承担的角色。

非常感谢

最佳答案

如评论中所述,使用 sessionCredentials 创建一个新的 BasicSessionCredentials,它可以传递给任何资源客户端。

示例代码在这里 https://docs.aws.amazon.com/AmazonS3/latest/dev/AuthUsingTempSessionTokenJava.html


import com.amazonaws.AmazonServiceException;
import com.amazonaws.SdkClientException;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicSessionCredentials;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
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 MakingRequestsWithIAMTempCredentials {
public static void main(String[] args) {
String clientRegion = "*** Client region ***";
String roleARN = "*** ARN for role to be assumed ***";
String roleSessionName = "*** Role session name ***";
String bucketName = "*** Bucket name ***";

try {
// 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 ProfileCredentialsProvider())
.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();
}
}
}

关于java - aws sdk assumeRole 没有生效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61902906/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com