Skip to content

Commit

Permalink
[Backport 2.x] [Maintenance-2930] Review use of JSON Flattener (#3782)
Browse files Browse the repository at this point in the history
Backport 87de7e2 from #3674.

---------

Signed-off-by: Prabhas Kurapati <prabhask@berkeley.edu>
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Signed-off-by: Darshit Chanpura <dchanp@amazon.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Darshit Chanpura <dchanp@amazon.com>
  • Loading branch information
3 people authored Dec 6, 2023
1 parent d7b10bb commit 47c977b
Show file tree
Hide file tree
Showing 4 changed files with 472 additions and 10 deletions.
7 changes: 0 additions & 7 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -575,13 +575,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;

//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 Expand Up @@ -106,7 +105,7 @@ public void binaryFieldRead(final FieldInfo fieldInfo, byte[] fieldValue) {
fieldValue = Utils.jsonMapToByteArray(filteredSource);
}

Map<String, Object> filteredSource = new JsonFlattener(new String(fieldValue, StandardCharsets.UTF_8)).flattenAsMap();
Map<String, Object> filteredSource = JsonFlattener.flattenAsMap(new String(fieldValue, StandardCharsets.UTF_8));
for (String k : filteredSource.keySet()) {
if (!recordField(k, filteredSource.get(k) instanceof String)) {
continue;
Expand Down
66 changes: 66 additions & 0 deletions src/main/java/org/opensearch/security/support/JsonFlattener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
*/

package org.opensearch.security.support;

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<>();
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++;
}
}

}
Loading

0 comments on commit 47c977b

Please sign in to comment.