Skip to content

Commit b1c9c5c

Browse files
authored
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.
1 parent 34b6067 commit b1c9c5c

File tree

4 files changed

+35
-7
lines changed

4 files changed

+35
-7
lines changed

x-pack/plugin/enrich/qa/common/src/main/java/org/elasticsearch/test/enrich/CommonEnrichRestTestCase.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.apache.http.util.EntityUtils;
99
import org.elasticsearch.client.Request;
1010
import org.elasticsearch.client.Response;
11+
import org.elasticsearch.client.ResponseException;
1112
import org.elasticsearch.common.xcontent.XContentHelper;
1213
import org.elasticsearch.common.xcontent.json.JsonXContent;
1314
import org.elasticsearch.test.rest.ESRestTestCase;
@@ -76,6 +77,16 @@ public void testBasicFlow() throws Exception {
7677
assertThat(_source.get("tld_rank"), equalTo(7));
7778
}
7879

80+
public void testImmutablePolicy() throws IOException {
81+
Request putPolicyRequest = new Request("PUT", "/_enrich/policy/my_policy");
82+
putPolicyRequest.setJsonEntity("{\"type\": \"exact_match\",\"indices\": [\"my-source-index\"], \"enrich_key\": \"host\", " +
83+
"\"enrich_values\": [\"globalRank\", \"tldRank\", \"tld\"]}");
84+
assertOK(client().performRequest(putPolicyRequest));
85+
86+
ResponseException exc = expectThrows(ResponseException.class, () -> client().performRequest(putPolicyRequest));
87+
assertTrue(exc.getMessage().contains("policy [my_policy] already exists"));
88+
}
89+
7990
private static Map<String, Object> toMap(Response response) throws IOException {
8091
return toMap(EntityUtils.toString(response.getEntity()));
8192
}

x-pack/plugin/enrich/src/main/java/org/elasticsearch/xpack/enrich/EnrichStore.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
package org.elasticsearch.xpack.enrich;
77

8+
import org.elasticsearch.ResourceAlreadyExistsException;
89
import org.elasticsearch.ResourceNotFoundException;
910
import org.elasticsearch.cluster.ClusterState;
1011
import org.elasticsearch.cluster.ClusterStateUpdateTask;
@@ -46,6 +47,9 @@ public static void putPolicy(String name, EnrichPolicy policy, ClusterService cl
4647

4748
updateClusterState(clusterService, handler, current -> {
4849
final Map<String, EnrichPolicy> policies = getPolicies(current);
50+
if (policies.get(name) != null) {
51+
throw new ResourceAlreadyExistsException("policy [{}] already exists", name);
52+
}
4953
policies.put(name, policy);
5054
return policies;
5155
});

x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/EnrichPolicyUpdateTests.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
package org.elasticsearch.xpack.enrich;
77

8+
import org.elasticsearch.ResourceAlreadyExistsException;
89
import org.elasticsearch.action.ingest.PutPipelineRequest;
910
import org.elasticsearch.common.bytes.BytesArray;
1011
import org.elasticsearch.common.xcontent.XContentType;
@@ -21,7 +22,6 @@
2122
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
2223
import static org.hamcrest.Matchers.equalTo;
2324
import static org.hamcrest.Matchers.instanceOf;
24-
import static org.hamcrest.Matchers.sameInstance;
2525

2626
public class EnrichPolicyUpdateTests extends ESSingleNodeTestCase {
2727

@@ -49,11 +49,8 @@ public void testUpdatePolicyOnly() {
4949

5050
EnrichPolicy instance2 =
5151
new EnrichPolicy(EnrichPolicy.EXACT_MATCH_TYPE, null, List.of("index"), "key2", List.of("field2"));
52-
putPolicyRequest = new PutEnrichPolicyAction.Request("my_policy", instance2);
53-
assertAcked(client().execute(PutEnrichPolicyAction.INSTANCE, putPolicyRequest).actionGet());
54-
assertThat(enrichProcessorFactory.policies.get("my_policy"), equalTo(instance2));
55-
56-
Pipeline pipelineInstance2 = ingestService.getPipeline("1");
57-
assertThat(pipelineInstance2, sameInstance(pipelineInstance1));
52+
ResourceAlreadyExistsException exc = expectThrows(ResourceAlreadyExistsException.class, () ->
53+
client().execute(PutEnrichPolicyAction.INSTANCE, new PutEnrichPolicyAction.Request("my_policy", instance2)).actionGet());
54+
assertTrue(exc.getMessage().contains("policy [my_policy] already exists"));
5855
}
5956
}

x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/EnrichStoreTests.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,22 @@ public void testCrud() throws Exception {
4141
assertThat(result, nullValue());
4242
}
4343

44+
public void testImmutability() throws Exception {
45+
EnrichPolicy policy = randomEnrichPolicy(XContentType.JSON);
46+
ClusterService clusterService = getInstanceFromNode(ClusterService.class);
47+
String name = "my-policy";
48+
49+
AtomicReference<Exception> error = saveEnrichPolicy(name, policy, clusterService);
50+
assertThat(error.get(), nullValue());
51+
52+
error = saveEnrichPolicy(name, policy, clusterService);
53+
assertTrue(error.get().getMessage().contains("policy [my-policy] already exists"));;
54+
55+
deleteEnrichPolicy(name, clusterService);
56+
EnrichPolicy result = EnrichStore.getPolicy(name, clusterService.state());
57+
assertThat(result, nullValue());
58+
}
59+
4460
public void testPutValidation() throws Exception {
4561
EnrichPolicy policy = randomEnrichPolicy(XContentType.JSON);
4662
ClusterService clusterService = getInstanceFromNode(ClusterService.class);

0 commit comments

Comments
 (0)