Amazon web services AWS Java SDK:如何构造使用带有AWS\u安全性\u令牌的身份验证配置文件的AWSClient
我正在尝试使用AWS Java SDK与S3和Lambda交互 因为我们的帐户需要使用多因素身份验证,所以我的~/.aws/credentials文件有两个配置文件,[default]和[auth]。默认设置有一个通过IAM管理控制台生成的Amazon web services AWS Java SDK:如何构造使用带有AWS\u安全性\u令牌的身份验证配置文件的AWSClient,amazon-web-services,aws-sdk,Amazon Web Services,Aws Sdk,我正在尝试使用AWS Java SDK与S3和Lambda交互 因为我们的帐户需要使用多因素身份验证,所以我的~/.aws/credentials文件有两个配置文件,[default]和[auth]。默认设置有一个通过IAM管理控制台生成的aws\u access\u key\u id和aws\u secret\u access\u key,但此配置文件不能用于执行任何操作。第二个配置文件,auth,由一个脚本生成,该脚本使用MFA代码创建一组持续12小时的凭证。除了aws\u访问密钥\u id
aws\u access\u key\u id
和aws\u secret\u access\u key
,但此配置文件不能用于执行任何操作。第二个配置文件,auth
,由一个脚本生成,该脚本使用MFA代码创建一组持续12小时的凭证。除了aws\u访问密钥\u id
和aws\u密码\u访问密钥
之外,这一个还有一个aws\u安全令牌
。(可能需要注意的是,此aws_security_令牌不在我的IAM管理控制台中,但该配置文件与aws CLI配合良好,我的同事也确认这一点。)
我尝试过各种创建S3或Lambda客户端的方法:
//private static AWSLambdaClient lambda = new AWSLambdaClient();
private static AWSLambda lambda = AWSLambdaClientBuilder.standard().withCredentials(new ProfileCredentialsProvider("auth")).withRegion(Regions.US_WEST_2).build();
//private static AmazonS3 s3client = new AmazonS3Client();
private static AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider("auth"));
在所有情况下,当我尝试与这些客户做任何事情时,它都失败了。(要么s3client.listbackes()
要么lambda.invoke(请求)
)
对于默认配置文件,它会以预期的方式失败,即用户没有该操作的权限
对于“auth”配置文件,S3和Lambda以不同的方式失败。S3因“您提供的AWS访问密钥Id在我们的记录中不存在”而失败。Lambda因“请求中包含的安全令牌无效”而失败
完整错误消息如下:
S3 with default profile:
Exception in thread "main" com.amazonaws.services.s3.model.AmazonS3Exception: Access Denied (Service: Amazon S3; Status Code: 403; Error Code: AccessDenied; Request ID: 1A8FE9A40C6CF4FC), S3 Extended Request ID: M+IaUw223nj7ClIv3oB4sp7pOw2af8VMsrhLl/18oQB0rLMRFNjf8LFyXKj+AQNanGoUVgMpPNM=
S3 with auth profile:
Exception in thread "main" com.amazonaws.services.s3.model.AmazonS3Exception: The AWS Access Key Id you provided does not exist in our records. (Service: Amazon S3; Status Code: 403; Error Code: InvalidAccessKeyId; Request ID: E388FFE85FC71492), S3 Extended Request ID: f3RU7uEXK+2tt98fQYCkHMbsa7odsnsgTIdRJvx2xbvKUmDtT2xXWZ2Eqor1I1ODqEZ0xDe3Quc=
Lambda with default profile:
Caused by: com.amazonaws.services.lambda.model.AWSLambdaException: User: arn:aws:iam::181136347741:user/bkc is not authorized to perform: lambda:InvokeFunction on resource: arn:aws:lambda:us-east-1:181136347741:function:sensor (Service: AWSLambda; Status Code: 403; Error Code: AccessDeniedException; Request ID: e4670c43-61b6-11e7-8eeb-db8360af9fe6)
Lambda with auth profile:
Caused by: com.amazonaws.services.lambda.model.AWSLambdaException: The security token included in the request is invalid. (Service: AWSLambda; Status Code: 403; Error Code: UnrecognizedClientException; Request ID: 00a397f0-61b7-11e7-beb6-df232368bb9e)
那么我做错了什么?我创建ProfileCredentialsProvider
并将其传递给AWS客户端的方式是否有问题?我的auth
配置文件是否有问题,即使它可以从CLI工作?(我们是否需要修复脚本中创建auth概要文件的某些内容?)
以下是生成auth概要文件的脚本的有趣部分,供参考:
def get_mfa_tokens(serial, code):
'''Generate MFA access tokens using the given device serial and MFA code.'''
sts = boto3.client('sts')
return sts.get_session_token(
SerialNumber=serial,
TokenCode=code)
def add_auth_tokens(config, tokens):
'''Modify the given config object, adding certain token fields to it.'''
#auth section will be there most of the time, so LBYL.
if not config.has_section('auth'):
config.add_section('auth')
config.set('auth', '# expires {}'.format(tokens['Credentials']['Expiration']))
config.set('auth', 'aws_access_key_id', tokens['Credentials']['AccessKeyId'])
config.set('auth', 'aws_secret_access_key', tokens['Credentials']['SecretAccessKey'])
config.set('auth', 'aws_security_token', tokens['Credentials']['SessionToken'])