diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/ingest/SetSecurityUserProcessor.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/ingest/SetSecurityUserProcessor.java index 0c30af1879cc9..791e700ff6937 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/ingest/SetSecurityUserProcessor.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/ingest/SetSecurityUserProcessor.java @@ -53,7 +53,11 @@ public IngestDocument execute(IngestDocument ingestDocument) throws Exception { throw new IllegalStateException("No user for authentication"); } - Map userObject = new HashMap<>(); + Object fieldValue = ingestDocument.getFieldValue(field, Object.class, true); + + @SuppressWarnings("unchecked") + Map userObject = fieldValue instanceof Map ? (Map) fieldValue : new HashMap<>(); + for (Property property : properties) { switch (property) { case USERNAME: diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/ingest/SetSecurityUserProcessorTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/ingest/SetSecurityUserProcessorTests.java index 77d1ba667d1b9..3c82da113aeea 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/ingest/SetSecurityUserProcessorTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/ingest/SetSecurityUserProcessorTests.java @@ -14,9 +14,9 @@ import org.elasticsearch.xpack.core.security.user.User; import org.elasticsearch.xpack.security.ingest.SetSecurityUserProcessor.Property; +import java.util.Arrays; import java.util.EnumSet; import java.util.HashMap; -import java.util.List; import java.util.Map; import static org.hamcrest.Matchers.equalTo; @@ -37,9 +37,7 @@ public void testProcessor() throws Exception { Map result = ingestDocument.getFieldValue("_field", Map.class); assertThat(result.size(), equalTo(5)); assertThat(result.get("username"), equalTo("_username")); - assertThat(((List) result.get("roles")).size(), equalTo(2)); - assertThat(((List) result.get("roles")).get(0), equalTo("role1")); - assertThat(((List) result.get("roles")).get(1), equalTo("role2")); + assertThat(result.get("roles"), equalTo(Arrays.asList("role1", "role2"))); assertThat(result.get("full_name"), equalTo("firstname lastname")); assertThat(result.get("email"), equalTo("_email")); assertThat(((Map) result.get("metadata")).size(), equalTo(1)); @@ -93,9 +91,7 @@ public void testRolesProperties() throws Exception { @SuppressWarnings("unchecked") Map result = ingestDocument.getFieldValue("_field", Map.class); assertThat(result.size(), equalTo(1)); - assertThat(((List) result.get("roles")).size(), equalTo(2)); - assertThat(((List) result.get("roles")).get(0), equalTo("role1")); - assertThat(((List) result.get("roles")).get(1), equalTo("role2")); + assertThat(result.get("roles"), equalTo(Arrays.asList("role1", "role2"))); } public void testFullNameProperties() throws Exception { @@ -147,4 +143,33 @@ public void testMetadataProperties() throws Exception { assertThat(((Map) result.get("metadata")).get("key"), equalTo("value")); } + public void testOverwriteExistingField() throws Exception { + ThreadContext threadContext = new ThreadContext(Settings.EMPTY); + User user = new User("_username", null, null); + Authentication.RealmRef realmRef = new Authentication.RealmRef("_name", "_type", "_node_name"); + threadContext.putTransient(AuthenticationField.AUTHENTICATION_KEY, new Authentication(user, realmRef, null)); + + SetSecurityUserProcessor processor = new SetSecurityUserProcessor("_tag", threadContext, "_field", EnumSet.of(Property.USERNAME)); + + IngestDocument ingestDocument = new IngestDocument(new HashMap<>(), new HashMap<>()); + ingestDocument.setFieldValue("_field", "test"); + processor.execute(ingestDocument); + + @SuppressWarnings("unchecked") + Map result = ingestDocument.getFieldValue("_field", Map.class); + assertThat(result.size(), equalTo(1)); + assertThat(result.get("username"), equalTo("_username")); + + ingestDocument = new IngestDocument(new HashMap<>(), new HashMap<>()); + ingestDocument.setFieldValue("_field.other", "test"); + ingestDocument.setFieldValue("_field.username", "test"); + processor.execute(ingestDocument); + + @SuppressWarnings("unchecked") + Map result2 = ingestDocument.getFieldValue("_field", Map.class); + assertThat(result2.size(), equalTo(2)); + assertThat(result2.get("username"), equalTo("_username")); + assertThat(result2.get("other"), equalTo("test")); + } + }