Validate policy type when storing an enrich policy (#48126)

This commit is contained in:
Martijn van Groningen 2019-10-18 16:14:07 +02:00
parent 1d06be2f05
commit 844825a13f
No known key found for this signature in database
GPG Key ID: AB236F4FCF2AF12A
2 changed files with 16 additions and 1 deletions

View File

@ -16,9 +16,12 @@ import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.Strings;
import org.elasticsearch.xpack.core.enrich.EnrichPolicy;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Function;
@ -53,7 +56,11 @@ public final class EnrichStore {
if (name.toLowerCase(Locale.ROOT).equals(name) == false) {
throw new IllegalArgumentException("Invalid policy name [" + name + "], must be lowercase");
}
// TODO: add policy validation
Set<String> supportedPolicyTypes = new HashSet<>(Arrays.asList(EnrichPolicy.SUPPORTED_POLICY_TYPES));
if (supportedPolicyTypes.contains(policy.getType()) == false) {
throw new IllegalArgumentException("unsupported policy type [" + policy.getType() +
"], supported types are " + Arrays.toString(EnrichPolicy.SUPPORTED_POLICY_TYPES));
}
final EnrichPolicy finalPolicy;
if (policy.getElasticsearchVersion() == null) {

View File

@ -11,6 +11,7 @@ import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.xpack.core.enrich.EnrichPolicy;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
@ -89,6 +90,13 @@ public class EnrichStoreCrudTests extends AbstractEnrichTestCase {
expectThrows(IllegalArgumentException.class, () -> saveEnrichPolicy("myPolicy", policy, clusterService));
assertThat(error.getMessage(), equalTo("Invalid policy name [myPolicy], must be lowercase"));
}
{
EnrichPolicy invalidPolicy = new EnrichPolicy("unsupported_type", null, Collections.singletonList("index"),
"field", Collections.singletonList("field"));
IllegalArgumentException error =
expectThrows(IllegalArgumentException.class, () -> saveEnrichPolicy("name", invalidPolicy, clusterService));
assertThat(error.getMessage(), equalTo("unsupported policy type [unsupported_type], supported types are [match, geo_match]"));
}
}
public void testDeleteValidation() {