简体   繁体   English

AWS 角色与密钥

[英]AWS Role vs. Keys

AWS Roles are meant for services which requires access to AWS Services eg S3 etc. using temporary credentials. AWS 角色适用于需要使用临时凭证访问 AWS 服务(例如 S3 等)的服务。 These are done using STS.这些是使用 STS 完成的。 This is useful when a user/application from one account needs access to a different account-owned resources on a temporary-basis.当来自一个帐户的用户/应用程序需要临时访问不同帐户拥有的资源时,这很有用。

However, STS will only issue a temporary credentials when the credentials are passed using Profile properties.但是,当使用配置文件属性传递凭据时,STS 只会发出临时凭据。 At least that's what the code provided by AWS implies anyway至少这就是 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();
        }
    }
}

The above code will only work without providing some credentials.上面的代码只有在不提供一些凭据的情况下才能工作。 So my question is, how is Role useful here when I can just simply use access/secret key ?所以我的问题是,当我可以简单地使用 access/secret key 时,Role 在这里有什么用?

Exactly the point you mentioned that the credentials are temporary is one of the many reasons why IAM roles are the recommended approach.您提到的凭证是临时的这一点正是 IAM 角色是推荐方法的众多原因之一。

A role can be applied to AWS services as well as resources, for example an EC2 instance can have a role attached with AWS automatically rotating these.角色可以应用于 AWS 服务和资源,例如 EC2 实例可以附加一个角色,AWS 会自动轮换这些。 Additionally you can use STS to assume a role as role, this can be assumed from an IAM user, a role or a federated user .此外,您可以使用 STS 将角色代入角色,这可以从 IAM 用户、角色或联合用户中代入

You should try to avoid using IAM users where possible, there are some usecases such as signed URLs (where you would like it to last more than a few hours) as well as in an on-premise location.您应该尽可能避免使用 IAM 用户,有一些用例,例如签名 URL(您希望它持续几个小时以上)以及在本地位置。 If you must use an IAM key you should make sure to rotate the key frequently.如果您必须使用 IAM 密钥,则应确保经常轮换密钥。

For more information take a look at the IAM Identities (users, groups, and roles) and Security best practices in IAM pages.有关更多信息,请查看IAM页面中的IAM 身份(用户、组和角色)安全最佳实践

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM