signer.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. var AWS = require('../core');
  2. /**
  3. * @api private
  4. */
  5. var service = null;
  6. /**
  7. * @api private
  8. */
  9. var api = {
  10. signatureVersion: 'v4',
  11. signingName: 'rds-db',
  12. operations: {}
  13. };
  14. /**
  15. * @api private
  16. */
  17. var requiredAuthTokenOptions = {
  18. region: 'string',
  19. hostname: 'string',
  20. port: 'number',
  21. username: 'string'
  22. };
  23. /**
  24. * A signer object can be used to generate an auth token to a database.
  25. */
  26. AWS.RDS.Signer = AWS.util.inherit({
  27. /**
  28. * Creates a signer object can be used to generate an auth token.
  29. *
  30. * @option options credentials [AWS.Credentials] the AWS credentials
  31. * to sign requests with. Uses the default credential provider chain
  32. * if not specified.
  33. * @option options hostname [String] the hostname of the database to connect to.
  34. * @option options port [Number] the port number the database is listening on.
  35. * @option options region [String] the region the database is located in.
  36. * @option options username [String] the username to login as.
  37. * @example Passing in options to constructor
  38. * var signer = new AWS.RDS.Signer({
  39. * credentials: new AWS.SharedIniFileCredentials({profile: 'default'}),
  40. * region: 'us-east-1',
  41. * hostname: 'db.us-east-1.rds.amazonaws.com',
  42. * port: 8000,
  43. * username: 'name'
  44. * });
  45. */
  46. constructor: function Signer(options) {
  47. this.options = options || {};
  48. },
  49. /**
  50. * @api private
  51. * Strips the protocol from a url.
  52. */
  53. convertUrlToAuthToken: function convertUrlToAuthToken(url) {
  54. // we are always using https as the protocol
  55. var protocol = 'https://';
  56. if (url.indexOf(protocol) === 0) {
  57. return url.substring(protocol.length);
  58. }
  59. },
  60. /**
  61. * @overload getAuthToken(options = {}, [callback])
  62. * Generate an auth token to a database.
  63. * @note You must ensure that you have static or previously resolved
  64. * credentials if you call this method synchronously (with no callback),
  65. * otherwise it may not properly sign the request. If you cannot guarantee
  66. * this (you are using an asynchronous credential provider, i.e., EC2
  67. * IAM roles), you should always call this method with an asynchronous
  68. * callback.
  69. *
  70. * @param options [map] The fields to use when generating an auth token.
  71. * Any options specified here will be merged on top of any options passed
  72. * to AWS.RDS.Signer:
  73. *
  74. * * **credentials** (AWS.Credentials) — the AWS credentials
  75. * to sign requests with. Uses the default credential provider chain
  76. * if not specified.
  77. * * **hostname** (String) — the hostname of the database to connect to.
  78. * * **port** (Number) — the port number the database is listening on.
  79. * * **region** (String) — the region the database is located in.
  80. * * **username** (String) — the username to login as.
  81. * @return [String] if called synchronously (with no callback), returns the
  82. * auth token.
  83. * @return [null] nothing is returned if a callback is provided.
  84. * @callback callback function (err, token)
  85. * If a callback is supplied, it is called when an auth token has been generated.
  86. * @param err [Error] the error object returned from the signer.
  87. * @param token [String] the auth token.
  88. *
  89. * @example Generating an auth token synchronously
  90. * var signer = new AWS.RDS.Signer({
  91. * // configure options
  92. * region: 'us-east-1',
  93. * username: 'default',
  94. * hostname: 'db.us-east-1.amazonaws.com',
  95. * port: 8000
  96. * });
  97. * var token = signer.getAuthToken({
  98. * // these options are merged with those defined when creating the signer, overriding in the case of a duplicate option
  99. * // credentials are not specified here or when creating the signer, so default credential provider will be used
  100. * username: 'test' // overriding username
  101. * });
  102. * @example Generating an auth token asynchronously
  103. * var signer = new AWS.RDS.Signer({
  104. * // configure options
  105. * region: 'us-east-1',
  106. * username: 'default',
  107. * hostname: 'db.us-east-1.amazonaws.com',
  108. * port: 8000
  109. * });
  110. * signer.getAuthToken({
  111. * // these options are merged with those defined when creating the signer, overriding in the case of a duplicate option
  112. * // credentials are not specified here or when creating the signer, so default credential provider will be used
  113. * username: 'test' // overriding username
  114. * }, function(err, token) {
  115. * if (err) {
  116. * // handle error
  117. * } else {
  118. * // use token
  119. * }
  120. * });
  121. *
  122. */
  123. getAuthToken: function getAuthToken(options, callback) {
  124. if (typeof options === 'function' && callback === undefined) {
  125. callback = options;
  126. options = {};
  127. }
  128. var self = this;
  129. var hasCallback = typeof callback === 'function';
  130. // merge options with existing options
  131. options = AWS.util.merge(this.options, options);
  132. // validate options
  133. var optionsValidation = this.validateAuthTokenOptions(options);
  134. if (optionsValidation !== true) {
  135. if (hasCallback) {
  136. return callback(optionsValidation, null);
  137. }
  138. throw optionsValidation;
  139. }
  140. // 15 minutes
  141. var expires = 900;
  142. // create service to generate a request from
  143. var serviceOptions = {
  144. region: options.region,
  145. endpoint: new AWS.Endpoint(options.hostname + ':' + options.port),
  146. paramValidation: false,
  147. signatureVersion: 'v4'
  148. };
  149. if (options.credentials) {
  150. serviceOptions.credentials = options.credentials;
  151. }
  152. service = new AWS.Service(serviceOptions);
  153. // ensure the SDK is using sigv4 signing (config is not enough)
  154. service.api = api;
  155. var request = service.makeRequest();
  156. // add listeners to request to properly build auth token
  157. this.modifyRequestForAuthToken(request, options);
  158. if (hasCallback) {
  159. request.presign(expires, function(err, url) {
  160. if (url) {
  161. url = self.convertUrlToAuthToken(url);
  162. }
  163. callback(err, url);
  164. });
  165. } else {
  166. var url = request.presign(expires);
  167. return this.convertUrlToAuthToken(url);
  168. }
  169. },
  170. /**
  171. * @api private
  172. * Modifies a request to allow the presigner to generate an auth token.
  173. */
  174. modifyRequestForAuthToken: function modifyRequestForAuthToken(request, options) {
  175. request.on('build', request.buildAsGet);
  176. var httpRequest = request.httpRequest;
  177. httpRequest.body = AWS.util.queryParamsToString({
  178. Action: 'connect',
  179. DBUser: options.username
  180. });
  181. },
  182. /**
  183. * @api private
  184. * Validates that the options passed in contain all the keys with values of the correct type that
  185. * are needed to generate an auth token.
  186. */
  187. validateAuthTokenOptions: function validateAuthTokenOptions(options) {
  188. // iterate over all keys in options
  189. var message = '';
  190. options = options || {};
  191. for (var key in requiredAuthTokenOptions) {
  192. if (!Object.prototype.hasOwnProperty.call(requiredAuthTokenOptions, key)) {
  193. continue;
  194. }
  195. if (typeof options[key] !== requiredAuthTokenOptions[key]) {
  196. message += 'option \'' + key + '\' should have been type \'' + requiredAuthTokenOptions[key] + '\', was \'' + typeof options[key] + '\'.\n';
  197. }
  198. }
  199. if (message.length) {
  200. return AWS.util.error(new Error(), {
  201. code: 'InvalidParameter',
  202. message: message
  203. });
  204. }
  205. return true;
  206. }
  207. });