This commit is contained in:
Anatoliy Sablin
2020-02-19 00:36:05 +03:00
parent 9b4aff58c7
commit b2f41d689b
12 changed files with 68 additions and 34 deletions

View File

@@ -19,7 +19,7 @@ public class HashingConfig {
private int requests = 10; private int requests = 10;
private List<Algorithm> algorithms = new ArrayList<>(); private List<Algorithm> algorithms = new ArrayList<>();
public void build() { public void build(MatrixConfig matrixConfig) {
if (isEnabled()) { if (isEnabled()) {
LOGGER.info("--- Hash configuration ---"); LOGGER.info("--- Hash configuration ---");
LOGGER.info(" Pepper length: {}", getPepperLength()); LOGGER.info(" Pepper length: {}", getPepperLength());
@@ -35,6 +35,9 @@ public class HashingConfig {
} }
LOGGER.info(" Algorithms: {}", getAlgorithms()); LOGGER.info(" Algorithms: {}", getAlgorithms());
} else { } else {
if (matrixConfig.isV2()) {
LOGGER.warn("V2 enabled without the hash configuration.");
}
LOGGER.info("Hash configuration disabled, used only `none` pepper."); LOGGER.info("Hash configuration disabled, used only `none` pepper.");
} }
} }

View File

@@ -393,7 +393,7 @@ public class MxisdConfig {
getView().build(); getView().build();
getWordpress().build(); getWordpress().build();
getPolicy().build(); getPolicy().build();
getHashing().build(); getHashing().build(getMatrix());
return this; return this;
} }

View File

@@ -1,6 +1,9 @@
package io.kamax.mxisd.hash; package io.kamax.mxisd.hash;
import io.kamax.mxisd.config.HashingConfig; 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.HashRotationStrategy;
import io.kamax.mxisd.hash.rotation.NoOpRotationStrategy; import io.kamax.mxisd.hash.rotation.NoOpRotationStrategy;
import io.kamax.mxisd.hash.rotation.RotationPerRequests; import io.kamax.mxisd.hash.rotation.RotationPerRequests;
@@ -21,7 +24,7 @@ public class HashManager {
private static final Logger LOGGER = LoggerFactory.getLogger(HashManager.class); private static final Logger LOGGER = LoggerFactory.getLogger(HashManager.class);
private HashEngine hashEngine; private Engine engine;
private HashRotationStrategy rotationStrategy; private HashRotationStrategy rotationStrategy;
private HashStorage hashStorage; private HashStorage hashStorage;
private HashingConfig config; private HashingConfig config;
@@ -32,7 +35,7 @@ public class HashManager {
this.config = config; this.config = config;
this.storage = storage; this.storage = storage;
initStorage(); initStorage();
hashEngine = new HashEngine(providers, getHashStorage(), config); engine = config.isEnabled() ? new HashEngine(providers, getHashStorage(), config) : new NoneEngine();
initRotationStrategy(); initRotationStrategy();
configured.set(true); configured.set(true);
} }
@@ -73,8 +76,8 @@ public class HashManager {
this.rotationStrategy.register(getHashEngine()); this.rotationStrategy.register(getHashEngine());
} }
public HashEngine getHashEngine() { public Engine getHashEngine() {
return hashEngine; return engine;
} }
public HashRotationStrategy getRotationStrategy() { public HashRotationStrategy getRotationStrategy() {

View File

@@ -0,0 +1,7 @@
package io.kamax.mxisd.hash.engine;
public interface Engine {
void updateHashes();
String getPepper();
}

View File

@@ -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.config.HashingConfig;
import io.kamax.mxisd.hash.storage.HashStorage; import io.kamax.mxisd.hash.storage.HashStorage;
@@ -12,7 +12,7 @@ import org.slf4j.LoggerFactory;
import java.util.Base64; import java.util.Base64;
import java.util.List; import java.util.List;
public class HashEngine { public class HashEngine implements Engine {
private static final Logger LOGGER = LoggerFactory.getLogger(HashEngine.class); private static final Logger LOGGER = LoggerFactory.getLogger(HashEngine.class);
@@ -28,6 +28,7 @@ public class HashEngine {
this.config = config; this.config = config;
} }
@Override
public void updateHashes() { public void updateHashes() {
LOGGER.info("Start update hashes."); LOGGER.info("Start update hashes.");
synchronized (hashStorage) { synchronized (hashStorage) {
@@ -48,6 +49,7 @@ public class HashEngine {
LOGGER.info("Finish update hashes."); LOGGER.info("Finish update hashes.");
} }
@Override
public String getPepper() { public String getPepper() {
synchronized (hashStorage) { synchronized (hashStorage) {
return pepper; return pepper;

View 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;
}
}

View File

@@ -1,12 +1,12 @@
package io.kamax.mxisd.hash.rotation; package io.kamax.mxisd.hash.rotation;
import io.kamax.mxisd.hash.HashEngine; import io.kamax.mxisd.hash.engine.Engine;
public interface HashRotationStrategy { public interface HashRotationStrategy {
void register(HashEngine hashEngine); void register(Engine engine);
HashEngine getHashEngine(); Engine getHashEngine();
void newRequest(); void newRequest();

View File

@@ -1,19 +1,19 @@
package io.kamax.mxisd.hash.rotation; package io.kamax.mxisd.hash.rotation;
import io.kamax.mxisd.hash.HashEngine; import io.kamax.mxisd.hash.engine.Engine;
public class NoOpRotationStrategy implements HashRotationStrategy { public class NoOpRotationStrategy implements HashRotationStrategy {
private HashEngine hashEngine; private Engine engine;
@Override @Override
public void register(HashEngine hashEngine) { public void register(Engine engine) {
this.hashEngine = hashEngine; this.engine = engine;
} }
@Override @Override
public HashEngine getHashEngine() { public Engine getHashEngine() {
return hashEngine; return engine;
} }
@Override @Override

View File

@@ -1,12 +1,12 @@
package io.kamax.mxisd.hash.rotation; 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; import java.util.concurrent.atomic.AtomicInteger;
public class RotationPerRequests implements HashRotationStrategy { public class RotationPerRequests implements HashRotationStrategy {
private HashEngine hashEngine; private Engine engine;
private final AtomicInteger counter = new AtomicInteger(0); private final AtomicInteger counter = new AtomicInteger(0);
private final int barrier; private final int barrier;
@@ -15,14 +15,14 @@ public class RotationPerRequests implements HashRotationStrategy {
} }
@Override @Override
public void register(HashEngine hashEngine) { public void register(Engine engine) {
this.hashEngine = hashEngine; this.engine = engine;
trigger(); trigger();
} }
@Override @Override
public HashEngine getHashEngine() { public Engine getHashEngine() {
return hashEngine; return engine;
} }
@Override @Override

View File

@@ -1,6 +1,6 @@
package io.kamax.mxisd.hash.rotation; 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.Executors;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
@@ -9,7 +9,7 @@ import java.util.concurrent.TimeUnit;
public class TimeBasedRotation implements HashRotationStrategy { public class TimeBasedRotation implements HashRotationStrategy {
private final long delay; private final long delay;
private HashEngine hashEngine; private Engine engine;
private final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor(); private final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
public TimeBasedRotation(long delay) { public TimeBasedRotation(long delay) {
@@ -17,15 +17,15 @@ public class TimeBasedRotation implements HashRotationStrategy {
} }
@Override @Override
public void register(HashEngine hashEngine) { public void register(Engine engine) {
this.hashEngine = hashEngine; this.engine = engine;
Runtime.getRuntime().addShutdownHook(new Thread(executorService::shutdown)); Runtime.getRuntime().addShutdownHook(new Thread(executorService::shutdown));
executorService.scheduleWithFixedDelay(this::trigger, 0, delay, TimeUnit.SECONDS); executorService.scheduleWithFixedDelay(this::trigger, 0, delay, TimeUnit.SECONDS);
} }
@Override @Override
public HashEngine getHashEngine() { public Engine getHashEngine() {
return hashEngine; return engine;
} }
@Override @Override

View File

@@ -31,6 +31,8 @@ public class HashDetailsHandler extends BasicHttpHandler {
for (HashingConfig.Algorithm algorithm : config.getAlgorithms()) { for (HashingConfig.Algorithm algorithm : config.getAlgorithms()) {
algorithms.add(algorithm.name().toLowerCase()); algorithms.add(algorithm.name().toLowerCase());
} }
} else {
algorithms.add(HashingConfig.Algorithm.none.name().toLowerCase());
} }
response.add("algorithms", algorithms); response.add("algorithms", algorithms);
return response; return response;

View File

@@ -67,10 +67,6 @@ public class HashLookupHandler extends LookupHandler implements ApiHandler {
log.info("Got bulk lookup request from {} with client {} - Is recursive? {}", log.info("Got bulk lookup request from {} with client {} - Is recursive? {}",
lookupRequest.getRequester(), lookupRequest.getUserAgent(), lookupRequest.isRecursive()); lookupRequest.getRequester(), lookupRequest.getUserAgent(), lookupRequest.isRecursive());
if (!hashManager.getConfig().isEnabled()) {
throw new InvalidParamException();
}
if (!hashManager.getHashEngine().getPepper().equals(input.getPepper())) { if (!hashManager.getHashEngine().getPepper().equals(input.getPepper())) {
throw new InvalidPepperException(); 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 { 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(); throw new InvalidParamException();
} }