aboutsummaryrefslogtreecommitdiffstats
path: root/config/src/main/java/com/yahoo/vespa/config/protocol/RequestValidation.java
blob: 66afaa910e66b5343d2f55e5d30164724fd38aba (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright Vespa.ai. 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.vespa.config.ConfigDefinition;
import com.yahoo.vespa.config.ConfigKey;
import com.yahoo.vespa.config.ErrorCode;
import com.yahoo.vespa.config.PayloadChecksum;

import java.util.logging.Logger;
import java.util.regex.Matcher;

import static com.yahoo.vespa.config.PayloadChecksum.Type.MD5;
import static java.util.logging.Level.WARNING;

/**
 * Static utility methods for verifying common request properties.
 *
 * @author Ulf Lilleengen
 */
public class RequestValidation {
    private static final Logger log = Logger.getLogger(RequestValidation.class.getName());

    public static int validateRequest(JRTConfigRequest request) {
        ConfigKey<?> key = request.getConfigKey();
        if (!RequestValidation.verifyName(key.getName())) {
            log.log(WARNING, "Illegal name '" + key.getName() + "'");
            return ErrorCode.ILLEGAL_NAME;
        }
        if (!RequestValidation.verifyNamespace(key.getNamespace())) {
            log.log(WARNING, "Illegal name space '" + key.getNamespace() + "'");
            return ErrorCode.ILLEGAL_NAME_SPACE;
        }
        if (!(new PayloadChecksum(request.getRequestDefMd5(), MD5).valid())) {
            log.log(WARNING, "Illegal checksum '" + key.getNamespace() + "'");
            return ErrorCode.ILLEGAL_DEF_MD5;  // TODO: Use ILLEGAL_DEF_CHECKSUM
        }
        if (! request.getRequestConfigChecksums().valid()) {
            log.log(WARNING, "Illegal config checksum '" + request.getRequestConfigChecksums() + "'");
            return ErrorCode.ILLEGAL_CONFIG_MD5; // TODO: Use ILLEGAL_CONFIG_CHECKSUM
        }
        if (!RequestValidation.verifyGeneration(request.getRequestGeneration())) {
            log.log(WARNING, "Illegal generation '" + request.getRequestGeneration() + "'");
            return ErrorCode.ILLEGAL_GENERATION;
        }
        if (!RequestValidation.verifyTimeout(request.getTimeout())) {
            log.log(WARNING, "Illegal timeout '" + request.getTimeout() + "'");
            return ErrorCode.ILLEGAL_TIMEOUT;
        }
        if (!RequestValidation.verifyHostname(request.getClientHostName())) {
            log.log(WARNING, "Illegal client host name '" + request.getClientHostName() + "'");
            return ErrorCode.ILLEGAL_CLIENT_HOSTNAME;
        }
        return 0;
    }

    public static boolean verifyName(String name) {
        Matcher m = ConfigDefinition.namePattern.matcher(name);
        return m.matches();
    }

    public static boolean verifyTimeout(Long timeout) {
        return (timeout > 0);
    }

    private static boolean verifyGeneration(Long generation) {
        return (generation >= 0);
    }

    private static boolean verifyNamespace(String namespace) {
        Matcher m = ConfigDefinition.namespacePattern.matcher(namespace);
        return m.matches();
    }

    private static boolean verifyHostname(String clientHostName) {
        return !("".equals(clientHostName));
    }

}