Skip to content

Commit

Permalink
Fix forbidden apis
Browse files Browse the repository at this point in the history
Signed-off-by: Craig Perkins <[email protected]>
  • Loading branch information
cwperks committed Aug 23, 2024
1 parent 9f68ba3 commit 30dee36
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.opensearch.OpenSearchException;
import org.opensearch.client.RestClient;
import org.opensearch.client.RestClientBuilder;
import org.opensearch.common.io.PathUtils;
import org.opensearch.common.settings.Settings;
import org.opensearch.core.common.Strings;

Expand All @@ -39,7 +40,6 @@
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.util.ArrayList;
Expand Down Expand Up @@ -191,16 +191,16 @@ private String resolve(final String originalFile, final Path configPath) {
throw new OpenSearchException("Empty file path for " + originalFile);
}

if (Files.isDirectory(Paths.get(path), LinkOption.NOFOLLOW_LINKS)) {
if (Files.isDirectory(PathUtils.get(path), LinkOption.NOFOLLOW_LINKS)) {
throw new OpenSearchException("Is a directory: " + path + " Expected a file for " + originalFile);
}

if (!Files.isReadable(Paths.get(path))) {
if (!Files.isReadable(PathUtils.get(path))) {
throw new OpenSearchException(
"Unable to read "
+ path
+ " ("
+ Paths.get(path)
+ PathUtils.get(path)
+ "). Please make sure this files exists and is readable regarding to permissions. Property: "
+ originalFile
);
Expand All @@ -227,7 +227,7 @@ private KeyStore getKeyStore() throws IOException, GeneralSecurityException {
return null;
}
String keyStorePath = resolve(keyStoreFile, configPath);
try (InputStream is = Files.newInputStream(Paths.get(keyStorePath))) {
try (InputStream is = Files.newInputStream(PathUtils.get(keyStorePath))) {
keyStore.load(is, passwd.toCharArray());
}
return keyStore;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
*/
package org.opensearch.jobscheduler.sampleextension;

import java.io.FileInputStream;
import org.opensearch.common.io.PathUtils;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.security.cert.Certificate;
Expand Down Expand Up @@ -41,7 +44,7 @@ private X509Certificate[] loadCertificatesFromFile(String file) throws IOExcepti
return null;
}
CertificateFactory fact = CertificateFactory.getInstance(certType);
try (FileInputStream is = new FileInputStream(file)) {
try (InputStream is = Files.newInputStream(PathUtils.get(file))) {
Collection<? extends Certificate> certs = fact.generateCertificates(is);
X509Certificate[] x509Certs = new X509Certificate[certs.size()];
int i = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.util.ArrayList;
Expand Down Expand Up @@ -43,6 +42,7 @@
import org.opensearch.OpenSearchException;
import org.opensearch.client.RestClient;
import org.opensearch.client.RestClientBuilder;
import org.opensearch.common.io.PathUtils;
import org.opensearch.common.settings.Settings;
import org.opensearch.core.common.Strings;

Expand Down Expand Up @@ -192,16 +192,16 @@ private String resolve(final String originalFile, final Path configPath) {
throw new OpenSearchException("Empty file path for " + originalFile);
}

if (Files.isDirectory(Paths.get(path), LinkOption.NOFOLLOW_LINKS)) {
if (Files.isDirectory(PathUtils.get(path), LinkOption.NOFOLLOW_LINKS)) {
throw new OpenSearchException("Is a directory: " + path + " Expected a file for " + originalFile);
}

if (!Files.isReadable(Paths.get(path))) {
if (!Files.isReadable(PathUtils.get(path))) {
throw new OpenSearchException(
"Unable to read "
+ path
+ " ("
+ Paths.get(path)
+ PathUtils.get(path)
+ "). Please make sure this files exists and is readable regarding to permissions. Property: "
+ originalFile
);
Expand All @@ -228,7 +228,7 @@ private KeyStore getKeyStore() throws IOException, GeneralSecurityException {
return null;
}
String keyStorePath = resolve(keyStoreFile, configPath);
try (InputStream is = Files.newInputStream(Paths.get(keyStorePath))) {
try (InputStream is = Files.newInputStream(PathUtils.get(keyStorePath))) {
keyStore.load(is, passwd.toCharArray());
}
return keyStore;
Expand Down
10 changes: 8 additions & 2 deletions src/test/java/org/opensearch/jobscheduler/TrustStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
*/
package org.opensearch.jobscheduler;

import java.io.FileInputStream;
import org.opensearch.common.io.PathUtils;

import java.io.InputStream;
import java.nio.file.Files;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
Expand Down Expand Up @@ -41,7 +44,10 @@ private X509Certificate[] loadCertificatesFromFile(String file) throws IOExcepti
return null;
}
CertificateFactory fact = CertificateFactory.getInstance(certType);
try (FileInputStream is = new FileInputStream(file)) {



try (InputStream is = Files.newInputStream(PathUtils.get(file))) {
Collection<? extends Certificate> certs = fact.generateCertificates(is);
X509Certificate[] x509Certs = new X509Certificate[certs.size()];
int i = 0;
Expand Down

0 comments on commit 30dee36

Please sign in to comment.