代码之家  ›  专栏  ›  技术社区  ›  ha9u63a7

AWS角色与密钥

  •  0
  • ha9u63a7  · 技术社区  · 4 年前

    AWS角色适用于需要使用临时凭据访问AWS服务(如S3等)的服务。这些都是使用STS完成的。当一个帐户的用户/应用程序需要临时访问另一个帐户拥有的资源时,这很有用。

    但是,当使用Profile属性传递凭据时,STS只会发出临时凭据。至少这就是AWS提供的代码所暗示的

    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();
            }
        }
    }
    

    上面的代码只会在不提供某些凭据的情况下工作。所以我的问题是,当我可以简单地使用访问/密钥时,角色在这里有什么用处?

    0 回复  |  直到 4 年前
        1
  •  2
  •   Chris Williams Tihomir Paskalev    4 年前

    您提到的凭据是临时的这一点正是IAM角色是推荐方法的众多原因之一。

    一个角色可以应用于AWS服务和资源,例如,一个EC2实例可以附加一个角色,AWS会自动轮换这些角色。此外,您可以使用STS将角色作为角色,这可以从IAM用户、角色或 federated user .

    您应该尽可能避免使用IAM用户,有一些用例,如签名URL(您希望它持续几个小时以上)以及在内部位置。如果必须使用IAM钥匙,应确保经常转动钥匙。

    有关更多信息,请查看 IAM Identities (users, groups, and roles) Security best practices in IAM 页。