Skip to content

Commit 8f87bb3

Browse files
authored
Don't overwrite target field with SetSecurityUserProcessor (elastic#51454)
* Don't overwrite target field with SetSecurityUserProcessor This change fix problem with `SetSecurityUserProcessor` which was overwriting whole target field and not only fields really filled by the processor. Closes elastic#51428 * Unused imports removed
1 parent 6c9ca44 commit 8f87bb3

File tree

2 files changed

+37
-8
lines changed

2 files changed

+37
-8
lines changed

x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/ingest/SetSecurityUserProcessor.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ public IngestDocument execute(IngestDocument ingestDocument) throws Exception {
5353
throw new IllegalStateException("No user for authentication");
5454
}
5555

56-
Map<String, Object> userObject = new HashMap<>();
56+
Object fieldValue = ingestDocument.getFieldValue(field, Object.class, true);
57+
58+
@SuppressWarnings("unchecked")
59+
Map<String, Object> userObject = fieldValue instanceof Map ? (Map<String, Object>) fieldValue : new HashMap<>();
60+
5761
for (Property property : properties) {
5862
switch (property) {
5963
case USERNAME:

x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/ingest/SetSecurityUserProcessorTests.java

+32-7
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
import org.elasticsearch.xpack.core.security.user.User;
1515
import org.elasticsearch.xpack.security.ingest.SetSecurityUserProcessor.Property;
1616

17+
import java.util.Arrays;
1718
import java.util.EnumSet;
1819
import java.util.HashMap;
19-
import java.util.List;
2020
import java.util.Map;
2121

2222
import static org.hamcrest.Matchers.equalTo;
@@ -37,9 +37,7 @@ public void testProcessor() throws Exception {
3737
Map<String, Object> result = ingestDocument.getFieldValue("_field", Map.class);
3838
assertThat(result.size(), equalTo(5));
3939
assertThat(result.get("username"), equalTo("_username"));
40-
assertThat(((List) result.get("roles")).size(), equalTo(2));
41-
assertThat(((List) result.get("roles")).get(0), equalTo("role1"));
42-
assertThat(((List) result.get("roles")).get(1), equalTo("role2"));
40+
assertThat(result.get("roles"), equalTo(Arrays.asList("role1", "role2")));
4341
assertThat(result.get("full_name"), equalTo("firstname lastname"));
4442
assertThat(result.get("email"), equalTo("_email"));
4543
assertThat(((Map) result.get("metadata")).size(), equalTo(1));
@@ -93,9 +91,7 @@ public void testRolesProperties() throws Exception {
9391
@SuppressWarnings("unchecked")
9492
Map<String, Object> result = ingestDocument.getFieldValue("_field", Map.class);
9593
assertThat(result.size(), equalTo(1));
96-
assertThat(((List) result.get("roles")).size(), equalTo(2));
97-
assertThat(((List) result.get("roles")).get(0), equalTo("role1"));
98-
assertThat(((List) result.get("roles")).get(1), equalTo("role2"));
94+
assertThat(result.get("roles"), equalTo(Arrays.asList("role1", "role2")));
9995
}
10096

10197
public void testFullNameProperties() throws Exception {
@@ -147,4 +143,33 @@ public void testMetadataProperties() throws Exception {
147143
assertThat(((Map) result.get("metadata")).get("key"), equalTo("value"));
148144
}
149145

146+
public void testOverwriteExistingField() throws Exception {
147+
ThreadContext threadContext = new ThreadContext(Settings.EMPTY);
148+
User user = new User("_username", null, null);
149+
Authentication.RealmRef realmRef = new Authentication.RealmRef("_name", "_type", "_node_name");
150+
threadContext.putTransient(AuthenticationField.AUTHENTICATION_KEY, new Authentication(user, realmRef, null));
151+
152+
SetSecurityUserProcessor processor = new SetSecurityUserProcessor("_tag", threadContext, "_field", EnumSet.of(Property.USERNAME));
153+
154+
IngestDocument ingestDocument = new IngestDocument(new HashMap<>(), new HashMap<>());
155+
ingestDocument.setFieldValue("_field", "test");
156+
processor.execute(ingestDocument);
157+
158+
@SuppressWarnings("unchecked")
159+
Map<String, Object> result = ingestDocument.getFieldValue("_field", Map.class);
160+
assertThat(result.size(), equalTo(1));
161+
assertThat(result.get("username"), equalTo("_username"));
162+
163+
ingestDocument = new IngestDocument(new HashMap<>(), new HashMap<>());
164+
ingestDocument.setFieldValue("_field.other", "test");
165+
ingestDocument.setFieldValue("_field.username", "test");
166+
processor.execute(ingestDocument);
167+
168+
@SuppressWarnings("unchecked")
169+
Map<String, Object> result2 = ingestDocument.getFieldValue("_field", Map.class);
170+
assertThat(result2.size(), equalTo(2));
171+
assertThat(result2.get("username"), equalTo("_username"));
172+
assertThat(result2.get("other"), equalTo("test"));
173+
}
174+
150175
}

0 commit comments

Comments
 (0)