UNPKG

deep-search

Version:
141 lines (102 loc) 5.53 kB
/** * Created by CCristi <ccovali@mitocgroup.com> on 3/28/16. */ 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.Aws4SignedHttpConnection = undefined; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } }; var _http = require('elasticsearch/src/lib/connectors/http'); var _http2 = _interopRequireDefault(_http); var _aws = require('aws4'); var _aws2 = _interopRequireDefault(_aws); var _util = require('util'); var _util2 = _interopRequireDefault(_util); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } /** * Aws4 Signed Http Connection Factory */ let Aws4SignedHttpConnection = exports.Aws4SignedHttpConnection = function (_HttpConnection) { _inherits(Aws4SignedHttpConnection, _HttpConnection); /** * @param {{accessKeyId: String, secretAccessKey: String}|null} awsCredentials * @param {Object[]} args */ function Aws4SignedHttpConnection(awsCredentials) { var _ref; _classCallCheck(this, Aws4SignedHttpConnection); for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { args[_key - 1] = arguments[_key]; } var _this = _possibleConstructorReturn(this, (_ref = Aws4SignedHttpConnection.__proto__ || Object.getPrototypeOf(Aws4SignedHttpConnection)).call.apply(_ref, [this].concat(args))); _this._awsCredentials = awsCredentials; return _this; } /** * @param {Object} params * @returns {*} */ _createClass(Aws4SignedHttpConnection, [{ key: 'makeReqParams', value: function makeReqParams(params) { let body = params.body; params = _get(Aws4SignedHttpConnection.prototype.__proto__ || Object.getPrototypeOf(Aws4SignedHttpConnection.prototype), 'makeReqParams', this).call(this, params); params.headers = params.headers || {}; let signParams = { service: 'es', region: Aws4SignedHttpConnection.getEsDomainRegion(params.hostname), host: params.hostname, method: params.method, path: params.path, headers: _util2.default._extend(params.headers, {}) }; if (body) { signParams.body = body; } let aws4Signature = this._createAws4Signature(signParams); ['X-Amz-Date', 'X-Amz-Security-Token', 'Authorization'].forEach(header => { params.headers[header] = aws4Signature.headers[header]; }); return params; } /** * @param {Object} optsToSign * @returns {Object} * @private */ }, { key: '_createAws4Signature', value: function _createAws4Signature(optsToSign) { return _aws2.default.sign(optsToSign, this._awsCredentials); } /** * @param {String} esDomainHostname * @returns {String} */ }], [{ key: 'getEsDomainRegion', value: function getEsDomainRegion(esDomainHostname) { let regionParts = esDomainHostname.match(/\.([^\.]+)\.es\.amazonaws\.com$/i); if (regionParts && regionParts.length >= 2) { return regionParts[1]; } throw new Error(`Invalid ES domain hostname "${esDomainHostname}".`); } /** * @param {{accessKeyId: String, secretAccessKey: String}|null} awsCredentials * @returns {Aws4SignedHttpConnection.prototype} */ }, { key: 'createPrototype', value: function createPrototype() { let awsCredentials = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null; return Aws4SignedHttpConnection.bind(null, awsCredentials); } }]); return Aws4SignedHttpConnection; }(_http2.default);