From b4b2ad3593a4fbff7e6f747ddcfe8c6ff984a208 Mon Sep 17 00:00:00 2001 From: Michael Basnight Date: Thu, 11 Jul 2019 12:40:09 -0500 Subject: [PATCH] Ensure enrich policy is immutable (#43604) This commit ensures the policy cannot be overwritten. An error is thrown if the policy exists. All tests have been updated accordingly. --- .../test/enrich/CommonEnrichRestTestCase.java | 11 +++++++++++ .../elasticsearch/xpack/enrich/EnrichStore.java | 4 ++++ .../xpack/enrich/EnrichPolicyUpdateTests.java | 11 ++++------- .../xpack/enrich/EnrichStoreTests.java | 16 ++++++++++++++++ 4 files changed, 35 insertions(+), 7 deletions(-) diff --git a/x-pack/plugin/enrich/qa/common/src/main/java/org/elasticsearch/test/enrich/CommonEnrichRestTestCase.java b/x-pack/plugin/enrich/qa/common/src/main/java/org/elasticsearch/test/enrich/CommonEnrichRestTestCase.java index da7579cfcbe..070859b4f48 100644 --- a/x-pack/plugin/enrich/qa/common/src/main/java/org/elasticsearch/test/enrich/CommonEnrichRestTestCase.java +++ b/x-pack/plugin/enrich/qa/common/src/main/java/org/elasticsearch/test/enrich/CommonEnrichRestTestCase.java @@ -8,6 +8,7 @@ package org.elasticsearch.test.enrich; import org.apache.http.util.EntityUtils; import org.elasticsearch.client.Request; import org.elasticsearch.client.Response; +import org.elasticsearch.client.ResponseException; import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.test.rest.ESRestTestCase; @@ -76,6 +77,16 @@ public abstract class CommonEnrichRestTestCase extends ESRestTestCase { assertThat(_source.get("tld_rank"), equalTo(7)); } + public void testImmutablePolicy() throws IOException { + Request putPolicyRequest = new Request("PUT", "/_enrich/policy/my_policy"); + putPolicyRequest.setJsonEntity("{\"type\": \"exact_match\",\"indices\": [\"my-source-index\"], \"enrich_key\": \"host\", " + + "\"enrich_values\": [\"globalRank\", \"tldRank\", \"tld\"]}"); + assertOK(client().performRequest(putPolicyRequest)); + + ResponseException exc = expectThrows(ResponseException.class, () -> client().performRequest(putPolicyRequest)); + assertTrue(exc.getMessage().contains("policy [my_policy] already exists")); + } + private static Map toMap(Response response) throws IOException { return toMap(EntityUtils.toString(response.getEntity())); } diff --git a/x-pack/plugin/enrich/src/main/java/org/elasticsearch/xpack/enrich/EnrichStore.java b/x-pack/plugin/enrich/src/main/java/org/elasticsearch/xpack/enrich/EnrichStore.java index 48dd36177ae..36b399b3d3d 100644 --- a/x-pack/plugin/enrich/src/main/java/org/elasticsearch/xpack/enrich/EnrichStore.java +++ b/x-pack/plugin/enrich/src/main/java/org/elasticsearch/xpack/enrich/EnrichStore.java @@ -5,6 +5,7 @@ */ package org.elasticsearch.xpack.enrich; +import org.elasticsearch.ResourceAlreadyExistsException; import org.elasticsearch.ResourceNotFoundException; import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.ClusterStateUpdateTask; @@ -46,6 +47,9 @@ public final class EnrichStore { updateClusterState(clusterService, handler, current -> { final Map policies = getPolicies(current); + if (policies.get(name) != null) { + throw new ResourceAlreadyExistsException("policy [{}] already exists", name); + } policies.put(name, policy); return policies; }); diff --git a/x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/EnrichPolicyUpdateTests.java b/x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/EnrichPolicyUpdateTests.java index 1e79a3e18f4..2847273485e 100644 --- a/x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/EnrichPolicyUpdateTests.java +++ b/x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/EnrichPolicyUpdateTests.java @@ -5,6 +5,7 @@ */ package org.elasticsearch.xpack.enrich; +import org.elasticsearch.ResourceAlreadyExistsException; import org.elasticsearch.action.ingest.PutPipelineRequest; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.xcontent.XContentType; @@ -21,7 +22,6 @@ import java.util.Collections; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.instanceOf; -import static org.hamcrest.Matchers.sameInstance; public class EnrichPolicyUpdateTests extends ESSingleNodeTestCase { @@ -49,11 +49,8 @@ public class EnrichPolicyUpdateTests extends ESSingleNodeTestCase { EnrichPolicy instance2 = new EnrichPolicy(EnrichPolicy.EXACT_MATCH_TYPE, null, Collections.singletonList("index"), "key2", Collections.singletonList("field2")); - putPolicyRequest = new PutEnrichPolicyAction.Request("my_policy", instance2); - assertAcked(client().execute(PutEnrichPolicyAction.INSTANCE, putPolicyRequest).actionGet()); - assertThat(enrichProcessorFactory.policies.get("my_policy"), equalTo(instance2)); - - Pipeline pipelineInstance2 = ingestService.getPipeline("1"); - assertThat(pipelineInstance2, sameInstance(pipelineInstance1)); + ResourceAlreadyExistsException exc = expectThrows(ResourceAlreadyExistsException.class, () -> + client().execute(PutEnrichPolicyAction.INSTANCE, new PutEnrichPolicyAction.Request("my_policy", instance2)).actionGet()); + assertTrue(exc.getMessage().contains("policy [my_policy] already exists")); } } diff --git a/x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/EnrichStoreTests.java b/x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/EnrichStoreTests.java index 05be4ae6ea2..c9c545b9501 100644 --- a/x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/EnrichStoreTests.java +++ b/x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/EnrichStoreTests.java @@ -41,6 +41,22 @@ public class EnrichStoreTests extends ESSingleNodeTestCase { assertThat(result, nullValue()); } + public void testImmutability() throws Exception { + EnrichPolicy policy = randomEnrichPolicy(XContentType.JSON); + ClusterService clusterService = getInstanceFromNode(ClusterService.class); + String name = "my-policy"; + + AtomicReference error = saveEnrichPolicy(name, policy, clusterService); + assertThat(error.get(), nullValue()); + + error = saveEnrichPolicy(name, policy, clusterService); + assertTrue(error.get().getMessage().contains("policy [my-policy] already exists"));; + + deleteEnrichPolicy(name, clusterService); + EnrichPolicy result = EnrichStore.getPolicy(name, clusterService.state()); + assertThat(result, nullValue()); + } + public void testPutValidation() throws Exception { EnrichPolicy policy = randomEnrichPolicy(XContentType.JSON); ClusterService clusterService = getInstanceFromNode(ClusterService.class);