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.
This commit is contained in:
Michael Basnight 2019-07-11 12:40:09 -05:00
parent d2c3f4bae9
commit b4b2ad3593
4 changed files with 35 additions and 7 deletions

View File

@ -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<String, Object> toMap(Response response) throws IOException {
return toMap(EntityUtils.toString(response.getEntity()));
}

View File

@ -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<String, EnrichPolicy> policies = getPolicies(current);
if (policies.get(name) != null) {
throw new ResourceAlreadyExistsException("policy [{}] already exists", name);
}
policies.put(name, policy);
return policies;
});

View File

@ -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"));
}
}

View File

@ -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<Exception> 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);