temporary_credentials.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. var AWS = require('../core');
  2. var STS = require('../../clients/sts');
  3. /**
  4. * Represents temporary credentials retrieved from {AWS.STS}. Without any
  5. * extra parameters, credentials will be fetched from the
  6. * {AWS.STS.getSessionToken} operation. If an IAM role is provided, the
  7. * {AWS.STS.assumeRole} operation will be used to fetch credentials for the
  8. * role instead.
  9. *
  10. * @note AWS.TemporaryCredentials is deprecated, but remains available for
  11. * backwards compatibility. {AWS.ChainableTemporaryCredentials} is the
  12. * preferred class for temporary credentials.
  13. *
  14. * To setup temporary credentials, configure a set of master credentials
  15. * using the standard credentials providers (environment, EC2 instance metadata,
  16. * or from the filesystem), then set the global credentials to a new
  17. * temporary credentials object:
  18. *
  19. * ```javascript
  20. * // Note that environment credentials are loaded by default,
  21. * // the following line is shown for clarity:
  22. * AWS.config.credentials = new AWS.EnvironmentCredentials('AWS');
  23. *
  24. * // Now set temporary credentials seeded from the master credentials
  25. * AWS.config.credentials = new AWS.TemporaryCredentials();
  26. *
  27. * // subsequent requests will now use temporary credentials from AWS STS.
  28. * new AWS.S3().listBucket(function(err, data) { ... });
  29. * ```
  30. *
  31. * @!attribute masterCredentials
  32. * @return [AWS.Credentials] the master (non-temporary) credentials used to
  33. * get and refresh temporary credentials from AWS STS.
  34. * @note (see constructor)
  35. */
  36. AWS.TemporaryCredentials = AWS.util.inherit(AWS.Credentials, {
  37. /**
  38. * Creates a new temporary credentials object.
  39. *
  40. * @note In order to create temporary credentials, you first need to have
  41. * "master" credentials configured in {AWS.Config.credentials}. These
  42. * master credentials are necessary to retrieve the temporary credentials,
  43. * as well as refresh the credentials when they expire.
  44. * @param params [map] a map of options that are passed to the
  45. * {AWS.STS.assumeRole} or {AWS.STS.getSessionToken} operations.
  46. * If a `RoleArn` parameter is passed in, credentials will be based on the
  47. * IAM role.
  48. * @param masterCredentials [AWS.Credentials] the master (non-temporary) credentials
  49. * used to get and refresh temporary credentials from AWS STS.
  50. * @example Creating a new credentials object for generic temporary credentials
  51. * AWS.config.credentials = new AWS.TemporaryCredentials();
  52. * @example Creating a new credentials object for an IAM role
  53. * AWS.config.credentials = new AWS.TemporaryCredentials({
  54. * RoleArn: 'arn:aws:iam::1234567890:role/TemporaryCredentials',
  55. * });
  56. * @see AWS.STS.assumeRole
  57. * @see AWS.STS.getSessionToken
  58. */
  59. constructor: function TemporaryCredentials(params, masterCredentials) {
  60. AWS.Credentials.call(this);
  61. this.loadMasterCredentials(masterCredentials);
  62. this.expired = true;
  63. this.params = params || {};
  64. if (this.params.RoleArn) {
  65. this.params.RoleSessionName =
  66. this.params.RoleSessionName || 'temporary-credentials';
  67. }
  68. },
  69. /**
  70. * Refreshes credentials using {AWS.STS.assumeRole} or
  71. * {AWS.STS.getSessionToken}, depending on whether an IAM role ARN was passed
  72. * to the credentials {constructor}.
  73. *
  74. * @callback callback function(err)
  75. * Called when the STS service responds (or fails). When
  76. * this callback is called with no error, it means that the credentials
  77. * information has been loaded into the object (as the `accessKeyId`,
  78. * `secretAccessKey`, and `sessionToken` properties).
  79. * @param err [Error] if an error occurred, this value will be filled
  80. * @see get
  81. */
  82. refresh: function refresh (callback) {
  83. this.coalesceRefresh(callback || AWS.util.fn.callback);
  84. },
  85. /**
  86. * @api private
  87. */
  88. load: function load (callback) {
  89. var self = this;
  90. self.createClients();
  91. self.masterCredentials.get(function () {
  92. self.service.config.credentials = self.masterCredentials;
  93. var operation = self.params.RoleArn ?
  94. self.service.assumeRole : self.service.getSessionToken;
  95. operation.call(self.service, function (err, data) {
  96. if (!err) {
  97. self.service.credentialsFrom(data, self);
  98. }
  99. callback(err);
  100. });
  101. });
  102. },
  103. /**
  104. * @api private
  105. */
  106. loadMasterCredentials: function loadMasterCredentials (masterCredentials) {
  107. this.masterCredentials = masterCredentials || AWS.config.credentials;
  108. while (this.masterCredentials.masterCredentials) {
  109. this.masterCredentials = this.masterCredentials.masterCredentials;
  110. }
  111. if (typeof this.masterCredentials.get !== 'function') {
  112. this.masterCredentials = new AWS.Credentials(this.masterCredentials);
  113. }
  114. },
  115. /**
  116. * @api private
  117. */
  118. createClients: function () {
  119. this.service = this.service || new STS({params: this.params});
  120. }
  121. });