vault-high-availability
Advanced tools
Comparing version 1.0.48 to 1.0.50
25
index.js
@@ -9,34 +9,27 @@ /** | ||
var logger = require('./utils/logger'); | ||
var callback = function () { | ||
logger.debug("Finish to find the new leader") | ||
}; | ||
var init = function (vaultAddress) { | ||
if (process.env.IGNORE_VAULT_LEADER_RESOLVER == "true") { | ||
this.addressResolver = vaultAddress; | ||
logger.debug("Leader resolver is disabled..") | ||
callback(); | ||
}else{ | ||
try{ | ||
this.addressResolver = new vaultLeaderResolver(vaultAddress); | ||
}catch(err){ | ||
logger.error("An error happened while trying to resolve the leader",err); | ||
} | ||
setStrategey(this.addressResolver); | ||
this.addressResolver = new vaultLeaderResolver(vaultAddress); | ||
setStrategey(this.addressResolver, callback); | ||
} | ||
logger.debug('Address resolver object: ' + JSON.stringify(this.addressResolver)); | ||
return { | ||
request: request, | ||
networkOrHttpErrorTriggerResolveDNS: this.networkOrHttpErrorTriggerResolveDNS, | ||
replaceDnsWithIp: this.addressResolver.replaceDnsWithIp | ||
replaceDnsWithIp: addressResolver.replaceDnsWithIp | ||
}; | ||
}; | ||
var setStrategey = function (addressResolver) { | ||
var setStrategey = function (addressResolver, callback) { | ||
logger.debug("Set strategey --> findActiveIp"); | ||
this.networkOrHttpErrorTriggerResolveDNS = NetworkOrHttpErrorTriggerResolveDNS(request.RetryStrategies.HTTPOrNetworkError, function () { | ||
logger.debug("Going to find the vault leader task"); | ||
addressResolver.findActiveIp(); | ||
addressResolver.findActiveIp(callback); | ||
}); | ||
@@ -43,0 +36,0 @@ |
{ | ||
"name": "vault-high-availability", | ||
"version": "1.0.48", | ||
"version": "1.0.50", | ||
"description": "", | ||
@@ -17,3 +17,3 @@ "main": "index.js", | ||
"dependencies": { | ||
"async": "2.5.0", | ||
"async": "2.4.0", | ||
"bunyan": "^1.8.10", | ||
@@ -24,4 +24,3 @@ "extend": "3.0.0", | ||
"requestretry-addressresolver": "1.12.0", | ||
"url": "0.11.0", | ||
"validator": "^8.1.0" | ||
"url": "0.11.0" | ||
}, | ||
@@ -28,0 +27,0 @@ "devDependencies": { |
@@ -9,5 +9,3 @@ 'use strict'; | ||
var extend = require('extend'); | ||
var validator = require('validator'); | ||
module.exports = function vaultLeaderResolver(url) { | ||
@@ -18,5 +16,4 @@ | ||
var findActiveIp = function () { | ||
return new Promise(function (resolveGeneralPromise, rejectGeneralPromise) { | ||
return new Promise(function (resolvePromise, reject) { | ||
log.info("will look for active ip only if isResolving is false"); | ||
@@ -34,3 +31,2 @@ if (!this.isResolving) { | ||
var tryAgainOrAbort = function () { | ||
log.debug("tryAgainOrAbort function is called..."); | ||
if (Date.now() - startResolvingTime <= 10 * 1000) { | ||
@@ -40,2 +36,3 @@ return setTimeout(resolve, 500); | ||
that.isResolving = false; | ||
return resolvePromise(); | ||
} | ||
@@ -45,71 +42,42 @@ }; | ||
var findTheLeader = function (addresses) { | ||
addresses.forEach(function (address) { | ||
log.debug("address.address: " + address.address); | ||
}); | ||
var resolvedAddress = []; | ||
addresses.forEach(function (address) { | ||
resolvedAddress.push(new Promise(function (resolvePromise, reject) { | ||
var newIp = null; | ||
async.each(addresses, function (address, callbackAsync) { | ||
var urlWithIpObject = extend({}, urlObject); | ||
urlWithIpObject.host = undefined; | ||
urlWithIpObject.hostname = address.address; | ||
request.get((urlLib.format(urlWithIpObject)) + "v1/sys/leader", { | ||
timeout: 500, | ||
timeout: 1000, | ||
gzip: true | ||
}, function (error, response, body) { | ||
if (error) { | ||
log.error("An error happened while trying to get the leader", error); | ||
return reject(error); | ||
log.debug("leader response for ip: ", address, "body: ", body, "error: ", error); | ||
var jsonBody = null; | ||
try { | ||
jsonBody = JSON.parse(body); | ||
} catch (e) { | ||
log.debug("response wasn't in json format", body) | ||
} | ||
else if (response.statusCode !== 200) { | ||
log.error("Got not good response when try to get the leader"); | ||
return reject("Got not good response while trying to get the leader: " + response.statusCode + ": " + JSON.stringify(response.body)); | ||
} | ||
else { | ||
log.debug("leader response for ip: ", address, "body: ", body, "error: ", error); | ||
var jsonBody = null; | ||
try { | ||
jsonBody = JSON.parse(body); | ||
} catch (e) { | ||
log.debug("response wasn't in json format", body) | ||
} | ||
if (response && response.statusCode === 200 && jsonBody && jsonBody.is_self === true) { | ||
log.info("leader found: ", address); | ||
log.debug("leader address :" + address.address); | ||
return resolvePromise(address.address); | ||
} else { | ||
log.debug("not the leader: ", address); | ||
return resolvePromise(); | ||
} | ||
if (response && response.statusCode === 200 && jsonBody && jsonBody.is_self === true) { | ||
log.info("leader found: ", address); | ||
newIp = address.address; | ||
} else { | ||
log.debug("not the leader: ", address); | ||
} | ||
}) | ||
})) | ||
}); | ||
callbackAsync(); | ||
}); | ||
}, | ||
function () { | ||
if (newIp) { | ||
that.ip = newIp; | ||
that.isResolving = false; | ||
log.info("active node set", id, new Date(), newIp); | ||
log.debug("Going to call the callback function..."); | ||
return resolvePromise(); | ||
Promise.all(resolvedAddress).then(function (responses) { | ||
log.debug("succeed to get the responses:" + JSON.stringify(responses)); | ||
var isLeaderFound = false; | ||
responses.forEach(function (ip) { | ||
log.debug("Got ip: " + ip); | ||
if ((typeof ip === 'string' || ip instanceof String) && validator.isIP(ip)) { | ||
that.ip = ip; | ||
that.isResolving = false; | ||
log.info("active node set", id, new Date(), ip); | ||
isLeaderFound = true; | ||
log.debug("isLeaderfound status changed to true"); | ||
resolveGeneralPromise(); | ||
} else { | ||
log.error("No active node was found."); | ||
tryAgainOrAbort(); | ||
} | ||
}); | ||
if (!isLeaderFound) { | ||
log.error("No active node was found."); | ||
tryAgainOrAbort(); | ||
} | ||
}).catch(function (err) { | ||
log.error("An error happened while trying to get vault leader", err); | ||
rejectGeneralPromise(err); | ||
}) | ||
); | ||
}; | ||
@@ -123,3 +91,2 @@ | ||
findTheLeader(addresses); | ||
log.debug("finish to resolve and succeed to get the leader"); | ||
} else { | ||
@@ -135,74 +102,19 @@ log.info("no address was found", err); | ||
} else { | ||
log.debug("No need to find the leader..."); | ||
return resolvePromise() | ||
} | ||
}) | ||
}); | ||
// var findTheLeader = function (addresses) { | ||
// var newIp = null; | ||
// async.each(addresses, function (address, callbackAsync) { | ||
// var urlWithIpObject = extend({}, urlObject); | ||
// urlWithIpObject.host = undefined; | ||
// urlWithIpObject.hostname = address.address; | ||
// request.get((urlLib.format(urlWithIpObject)) + "v1/sys/leader", { | ||
// timeout: 500, | ||
// gzip: true | ||
// }, function (error, response, body) { | ||
// if(error) { | ||
// log.error("An error happened while trying to get the leader",error); | ||
// callbackAsync(error); | ||
// } | ||
// else if(response.statusCode !== 200){ | ||
// log.error("Got not good response when try to get the leader"); | ||
// callbackAsync(new error("Got not good response while trying to get the leader: " + response.statusCode + ": " + JSON.stringify(response.body) )); | ||
// } | ||
// else { | ||
// log.debug("leader response for ip: ", address, "body: ", body, "error: ", error); | ||
// var jsonBody = null; | ||
// try { | ||
// jsonBody = JSON.parse(body); | ||
// } catch (e) { | ||
// log.debug("response wasn't in json format", body) | ||
// } | ||
// | ||
// if (response && response.statusCode === 200 && jsonBody && jsonBody.is_self === true) { | ||
// log.info("leader found: ", address); | ||
// newIp = address.address; | ||
// } else { | ||
// log.debug("not the leader: ", address); | ||
// } | ||
// callbackAsync(); | ||
// } | ||
// }); | ||
// }, | ||
// function (err) { | ||
// if(err){ | ||
// log.error("An error happened while trying to get vault leader",err); | ||
// process.exit(0); | ||
// }else{ | ||
// if (newIp) { | ||
// that.ip = newIp; | ||
// that.isResolving = false; | ||
// log.info("active node set", id, new Date(), newIp); | ||
// } else { | ||
// log.info("No active node was found."); | ||
// log.error("No active node was found."); | ||
// tryAgainOrAbort(); | ||
// } | ||
// } | ||
// } | ||
// ); | ||
// }; | ||
}; | ||
var replaceDnsWithIp = function (url) { | ||
log.debug("replaceDnsWithIp function called"); | ||
if (this.ip) { | ||
url = getIpAfterResolving(this.ip,url); | ||
log.debug('The leader already found , leader ip:' + this.ip); | ||
url = replaceToIp(url,this.ip); | ||
return url; | ||
} else { // in case dns resolving failed on startup we still want to find the active. otherwise it will wait until the first failure. | ||
log.debug("leader IP not found, calling findActiveIp function"); | ||
log.debug("leader ip doesn't resolved it, going to resolve it"); | ||
findActiveIp.call(this).then(function () { | ||
url = getIpAfterResolving(this.ip,url); | ||
log.debug('Succeed to resolve leader ip: ' + this.ip); | ||
log.deub('Going to replace the dns with the ip'); | ||
url = replaceToIp(url,this.ip); | ||
return url; | ||
@@ -214,4 +126,4 @@ }); | ||
function getIpAfterResolving(ip,url){ | ||
log.debug("Found the ip, going to replace the url with the ip: " + ip); | ||
function replaceToIp(url,ip) { | ||
log.debug('Going to replace url:' + url + ' With ip: ' ); | ||
var urlObject = urlLib.parse(url); | ||
@@ -221,3 +133,2 @@ urlObject.host = undefined; | ||
url = urlLib.format(urlObject); | ||
log.debug("URL after resolving:" + url); | ||
return url; | ||
@@ -224,0 +135,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7
32254
619
+ Addedasync@2.4.0(transitive)
- Removedvalidator@^8.1.0
- Removedasync@2.5.0(transitive)
- Removedvalidator@8.2.0(transitive)
Updatedasync@2.4.0