environment_credentials.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. var AWS = require('../core');
  2. /**
  3. * Represents credentials from the environment.
  4. *
  5. * By default, this class will look for the matching environment variables
  6. * prefixed by a given {envPrefix}. The un-prefixed environment variable names
  7. * for each credential value is listed below:
  8. *
  9. * ```javascript
  10. * accessKeyId: ACCESS_KEY_ID
  11. * secretAccessKey: SECRET_ACCESS_KEY
  12. * sessionToken: SESSION_TOKEN
  13. * ```
  14. *
  15. * With the default prefix of 'AWS', the environment variables would be:
  16. *
  17. * AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_SESSION_TOKEN
  18. *
  19. * @!attribute envPrefix
  20. * @readonly
  21. * @return [String] the prefix for the environment variable names excluding
  22. * the separating underscore ('_').
  23. */
  24. AWS.EnvironmentCredentials = AWS.util.inherit(AWS.Credentials, {
  25. /**
  26. * Creates a new EnvironmentCredentials class with a given variable
  27. * prefix {envPrefix}. For example, to load credentials using the 'AWS'
  28. * prefix:
  29. *
  30. * ```javascript
  31. * var creds = new AWS.EnvironmentCredentials('AWS');
  32. * creds.accessKeyId == 'AKID' // from AWS_ACCESS_KEY_ID env var
  33. * ```
  34. *
  35. * @param envPrefix [String] the prefix to use (e.g., 'AWS') for environment
  36. * variables. Do not include the separating underscore.
  37. */
  38. constructor: function EnvironmentCredentials(envPrefix) {
  39. AWS.Credentials.call(this);
  40. this.envPrefix = envPrefix;
  41. this.get(function() {});
  42. },
  43. /**
  44. * Loads credentials from the environment using the prefixed
  45. * environment variables.
  46. *
  47. * @callback callback function(err)
  48. * Called after the (prefixed) ACCESS_KEY_ID, SECRET_ACCESS_KEY, and
  49. * SESSION_TOKEN environment variables are read. When this callback is
  50. * called with no error, it means that the credentials information has
  51. * been loaded into the object (as the `accessKeyId`, `secretAccessKey`,
  52. * and `sessionToken` properties).
  53. * @param err [Error] if an error occurred, this value will be filled
  54. * @see get
  55. */
  56. refresh: function refresh(callback) {
  57. if (!callback) callback = AWS.util.fn.callback;
  58. if (!process || !process.env) {
  59. callback(AWS.util.error(
  60. new Error('No process info or environment variables available'),
  61. { code: 'EnvironmentCredentialsProviderFailure' }
  62. ));
  63. return;
  64. }
  65. var keys = ['ACCESS_KEY_ID', 'SECRET_ACCESS_KEY', 'SESSION_TOKEN'];
  66. var values = [];
  67. for (var i = 0; i < keys.length; i++) {
  68. var prefix = '';
  69. if (this.envPrefix) prefix = this.envPrefix + '_';
  70. values[i] = process.env[prefix + keys[i]];
  71. if (!values[i] && keys[i] !== 'SESSION_TOKEN') {
  72. callback(AWS.util.error(
  73. new Error('Variable ' + prefix + keys[i] + ' not set.'),
  74. { code: 'EnvironmentCredentialsProviderFailure' }
  75. ));
  76. return;
  77. }
  78. }
  79. this.expired = false;
  80. AWS.Credentials.apply(this, values);
  81. callback();
  82. }
  83. });