使用在AWS-SDK(AWS JavaScript SDK)中扮演角色的配置文件



使用AWS SDK用于JavaScript,我想使用一个假定A角色的默认配置文件。这与AWS CLI完美搭配。使用node.js与SDK使用不承担该角色,而仅对访问密钥属于的AWS帐户使用凭据。我找到了此文档,但它不处理一个角色:从共享凭据文件中加载凭据

有什么提示?

这是我的配置文件:

[default]
role_arn = arn:aws:iam::123456789:role/Developer
source_profile = default
output = json
region = us-east-1

在代码中使用多个交叉帐户角色的正确方法

获得STS的跨帐户角色的凭据,并在每次您需要获得该特定跨帐户角色身份验证的服务时使用这些凭据。

示例:

创建一个函数以获取交叉帐户凭据:

const AWS = require('aws-sdk');
const sts = new AWS.STS();
const getCrossAccountCredentials = async () => {
  return new Promise((resolve, reject) => {
    const timestamp = (new Date()).getTime();
    const params = {
      RoleArn: 'arn:aws:iam::123456789:role/Developer',
      RoleSessionName: `be-descriptibe-here-${timestamp}`
    };
    sts.assumeRole(params, (err, data) => {
      if (err) reject(err);
      else {
        resolve({
          accessKeyId: data.Credentials.AccessKeyId,
          secretAccessKey: data.Credentials.SecretAccessKey,
          sessionToken: data.Credentials.SessionToken,
        });
      }
    });
  });
}

然后您可以使用它而没有:

const main = async () => {
  // Get the Cross account credentials
  const accessparams = await getCrossAccountCredentials();
  // Get the ec2 service for current account
  const ec2 = new AWS.EC2();
  // Get the ec2 service for cross account role
  const ca_ec2 = new AWS.EC2(accessparams);
  // Get the autoscaling service for current account
  const autoscaling = new AWS.AutoScaling();
  // Get the autoscaling service for cross account role
  const ca_autoscaling = new AWS.AutoScaling(accessparams);
  // This will describe instances within the cross account role
  ca_ec2.describeInstances(...) 
  // This will describe instances within the original account
  ec2.describeInstances(...)
  // Here you can access both accounts without issues.
}

好处:

  • 不会在全球更改凭据,因此您仍然可以针对自己的AWS帐户,而无需事先备份凭据以还原。
  • 允许在每时每刻准确地控制您要定位的帐户。
  • 允许处理多个跨帐户角色和服务。

错误的方式

不要使用AWS.config.update覆盖全局凭据AWS.config.credentials !!!

超越全球凭据是一种不好的做法!!这与 @Brant的批准解决方案相同,但这不是很好的解决方案!这是原因:

const main = async () => {
  // Get the Cross account credentials
  const accessparams = await getCrossAccountCredentials();
  // Get the ec2 service for current account
  const ec2 = new AWS.EC2();
  // Overwrite the AWS credentials with cross account credentilas
  AWS.config.update(accessparams);
  // Get the ec2 service for cross account role
  const ca_ec2 = new AWS.EC2();
  // This will describe instances within the cross account role
  ca_ec2.describeInstances(...) 
  // This will ALSO describe instances within the cross account role
  ec2.describeInstances(...)
  // WARNING: Here you only will access the cross account role. You may get
  // confused on what you're accessing!!!
}

问题:

  • 直接或通过AWS.config.update更新全局AWS.config.credentials,将覆盖当前凭据。
  • 一切都会指出跨帐户角色,即使是您可能不会期望的未来服务电话。
  • 要切换回第一个帐户,您可能需要临时备份AWS.config.credentials并再次更新以还原。当您使用每个帐户时,很难控制执行上下文时,很难通过定位错误的帐户来弄乱。

再次,请勿使用AWS.config.update覆盖全局凭据AWS.config.credentials !!!

如果您需要在另一个帐户中完全运行代码

如果您需要完全为另一个帐户执行代码,而无需在凭据之间切换。您可以遵循@kanak Singhal的建议并将角色_ARN存储在配置文件中,并将AWS_SDK_LOAD_CONFIG="true"AWS_PROFILE="assume-role-profile"一起添加到环境变量。

找到了正确的方法!查看此公关:https://github.com/aws/aws-sdk-js/pull/1391

只需要与AWS_PROFILE="assume-role-profile"

一起将AWS_SDK_LOAD_CONFIG="true"添加到环境变量

因此,它不需要任何代码更新

这是因为,SDK仅默认加载credentials文件,而不是config文件,但是由于AWS COOL_ARN存储在config文件中,因此我们也必须启用加载config文件。

CLI和SDK的工作方式有所不同,因为您必须在使用SDK时明确担任角色。SDK不会像CLI一样自动从配置中扮演角色。

假设角色后,必须使用新的凭据更新。

这对我有用:

var AWS = require('aws-sdk');
AWS.config.region = 'us-east-1';
var sts = new AWS.STS();
sts.assumeRole({
  RoleArn: 'arn:aws:iam::123456789:role/Developer',
  RoleSessionName: 'awssdk'
}, function(err, data) {
  if (err) { // an error occurred
    console.log('Cannot assume role');
    console.log(err, err.stack);
  } else { // successful response
    AWS.config.update({
      accessKeyId: data.Credentials.AccessKeyId,
      secretAccessKey: data.Credentials.SecretAccessKey,
      sessionToken: data.Credentials.SessionToken
    });
  }
});

聚会很晚,但是现在最简单的方法可能是使用AWS.ChainableTemporaryCredentials

它会自动刷新凭据,并且可以在许多层中链接,或者在此使用默认凭据

AWS.config.credentials = new ChainableTemporaryCredentials({
  params: {RoleArn: 'RoleARN'}
});

https://docs.aws.amazon.com/awsjavascriptsdk/latest/aws/chainabletemporarycredentials.html

当我想扮演假定角色时,我自己发现了这个问题,所以猜想它仍然具有SEO功率!

最新更新