From 6d66e0f4f5ff2c4a69fba781798258504261a886 Mon Sep 17 00:00:00 2001 From: Robert Muir Date: Thu, 21 May 2015 12:25:20 -0400 Subject: [PATCH] Resolve paths against environment Original commit: elastic/x-pack-elasticsearch@7e30f66bc0ca37166ead14faba31312f70400cd6 --- .../licensor/tools/KeyPairGeneratorTool.java | 11 ++++------- .../licensor/tools/LicenseGeneratorTool.java | 13 +++++-------- .../licensor/tools/LicenseVerificationTool.java | 12 ++++-------- 3 files changed, 13 insertions(+), 23 deletions(-) diff --git a/licensor/src/main/java/org/elasticsearch/license/licensor/tools/KeyPairGeneratorTool.java b/licensor/src/main/java/org/elasticsearch/license/licensor/tools/KeyPairGeneratorTool.java index a793f4f3e1c..fd401cee6a3 100644 --- a/licensor/src/main/java/org/elasticsearch/license/licensor/tools/KeyPairGeneratorTool.java +++ b/licensor/src/main/java/org/elasticsearch/license/licensor/tools/KeyPairGeneratorTool.java @@ -5,13 +5,11 @@ */ package org.elasticsearch.license.licensor.tools; -import org.elasticsearch.common.SuppressForbidden; import org.elasticsearch.common.cli.CliTool; import org.elasticsearch.common.cli.CliToolConfig; import org.elasticsearch.common.cli.Terminal; import org.elasticsearch.common.cli.commons.CommandLine; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.common.io.PathUtils; import org.elasticsearch.env.Environment; import java.io.IOException; @@ -41,10 +39,9 @@ public class KeyPairGeneratorTool extends CliTool { @Override protected Command parse(String s, CommandLine commandLine) throws Exception { - return KeyGenerator.parse(terminal, commandLine); + return KeyGenerator.parse(terminal, commandLine, env); } - @SuppressForbidden(reason = "command line tool") public static class KeyGenerator extends Command { private static final CliToolConfig.Cmd CMD = cmd(NAME, KeyGenerator.class) @@ -62,9 +59,9 @@ public class KeyPairGeneratorTool extends CliTool { this.publicKeyPath = publicKeyPath; } - public static Command parse(Terminal terminal, CommandLine commandLine) { - Path publicKeyPath = PathUtils.get(commandLine.getOptionValue("publicKeyPath")); - Path privateKeyPath = PathUtils.get(commandLine.getOptionValue("privateKeyPath")); + public static Command parse(Terminal terminal, CommandLine commandLine, Environment environment) { + Path publicKeyPath = environment.homeFile().resolve(commandLine.getOptionValue("publicKeyPath")); + Path privateKeyPath = environment.homeFile().resolve(commandLine.getOptionValue("privateKeyPath")); if (Files.exists(privateKeyPath)) { return exitCmd(ExitStatus.USAGE, terminal, privateKeyPath + " already exists"); diff --git a/licensor/src/main/java/org/elasticsearch/license/licensor/tools/LicenseGeneratorTool.java b/licensor/src/main/java/org/elasticsearch/license/licensor/tools/LicenseGeneratorTool.java index 485471723e1..a6a586990bb 100644 --- a/licensor/src/main/java/org/elasticsearch/license/licensor/tools/LicenseGeneratorTool.java +++ b/licensor/src/main/java/org/elasticsearch/license/licensor/tools/LicenseGeneratorTool.java @@ -5,13 +5,11 @@ */ package org.elasticsearch.license.licensor.tools; -import org.elasticsearch.common.SuppressForbidden; import org.elasticsearch.common.cli.CliTool; import org.elasticsearch.common.cli.CliToolConfig; import org.elasticsearch.common.cli.Terminal; import org.elasticsearch.common.cli.commons.CommandLine; import org.elasticsearch.common.collect.ImmutableSet; -import org.elasticsearch.common.io.PathUtils; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; @@ -46,10 +44,9 @@ public class LicenseGeneratorTool extends CliTool { @Override protected Command parse(String s, CommandLine commandLine) throws Exception { - return LicenseGenerator.parse(terminal, commandLine); + return LicenseGenerator.parse(terminal, commandLine, env); } - @SuppressForbidden(reason = "command line tool") public static class LicenseGenerator extends Command { private static final CliToolConfig.Cmd CMD = cmd(NAME, LicenseGenerator.class) @@ -71,9 +68,9 @@ public class LicenseGeneratorTool extends CliTool { this.publicKeyFilePath = publicKeyFilePath; } - public static Command parse(Terminal terminal, CommandLine commandLine) throws IOException { - Path publicKeyPath = PathUtils.get(commandLine.getOptionValue("publicKeyPath")); - Path privateKeyPath = PathUtils.get(commandLine.getOptionValue("privateKeyPath")); + public static Command parse(Terminal terminal, CommandLine commandLine, Environment environment) throws IOException { + Path publicKeyPath = environment.homeFile().resolve(commandLine.getOptionValue("publicKeyPath")); + Path privateKeyPath = environment.homeFile().resolve(commandLine.getOptionValue("privateKeyPath")); String[] licenseSpecSources = commandLine.getOptionValues("license"); String[] licenseSpecSourceFiles = commandLine.getOptionValues("licenseFile"); @@ -92,7 +89,7 @@ public class LicenseGeneratorTool extends CliTool { if (licenseSpecSourceFiles != null) { for (String licenseSpecFilePath : licenseSpecSourceFiles) { - Path licenseSpecPath = PathUtils.get(licenseSpecFilePath); + Path licenseSpecPath = environment.homeFile().resolve(licenseSpecFilePath); if (!Files.exists(licenseSpecPath)) { return exitCmd(ExitStatus.USAGE, terminal, licenseSpecFilePath + " does not exist"); } diff --git a/licensor/src/main/java/org/elasticsearch/license/licensor/tools/LicenseVerificationTool.java b/licensor/src/main/java/org/elasticsearch/license/licensor/tools/LicenseVerificationTool.java index 7d569262bf8..89fe9bdb425 100644 --- a/licensor/src/main/java/org/elasticsearch/license/licensor/tools/LicenseVerificationTool.java +++ b/licensor/src/main/java/org/elasticsearch/license/licensor/tools/LicenseVerificationTool.java @@ -9,19 +9,16 @@ import org.elasticsearch.common.cli.CliTool; import org.elasticsearch.common.cli.CliToolConfig; import org.elasticsearch.common.cli.Terminal; import org.elasticsearch.common.cli.commons.CommandLine; -import org.elasticsearch.common.io.PathUtils; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentType; -import org.elasticsearch.common.SuppressForbidden; import org.elasticsearch.env.Environment; import org.elasticsearch.license.core.License; import org.elasticsearch.license.core.LicenseVerifier; import org.elasticsearch.license.core.Licenses; -import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; @@ -47,10 +44,9 @@ public class LicenseVerificationTool extends CliTool { @Override protected Command parse(String s, CommandLine commandLine) throws Exception { - return LicenseVerifier.parse(terminal, commandLine); + return LicenseVerifier.parse(terminal, commandLine, env); } - @SuppressForbidden(reason = "command line tool") public static class LicenseVerifier extends Command { private static final CliToolConfig.Cmd CMD = cmd(NAME, LicenseVerifier.class) @@ -69,7 +65,7 @@ public class LicenseVerificationTool extends CliTool { this.publicKeyPath = publicKeyPath; } - public static Command parse(Terminal terminal, CommandLine commandLine) throws IOException { + public static Command parse(Terminal terminal, CommandLine commandLine, Environment environment) throws IOException { String publicKeyPathString = commandLine.getOptionValue("publicKeyPath"); String[] licenseSources = commandLine.getOptionValues("license"); String[] licenseSourceFiles = commandLine.getOptionValues("licenseFile"); @@ -83,7 +79,7 @@ public class LicenseVerificationTool extends CliTool { if (licenseSourceFiles != null) { for (String licenseFilePath : licenseSourceFiles) { - Path licensePath = PathUtils.get(licenseFilePath); + Path licensePath = environment.homeFile().resolve(licenseFilePath); if (!Files.exists(licensePath)) { return exitCmd(ExitStatus.USAGE, terminal, licenseFilePath + " does not exist"); } @@ -95,7 +91,7 @@ public class LicenseVerificationTool extends CliTool { return exitCmd(ExitStatus.USAGE, terminal, "no license provided"); } - Path publicKeyPath = PathUtils.get(publicKeyPathString); + Path publicKeyPath = environment.homeFile().resolve(publicKeyPathString); if (!Files.exists(publicKeyPath)) { return exitCmd(ExitStatus.USAGE, terminal, publicKeyPath + " does not exist"); }