diff options
author | Martin Polden <mpolden@mpolden.no> | 2019-06-06 14:45:50 +0200 |
---|---|---|
committer | Martin Polden <mpolden@mpolden.no> | 2019-06-06 14:48:31 +0200 |
commit | 066dbed6da6e52e71112f478b44338ba87ab1944 (patch) | |
tree | 661c1672a373cd744324e8fe3f0a0d9ca6aa67ad /config-model-api/src/main/java/com/yahoo/config/application/api/Endpoint.java | |
parent | df757401715013727123f58f3b9e1aac6dae7f87 (diff) |
Improve validation of endpoint IDs
Same validation rules that are in place for `<rotations/>' in `services.xml`,
which we are removing.
Diffstat (limited to 'config-model-api/src/main/java/com/yahoo/config/application/api/Endpoint.java')
-rw-r--r-- | config-model-api/src/main/java/com/yahoo/config/application/api/Endpoint.java | 24 |
1 files changed, 22 insertions, 2 deletions
diff --git a/config-model-api/src/main/java/com/yahoo/config/application/api/Endpoint.java b/config-model-api/src/main/java/com/yahoo/config/application/api/Endpoint.java index 73328027540..7d31e07dac5 100644 --- a/config-model-api/src/main/java/com/yahoo/config/application/api/Endpoint.java +++ b/config-model-api/src/main/java/com/yahoo/config/application/api/Endpoint.java @@ -5,6 +5,7 @@ import com.yahoo.config.provision.RegionName; import java.util.Objects; import java.util.Optional; import java.util.Set; +import java.util.regex.Pattern; import java.util.stream.Collectors; /** @@ -13,19 +14,37 @@ import java.util.stream.Collectors; * should point to. * * If the endpointId is not set, it will default to the same as the containerId. + * + * @author ogronnesby */ public class Endpoint { + + /* + * Endpoint IDs must be: + * - lowercase + * - alphanumeric + * - begin with a character + * - contain zero consecutive dashes + * - have a length between 1 and 12 + */ + private static final Pattern endpointPattern = Pattern.compile("^[a-z](?:-?[a-z0-9]+)*$"); + private static final int endpointMaxLength = 12; + private final Optional<String> endpointId; private final String containerId; private final Set<RegionName> regions; public Endpoint(Optional<String> endpointId, String containerId, Set<String> regions) { - this.endpointId = endpointId; - this.containerId = containerId; + this.endpointId = Objects.requireNonNull(endpointId, "endpointId must be non-null"); + this.containerId = Objects.requireNonNull(containerId, "containerId must be non-null"); this.regions = Set.copyOf( Objects.requireNonNull( regions.stream().map(RegionName::from).collect(Collectors.toList()), "Missing 'regions' parameter")); + + if (endpointId().length() > endpointMaxLength || !endpointPattern.matcher(endpointId()).matches()) { + throw new IllegalArgumentException("Invalid endpoint ID: '" + endpointId() + "'"); + } } public String endpointId() { @@ -54,4 +73,5 @@ public class Endpoint { public int hashCode() { return Objects.hash(endpointId, containerId, regions); } + } |