Skip to content

Commit

Permalink
[Maintenance-2930] Review use of JSON Flattener (opensearch-project#3674
Browse files Browse the repository at this point in the history
)

### Description

Implement JsonFlattener helper class as written in opensearch-project#2926 to deprecate
the use of the unnecessary JsonFlattener third party module.

* Category (Enhancement, New feature, Bug fix, Test fix, Refactoring,
Maintenance, Documentation)

Maintenance

* Why these changes are required?

The JsonFlattener module was being utilized in only one place for one
specific purpose, so these functions can be implemented as part of the
OpenSearch codebase instead of importing an unnecessary third party
module.

* What is the old behavior before changes and new behavior after
changes?

Hopefully nothing.

### Issues Resolved
- opensearch-project#2930 

Is this a backport? If so, please add backport PR # and/or commits #

No

### Testing
Tests checked to make sure functions are not broken.

### Check List
- [x] New functionality includes testing
- [x] New functionality has been documented
- [x] Commits are signed per the DCO using --signoff

By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and
signing off your commits, please check
[here](https://github.com/opensearch-project/OpenSearch/blob/main/CONTRIBUTING.md#developer-certificate-of-origin).

---------

Signed-off-by: Prabhas Kurapati <[email protected]>
  • Loading branch information
prabhask5 authored Dec 2, 2023
1 parent 481b373 commit 87de7e2
Show file tree
Hide file tree
Showing 4 changed files with 471 additions and 9 deletions.
7 changes: 0 additions & 7 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -585,13 +585,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
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 87de7e2

Please sign in to comment.