ini-loader.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. var AWS = require('../core');
  2. var os = require('os');
  3. var path = require('path');
  4. function parseFile(filename) {
  5. return AWS.util.ini.parse(AWS.util.readFileSync(filename));
  6. }
  7. function getProfiles(fileContent) {
  8. var tmpContent = {};
  9. Object.keys(fileContent).forEach(function(sectionName) {
  10. if (/^sso-session\s/.test(sectionName)) return;
  11. Object.defineProperty(tmpContent, sectionName.replace(/^profile\s/, ''), {
  12. value: fileContent[sectionName],
  13. enumerable: true
  14. });
  15. });
  16. return tmpContent;
  17. }
  18. function getSsoSessions(fileContent) {
  19. var tmpContent = {};
  20. Object.keys(fileContent).forEach(function(sectionName) {
  21. if (!/^sso-session\s/.test(sectionName)) return;
  22. Object.defineProperty(tmpContent, sectionName.replace(/^sso-session\s/, ''), {
  23. value: fileContent[sectionName],
  24. enumerable: true
  25. });
  26. });
  27. return tmpContent;
  28. }
  29. /**
  30. * Ini file loader class the same as that used in the SDK. It loads and
  31. * parses config and credentials files in .ini format and cache the content
  32. * to assure files are only read once.
  33. * Note that calling operations on the instance instantiated from this class
  34. * won't affect the behavior of SDK since SDK uses an internal singleton of
  35. * this class.
  36. * @!macro nobrowser
  37. */
  38. AWS.IniLoader = AWS.util.inherit({
  39. constructor: function IniLoader() {
  40. this.resolvedProfiles = {};
  41. this.resolvedSsoSessions = {};
  42. },
  43. /** Remove all cached files. Used after config files are updated. */
  44. clearCachedFiles: function clearCachedFiles() {
  45. this.resolvedProfiles = {};
  46. this.resolvedSsoSessions = {};
  47. },
  48. /**
  49. * Load configurations from config/credentials files and cache them
  50. * for later use. If no file is specified it will try to load default files.
  51. *
  52. * @param options [map] information describing the file
  53. * @option options filename [String] ('~/.aws/credentials' or defined by
  54. * AWS_SHARED_CREDENTIALS_FILE process env var or '~/.aws/config' if
  55. * isConfig is set to true)
  56. * path to the file to be read.
  57. * @option options isConfig [Boolean] (false) True to read config file.
  58. * @return [map<String,String>] object containing contents from file in key-value
  59. * pairs.
  60. */
  61. loadFrom: function loadFrom(options) {
  62. options = options || {};
  63. var isConfig = options.isConfig === true;
  64. var filename = options.filename || this.getDefaultFilePath(isConfig);
  65. if (!this.resolvedProfiles[filename]) {
  66. var fileContent = parseFile(filename);
  67. if (isConfig) {
  68. Object.defineProperty(this.resolvedProfiles, filename, {
  69. value: getProfiles(fileContent)
  70. });
  71. } else {
  72. Object.defineProperty(this.resolvedProfiles, filename, { value: fileContent });
  73. }
  74. }
  75. return this.resolvedProfiles[filename];
  76. },
  77. /**
  78. * Load sso sessions from config/credentials files and cache them
  79. * for later use. If no file is specified it will try to load default file.
  80. *
  81. * @param options [map] information describing the file
  82. * @option options filename [String] ('~/.aws/config' or defined by
  83. * AWS_CONFIG_FILE process env var)
  84. * @return [map<String,String>] object containing contents from file in key-value
  85. * pairs.
  86. */
  87. loadSsoSessionsFrom: function loadSsoSessionsFrom(options) {
  88. options = options || {};
  89. var filename = options.filename || this.getDefaultFilePath(true);
  90. if (!this.resolvedSsoSessions[filename]) {
  91. var fileContent = parseFile(filename);
  92. Object.defineProperty(this.resolvedSsoSessions, filename, {
  93. value: getSsoSessions(fileContent)
  94. });
  95. }
  96. return this.resolvedSsoSessions[filename];
  97. },
  98. /**
  99. * @api private
  100. */
  101. getDefaultFilePath: function getDefaultFilePath(isConfig) {
  102. return path.join(
  103. this.getHomeDir(),
  104. '.aws',
  105. isConfig ? 'config' : 'credentials'
  106. );
  107. },
  108. /**
  109. * @api private
  110. */
  111. getHomeDir: function getHomeDir() {
  112. var env = process.env;
  113. var home = env.HOME ||
  114. env.USERPROFILE ||
  115. (env.HOMEPATH ? ((env.HOMEDRIVE || 'C:/') + env.HOMEPATH) : null);
  116. if (home) {
  117. return home;
  118. }
  119. if (typeof os.homedir === 'function') {
  120. return os.homedir();
  121. }
  122. throw AWS.util.error(
  123. new Error('Cannot load credentials, HOME path not set')
  124. );
  125. }
  126. });
  127. var IniLoader = AWS.IniLoader;
  128. module.exports = {
  129. IniLoader: IniLoader
  130. };