Commit 11e2363f authored by Georg Mittendorfer's avatar Georg Mittendorfer

Fix pow node initialization

Fixed issue when normal and pow node with same url were initialized from
the config file. In this case the url check failed if both of these nodes
had the same IP.
parent 7b624aa4
Pipeline #74528523 passed with stage
in 3 minutes and 52 seconds
......@@ -86,7 +86,10 @@ public class NodeRegistry {
}
public IriNode registerIriNode(String name, String url, String key, boolean pow) {
assertNodePropertiesAndThrowIfFailure(name, url, key);
validateNodeParameters(name, url, key);
checkForDuplicateName(name);
checkForDuplicateNonPowNodeUrl(url);
CircuitBreaker circuitBreaker = circuitBreakerFactory.circuitBreaker(name);
IriNode node = new IriNode(name, url, key, pow, nodeClient, meterFactory, circuitBreaker);
nodes.put(node.getName(), node);
......@@ -96,7 +99,10 @@ public class NodeRegistry {
}
public PowNode registerPowNode(String name, String url, String key) {
assertNodePropertiesAndThrowIfFailure(name, url, key);
validateNodeParameters(name, url, key);
checkForDuplicateName(name);
// duplicate urls are allowed for pow nodes.
CircuitBreaker circuitBreaker = circuitBreakerFactory.circuitBreaker(name);
PowNode node = new PowNode(name, url, key, nodeClient, meterFactory, circuitBreaker);
nodes.put(node.getName(), node);
......@@ -104,23 +110,6 @@ public class NodeRegistry {
return node;
}
private void assertNodePropertiesAndThrowIfFailure(String name, String url, String key) {
validateNodeParameters(name, url, key);
if (nodes.containsKey(name) || nodeNames.contains(name)) {
String msg = String.format("Node with name [%s] is already registered.", name);
logger.debug("Cannot register node. {}", msg);
throw new CannotRegisterNode(CONFLICTING, msg);
}
Collection<? extends Node> nodes = this.nodes.values();
if (nodes.stream()
.filter(n -> !(n instanceof PowNode)) // do not compare pow nodes (they might have the same url as iri nodes)
.anyMatch(n -> urlValidator.areEqual(url, n.getUrl()) || urlValidator.resolveToSameAddress(url, n.getUrl()))) {
String msg = String.format("Node with url [%s] is already registered.", url);
logger.debug("Cannot register node. {}", msg);
throw new CannotRegisterNode(CONFLICTING, msg);
}
}
public IriNode unregisterNode(String name) {
if (nodeNames.contains(name) || nodes.containsKey(name)) {
logger.info("Unregistering node with name [{}]...", name);
......@@ -141,6 +130,24 @@ public class NodeRegistry {
return io.vavr.collection.HashMap.ofAll(nodes);
}
private void checkForDuplicateNonPowNodeUrl(String url) {
Collection<? extends Node> nodes = this.nodes.values();
if (nodes.stream()
.filter(n -> !(n instanceof PowNode)) // do not compare pow nodes (they might have the same url as iri nodes)
.anyMatch(n -> urlValidator.areEqual(url, n.getUrl()) || urlValidator.resolveToSameAddress(url, n.getUrl()))) {
String msg = String.format("Node with url [%s] is already registered.", url);
logger.debug("Cannot register node. {}", msg);
throw new CannotRegisterNode(CONFLICTING, msg);
}
}
private void checkForDuplicateName(String name) {
if (nodes.containsKey(name) || nodeNames.contains(name)) {
String msg = String.format("Node with name [%s] is already registered.", name);
logger.debug("Cannot register node. {}", msg);
throw new CannotRegisterNode(CONFLICTING, msg);
}
}
private void validateNodeParameters(String name, String url, String key) {
if (!isValidNodeName(name)) {
......
......@@ -35,10 +35,7 @@ public class NodeRegistryInitializer {
// TODO try to move this into the piri initialization bean
public static void initConfiguredNodes(NodeRegistry nodeRegistry, Environment env) {
for (String nodeName : splitPropertyList(env.getProperty("iri.nodes"))) {
String powEnabled = env.getProperty("iri." + nodeName + ".pow");
registerNodeAndSkipIfError(nodeRegistry, nodeName, env.getRequiredProperty("iri." + nodeName + ".url"), nodeName, Boolean.parseBoolean(powEnabled));
}
// register pow nodes first to avoid any name url conflicts
for (String nodeName : splitPropertyList(env.getProperty("pow.nodes"))) {
PowNode powNode = nodeRegistry.registerPowNode(nodeName, env.getRequiredProperty("pow." + nodeName + ".url"), nodeName);
String authHeaderValue = env.getProperty("pow." + nodeName + ".header.Authorization");
......@@ -46,6 +43,10 @@ public class NodeRegistryInitializer {
powNode.setHeadersConsumer(httpHeaders -> httpHeaders.add("Authorization", authHeaderValue));
}
}
for (String nodeName : splitPropertyList(env.getProperty("iri.nodes"))) {
String powEnabled = env.getProperty("iri." + nodeName + ".pow");
registerNodeAndSkipIfError(nodeRegistry, nodeName, env.getRequiredProperty("iri." + nodeName + ".url"), nodeName, Boolean.parseBoolean(powEnabled));
}
}
public static void initPersistedNodes(NodeRegistry nodeRegistry, NodesRepository nodesRepository) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment