简体   繁体   中英

aws sdk for nodejs - getting credentials from ChainableTemporaryCredentials object

I'm trying to use AWS nodejs sdk ChainableTemporaryCredentials class to get temporary credentials for a customer AWS account. Here is my code snippet:

  
const credentials = new ChainableTemporaryCredentials({
  params: {
    RoleArn: "arn:aws:iam::123456789:role/Test-Customer-Role"
  },
  masterCredentials: new ChainableTemporaryCredentials({
    params: {
      RoleArn: "arn:aws:iam::123456789:role/Test-Automation-Role"
    }
  })
});

console.log("Credentials ", credentials.accessKeyId)   //it prints undefined

const client = new S3({ credentials });

s3.listBuckets(function(err, data) {
  if (err) console.log(err, err.stack);
  else     console.log(data);           // successful response with s3 buckets list is printed
});

My requirement is to get sts temporary accessKeyId, secretAccessKey and sessionToken variables from the credentials object above and pass them on to another nodejs module for further AWS actions. However, I get undefined as value for all accessKeyId, secretAccessKey and sessionToken properties of credentials object.

I know the credentials object is getting the right values since I'm able to list the s3 buckets using those credentials.

How can I get the temporary credentials generated by the ChainableTemporaryCredentials class?

Thanks.

or you can do it like this without wrapping manually with Promise.

async function getAWSCredentials() {
  const credentials = new ChainableTemporaryCredentials({
    params: {
      RoleArn: "arn:aws:iam::123456789:role/Test-Customer-Role",
    },
    masterCredentials: new ChainableTemporaryCredentials({
      params: {
        RoleArn: "arn:aws:iam::123456789:role/Test-Automation-Role",
      },
    }),
  });

  return credentials.getPromise();
}

I got it working by wrapping the credentials code in a Promise like this.

let credentials
AWS.config.credentials = new ChainableTemporaryCredentials({
  params: {
    RoleArn: "arn:aws:iam::123456789:role/Test-Customer-Role"
  },
  masterCredentials: new ChainableTemporaryCredentials({
    params: {
      RoleArn: "arn:aws:iam::123456789:role/Test-Automation-Role"
    }
  })
});

        let promise = new Promise((resolve, reject) => {
          config.getCredentials(async (err) => {
            if (err) {
              console.error("Unable to load aws credentials", err.message);
              reject(err);
            } else {
              credentials = {
                accessKeyId: config.credentials.accessKeyId,
                secretAccessKey: config.credentials.secretAccessKey,
                sessionToken: config.credentials.sessionToken,
              };
              resolve(credentials);
            }
          });
        });

        credentials = await promise;
        console.log("Credentials ", credentials);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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