Validate policy type when storing an enrich policy (#48126)
This commit is contained in:
parent
1d06be2f05
commit
844825a13f
|
@ -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) {
|
||||
|
|
|
@ -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() {
|
||||
|
|
Loading…
Reference in New Issue