From 623eaa804ceeebe8fa5facc0e271e040f5e624d4 Mon Sep 17 00:00:00 2001 From: Suraj Singh Date: Mon, 5 Jun 2023 14:23:27 -0700 Subject: [PATCH] Resolve forbidden APIs due to inputstream Signed-off-by: Suraj Singh --- .../replication/SegmentReplicationIT.java | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/server/src/internalClusterTest/java/org/opensearch/indices/replication/SegmentReplicationIT.java b/server/src/internalClusterTest/java/org/opensearch/indices/replication/SegmentReplicationIT.java index 2af24af513b9e..bdd8320a412d1 100644 --- a/server/src/internalClusterTest/java/org/opensearch/indices/replication/SegmentReplicationIT.java +++ b/server/src/internalClusterTest/java/org/opensearch/indices/replication/SegmentReplicationIT.java @@ -74,6 +74,8 @@ import java.io.IOException; import java.net.URISyntaxException; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -1350,16 +1352,17 @@ public void testWithOlderCodecs() throws Exception { // Return oldest lucene codec name. It does this by iterating throw all directories under lucene library and filter // ones matching lucene codec name regex - private String getOlderLuceneCodec() throws IOException { + private String getOlderLuceneCodec() throws IOException, URISyntaxException { final TreeSet codecNames = new TreeSet<>(); - ZipInputStream zip = new ZipInputStream(Codec.class.getProtectionDomain().getCodeSource().getLocation().openStream()); + final String separator = PathUtils.getDefaultFileSystem().getSeparator(); Pattern pattern = Pattern.compile("lucene[0-9]+"); - for (ZipEntry entry = zip.getNextEntry(); entry != null; entry = zip.getNextEntry()) { - if (entry.isDirectory()) { - // Remove trailing file separator from path i.e. '../lucene94/' -> '../lucene94' - final String dirPath = entry.getName().substring(0, entry.getName().length() - 1); - // Fetch name i.e. '../lucene94' -> 'lucene94' - final String dirName = dirPath.substring(dirPath.lastIndexOf(PathUtils.getDefaultFileSystem().getSeparator()) + 1); + Path luceneJarPath = PathUtils.get(Codec.class.getProtectionDomain().getCodeSource().getLocation().toURI()); + try (var fileIn = Files.newInputStream(luceneJarPath)) { + ZipInputStream zip = new ZipInputStream(fileIn); + for (ZipEntry entry = zip.getNextEntry(); entry != null; entry = zip.getNextEntry()) { + final String dirPath = entry.getName(); + // Fetch name i.e. '../lucene94/' -> 'lucene94' + final String dirName = dirPath.substring(dirPath.lastIndexOf(separator, dirPath.length() - 2) + 1, dirPath.length() - 1); Matcher matcher = pattern.matcher(dirName); if (matcher.matches()) { codecNames.add(dirName);