[LICENSOR] use Paths and Files instead of File
Original commit: elastic/x-pack-elasticsearch@1aefc6867d
This commit is contained in:
parent
99806cbd27
commit
b08f459c57
|
@ -12,9 +12,9 @@ import org.elasticsearch.common.cli.commons.CommandLine;
|
|||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.env.Environment;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.security.KeyPair;
|
||||
import java.security.KeyPairGenerator;
|
||||
|
@ -51,22 +51,22 @@ public class KeyPairGeneratorTool extends CliTool {
|
|||
option("pri", "privateKeyPath").required(true).hasArg(true)
|
||||
).build();
|
||||
|
||||
public final String publicKeyPath;
|
||||
public final String privateKeyPath;
|
||||
public final Path publicKeyPath;
|
||||
public final Path privateKeyPath;
|
||||
|
||||
protected KeyGenerator(Terminal terminal, String publicKeyPath, String privateKeyPath) {
|
||||
protected KeyGenerator(Terminal terminal, Path publicKeyPath, Path privateKeyPath) {
|
||||
super(terminal);
|
||||
this.privateKeyPath = privateKeyPath;
|
||||
this.publicKeyPath = publicKeyPath;
|
||||
}
|
||||
|
||||
public static Command parse(Terminal terminal, CommandLine commandLine) {
|
||||
String publicKeyPath = commandLine.getOptionValue("publicKeyPath");
|
||||
String privateKeyPath = commandLine.getOptionValue("privateKeyPath");
|
||||
Path publicKeyPath = Paths.get(commandLine.getOptionValue("publicKeyPath"));
|
||||
Path privateKeyPath = Paths.get(commandLine.getOptionValue("privateKeyPath"));
|
||||
|
||||
if (exists(privateKeyPath)) {
|
||||
if (Files.exists(privateKeyPath)) {
|
||||
return exitCmd(ExitStatus.USAGE, terminal, privateKeyPath + " already exists");
|
||||
} else if (exists(publicKeyPath)) {
|
||||
} else if (Files.exists(publicKeyPath)) {
|
||||
return exitCmd(ExitStatus.USAGE, terminal, publicKeyPath + " already exists");
|
||||
}
|
||||
return new KeyGenerator(terminal, publicKeyPath, privateKeyPath);
|
||||
|
@ -79,25 +79,21 @@ public class KeyPairGeneratorTool extends CliTool {
|
|||
return (keyPair != null) ? ExitStatus.OK : ExitStatus.CANT_CREATE;
|
||||
}
|
||||
|
||||
private static boolean exists(String filePath) {
|
||||
return new File(filePath).exists();
|
||||
}
|
||||
|
||||
private static KeyPair generateKeyPair(String privateKeyFileName, String publicKeyFileName) throws IOException, NoSuchAlgorithmException {
|
||||
private static KeyPair generateKeyPair(Path privateKeyPath, Path publicKeyPath) throws IOException, NoSuchAlgorithmException {
|
||||
SecureRandom random = new SecureRandom();
|
||||
|
||||
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
|
||||
keyGen.initialize(2048, random);
|
||||
KeyPair keyPair = keyGen.generateKeyPair();
|
||||
|
||||
saveKeyPairToFiles(keyPair, privateKeyFileName, publicKeyFileName);
|
||||
saveKeyPairToFiles(keyPair, privateKeyPath, publicKeyPath);
|
||||
return keyPair;
|
||||
}
|
||||
}
|
||||
|
||||
private static void saveKeyPairToFiles(KeyPair keyPair, String privateKeyFileName, String publicKeyFileName) throws IOException {
|
||||
Files.write(Paths.get(privateKeyFileName), writeEncryptedPrivateKey(keyPair.getPrivate()));
|
||||
Files.write(Paths.get(publicKeyFileName), writeEncryptedPublicKey(keyPair.getPublic()));
|
||||
private static void saveKeyPairToFiles(KeyPair keyPair, Path privateKeyPath, Path publicKeyPath) throws IOException {
|
||||
Files.write(privateKeyPath, writeEncryptedPrivateKey(keyPair.getPrivate()));
|
||||
Files.write(publicKeyPath, writeEncryptedPublicKey(keyPair.getPublic()));
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
|
|
@ -20,7 +20,6 @@ import org.elasticsearch.license.core.License;
|
|||
import org.elasticsearch.license.core.Licenses;
|
||||
import org.elasticsearch.license.licensor.LicenseSigner;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
|
@ -60,10 +59,10 @@ public class LicenseGeneratorTool extends CliTool {
|
|||
).build();
|
||||
|
||||
public final Set<License> licenseSpecs;
|
||||
public final String publicKeyFilePath;
|
||||
public final String privateKeyFilePath;
|
||||
public final Path publicKeyFilePath;
|
||||
public final Path privateKeyFilePath;
|
||||
|
||||
public LicenseGenerator(Terminal terminal, String publicKeyFilePath, String privateKeyFilePath, Set<License> licenseSpecs) {
|
||||
public LicenseGenerator(Terminal terminal, Path publicKeyFilePath, Path privateKeyFilePath, Set<License> licenseSpecs) {
|
||||
super(terminal);
|
||||
this.licenseSpecs = licenseSpecs;
|
||||
this.privateKeyFilePath = privateKeyFilePath;
|
||||
|
@ -71,14 +70,14 @@ public class LicenseGeneratorTool extends CliTool {
|
|||
}
|
||||
|
||||
public static Command parse(Terminal terminal, CommandLine commandLine) throws IOException {
|
||||
String publicKeyPath = commandLine.getOptionValue("publicKeyPath");
|
||||
String privateKeyPath = commandLine.getOptionValue("privateKeyPath");
|
||||
Path publicKeyPath = Paths.get(commandLine.getOptionValue("publicKeyPath"));
|
||||
Path privateKeyPath = Paths.get(commandLine.getOptionValue("privateKeyPath"));
|
||||
String[] licenseSpecSources = commandLine.getOptionValues("license");
|
||||
String[] licenseSpecSourceFiles = commandLine.getOptionValues("licenseFile");
|
||||
|
||||
if (doesNotExist(privateKeyPath)) {
|
||||
if (!Files.exists(privateKeyPath)) {
|
||||
return exitCmd(ExitStatus.USAGE, terminal, privateKeyPath + " does not exist");
|
||||
} else if (doesNotExist(publicKeyPath)) {
|
||||
} else if (!Files.exists(publicKeyPath)) {
|
||||
return exitCmd(ExitStatus.USAGE, terminal, publicKeyPath + " does not exist");
|
||||
}
|
||||
|
||||
|
@ -92,7 +91,7 @@ public class LicenseGeneratorTool extends CliTool {
|
|||
if (licenseSpecSourceFiles != null) {
|
||||
for (String licenseSpecFilePath : licenseSpecSourceFiles) {
|
||||
Path licenseSpecPath = Paths.get(licenseSpecFilePath);
|
||||
if (doesNotExist(licenseSpecFilePath)) {
|
||||
if (!Files.exists(licenseSpecPath)) {
|
||||
return exitCmd(ExitStatus.USAGE, terminal, licenseSpecFilePath + " does not exist");
|
||||
}
|
||||
licenseSpecs.addAll(Licenses.fromSource(Files.readAllBytes(licenseSpecPath), false));
|
||||
|
@ -119,11 +118,6 @@ public class LicenseGeneratorTool extends CliTool {
|
|||
|
||||
return ExitStatus.OK;
|
||||
}
|
||||
|
||||
|
||||
private static boolean doesNotExist(String filePath) {
|
||||
return !new File(filePath).exists();
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
|
|
@ -68,7 +68,7 @@ public class LicenseVerificationTool extends CliTool {
|
|||
}
|
||||
|
||||
public static Command parse(Terminal terminal, CommandLine commandLine) throws IOException {
|
||||
String publicKeyPath = commandLine.getOptionValue("publicKeyPath");
|
||||
String publicKeyPathString = commandLine.getOptionValue("publicKeyPath");
|
||||
String[] licenseSources = commandLine.getOptionValues("license");
|
||||
String[] licenseSourceFiles = commandLine.getOptionValues("licenseFile");
|
||||
|
||||
|
@ -82,7 +82,7 @@ public class LicenseVerificationTool extends CliTool {
|
|||
if (licenseSourceFiles != null) {
|
||||
for (String licenseFilePath : licenseSourceFiles) {
|
||||
Path licensePath = Paths.get(licenseFilePath);
|
||||
if (!exists(licenseFilePath)) {
|
||||
if (!Files.exists(licensePath)) {
|
||||
return exitCmd(ExitStatus.USAGE, terminal, licenseFilePath + " does not exist");
|
||||
}
|
||||
licenses.addAll(Licenses.fromSource(Files.readAllBytes(licensePath)));
|
||||
|
@ -93,11 +93,12 @@ public class LicenseVerificationTool extends CliTool {
|
|||
return exitCmd(ExitStatus.USAGE, terminal, "no license provided");
|
||||
}
|
||||
|
||||
if (!exists(publicKeyPath)) {
|
||||
Path publicKeyPath = Paths.get(publicKeyPathString);
|
||||
if (!Files.exists(publicKeyPath)) {
|
||||
return exitCmd(ExitStatus.USAGE, terminal, publicKeyPath + " does not exist");
|
||||
}
|
||||
|
||||
return new LicenseVerifier(terminal, licenses, Paths.get(publicKeyPath));
|
||||
return new LicenseVerifier(terminal, licenses, publicKeyPath);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -119,10 +120,6 @@ public class LicenseVerificationTool extends CliTool {
|
|||
|
||||
return ExitStatus.OK;
|
||||
}
|
||||
|
||||
private static boolean exists(String filePath) {
|
||||
return new File(filePath).exists();
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
|
|
@ -15,6 +15,8 @@ import org.junit.Test;
|
|||
import org.junit.rules.TemporaryFolder;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import static org.elasticsearch.common.cli.CliTool.Command;
|
||||
|
@ -78,6 +80,8 @@ public class KeyPairGenerationToolTests extends CliToolTestCase {
|
|||
File tempFile2 = temporaryFolder.newFile();
|
||||
String publicKeyPath = tempFile1.getAbsolutePath();
|
||||
String privateKeyPath = tempFile2.getAbsolutePath();
|
||||
Path publicKeyFilePath = Paths.get(publicKeyPath);
|
||||
Path privateKeyFilePath = Paths.get(privateKeyPath);
|
||||
|
||||
assertThat(tempFile1.delete(), equalTo(true));
|
||||
assertThat(tempFile2.delete(), equalTo(true));
|
||||
|
@ -87,17 +91,17 @@ public class KeyPairGenerationToolTests extends CliToolTestCase {
|
|||
|
||||
assertThat(command, instanceOf(KeyGenerator.class));
|
||||
KeyGenerator keyGenerator = (KeyGenerator) command;
|
||||
assertThat(keyGenerator.privateKeyPath, equalTo(privateKeyPath));
|
||||
assertThat(keyGenerator.publicKeyPath, equalTo(publicKeyPath));
|
||||
assertThat(keyGenerator.privateKeyPath, equalTo(privateKeyFilePath));
|
||||
assertThat(keyGenerator.publicKeyPath, equalTo(publicKeyFilePath));
|
||||
|
||||
assertThat(Paths.get(publicKeyPath).toFile().exists(), equalTo(false));
|
||||
assertThat(Paths.get(privateKeyPath).toFile().exists(), equalTo(false));
|
||||
assertThat(Files.exists(publicKeyFilePath), equalTo(false));
|
||||
assertThat(Files.exists(privateKeyFilePath), equalTo(false));
|
||||
|
||||
assertThat(keyGenerator.execute(ImmutableSettings.EMPTY, new Environment(ImmutableSettings.EMPTY)), equalTo(ExitStatus.OK));
|
||||
assertThat(Paths.get(publicKeyPath).toFile().exists(), equalTo(true));
|
||||
assertThat(Paths.get(privateKeyPath).toFile().exists(), equalTo(true));
|
||||
assertThat(Files.exists(publicKeyFilePath), equalTo(true));
|
||||
assertThat(Files.exists(privateKeyFilePath), equalTo(true));
|
||||
|
||||
assertThat(Paths.get(publicKeyPath).toFile().delete(), equalTo(true));
|
||||
assertThat(Paths.get(privateKeyPath).toFile().delete(), equalTo(true));
|
||||
Files.delete(publicKeyFilePath);
|
||||
Files.delete(privateKeyFilePath);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,6 +22,7 @@ import org.junit.rules.TemporaryFolder;
|
|||
import java.io.File;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.*;
|
||||
|
||||
|
@ -110,8 +111,8 @@ public class LicenseGenerationToolTests extends CliToolTestCase {
|
|||
|
||||
assertThat(command, instanceOf(LicenseGenerator.class));
|
||||
LicenseGenerator licenseGenerator = (LicenseGenerator) command;
|
||||
assertThat(licenseGenerator.publicKeyFilePath, equalTo(pubKeyPath));
|
||||
assertThat(licenseGenerator.privateKeyFilePath, equalTo(priKeyPath));
|
||||
assertThat(licenseGenerator.publicKeyFilePath, equalTo(Paths.get(pubKeyPath)));
|
||||
assertThat(licenseGenerator.privateKeyFilePath, equalTo(Paths.get(priKeyPath)));
|
||||
assertThat(licenseGenerator.licenseSpecs.size(), equalTo(1));
|
||||
License outputLicenseSpec = licenseGenerator.licenseSpecs.iterator().next();
|
||||
|
||||
|
@ -132,8 +133,8 @@ public class LicenseGenerationToolTests extends CliToolTestCase {
|
|||
|
||||
assertThat(command, instanceOf(LicenseGenerator.class));
|
||||
LicenseGenerator licenseGenerator = (LicenseGenerator) command;
|
||||
assertThat(licenseGenerator.publicKeyFilePath, equalTo(pubKeyPath));
|
||||
assertThat(licenseGenerator.privateKeyFilePath, equalTo(priKeyPath));
|
||||
assertThat(licenseGenerator.publicKeyFilePath, equalTo(Paths.get(pubKeyPath)));
|
||||
assertThat(licenseGenerator.privateKeyFilePath, equalTo(Paths.get(priKeyPath)));
|
||||
assertThat(licenseGenerator.licenseSpecs.size(), equalTo(1));
|
||||
License outputLicenseSpec = licenseGenerator.licenseSpecs.iterator().next();
|
||||
|
||||
|
@ -156,8 +157,8 @@ public class LicenseGenerationToolTests extends CliToolTestCase {
|
|||
|
||||
assertThat(command, instanceOf(LicenseGenerator.class));
|
||||
LicenseGenerator licenseGenerator = (LicenseGenerator) command;
|
||||
assertThat(licenseGenerator.publicKeyFilePath, equalTo(pubKeyPath));
|
||||
assertThat(licenseGenerator.privateKeyFilePath, equalTo(priKeyPath));
|
||||
assertThat(licenseGenerator.publicKeyFilePath, equalTo(Paths.get(pubKeyPath)));
|
||||
assertThat(licenseGenerator.privateKeyFilePath, equalTo(Paths.get(priKeyPath)));
|
||||
assertThat(licenseGenerator.licenseSpecs.size(), equalTo(inputLicenseSpecs.size()));
|
||||
|
||||
for (License outputLicenseSpec : licenseGenerator.licenseSpecs) {
|
||||
|
@ -177,7 +178,7 @@ public class LicenseGenerationToolTests extends CliToolTestCase {
|
|||
}
|
||||
List<License> licenseSpecs = Licenses.fromSource(TestUtils.generateLicenseSpecString(new ArrayList<>(inputLicenseSpecs.values())).getBytes(StandardCharsets.UTF_8), false);
|
||||
|
||||
String output = runLicenseGenerationTool(pubKeyPath, priKeyPath, new HashSet<>(licenseSpecs), ExitStatus.OK);
|
||||
String output = runLicenseGenerationTool(Paths.get(pubKeyPath), Paths.get(priKeyPath), new HashSet<>(licenseSpecs), ExitStatus.OK);
|
||||
List<License> outputLicenses = Licenses.fromSource(output.getBytes(StandardCharsets.UTF_8), true);
|
||||
assertThat(outputLicenses.size(), equalTo(inputLicenseSpecs.size()));
|
||||
|
||||
|
@ -188,7 +189,7 @@ public class LicenseGenerationToolTests extends CliToolTestCase {
|
|||
}
|
||||
}
|
||||
|
||||
private String runLicenseGenerationTool(String pubKeyPath, String priKeyPath, Set<License> licenseSpecs, ExitStatus expectedExitStatus) throws Exception {
|
||||
private String runLicenseGenerationTool(Path pubKeyPath, Path priKeyPath, Set<License> licenseSpecs, ExitStatus expectedExitStatus) throws Exception {
|
||||
CaptureOutputTerminal outputTerminal = new CaptureOutputTerminal();
|
||||
LicenseGenerator licenseGenerator = new LicenseGenerator(outputTerminal, pubKeyPath, priKeyPath, licenseSpecs);
|
||||
assertThat(execute(licenseGenerator, ImmutableSettings.EMPTY), equalTo(expectedExitStatus));
|
||||
|
|
Loading…
Reference in New Issue