Skip to content

Commit

Permalink
[Backport 2.x] Clean up and bump Apache libs (opensearch-project#2925)
Browse files Browse the repository at this point in the history
Signed-off-by: Andrey Pleskach <ples@aiven.io>
  • Loading branch information
willyborankin authored and peternied committed Aug 3, 2023
1 parent dcec047 commit a363716
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 31 deletions.
7 changes: 4 additions & 3 deletions src/test/java/org/opensearch/security/ConfigTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@
package org.opensearch.security;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import org.apache.commons.io.FileUtils;
import org.junit.Assert;
import org.junit.Test;

Expand Down Expand Up @@ -107,7 +108,7 @@ public void testParseSg67Config() throws Exception {

private void check(String file, CType cType) throws Exception {
final String adjustedFilePath = SingleClusterTest.TEST_RESOURCE_RELATIVE_PATH + file;
JsonNode jsonNode = YAML.readTree(FileUtils.readFileToString(new File(adjustedFilePath), "UTF-8"));
JsonNode jsonNode = YAML.readTree(Files.readString(new File(adjustedFilePath).toPath(), StandardCharsets.UTF_8));
int configVersion = 1;
System.out.println("%%%%%%%% THIS IS A LINE OF INTEREST %%%%%%%");
if (jsonNode.get("_meta") != null) {
Expand All @@ -128,7 +129,7 @@ private void check(String file, CType cType) throws Exception {

private SecurityDynamicConfiguration<?> load(String file, CType cType) throws Exception {
final String adjustedFilePath = SingleClusterTest.TEST_RESOURCE_RELATIVE_PATH + file;
JsonNode jsonNode = YAML.readTree(FileUtils.readFileToString(new File(adjustedFilePath), "UTF-8"));
JsonNode jsonNode = YAML.readTree(Files.readString(new File(adjustedFilePath).toPath(), StandardCharsets.UTF_8));
int configVersion = 1;

System.out.println("%%%%%%%% THIS IS A LINE OF INTEREST LOAD: CONFIG VERSION: %%%%%%%");
Expand Down
11 changes: 5 additions & 6 deletions src/test/java/org/opensearch/security/HttpIntegrationTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,13 @@

package org.opensearch.security;

import java.io.File;
import java.nio.charset.StandardCharsets;

import com.fasterxml.jackson.databind.JsonNode;
import org.apache.commons.io.FileUtils;
import org.apache.http.HttpStatus;
import org.apache.http.NoHttpResponseException;
import org.apache.http.message.BasicHeader;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;

import org.opensearch.action.admin.indices.alias.IndicesAliasesRequest;
import org.opensearch.action.admin.indices.alias.IndicesAliasesRequest.AliasActions;
import org.opensearch.action.admin.indices.create.CreateIndexRequest;
Expand All @@ -57,6 +52,10 @@
import org.opensearch.security.test.helper.rest.RestHelper;
import org.opensearch.security.test.helper.rest.RestHelper.HttpResponse;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;

import static org.opensearch.security.DefaultObjectMapper.readTree;

public class HttpIntegrationTests extends SingleClusterTest {
Expand Down Expand Up @@ -574,7 +573,7 @@ public void testHTTPPlaintextErrMsg() throws Exception {
rh.executeGetRequest("", encodeBasicHeader("worf", "worf"));
Assert.fail("NoHttpResponseException expected");
} catch (NoHttpResponseException e) {
String log = FileUtils.readFileToString(new File("unittest.log"), StandardCharsets.UTF_8);
String log = Files.readString(new File("unittest.log").toPath(), StandardCharsets.UTF_8);
Assert.assertTrue(log, log.contains("speaks http plaintext instead of ssl, will close the channel"));
} catch (Exception e) {
Assert.fail("NoHttpResponseException expected but was " + e.getClass() + "#" + e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
// CS-SUPPRESS-SINGLE: RegexpSingleline https://github.com/opensearch-project/OpenSearch/issues/3663
import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedList;
Expand All @@ -40,7 +45,6 @@
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

import org.apache.commons.io.FileUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

Expand Down Expand Up @@ -129,7 +133,7 @@ public final synchronized ClusterInfo startCluster(

switch (clusterState) {
case UNINITIALIZED:
FileUtils.deleteDirectory(new File("./target/data/" + clustername));
deleteTestsDataDirectory();
break;
case STARTED:
closeAllNodes();
Expand Down Expand Up @@ -251,19 +255,15 @@ public void run() {
PluginAwareNode node = new PluginAwareNode(setting.clusterManagerNode, settingsForNode, setting.getPlugins());
System.out.println(node.settings());

new Thread(new Runnable() {

@Override
public void run() {
try {
node.start();
latch.countDown();
} catch (Exception e) {
e.printStackTrace();
log.error("Unable to start node: ", e);
err.set(e);
latch.countDown();
}
new Thread(() -> {
try {
node.start();
latch.countDown();
} catch (Exception e) {
e.printStackTrace();
log.error("Unable to start node: ", e);
err.set(e);
latch.countDown();
}
}).start();
opensearchNodes.add(node);
Expand Down Expand Up @@ -308,9 +308,30 @@ public void run() {
return cInfo;
}

public final void stopCluster() throws Exception {
public void stopCluster() throws Exception {
closeAllNodes();
FileUtils.deleteDirectory(new File("./target/data/" + clustername));
deleteTestsDataDirectory();
}

private void deleteTestsDataDirectory() throws IOException {
final File testsDataDir = new File("target/data/" + clustername);
if (testsDataDir.exists()) {
Files.walkFileTree(testsDataDir.toPath(), new SimpleFileVisitor<>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
System.out.println("Deleting file " + file.getFileName());
Files.delete(file);
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
System.out.println("Deleting directory " + dir.getFileName());
Files.delete(dir);
return FileVisitResult.CONTINUE;
}
});
}
}

private void closeAllNodes() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
Expand All @@ -42,11 +44,11 @@
import java.nio.file.Paths;
import java.security.KeyStore;

import org.apache.commons.io.IOUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import org.opensearch.core.common.bytes.BytesReference;
import org.opensearch.common.io.Streams;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.core.xcontent.NamedXContentRegistry;
Expand Down Expand Up @@ -94,10 +96,14 @@ public static Path getAbsoluteFilePathFromClassPath(final String fileNameFromCla
return null;
}

public static final String loadFile(final String file) throws IOException {
final StringWriter sw = new StringWriter();
IOUtils.copy(FileHelper.class.getResourceAsStream("/" + file), sw, StandardCharsets.UTF_8);
return sw.toString();
public static String loadFile(final String file) throws IOException {
try (
final StringWriter sw = new StringWriter();
final Reader reader = new InputStreamReader(FileHelper.class.getResourceAsStream("/" + file), StandardCharsets.UTF_8)
) {
Streams.copy(reader, sw);
return sw.toString();
}
}

public static BytesReference readYamlContent(final String file) {
Expand Down

0 comments on commit a363716

Please sign in to comment.