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

[Backport 2.x] Eliminate Google Guava dependency (#383) #389

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 1 addition & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ configurations {
dependencies {
implementation "org.opensearch:opensearch:${opensearch_version}"
implementation 'org.junit.jupiter:junit-jupiter:5.10.1'
implementation "com.google.guava:guava:33.0.0-jre"
api group: 'org.opensearch', name:'opensearch-ml-client', version: "${opensearch_build}"
api group: 'org.opensearch.client', name: 'opensearch-rest-client', version: "${opensearch_version}"
implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.14.0'
Expand All @@ -159,7 +158,7 @@ dependencies {

configurations.all {
resolutionStrategy {
force("com.google.guava:guava:33.0.0-jre") // CVE for 31.1
force("com.google.guava:guava:33.0.0-jre") // CVE for 31.1, keep to force transitive dependencies
force("org.eclipse.platform:org.eclipse.core.runtime:3.30.0") // CVE for < 3.29.0
force("com.fasterxml.jackson.core:jackson-core:2.16.1") // Dependency Jar Hell
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
*/
package org.opensearch.flowframework.indices;

import com.google.common.io.Resources;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.opensearch.ExceptionsHelper;
Expand Down Expand Up @@ -39,12 +38,11 @@
import org.opensearch.flowframework.model.Template;
import org.opensearch.flowframework.model.WorkflowState;
import org.opensearch.flowframework.util.EncryptorUtils;
import org.opensearch.flowframework.util.ParseUtils;
import org.opensearch.script.Script;
import org.opensearch.script.ScriptType;

import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -292,8 +290,7 @@ private void shouldUpdateIndex(String indexName, Integer newVersion, ActionListe
* @throws IOException IOException if mapping file can't be read correctly
*/
public static String getIndexMappings(String mapping) throws IOException {
URL url = FlowFrameworkIndicesHandler.class.getClassLoader().getResource(mapping);
return Resources.toString(url, StandardCharsets.UTF_8);
return ParseUtils.resourceToString("/" + mapping);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@
*/
package org.opensearch.flowframework.model;

import com.google.common.io.Resources;
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.flowframework.util.ParseUtils;

import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -62,8 +59,7 @@ public static WorkflowValidator parse(XContentParser parser) throws IOException
* @throws IOException on failure to read and parse the json file
*/
public static WorkflowValidator parse(String file) throws IOException {
URL url = WorkflowValidator.class.getClassLoader().getResource(file);
String json = Resources.toString(url, StandardCharsets.UTF_8);
String json = ParseUtils.resourceToString("/" + file);
return parse(ParseUtils.jsonToParser(json));
}

Expand Down
22 changes: 22 additions & 0 deletions src/main/java/org/opensearch/flowframework/util/ParseUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.opensearch.client.Client;
import org.opensearch.common.io.Streams;
import org.opensearch.common.xcontent.LoggingDeprecationHandler;
import org.opensearch.common.xcontent.XContentHelper;
import org.opensearch.common.xcontent.XContentType;
Expand All @@ -26,7 +27,9 @@
import org.opensearch.flowframework.workflow.WorkflowData;
import org.opensearch.ml.common.agent.LLMSpec;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.time.Instant;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -71,6 +74,25 @@
return parser;
}

/**
* Reads a file from the classpath into a String. Useful for reading JSON mapping files.
*
* @param path A string identifying the resource on the class path
* @return A string containing the contents of the file as UTF-8
* @throws IOException if file is not found or error reading
*/
public static String resourceToString(String path) throws IOException {
try (InputStream is = ParseUtils.class.getResourceAsStream(path)) {
if (is == null) {
throw new FileNotFoundException("Resource [" + path + "] not found in classpath");

Check warning on line 87 in src/main/java/org/opensearch/flowframework/util/ParseUtils.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/flowframework/util/ParseUtils.java#L87

Added line #L87 was not covered by tests
}
final StringBuilder sb = new StringBuilder();
// Read as UTF-8
Streams.readAllLines(is, sb::append);
return sb.toString();
}
}

/**
* Builds an XContent object representing a map of String keys to String values.
*
Expand Down
7 changes: 2 additions & 5 deletions src/test/java/org/opensearch/flowframework/TestHelpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
*/
package org.opensearch.flowframework;

import com.google.common.io.Resources;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.nio.entity.NStringEntity;
Expand All @@ -31,13 +30,12 @@
import org.opensearch.core.xcontent.ToXContentObject;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.flowframework.model.Template;
import org.opensearch.flowframework.util.ParseUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
Expand All @@ -51,8 +49,7 @@
public class TestHelpers {

public static Template createTemplateFromFile(String fileName) throws IOException {
URL url = TestHelpers.class.getClassLoader().getResource("template/" + fileName);
String json = Resources.toString(url, StandardCharsets.UTF_8);
String json = ParseUtils.resourceToString("/template/" + fileName);
return Template.parse(json);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.opensearch.core.rest.RestStatus;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.core.xcontent.XContentParser.Token;
import org.opensearch.flowframework.exception.FlowFrameworkException;
import org.opensearch.flowframework.workflow.WorkflowData;
import org.opensearch.test.OpenSearchTestCase;
Expand All @@ -23,6 +24,16 @@
import java.util.Set;

public class ParseUtilsTests extends OpenSearchTestCase {
public void testResourceToStringToJson() throws IOException {
String json = ParseUtils.resourceToString("/template/finaltemplate.json");
assertTrue(json.startsWith("{"));
assertTrue(json.contains("name"));
try (XContentParser parser = ParseUtils.jsonToParser(json)) {
assertEquals(Token.FIELD_NAME, parser.nextToken());
assertEquals("name", parser.currentName());
}
}

public void testToInstant() throws IOException {
long epochMilli = Instant.now().toEpochMilli();
XContentBuilder builder = XContentFactory.jsonBuilder().value(epochMilli);
Expand Down
Loading