Skip to content

Simplify conversions from java.nio.file.Path to File #5547

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

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ String process(String value) {
justification = "app is run in same security context as user providing the filename")
@Override
String process(String value) {
try (Scanner scanner = new Scanner(Path.of(value).toFile(), UTF_8)) {
try (Scanner scanner = new Scanner(Path.of(value), UTF_8)) {
return scanner.nextLine();
} catch (IOException e) {
throw new ParameterException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ public static void validateGroupNames(List<String> names) {
|| VALID_CONFIG_PREFIXES.stream().anyMatch(section::startsWith);

@SuppressFBWarnings(value = "PATH_TRAVERSAL_IN", justification = "paths not set by user input")
public static Map<String,String> parseConfiguration(String configFile) throws IOException {
public static Map<String,String> parseConfiguration(Path configFile) throws IOException {
Map<String,String> results = new HashMap<>();
try (InputStream fis = Files.newInputStream(Path.of(configFile), StandardOpenOption.READ)) {
try (InputStream fis = Files.newInputStream(configFile, StandardOpenOption.READ)) {
Yaml y = new Yaml();
Map<String,Object> config = y.load(fis);
config.forEach((k, v) -> flatten("", k, v, results));
Expand Down Expand Up @@ -228,10 +228,10 @@ public static void main(String[] args) throws IOException {
// Write to a file instead of System.out if provided as an argument
try (OutputStream os = Files.newOutputStream(Path.of(args[1]));
PrintStream out = new PrintStream(os)) {
outputShellVariables(parseConfiguration(args[0]), new PrintStream(out));
outputShellVariables(parseConfiguration(Path.of(args[0])), new PrintStream(out));
}
} else {
outputShellVariables(parseConfiguration(args[0]), System.out);
outputShellVariables(parseConfiguration(Path.of(args[0])), System.out);
}
} catch (Exception e) {
System.err.println("Processing error: " + e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
*/
package org.apache.accumulo.core.rpc;

import java.io.File;
import java.io.FileNotFoundException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;

Expand All @@ -39,12 +39,12 @@ public class SslConnectionParams {
private boolean clientAuth = false;

private boolean keyStoreSet;
private String keyStorePath;
private Path keyStorePath;
private String keyStorePass;
private String keyStoreType;

private boolean trustStoreSet;
private String trustStorePath;
private Path trustStorePath;
private String trustStorePass;
private String trustStoreType;

Expand Down Expand Up @@ -108,7 +108,7 @@ private static String passwordFromConf(AccumuloConfiguration conf, String defaul
return keystorePassword;
}

private static String storePathFromConf(AccumuloConfiguration conf, Property pathProperty)
private static Path storePathFromConf(AccumuloConfiguration conf, Property pathProperty)
throws FileNotFoundException {
return findKeystore(conf.getPath(pathProperty));
}
Expand Down Expand Up @@ -138,20 +138,20 @@ public static SslConnectionParams forClient(AccumuloConfiguration configuration)

@SuppressFBWarnings(value = "PATH_TRAVERSAL_IN",
justification = "code runs in same security context as user who providing the keystore file")
private static String findKeystore(String keystorePath) throws FileNotFoundException {
private static Path findKeystore(String keystorePath) throws FileNotFoundException {
try {
// first just try the file
File file = Path.of(keystorePath).toFile();
if (file.exists()) {
return file.getAbsolutePath();
Path path = Path.of(keystorePath);
if (Files.exists(path)) {
return path.toAbsolutePath();
}
if (!file.isAbsolute()) {
if (!path.isAbsolute()) {
// try classpath
URL url = SslConnectionParams.class.getClassLoader().getResource(keystorePath);
if (url != null) {
file = Path.of(url.toURI()).toFile();
if (file.exists()) {
return file.getAbsolutePath();
path = Path.of(url.toURI());
if (Files.exists(path)) {
return path.toAbsolutePath();
}
}
}
Expand Down Expand Up @@ -189,7 +189,7 @@ public boolean isKeyStoreSet() {
return keyStoreSet;
}

public String getKeyStorePath() {
public Path getKeyStorePath() {
return keyStorePath;
}

Expand All @@ -208,7 +208,7 @@ public boolean isTrustStoreSet() {
return trustStoreSet;
}

public String getTrustStorePath() {
public Path getTrustStorePath() {
return trustStorePath;
}

Expand All @@ -231,10 +231,10 @@ public TSSLTransportParameters getTSSLTransportParameters() {
TSSLTransportParameters params = new TSSLTransportParameters(clientProtocol, cipherSuites);
params.requireClientAuth(clientAuth);
if (keyStoreSet) {
params.setKeyStore(keyStorePath, keyStorePass, null, keyStoreType);
params.setKeyStore(keyStorePath.toString(), keyStorePass, null, keyStoreType);
}
if (trustStoreSet) {
params.setTrustStore(trustStorePath, trustStorePass, null, trustStoreType);
params.setTrustStore(trustStorePath.toString(), trustStorePass, null, trustStoreType);
}
return params;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@
package org.apache.accumulo.core.classloader;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Objects;

import org.apache.accumulo.core.WithTestNames;
Expand All @@ -39,27 +40,33 @@
public class ContextClassLoaderFactoryTest extends WithTestNames {

@TempDir
private static File tempFolder;
private static Path tempFolder;

private String uri1;
private String uri2;
private URL uri1;
private URL uri2;

@BeforeEach
public void setup() throws Exception {

File folder1 = tempFolder.toPath().resolve(testName() + "_1").toFile();
assertTrue(folder1.isDirectory() || folder1.mkdir(), "Failed to make a new sub-directory");
Path folder1 = tempFolder.resolve(testName() + "_1");
if (!Files.isDirectory(folder1)) {
Files.createDirectories(folder1);
}
Path propsFile = folder1.resolve("accumulo.properties");
FileUtils.copyURLToFile(
Objects.requireNonNull(this.getClass().getResource("/accumulo.properties")),
folder1.toPath().resolve("accumulo.properties").toFile());
uri1 = folder1.toPath().resolve("accumulo.properties").toFile().toURI().toString();

File folder2 = tempFolder.toPath().resolve(testName() + "_2").toFile();
assertTrue(folder2.isDirectory() || folder2.mkdir(), "Failed to make a new sub-directory");
propsFile.toFile());
uri1 = propsFile.toUri().toURL();

Path folder2 = tempFolder.resolve(testName() + "_2");
if (!Files.isDirectory(folder2)) {
Files.createDirectories(folder2);
}
Path propsFile2 = folder2.resolve("accumulo2.properties");
FileUtils.copyURLToFile(
Objects.requireNonNull(this.getClass().getResource("/accumulo2.properties")),
folder2.toPath().resolve("accumulo2.properties").toFile());
uri2 = folder2.toURI() + ".*";
propsFile2.toFile());
uri2 = propsFile2.toUri().toURL();

}

Expand All @@ -72,15 +79,17 @@ public void differentContexts() {
ClassLoaderUtil.resetContextFactoryForTests();
ClassLoaderUtil.initContextFactory(cc);

URLClassLoader cl1 = (URLClassLoader) ClassLoaderUtil.getContextFactory().getClassLoader(uri1);
URLClassLoader cl1 =
(URLClassLoader) ClassLoaderUtil.getContextFactory().getClassLoader(uri1.toString());
var urls1 = cl1.getURLs();
assertEquals(1, urls1.length);
assertEquals(uri1, urls1[0].toString());
assertEquals(uri1, urls1[0]);

URLClassLoader cl2 = (URLClassLoader) ClassLoaderUtil.getContextFactory().getClassLoader(uri2);
URLClassLoader cl2 =
(URLClassLoader) ClassLoaderUtil.getContextFactory().getClassLoader(uri2.toString());
var urls2 = cl2.getURLs();
assertEquals(1, urls2.length);
assertEquals(uri2, urls2[0].toString());
assertEquals(uri2, urls2[0]);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void testEnv() {
@Test
public void testFile() throws IOException {
argv[1] = "file:pom.xml";
Scanner scan = new Scanner(Path.of("pom.xml").toFile(), UTF_8);
Scanner scan = new Scanner(Path.of("pom.xml"), UTF_8);
String expected = scan.nextLine();
scan.close();
new JCommander(password).parse(argv);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.accumulo.core.WithTestNames;
import org.apache.accumulo.core.client.IteratorSetting;
import org.apache.accumulo.core.client.Scanner;
import org.apache.accumulo.core.client.admin.NewTableConfiguration;
Expand Down Expand Up @@ -83,17 +84,18 @@
import org.apache.hadoop.hdfs.DistributedFileSystem;
import org.apache.hadoop.io.Text;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

@SuppressFBWarnings(value = "PATH_TRAVERSAL_IN", justification = "path is set by test, not user")
public class RFileClientTest {
public class RFileClientTest extends WithTestNames {

@TempDir
private static java.nio.file.Path tempDir;

private String createTmpTestFile() throws IOException {
File dir = java.nio.file.Path.of(System.getProperty("user.dir")).resolve("target")
.resolve("rfile-test").toFile();
assertTrue(dir.mkdirs() || dir.isDirectory());
File testFile = File.createTempFile("test", ".rf", dir);
File testFile = File.createTempFile("test", ".rf", tempDir.toFile());
assertTrue(testFile.delete() || !testFile.exists());
return testFile.getAbsolutePath();
}
Expand Down Expand Up @@ -244,7 +246,7 @@ public void testFencingScanner() throws Exception {

Range range = new Range(rowStr(3), false, rowStr(14), true);
Scanner scanner = RFile.newScanner()
.from(new FencedPath(new Path(java.nio.file.Path.of(testFile).toFile().toURI()), range))
.from(new FencedPath(new Path(java.nio.file.Path.of(testFile).toUri()), range))
.withFileSystem(localFs).build();

TreeMap<Key,Value> expected = new TreeMap<>(testData);
Expand All @@ -266,7 +268,7 @@ public void testRequiresRowRange() throws Exception {
// Lastly only the row portion of a key is allowed.

// Test valid Row Ranges
URI testFileURI = java.nio.file.Path.of(testFile).toFile().toURI();
URI testFileURI = java.nio.file.Path.of(testFile).toUri();
new FencedPath(new Path(testFileURI), new Range());
// This constructor converts to the proper inclusive/exclusive rows
new FencedPath(new Path(testFileURI), new Range(rowStr(3), false, rowStr(14), true));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class CredentialProviderTokenTest {
public static void setup() throws Exception {
URL keystoreUrl = CredentialProviderTokenTest.class.getResource("/passwords.jceks");
assertNotNull(keystoreUrl);
keystorePath = "jceks://file/" + Path.of(keystoreUrl.toURI()).toFile().getAbsolutePath();
keystorePath = "jceks://file/" + Path.of(keystoreUrl.toURI()).toAbsolutePath();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import java.io.File;
import java.net.URL;
import java.nio.file.Path;
import java.util.HashMap;
Expand All @@ -49,26 +48,22 @@ public class ClientContextTest {

// site-cfg.jceks={'ignored.property'=>'ignored', 'instance.secret'=>'mysecret',
// 'general.rpc.timeout'=>'timeout'}
private static File keystore;
private static String keystoreUri;

@SuppressFBWarnings(value = "PATH_TRAVERSAL_IN",
justification = "provided keystoreUrl path isn't user provided")
@BeforeAll
public static void setUpBeforeAll() throws Exception {
URL keystoreUrl = ClientContextTest.class.getResource(keystoreName);
assertNotNull(keystoreUrl, "Could not find " + keystoreName);
keystore = Path.of(keystoreUrl.toURI()).toFile();
}

protected String getKeyStoreUrl(File absoluteFilePath) {
return "jceks://file" + absoluteFilePath.getAbsolutePath();
Path keystorePath = Path.of(keystoreUrl.toURI());
keystoreUri = "jceks://file" + keystorePath.toAbsolutePath();
}

@Test
public void loadSensitivePropertyFromCredentialProvider() {
String absPath = getKeyStoreUrl(keystore);
Properties props = new Properties();
props.setProperty(Property.GENERAL_SECURITY_CREDENTIAL_PROVIDER_PATHS.getKey(), absPath);
props.setProperty(Property.GENERAL_SECURITY_CREDENTIAL_PROVIDER_PATHS.getKey(), keystoreUri);
AccumuloConfiguration accClientConf = ClientConfConverter.toAccumuloConf(props);
assertEquals("mysecret", accClientConf.get(Property.INSTANCE_SECRET));
}
Expand All @@ -83,9 +78,9 @@ public void defaultValueForSensitiveProperty() {

@Test
public void sensitivePropertiesIncludedInProperties() {
String absPath = getKeyStoreUrl(keystore);
Properties clientProps = new Properties();
clientProps.setProperty(Property.GENERAL_SECURITY_CREDENTIAL_PROVIDER_PATHS.getKey(), absPath);
clientProps.setProperty(Property.GENERAL_SECURITY_CREDENTIAL_PROVIDER_PATHS.getKey(),
keystoreUri);

AccumuloConfiguration accClientConf = ClientConfConverter.toAccumuloConf(clientProps);
Map<String,String> props = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import java.io.File;
import java.net.URL;
import java.nio.file.Files;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
Expand All @@ -38,6 +39,7 @@
import org.apache.hadoop.hdfs.MiniDFSCluster;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -46,6 +48,9 @@
@SuppressFBWarnings(value = "PATH_TRAVERSAL_IN", justification = "paths not set by user input")
public class HadoopCredentialProviderTest {

@TempDir
private static java.nio.file.Path tempDir;

private static final Configuration hadoopConf = new Configuration();
private static final Logger log = LoggerFactory.getLogger(HadoopCredentialProviderTest.class);

Expand Down Expand Up @@ -150,16 +155,12 @@ public void testConfigurationCreation() {

@Test
public void createKeystoreProvider() throws Exception {
File targetDir =
java.nio.file.Path.of(System.getProperty("user.dir")).resolve("target").toFile();
File keystoreFile = targetDir.toPath().resolve("create.jks").toFile();
if (keystoreFile.exists()) {
if (!keystoreFile.delete()) {
log.error("Unable to delete {}", keystoreFile);
}
java.nio.file.Path keystoreFile = tempDir.resolve("create.jks");
if (Files.exists(keystoreFile)) {
Files.delete(keystoreFile);
}

String providerUrl = "jceks://file" + keystoreFile.getAbsolutePath();
String providerUrl = "jceks://file" + keystoreFile.toAbsolutePath();
Configuration conf = new Configuration();
HadoopCredentialProvider.setPath(conf, providerUrl);

Expand All @@ -172,11 +173,9 @@ public void createKeystoreProvider() throws Exception {

@Test
public void extractFromHdfs() throws Exception {
File target = java.nio.file.Path.of(System.getProperty("user.dir")).resolve("target").toFile();
String prevValue = System.setProperty("test.build.data",
target.toPath().resolve(this.getClass().getName() + "_minidfs").toFile().toString());
MiniDFSCluster dfsCluster = new MiniDFSCluster.Builder(new Configuration()).build();
try {
tempDir.resolve(this.getClass().getName() + "_minidfs").toString());
try (MiniDFSCluster dfsCluster = new MiniDFSCluster.Builder(new Configuration()).build()) {
if (null != prevValue) {
System.setProperty("test.build.data", prevValue);
} else {
Expand All @@ -199,8 +198,6 @@ public void extractFromHdfs() throws Exception {
expectations.put("key2", "value2");

checkCredentialProviders(cpConf, expectations);
} finally {
dfsCluster.shutdown();
}
}

Expand Down
Loading
Loading