Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Maintenance-2930] Review use of JSON Flattener #3674

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -579,13 +579,6 @@ dependencies {
implementation "io.jsonwebtoken:jjwt-api:${jjwt_version}"
implementation "io.jsonwebtoken:jjwt-impl:${jjwt_version}"
implementation "io.jsonwebtoken:jjwt-jackson:${jjwt_version}"
// JSON flattener
implementation ("com.github.wnameless.json:json-base:2.4.3") {
exclude group: "org.glassfish", module: "jakarta.json"
exclude group: "com.google.code.gson", module: "gson"
exclude group: "org.json", module: "json"
}
implementation 'com.github.wnameless.json:json-flattener:0.16.6'
// JSON patch
implementation 'com.flipkart.zjsonpatch:zjsonpatch:0.4.14'
implementation 'org.apache.commons:commons-collections4:4.4'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,10 @@
import org.opensearch.security.auditlog.AuditLog;
import org.opensearch.security.dlic.rest.support.Utils;
import org.opensearch.security.support.HeaderHelper;
import org.opensearch.security.support.JsonFlattener;
import org.opensearch.security.support.SourceFieldsContext;
import org.opensearch.security.support.WildcardMatcher;

import com.github.wnameless.json.flattener.JsonFlattener;
prabhask5 marked this conversation as resolved.
Show resolved Hide resolved

//TODO We need to deal with caching!!
//Currently we disable caching (and realtime requests) when FLS or DLS is applied
//Check if we can hook in into the caches
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.opensearch.security.support;
prabhask5 marked this conversation as resolved.
Show resolved Hide resolved

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;

import com.fasterxml.jackson.core.type.TypeReference;

import org.opensearch.core.common.Strings;
import org.opensearch.security.DefaultObjectMapper;

public class JsonFlattener {

public static Map<String, Object> flattenAsMap(String jsonString) {
try {
final TypeReference<Map<String, Object>> typeReference = new TypeReference<>() {
};
final Map<String, Object> jsonMap = DefaultObjectMapper.objectMapper.readValue(jsonString, typeReference);
final Map<String, Object> flattenMap = new LinkedHashMap<>();
prabhask5 marked this conversation as resolved.
Show resolved Hide resolved
flattenEntries("", jsonMap.entrySet(), flattenMap);
return flattenMap;
} catch (final IOException ioe) {
throw new IllegalArgumentException("Unparseable json", ioe);
}
}

private static void flattenEntries(String prefix, final Iterable<Map.Entry<String, Object>> entries, final Map<String, Object> result) {
if (!Strings.isNullOrEmpty(prefix)) {
prefix += ".";
}

for (final Map.Entry<String, Object> e : entries) {
flattenElement(prefix.concat(e.getKey()), e.getValue(), result);
}
}

@SuppressWarnings("unchecked")
private static void flattenElement(String prefix, final Object source, final Map<String, Object> result) {
if (source instanceof Iterable) {
flattenCollection(prefix, (Iterable<Object>) source, result);
}
if (source instanceof Map) {
flattenEntries(prefix, ((Map<String, Object>) source).entrySet(), result);
}
result.put(prefix, source);
}

private static void flattenCollection(String prefix, final Iterable<Object> objects, final Map<String, Object> result) {
int counter = 0;
for (final Object o : objects) {
flattenElement(prefix + "[" + counter + "]", o, result);
counter++;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.opensearch.security.support;
prabhask5 marked this conversation as resolved.
Show resolved Hide resolved

import java.util.Arrays;
import java.util.Collections;
import java.util.Map;

import org.junit.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;

public class JsonFlattenerTest {
@Test
public void testFlattenAsMapBasic() {
Map<String, Object> flattenedMap = JsonFlattener.flattenAsMap("{\"key\": {\"nested\": 1}, \"another.key\": [\"one\", \"two\"] }");
prabhask5 marked this conversation as resolved.
Show resolved Hide resolved
assertThat(flattenedMap.keySet(), containsInAnyOrder("key.nested", "key", "another.key[0]", "another.key[1]", "another.key"));
assertThat(
flattenedMap.values(),
containsInAnyOrder(1, "one", "two", Arrays.asList("one", "two"), Collections.singletonMap("nested", 1))
);
}
}
Loading