#26 fix.
This commit is contained in:
@@ -19,7 +19,7 @@ public class HashingConfig {
|
||||
private int requests = 10;
|
||||
private List<Algorithm> algorithms = new ArrayList<>();
|
||||
|
||||
public void build() {
|
||||
public void build(MatrixConfig matrixConfig) {
|
||||
if (isEnabled()) {
|
||||
LOGGER.info("--- Hash configuration ---");
|
||||
LOGGER.info(" Pepper length: {}", getPepperLength());
|
||||
@@ -35,6 +35,9 @@ public class HashingConfig {
|
||||
}
|
||||
LOGGER.info(" Algorithms: {}", getAlgorithms());
|
||||
} else {
|
||||
if (matrixConfig.isV2()) {
|
||||
LOGGER.warn("V2 enabled without the hash configuration.");
|
||||
}
|
||||
LOGGER.info("Hash configuration disabled, used only `none` pepper.");
|
||||
}
|
||||
}
|
||||
|
@@ -393,7 +393,7 @@ public class MxisdConfig {
|
||||
getView().build();
|
||||
getWordpress().build();
|
||||
getPolicy().build();
|
||||
getHashing().build();
|
||||
getHashing().build(getMatrix());
|
||||
|
||||
return this;
|
||||
}
|
||||
|
@@ -1,6 +1,9 @@
|
||||
package io.kamax.mxisd.hash;
|
||||
|
||||
import io.kamax.mxisd.config.HashingConfig;
|
||||
import io.kamax.mxisd.hash.engine.Engine;
|
||||
import io.kamax.mxisd.hash.engine.HashEngine;
|
||||
import io.kamax.mxisd.hash.engine.NoneEngine;
|
||||
import io.kamax.mxisd.hash.rotation.HashRotationStrategy;
|
||||
import io.kamax.mxisd.hash.rotation.NoOpRotationStrategy;
|
||||
import io.kamax.mxisd.hash.rotation.RotationPerRequests;
|
||||
@@ -21,7 +24,7 @@ public class HashManager {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(HashManager.class);
|
||||
|
||||
private HashEngine hashEngine;
|
||||
private Engine engine;
|
||||
private HashRotationStrategy rotationStrategy;
|
||||
private HashStorage hashStorage;
|
||||
private HashingConfig config;
|
||||
@@ -32,7 +35,7 @@ public class HashManager {
|
||||
this.config = config;
|
||||
this.storage = storage;
|
||||
initStorage();
|
||||
hashEngine = new HashEngine(providers, getHashStorage(), config);
|
||||
engine = config.isEnabled() ? new HashEngine(providers, getHashStorage(), config) : new NoneEngine();
|
||||
initRotationStrategy();
|
||||
configured.set(true);
|
||||
}
|
||||
@@ -73,8 +76,8 @@ public class HashManager {
|
||||
this.rotationStrategy.register(getHashEngine());
|
||||
}
|
||||
|
||||
public HashEngine getHashEngine() {
|
||||
return hashEngine;
|
||||
public Engine getHashEngine() {
|
||||
return engine;
|
||||
}
|
||||
|
||||
public HashRotationStrategy getRotationStrategy() {
|
||||
|
7
src/main/java/io/kamax/mxisd/hash/engine/Engine.java
Normal file
7
src/main/java/io/kamax/mxisd/hash/engine/Engine.java
Normal file
@@ -0,0 +1,7 @@
|
||||
package io.kamax.mxisd.hash.engine;
|
||||
|
||||
public interface Engine {
|
||||
void updateHashes();
|
||||
|
||||
String getPepper();
|
||||
}
|
@@ -1,4 +1,4 @@
|
||||
package io.kamax.mxisd.hash;
|
||||
package io.kamax.mxisd.hash.engine;
|
||||
|
||||
import io.kamax.mxisd.config.HashingConfig;
|
||||
import io.kamax.mxisd.hash.storage.HashStorage;
|
||||
@@ -12,7 +12,7 @@ import org.slf4j.LoggerFactory;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
|
||||
public class HashEngine {
|
||||
public class HashEngine implements Engine {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(HashEngine.class);
|
||||
|
||||
@@ -28,6 +28,7 @@ public class HashEngine {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateHashes() {
|
||||
LOGGER.info("Start update hashes.");
|
||||
synchronized (hashStorage) {
|
||||
@@ -48,6 +49,7 @@ public class HashEngine {
|
||||
LOGGER.info("Finish update hashes.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPepper() {
|
||||
synchronized (hashStorage) {
|
||||
return pepper;
|
21
src/main/java/io/kamax/mxisd/hash/engine/NoneEngine.java
Normal file
21
src/main/java/io/kamax/mxisd/hash/engine/NoneEngine.java
Normal file
@@ -0,0 +1,21 @@
|
||||
package io.kamax.mxisd.hash.engine;
|
||||
|
||||
import org.apache.commons.lang3.RandomStringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class NoneEngine implements Engine {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(NoneEngine.class);
|
||||
private static final String PEPPER = RandomStringUtils.random(8, true, true);
|
||||
|
||||
@Override
|
||||
public void updateHashes() {
|
||||
LOGGER.info("Nothing to update.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPepper() {
|
||||
return PEPPER;
|
||||
}
|
||||
}
|
@@ -1,12 +1,12 @@
|
||||
package io.kamax.mxisd.hash.rotation;
|
||||
|
||||
import io.kamax.mxisd.hash.HashEngine;
|
||||
import io.kamax.mxisd.hash.engine.Engine;
|
||||
|
||||
public interface HashRotationStrategy {
|
||||
|
||||
void register(HashEngine hashEngine);
|
||||
void register(Engine engine);
|
||||
|
||||
HashEngine getHashEngine();
|
||||
Engine getHashEngine();
|
||||
|
||||
void newRequest();
|
||||
|
||||
|
@@ -1,19 +1,19 @@
|
||||
package io.kamax.mxisd.hash.rotation;
|
||||
|
||||
import io.kamax.mxisd.hash.HashEngine;
|
||||
import io.kamax.mxisd.hash.engine.Engine;
|
||||
|
||||
public class NoOpRotationStrategy implements HashRotationStrategy {
|
||||
|
||||
private HashEngine hashEngine;
|
||||
private Engine engine;
|
||||
|
||||
@Override
|
||||
public void register(HashEngine hashEngine) {
|
||||
this.hashEngine = hashEngine;
|
||||
public void register(Engine engine) {
|
||||
this.engine = engine;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HashEngine getHashEngine() {
|
||||
return hashEngine;
|
||||
public Engine getHashEngine() {
|
||||
return engine;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -1,12 +1,12 @@
|
||||
package io.kamax.mxisd.hash.rotation;
|
||||
|
||||
import io.kamax.mxisd.hash.HashEngine;
|
||||
import io.kamax.mxisd.hash.engine.Engine;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public class RotationPerRequests implements HashRotationStrategy {
|
||||
|
||||
private HashEngine hashEngine;
|
||||
private Engine engine;
|
||||
private final AtomicInteger counter = new AtomicInteger(0);
|
||||
private final int barrier;
|
||||
|
||||
@@ -15,14 +15,14 @@ public class RotationPerRequests implements HashRotationStrategy {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void register(HashEngine hashEngine) {
|
||||
this.hashEngine = hashEngine;
|
||||
public void register(Engine engine) {
|
||||
this.engine = engine;
|
||||
trigger();
|
||||
}
|
||||
|
||||
@Override
|
||||
public HashEngine getHashEngine() {
|
||||
return hashEngine;
|
||||
public Engine getHashEngine() {
|
||||
return engine;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -1,6 +1,6 @@
|
||||
package io.kamax.mxisd.hash.rotation;
|
||||
|
||||
import io.kamax.mxisd.hash.HashEngine;
|
||||
import io.kamax.mxisd.hash.engine.Engine;
|
||||
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
@@ -9,7 +9,7 @@ import java.util.concurrent.TimeUnit;
|
||||
public class TimeBasedRotation implements HashRotationStrategy {
|
||||
|
||||
private final long delay;
|
||||
private HashEngine hashEngine;
|
||||
private Engine engine;
|
||||
private final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
|
||||
|
||||
public TimeBasedRotation(long delay) {
|
||||
@@ -17,15 +17,15 @@ public class TimeBasedRotation implements HashRotationStrategy {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void register(HashEngine hashEngine) {
|
||||
this.hashEngine = hashEngine;
|
||||
public void register(Engine engine) {
|
||||
this.engine = engine;
|
||||
Runtime.getRuntime().addShutdownHook(new Thread(executorService::shutdown));
|
||||
executorService.scheduleWithFixedDelay(this::trigger, 0, delay, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
@Override
|
||||
public HashEngine getHashEngine() {
|
||||
return hashEngine;
|
||||
public Engine getHashEngine() {
|
||||
return engine;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -31,6 +31,8 @@ public class HashDetailsHandler extends BasicHttpHandler {
|
||||
for (HashingConfig.Algorithm algorithm : config.getAlgorithms()) {
|
||||
algorithms.add(algorithm.name().toLowerCase());
|
||||
}
|
||||
} else {
|
||||
algorithms.add(HashingConfig.Algorithm.none.name().toLowerCase());
|
||||
}
|
||||
response.add("algorithms", algorithms);
|
||||
return response;
|
||||
|
@@ -67,10 +67,6 @@ public class HashLookupHandler extends LookupHandler implements ApiHandler {
|
||||
log.info("Got bulk lookup request from {} with client {} - Is recursive? {}",
|
||||
lookupRequest.getRequester(), lookupRequest.getUserAgent(), lookupRequest.isRecursive());
|
||||
|
||||
if (!hashManager.getConfig().isEnabled()) {
|
||||
throw new InvalidParamException();
|
||||
}
|
||||
|
||||
if (!hashManager.getHashEngine().getPepper().equals(input.getPepper())) {
|
||||
throw new InvalidPepperException();
|
||||
}
|
||||
@@ -89,7 +85,7 @@ public class HashLookupHandler extends LookupHandler implements ApiHandler {
|
||||
}
|
||||
|
||||
private void noneAlgorithm(HttpServerExchange exchange, HashLookupRequest request, ClientHashLookupRequest input) throws Exception {
|
||||
if (!hashManager.getConfig().getAlgorithms().contains(HashingConfig.Algorithm.none)) {
|
||||
if (hashManager.getConfig().isEnabled() && !hashManager.getConfig().getAlgorithms().contains(HashingConfig.Algorithm.none)) {
|
||||
throw new InvalidParamException();
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user