diff options
Diffstat (limited to 'config')
4 files changed, 45 insertions, 27 deletions
diff --git a/config/src/main/java/com/yahoo/config/subscription/impl/PayloadChecksum.java b/config/src/main/java/com/yahoo/config/subscription/impl/PayloadChecksum.java index e2af5132d92..5877959ffe8 100644 --- a/config/src/main/java/com/yahoo/config/subscription/impl/PayloadChecksum.java +++ b/config/src/main/java/com/yahoo/config/subscription/impl/PayloadChecksum.java @@ -1,7 +1,18 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.config.subscription.impl; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * Class handling various type of checksums of config definition payload or config payload, + * md5 and xxhash64 are the supported types at the moment. + * + * @author hmusum + */ public class PayloadChecksum { + private static final Pattern hexChecksumPattern = Pattern.compile("[0-9a-zA-Z]+"); private final String checksum; private final Type type; @@ -19,6 +30,19 @@ public class PayloadChecksum { public Type type() { return type; } - enum Type { MD5, XXHASH64 /* not in use yet */ } + public enum Type {MD5, XXHASH64} + + public boolean valid() { + if (checksum.equals("")) return true; // Empty checksum is ok (e.g. when running 'vespa-get-config') + + if (type == Type.MD5 && checksum.length() != 32) { + return false; + } else if (type == Type.XXHASH64 && checksum.length() != 16) { + return false; + } + + Matcher m = hexChecksumPattern.matcher(checksum); + return m.matches(); + } } diff --git a/config/src/main/java/com/yahoo/vespa/config/ErrorCode.java b/config/src/main/java/com/yahoo/vespa/config/ErrorCode.java index 5fb99295325..3427dc7ddfd 100644 --- a/config/src/main/java/com/yahoo/vespa/config/ErrorCode.java +++ b/config/src/main/java/com/yahoo/vespa/config/ErrorCode.java @@ -11,6 +11,7 @@ public final class ErrorCode { public static final int UNKNOWN_DEFINITION = UNKNOWN_CONFIG + 1; public static final int UNKNOWN_DEF_MD5 = UNKNOWN_CONFIG + 4; public static final int UNKNOWN_VESPA_VERSION = UNKNOWN_CONFIG + 5; + public static final int UNKNOWN_DEF_CHECKSUM = UNKNOWN_CONFIG + 6; public static final int ILLEGAL_NAME = UNKNOWN_CONFIG + 100; // Version is not a number @@ -25,6 +26,8 @@ public final class ErrorCode { public static final int ILLEGAL_NAME_SPACE = UNKNOWN_CONFIG + 108; public static final int ILLEGAL_PROTOCOL_VERSION = UNKNOWN_CONFIG + 109; public static final int ILLEGAL_CLIENT_HOSTNAME = UNKNOWN_CONFIG + 110; + public static final int ILLEGAL_DEF_CHECKSUM = UNKNOWN_CONFIG + 111; + public static final int ILLEGAL_CONFIG_CHECKSUM = UNKNOWN_CONFIG + 112; // hasUpdatedConfig() is true, but generation says the config is older than previous config. public static final int OUTDATED_CONFIG = UNKNOWN_CONFIG + 150; diff --git a/config/src/main/java/com/yahoo/vespa/config/protocol/RequestValidation.java b/config/src/main/java/com/yahoo/vespa/config/protocol/RequestValidation.java index 478dc6c45d6..81ea99e38b4 100644 --- a/config/src/main/java/com/yahoo/vespa/config/protocol/RequestValidation.java +++ b/config/src/main/java/com/yahoo/vespa/config/protocol/RequestValidation.java @@ -1,13 +1,13 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.vespa.config.protocol; +import com.yahoo.config.subscription.impl.PayloadChecksum; import com.yahoo.vespa.config.ConfigDefinition; import com.yahoo.vespa.config.ConfigKey; import com.yahoo.vespa.config.ErrorCode; import java.util.logging.Logger; import java.util.regex.Matcher; -import java.util.regex.Pattern; import static java.util.logging.Level.INFO; @@ -19,8 +19,6 @@ import static java.util.logging.Level.INFO; public class RequestValidation { private static final Logger log = Logger.getLogger(RequestValidation.class.getName()); - private static final Pattern md5Pattern = Pattern.compile("[0-9a-zA-Z]+"); - public static int validateRequest(JRTConfigRequest request) { ConfigKey<?> key = request.getConfigKey(); if (!RequestValidation.verifyName(key.getName())) { @@ -31,13 +29,13 @@ public class RequestValidation { log.log(INFO, "Illegal name space '" + key.getNamespace() + "'"); return ErrorCode.ILLEGAL_NAME_SPACE; } - if (!RequestValidation.verifyMd5(key.getMd5())) { - log.log(INFO, "Illegal md5 sum '" + key.getNamespace() + "'"); - return ErrorCode.ILLEGAL_DEF_MD5; + if (!(new PayloadChecksum(key.getMd5()).valid())) { + log.log(INFO, "Illegal checksum '" + key.getNamespace() + "'"); + return ErrorCode.ILLEGAL_DEF_MD5; // TODO: Use ILLEGAL_DEF_CHECKSUM } - if (!RequestValidation.verifyMd5(request.getRequestConfigMd5())) { - log.log(INFO, "Illegal config md5 '" + request.getRequestConfigMd5() + "'"); - return ErrorCode.ILLEGAL_CONFIG_MD5; + if (!new PayloadChecksum(request.getRequestConfigMd5()).valid()) { + log.log(INFO, "Illegal config checksum '" + request.getRequestConfigMd5() + "'"); + return ErrorCode.ILLEGAL_CONFIG_MD5; // TODO: Use ILLEGAL_CONFIG_CHECKSUM } if (!RequestValidation.verifyGeneration(request.getRequestGeneration())) { log.log(INFO, "Illegal generation '" + request.getRequestGeneration() + "'"); @@ -59,16 +57,6 @@ public class RequestValidation { return m.matches(); } - public static boolean verifyMd5(String md5) { - if (md5.equals("")) { - return true; // Empty md5 is ok (e.g. upon getconfig from command line tools) - } else if (md5.length() != 32) { - return false; - } - Matcher m = md5Pattern.matcher(md5); - return m.matches(); - } - public static boolean verifyTimeout(Long timeout) { return (timeout > 0); } @@ -85,4 +73,5 @@ public class RequestValidation { private static boolean verifyHostname(String clientHostName) { return !("".equals(clientHostName)); } + } diff --git a/config/src/test/java/com/yahoo/vespa/config/RequestValidationTest.java b/config/src/test/java/com/yahoo/vespa/config/RequestValidationTest.java index 3f625013f27..8c11db15f7c 100644 --- a/config/src/test/java/com/yahoo/vespa/config/RequestValidationTest.java +++ b/config/src/test/java/com/yahoo/vespa/config/RequestValidationTest.java @@ -1,6 +1,7 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.vespa.config; +import com.yahoo.config.subscription.impl.PayloadChecksum; import com.yahoo.vespa.config.protocol.RequestValidation; import org.junit.Test; @@ -20,11 +21,11 @@ public class RequestValidationTest { @Test public void testVerifyDefMd5() { - assertTrue(RequestValidation.verifyMd5("")); - assertTrue(RequestValidation.verifyMd5("e8f0c01c7c3dcb8d3f62d7ff777fce6b")); - assertTrue(RequestValidation.verifyMd5("e8f0c01c7c3dcb8d3f62d7ff777fce6B")); - assertFalse(RequestValidation.verifyMd5("aaaaaaaaaaaaaaaaaa")); - assertFalse(RequestValidation.verifyMd5("-8f0c01c7c3dcb8d3f62d7ff777fce6b")); + assertTrue(PayloadChecksum.empty().valid()); + assertTrue(new PayloadChecksum("e8f0c01c7c3dcb8d3f62d7ff777fce6b").valid()); + assertTrue(new PayloadChecksum("e8f0c01c7c3dcb8d3f62d7ff777fce6B").valid()); + assertFalse(new PayloadChecksum("aaaaaaaaaaaaaaaaaa").valid()); + assertFalse(new PayloadChecksum("-8f0c01c7c3dcb8d3f62d7ff777fce6b").valid()); } @Test @@ -32,4 +33,5 @@ public class RequestValidationTest { assertTrue(RequestValidation.verifyTimeout(1000L)); assertFalse(RequestValidation.verifyTimeout(-1000L)); } + } |