Skip to content

Commit

Permalink
SOLR-16903: Update CLI tools to use java.nio.file.Path instead of jav…
Browse files Browse the repository at this point in the history
…a.io.File (#2883)
  • Loading branch information
AndreyBozhko authored Nov 26, 2024
1 parent 656397c commit 6d167b1
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 164 deletions.
2 changes: 2 additions & 0 deletions solr/CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ Other Changes

* SOLR-17321: Minimum Java version for Apache Solr is now 21, and for SolrJ, it is 17. (Sanjay Dutt, David Smiley)

* SOLR-16903: Update CLI tools to use java.nio.file.Path instead of java.io.File (Andrey Bozhko)

================== 9.8.0 ==================
New Features
---------------------
Expand Down
33 changes: 16 additions & 17 deletions solr/core/src/java/org/apache/solr/cli/AuthTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.io.Console;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.net.URL;
Expand Down Expand Up @@ -250,25 +249,25 @@ private void handleKerberos(CommandLine cli) throws Exception {
config = config.replace("\n", "").replace("\r", "");

String solrIncludeFilename = cli.getOptionValue(SOLR_INCLUDE_FILE_OPTION);
File includeFile = new File(solrIncludeFilename);
if (!includeFile.exists() || !includeFile.canWrite()) {
Path includeFile = Path.of(solrIncludeFilename);
if (Files.notExists(includeFile) || !Files.isWritable(includeFile)) {
CLIO.out(
"Solr include file " + solrIncludeFilename + " doesn't exist or is not writeable.");
printAuthEnablingInstructions(config);
System.exit(0);
}

// update the solr.in.sh file to contain the necessary authentication lines
updateIncludeFileEnableAuth(includeFile.toPath(), null, config);
updateIncludeFileEnableAuth(includeFile, null, config);
echo(
"Successfully enabled Kerberos authentication; please restart any running Solr nodes.");
return;
case "disable":
clearSecurityJson(cli, updateIncludeFileOnly);

solrIncludeFilename = cli.getOptionValue(SOLR_INCLUDE_FILE_OPTION);
includeFile = new File(solrIncludeFilename);
if (!includeFile.exists() || !includeFile.canWrite()) {
includeFile = Path.of(solrIncludeFilename);
if (Files.notExists(includeFile) || !Files.isWritable(includeFile)) {
CLIO.out(
"Solr include file " + solrIncludeFilename + " doesn't exist or is not writeable.");
CLIO.out(
Expand All @@ -277,7 +276,7 @@ private void handleKerberos(CommandLine cli) throws Exception {
}

// update the solr.in.sh file to comment out the necessary authentication lines
updateIncludeFileDisableAuth(includeFile.toPath());
updateIncludeFileDisableAuth(includeFile);
return;
default:
CLIO.out("Valid auth commands are: enable, disable.");
Expand Down Expand Up @@ -387,30 +386,30 @@ private void handleBasicAuth(CommandLine cli) throws Exception {
}

String solrIncludeFilename = cli.getOptionValue(SOLR_INCLUDE_FILE_OPTION);
File includeFile = new File(solrIncludeFilename);
if (!includeFile.exists() || !includeFile.canWrite()) {
Path includeFile = Path.of(solrIncludeFilename);
if (Files.notExists(includeFile) || !Files.isWritable(includeFile)) {
CLIO.out(
"Solr include file " + solrIncludeFilename + " doesn't exist or is not writeable.");
printAuthEnablingInstructions(username, password);
System.exit(0);
}
String authConfDir = cli.getOptionValue(AUTH_CONF_DIR_OPTION);
File basicAuthConfFile = new File(authConfDir + File.separator + "basicAuth.conf");
Path basicAuthConfFile = Path.of(authConfDir, "basicAuth.conf");

if (!basicAuthConfFile.getParentFile().canWrite()) {
CLIO.out("Cannot write to file: " + basicAuthConfFile.getAbsolutePath());
if (!Files.isWritable(basicAuthConfFile.getParent())) {
CLIO.out("Cannot write to file: " + basicAuthConfFile.toAbsolutePath());
printAuthEnablingInstructions(username, password);
System.exit(0);
}

Files.writeString(
basicAuthConfFile.toPath(),
basicAuthConfFile,
"httpBasicAuthUser=" + username + "\nhttpBasicAuthPassword=" + password,
StandardCharsets.UTF_8);

// update the solr.in.sh file to contain the necessary authentication lines
updateIncludeFileEnableAuth(
includeFile.toPath(), basicAuthConfFile.getAbsolutePath(), null);
includeFile, basicAuthConfFile.toAbsolutePath().toString(), null);
final String successMessage =
String.format(
Locale.ROOT,
Expand All @@ -423,8 +422,8 @@ private void handleBasicAuth(CommandLine cli) throws Exception {
clearSecurityJson(cli, updateIncludeFileOnly);

solrIncludeFilename = cli.getOptionValue(SOLR_INCLUDE_FILE_OPTION);
includeFile = new File(solrIncludeFilename);
if (!includeFile.exists() || !includeFile.canWrite()) {
includeFile = Path.of(solrIncludeFilename);
if (Files.notExists(includeFile) || !Files.isWritable(includeFile)) {
CLIO.out(
"Solr include file " + solrIncludeFilename + " doesn't exist or is not writeable.");
CLIO.out(
Expand All @@ -433,7 +432,7 @@ private void handleBasicAuth(CommandLine cli) throws Exception {
}

// update the solr.in.sh file to comment out the necessary authentication lines
updateIncludeFileDisableAuth(includeFile.toPath());
updateIncludeFileDisableAuth(includeFile);
return;
default:
CLIO.out("Valid auth commands are: enable, disable.");
Expand Down
3 changes: 1 addition & 2 deletions solr/core/src/java/org/apache/solr/cli/ExportTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import static org.apache.solr.common.util.JavaBinCodec.SOLRINPUTDOC;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
Expand Down Expand Up @@ -599,7 +598,7 @@ void exportDocs() throws Exception {
consumerThreadpool.shutdownNow();
if (failed) {
try {
Files.delete(new File(out).toPath());
Files.delete(Path.of(out));
} catch (IOException e) {
// ignore
}
Expand Down
Loading

0 comments on commit 6d167b1

Please sign in to comment.