diff --git a/dev-tools/checkstyle_suppressions.xml b/dev-tools/checkstyle_suppressions.xml index 2c3269cfb9c..b759549b60e 100644 --- a/dev-tools/checkstyle_suppressions.xml +++ b/dev-tools/checkstyle_suppressions.xml @@ -5,17 +5,6 @@ - - - - - - - - - - - @@ -1404,6 +1393,8 @@ + + diff --git a/license-tools/src/main/java/org/elasticsearch/license/licensor/LicenseSigner.java b/license-tools/src/main/java/org/elasticsearch/license/licensor/LicenseSigner.java index ab6b9a61d5b..ca080362db1 100644 --- a/license-tools/src/main/java/org/elasticsearch/license/licensor/LicenseSigner.java +++ b/license-tools/src/main/java/org/elasticsearch/license/licensor/LicenseSigner.java @@ -25,10 +25,11 @@ import java.security.Signature; import java.security.SignatureException; import java.util.Base64; import java.util.Collections; +import java.util.Map; /** - * Responsible for generating a license signature according to - * the signature spec and sign it with the provided encrypted private key + * Responsible for generating a license signature according to the signature spec and sign it with + * the provided encrypted private key */ public class LicenseSigner { @@ -44,15 +45,18 @@ public class LicenseSigner { } /** - * Generates a signature for the licenseSpec. - * Signature structure: + * Generates a signature for the {@code licenseSpec}. Signature structure: + * * | VERSION | MAGIC | PUB_KEY_DIGEST | SIGNED_LICENSE_CONTENT | + * * * @return a signed License */ public License sign(License licenseSpec) throws IOException { XContentBuilder contentBuilder = XContentFactory.contentBuilder(XContentType.JSON); - licenseSpec.toXContent(contentBuilder, new ToXContent.MapParams(Collections.singletonMap(License.LICENSE_SPEC_VIEW_MODE, "true"))); + final Map licenseSpecViewMode = + Collections.singletonMap(License.LICENSE_SPEC_VIEW_MODE, "true"); + licenseSpec.toXContent(contentBuilder, new ToXContent.MapParams(licenseSpecViewMode)); final byte[] signedContent; try { final Signature rsa = Signature.getInstance("SHA512withRSA"); @@ -63,7 +67,10 @@ public class LicenseSigner { rsa.update(ref.bytes, ref.offset, ref.length); } signedContent = rsa.sign(); - } catch (InvalidKeyException | IOException | NoSuchAlgorithmException | SignatureException e) { + } catch (InvalidKeyException + | IOException + | NoSuchAlgorithmException + | SignatureException e) { throw new IllegalStateException(e); } final byte[] magic = new byte[MAGIC_LENGTH]; diff --git a/license-tools/src/main/java/org/elasticsearch/license/licensor/tools/KeyPairGeneratorTool.java b/license-tools/src/main/java/org/elasticsearch/license/licensor/tools/KeyPairGeneratorTool.java index ba3a0c82d50..80cf2d350f0 100644 --- a/license-tools/src/main/java/org/elasticsearch/license/licensor/tools/KeyPairGeneratorTool.java +++ b/license-tools/src/main/java/org/elasticsearch/license/licensor/tools/KeyPairGeneratorTool.java @@ -44,8 +44,8 @@ public class KeyPairGeneratorTool extends Command { @Override protected void printAdditionalHelp(Terminal terminal) { terminal.println("This tool generates and saves a key pair to the provided publicKeyPath"); - terminal.println("and privateKeyPath. The tool checks the existence of the provided key paths"); - terminal.println("and will not override if any existing keys are found."); + terminal.println("and privateKeyPath. The tool checks the existence of the provided key"); + terminal.println("paths and will not override if any existing keys are found."); terminal.println(""); } @@ -67,12 +67,17 @@ public class KeyPairGeneratorTool extends Command { Files.write(privateKeyPath, writeEncryptedPrivateKey(keyPair.getPrivate())); Files.write(publicKeyPath, writeEncryptedPublicKey(keyPair.getPublic())); - terminal.println(Terminal.Verbosity.VERBOSE, "generating key pair [public key: " + publicKeyPath + ", private key: " - + privateKeyPath + "]"); + terminal.println( + Terminal.Verbosity.VERBOSE, + "generating key pair [public key: " + + publicKeyPath + + ", private key: " + + privateKeyPath + "]"); } @SuppressForbidden(reason = "Parsing command line path") private static Path parsePath(String path) { return PathUtils.get(path); } + } diff --git a/license-tools/src/main/java/org/elasticsearch/license/licensor/tools/LicenseGeneratorTool.java b/license-tools/src/main/java/org/elasticsearch/license/licensor/tools/LicenseGeneratorTool.java index 21a1fc3ebf4..f3ceb1810bb 100644 --- a/license-tools/src/main/java/org/elasticsearch/license/licensor/tools/LicenseGeneratorTool.java +++ b/license-tools/src/main/java/org/elasticsearch/license/licensor/tools/LicenseGeneratorTool.java @@ -5,16 +5,12 @@ */ package org.elasticsearch.license.licensor.tools; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; - import joptsimple.OptionSet; import joptsimple.OptionSpec; import org.elasticsearch.cli.Command; import org.elasticsearch.cli.ExitCodes; -import org.elasticsearch.cli.UserException; import org.elasticsearch.cli.Terminal; +import org.elasticsearch.cli.UserException; import org.elasticsearch.common.SuppressForbidden; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.io.PathUtils; @@ -25,6 +21,10 @@ import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.license.License; import org.elasticsearch.license.licensor.LicenseSigner; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; + public class LicenseGeneratorTool extends Command { private final OptionSpec publicKeyPathOption; @@ -71,16 +71,21 @@ public class LicenseGeneratorTool extends Command { final License licenseSpec; if (options.has(licenseOption)) { + final BytesArray bytes = + new BytesArray(licenseOption.value(options).getBytes(StandardCharsets.UTF_8)); licenseSpec = - License.fromSource(new BytesArray(licenseOption.value(options).getBytes(StandardCharsets.UTF_8)), XContentType.JSON); + License.fromSource(bytes, XContentType.JSON); } else if (options.has(licenseFileOption)) { Path licenseSpecPath = parsePath(licenseFileOption.value(options)); if (Files.exists(licenseSpecPath) == false) { throw new UserException(ExitCodes.USAGE, licenseSpecPath + " does not exist"); } - licenseSpec = License.fromSource(new BytesArray(Files.readAllBytes(licenseSpecPath)), XContentType.JSON); + final BytesArray bytes = new BytesArray(Files.readAllBytes(licenseSpecPath)); + licenseSpec = License.fromSource(bytes, XContentType.JSON); } else { - throw new UserException(ExitCodes.USAGE, "Must specify either --license or --licenseFile"); + throw new UserException( + ExitCodes.USAGE, + "Must specify either --license or --licenseFile"); } // sign @@ -101,4 +106,5 @@ public class LicenseGeneratorTool extends Command { private static Path parsePath(String path) { return PathUtils.get(path); } + } diff --git a/license-tools/src/main/java/org/elasticsearch/license/licensor/tools/LicenseVerificationTool.java b/license-tools/src/main/java/org/elasticsearch/license/licensor/tools/LicenseVerificationTool.java index 3c4daa67767..1936a37844c 100644 --- a/license-tools/src/main/java/org/elasticsearch/license/licensor/tools/LicenseVerificationTool.java +++ b/license-tools/src/main/java/org/elasticsearch/license/licensor/tools/LicenseVerificationTool.java @@ -5,16 +5,12 @@ */ package org.elasticsearch.license.licensor.tools; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; - import joptsimple.OptionSet; import joptsimple.OptionSpec; import org.elasticsearch.cli.Command; import org.elasticsearch.cli.ExitCodes; -import org.elasticsearch.cli.UserException; import org.elasticsearch.cli.Terminal; +import org.elasticsearch.cli.UserException; import org.elasticsearch.common.SuppressForbidden; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.io.PathUtils; @@ -25,6 +21,10 @@ import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.license.License; import org.elasticsearch.license.LicenseVerifier; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; + public class LicenseVerificationTool extends Command { private final OptionSpec publicKeyPathOption; @@ -56,20 +56,25 @@ public class LicenseVerificationTool extends Command { final License licenseSpec; if (options.has(licenseOption)) { + final BytesArray bytes = + new BytesArray(licenseOption.value(options).getBytes(StandardCharsets.UTF_8)); licenseSpec = - License.fromSource(new BytesArray(licenseOption.value(options).getBytes(StandardCharsets.UTF_8)), XContentType.JSON); + License.fromSource(bytes, XContentType.JSON); } else if (options.has(licenseFileOption)) { Path licenseSpecPath = parsePath(licenseFileOption.value(options)); if (Files.exists(licenseSpecPath) == false) { throw new UserException(ExitCodes.USAGE, licenseSpecPath + " does not exist"); } - licenseSpec = License.fromSource(new BytesArray(Files.readAllBytes(licenseSpecPath)), XContentType.JSON); + final BytesArray bytes = new BytesArray(Files.readAllBytes(licenseSpecPath)); + licenseSpec = License.fromSource(bytes, XContentType.JSON); } else { - throw new UserException(ExitCodes.USAGE, "Must specify either --license or --licenseFile"); + throw new UserException( + ExitCodes.USAGE, + "Must specify either --license or --licenseFile"); } // verify - if (LicenseVerifier.verifyLicense(licenseSpec, Files.readAllBytes(publicKeyPath)) == false) { + if (!LicenseVerifier.verifyLicense(licenseSpec, Files.readAllBytes(publicKeyPath))) { throw new UserException(ExitCodes.DATA_ERROR, "Invalid License!"); } XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON); diff --git a/license-tools/src/test/java/org/elasticsearch/license/licensor/LicenseVerificationTests.java b/license-tools/src/test/java/org/elasticsearch/license/licensor/LicenseVerificationTests.java index fc0b459a80a..34383739b50 100644 --- a/license-tools/src/test/java/org/elasticsearch/license/licensor/LicenseVerificationTests.java +++ b/license-tools/src/test/java/org/elasticsearch/license/licensor/LicenseVerificationTests.java @@ -16,9 +16,8 @@ import org.junit.Before; import java.nio.file.Files; import java.nio.file.Path; -import static org.hamcrest.Matchers.equalTo; - public class LicenseVerificationTests extends ESTestCase { + protected Path pubKeyPath = null; protected Path priKeyPath = null; @@ -35,12 +34,15 @@ public class LicenseVerificationTests extends ESTestCase { } public void testGeneratedLicenses() throws Exception { - assertThat(LicenseVerifier.verifyLicense(TestUtils.generateSignedLicense(TimeValue.timeValueHours(2 * 24), pubKeyPath, priKeyPath), - Files.readAllBytes(pubKeyPath)), equalTo(true)); + final TimeValue fortyEightHours = TimeValue.timeValueHours(2 * 24); + final License license = + TestUtils.generateSignedLicense(fortyEightHours, pubKeyPath, priKeyPath); + assertTrue(LicenseVerifier.verifyLicense(license, Files.readAllBytes(pubKeyPath))); } public void testLicenseTampering() throws Exception { - License license = TestUtils.generateSignedLicense(TimeValue.timeValueHours(2), pubKeyPath, priKeyPath); + final TimeValue twoHours = TimeValue.timeValueHours(2); + License license = TestUtils.generateSignedLicense(twoHours, pubKeyPath, priKeyPath); final License tamperedLicense = License.builder() .fromLicenseSpec(license, license.signature()) @@ -48,17 +50,18 @@ public class LicenseVerificationTests extends ESTestCase { .validate() .build(); - assertThat(LicenseVerifier.verifyLicense(tamperedLicense, Files.readAllBytes(pubKeyPath)), equalTo(false)); + assertFalse(LicenseVerifier.verifyLicense(tamperedLicense, Files.readAllBytes(pubKeyPath))); } public void testRandomLicenseVerification() throws Exception { TestUtils.LicenseSpec licenseSpec = TestUtils.generateRandomLicenseSpec( randomIntBetween(License.VERSION_START, License.VERSION_CURRENT)); License generatedLicense = generateSignedLicense(licenseSpec, pubKeyPath, priKeyPath); - assertThat(LicenseVerifier.verifyLicense(generatedLicense, Files.readAllBytes(pubKeyPath)), equalTo(true)); + assertTrue(LicenseVerifier.verifyLicense(generatedLicense, Files.readAllBytes(pubKeyPath))); } - private static License generateSignedLicense(TestUtils.LicenseSpec spec, Path pubKeyPath, Path priKeyPath) throws Exception { + private static License generateSignedLicense( + TestUtils.LicenseSpec spec, Path pubKeyPath, Path priKeyPath) throws Exception { LicenseSigner signer = new LicenseSigner(priKeyPath, pubKeyPath); License.Builder builder = License.builder() .uid(spec.uid) @@ -82,4 +85,5 @@ public class LicenseVerificationTests extends ESTestCase { builder.version(spec.version); return signer.sign(builder.build()); } + } diff --git a/license-tools/src/test/java/org/elasticsearch/license/licensor/TestUtils.java b/license-tools/src/test/java/org/elasticsearch/license/licensor/TestUtils.java index f45fa0f4b4f..1d5ac00f38d 100644 --- a/license-tools/src/test/java/org/elasticsearch/license/licensor/TestUtils.java +++ b/license-tools/src/test/java/org/elasticsearch/license/licensor/TestUtils.java @@ -22,6 +22,7 @@ import org.joda.time.format.DateTimeFormatter; import java.io.IOException; import java.nio.file.Path; import java.util.UUID; + import static com.carrotsearch.randomizedtesting.RandomizedTest.randomBoolean; import static com.carrotsearch.randomizedtesting.RandomizedTest.randomInt; import static com.carrotsearch.randomizedtesting.RandomizedTest.randomIntBetween; @@ -34,8 +35,10 @@ public class TestUtils { public static final String PUBLIC_KEY_RESOURCE = "/public.key"; public static final String PRIVATE_KEY_RESOURCE = "/private.key"; - private static final FormatDateTimeFormatter formatDateTimeFormatter = Joda.forPattern("yyyy-MM-dd"); - private static final DateMathParser dateMathParser = new DateMathParser(formatDateTimeFormatter); + private static final FormatDateTimeFormatter formatDateTimeFormatter = + Joda.forPattern("yyyy-MM-dd"); + private static final DateMathParser dateMathParser = + new DateMathParser(formatDateTimeFormatter); private static final DateTimeFormatter dateTimeFormatter = formatDateTimeFormatter.printer(); public static String dumpLicense(License license) throws Exception { @@ -78,12 +81,30 @@ public class TestUtils { if (datesInMillis) { long issueDateInMillis = dateMath("now", now); long expiryDateInMillis = dateMath("now+10d/d", now); - return new LicenseSpec(version, uid, feature, issueDateInMillis, expiryDateInMillis, type, subscriptionType, issuedTo, issuer, + return new LicenseSpec( + version, + uid, + feature, + issueDateInMillis, + expiryDateInMillis, + type, + subscriptionType, + issuedTo, + issuer, maxNodes); } else { String issueDate = dateMathString("now", now); String expiryDate = dateMathString("now+10d/d", now); - return new LicenseSpec(version, uid, feature, issueDate, expiryDate, type, subscriptionType, issuedTo, issuer, maxNodes); + return new LicenseSpec( + version, + uid, + feature, + issueDate, + expiryDate, type, + subscriptionType, + issuedTo, + issuer, + maxNodes); } } @@ -122,18 +143,23 @@ public class TestUtils { MatcherAssert.assertThat(license.type(), equalTo(spec.type)); MatcherAssert.assertThat(license.maxNodes(), equalTo(spec.maxNodes)); if (spec.issueDate != null) { - MatcherAssert.assertThat(license.issueDate(), equalTo(DateUtils.beginningOfTheDay(spec.issueDate))); + MatcherAssert.assertThat( + license.issueDate(), + equalTo(DateUtils.beginningOfTheDay(spec.issueDate))); } else { MatcherAssert.assertThat(license.issueDate(), equalTo(spec.issueDateInMillis)); } if (spec.expiryDate != null) { - MatcherAssert.assertThat(license.expiryDate(), equalTo(DateUtils.endOfTheDay(spec.expiryDate))); + MatcherAssert.assertThat( + license.expiryDate(), + equalTo(DateUtils.endOfTheDay(spec.expiryDate))); } else { MatcherAssert.assertThat(license.expiryDate(), equalTo(spec.expiryDateInMillis)); } } - public static License generateSignedLicense(TimeValue expiryDuration, Path pubKeyPath, Path priKeyPath) throws Exception { + public static License generateSignedLicense( + TimeValue expiryDuration, Path pubKeyPath, Path priKeyPath) throws Exception { long issue = System.currentTimeMillis(); int version = ESTestCase.randomIntBetween(License.VERSION_START, License.VERSION_CURRENT); String type = version < License.VERSION_NO_FEATURE_TYPE ? @@ -170,8 +196,17 @@ public class TestUtils { public final String issuer; public final int maxNodes; - public LicenseSpec(int version, String uid, String feature, long issueDateInMillis, long expiryDateInMillis, String type, - String subscriptionType, String issuedTo, String issuer, int maxNodes) { + public LicenseSpec( + int version, + String uid, + String feature, + long issueDateInMillis, + long expiryDateInMillis, + String type, + String subscriptionType, + String issuedTo, + String issuer, + int maxNodes) { this.version = version; this.feature = feature; this.issueDateInMillis = issueDateInMillis; @@ -186,8 +221,17 @@ public class TestUtils { this.maxNodes = maxNodes; } - public LicenseSpec(int version, String uid, String feature, String issueDate, String expiryDate, String type, - String subscriptionType, String issuedTo, String issuer, int maxNodes) { + public LicenseSpec( + int version, + String uid, + String feature, + String issueDate, + String expiryDate, + String type, + String subscriptionType, + String issuedTo, + String issuer, + int maxNodes) { this.version = version; this.feature = feature; this.issueDate = issueDate; @@ -202,4 +246,5 @@ public class TestUtils { this.maxNodes = maxNodes; } } + } diff --git a/license-tools/src/test/java/org/elasticsearch/license/licensor/tools/KeyPairGenerationToolTests.java b/license-tools/src/test/java/org/elasticsearch/license/licensor/tools/KeyPairGenerationToolTests.java index 35e70e8d756..a55037e3f7c 100644 --- a/license-tools/src/test/java/org/elasticsearch/license/licensor/tools/KeyPairGenerationToolTests.java +++ b/license-tools/src/test/java/org/elasticsearch/license/licensor/tools/KeyPairGenerationToolTests.java @@ -5,14 +5,14 @@ */ package org.elasticsearch.license.licensor.tools; -import java.nio.file.Files; -import java.nio.file.Path; - import org.elasticsearch.cli.Command; import org.elasticsearch.cli.CommandTestCase; import org.elasticsearch.cli.ExitCodes; import org.elasticsearch.cli.UserException; +import java.nio.file.Files; +import java.nio.file.Path; + import static org.hamcrest.CoreMatchers.containsString; public class KeyPairGenerationToolTests extends CommandTestCase { @@ -25,14 +25,22 @@ public class KeyPairGenerationToolTests extends CommandTestCase { public void testMissingKeyPaths() throws Exception { Path exists = createTempFile("", "existing"); Path dne = createTempDir().resolve("dne"); - UserException e = expectThrows(UserException.class, () -> { - execute("--publicKeyPath", exists.toString(), "--privateKeyPath", dne.toString()); - }); + UserException e = expectThrows( + UserException.class, + () -> execute( + "--publicKeyPath", + exists.toString(), + "--privateKeyPath", + dne.toString())); assertThat(e.getMessage(), containsString("existing")); assertEquals(ExitCodes.USAGE, e.exitCode); - e = expectThrows(UserException.class, () -> { - execute("--publicKeyPath", dne.toString(), "--privateKeyPath", exists.toString()); - }); + e = expectThrows( + UserException.class, + () -> execute( + "--publicKeyPath", + dne.toString(), + "--privateKeyPath", + exists.toString())); assertThat(e.getMessage(), containsString("existing")); assertEquals(ExitCodes.USAGE, e.exitCode); } @@ -42,8 +50,13 @@ public class KeyPairGenerationToolTests extends CommandTestCase { Path publicKeyFilePath = keysDir.resolve("public"); Path privateKeyFilePath = keysDir.resolve("private"); - execute("--publicKeyPath", publicKeyFilePath.toString(), "--privateKeyPath", privateKeyFilePath.toString()); + execute( + "--publicKeyPath", + publicKeyFilePath.toString(), + "--privateKeyPath", + privateKeyFilePath.toString()); assertTrue(publicKeyFilePath.toString(), Files.exists(publicKeyFilePath)); assertTrue(privateKeyFilePath.toString(), Files.exists(privateKeyFilePath)); } + } diff --git a/license-tools/src/test/java/org/elasticsearch/license/licensor/tools/LicenseGenerationToolTests.java b/license-tools/src/test/java/org/elasticsearch/license/licensor/tools/LicenseGenerationToolTests.java index f00b4f8e201..28971e9b5a8 100644 --- a/license-tools/src/test/java/org/elasticsearch/license/licensor/tools/LicenseGenerationToolTests.java +++ b/license-tools/src/test/java/org/elasticsearch/license/licensor/tools/LicenseGenerationToolTests.java @@ -5,10 +5,6 @@ */ package org.elasticsearch.license.licensor.tools; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; - import org.elasticsearch.cli.Command; import org.elasticsearch.cli.CommandTestCase; import org.elasticsearch.cli.ExitCodes; @@ -19,7 +15,12 @@ import org.elasticsearch.license.License; import org.elasticsearch.license.licensor.TestUtils; import org.junit.Before; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; + public class LicenseGenerationToolTests extends CommandTestCase { + protected Path pubKeyPath = null; protected Path priKeyPath = null; @@ -37,47 +38,74 @@ public class LicenseGenerationToolTests extends CommandTestCase { public void testMissingKeyPaths() throws Exception { Path pub = createTempDir().resolve("pub"); Path pri = createTempDir().resolve("pri"); - UserException e = expectThrows(UserException.class, () -> { - execute("--publicKeyPath", pub.toString(), "--privateKeyPath", pri.toString()); - }); + UserException e = expectThrows( + UserException.class, + () -> execute( + "--publicKeyPath", + pub.toString(), + "--privateKeyPath", + pri.toString())); assertTrue(e.getMessage(), e.getMessage().contains("pri does not exist")); assertEquals(ExitCodes.USAGE, e.exitCode); Files.createFile(pri); - e = expectThrows(UserException.class, () -> { - execute("--publicKeyPath", pub.toString(), "--privateKeyPath", pri.toString()); - }); + e = expectThrows( + UserException.class, + () -> execute( + "--publicKeyPath", + pub.toString(), + "--privateKeyPath", + pri.toString())); assertTrue(e.getMessage(), e.getMessage().contains("pub does not exist")); assertEquals(ExitCodes.USAGE, e.exitCode); } public void testMissingLicenseSpec() throws Exception { - UserException e = expectThrows(UserException.class, () -> { - execute("--publicKeyPath", pubKeyPath.toString(), "--privateKeyPath", priKeyPath.toString()); - }); - assertTrue(e.getMessage(), e.getMessage().contains("Must specify either --license or --licenseFile")); + UserException e = expectThrows( + UserException.class, + () -> execute( + "--publicKeyPath", + pubKeyPath.toString(), + "--privateKeyPath", + priKeyPath.toString())); + assertTrue( + e.getMessage(), + e.getMessage().contains("Must specify either --license or --licenseFile")); assertEquals(ExitCodes.USAGE, e.exitCode); } public void testLicenseSpecString() throws Exception { - TestUtils.LicenseSpec inputLicenseSpec = TestUtils.generateRandomLicenseSpec(License.VERSION_CURRENT); + TestUtils.LicenseSpec inputLicenseSpec = + TestUtils.generateRandomLicenseSpec(License.VERSION_CURRENT); String licenseSpecString = TestUtils.generateLicenseSpecString(inputLicenseSpec); - String output = execute("--publicKeyPath", pubKeyPath.toString(), - "--privateKeyPath", priKeyPath.toString(), - "--license", licenseSpecString); - License outputLicense = License.fromSource(new BytesArray(output.getBytes(StandardCharsets.UTF_8)), XContentType.JSON); + String output = execute( + "--publicKeyPath", + pubKeyPath.toString(), + "--privateKeyPath", + priKeyPath.toString(), + "--license", + licenseSpecString); + final BytesArray bytes = new BytesArray(output.getBytes(StandardCharsets.UTF_8)); + License outputLicense = License.fromSource(bytes, XContentType.JSON); TestUtils.assertLicenseSpec(inputLicenseSpec, outputLicense); } public void testLicenseSpecFile() throws Exception { - TestUtils.LicenseSpec inputLicenseSpec = TestUtils.generateRandomLicenseSpec(License.VERSION_CURRENT); + TestUtils.LicenseSpec inputLicenseSpec = + TestUtils.generateRandomLicenseSpec(License.VERSION_CURRENT); String licenseSpecString = TestUtils.generateLicenseSpecString(inputLicenseSpec); Path licenseSpecFile = createTempFile(); Files.write(licenseSpecFile, licenseSpecString.getBytes(StandardCharsets.UTF_8)); - String output = execute("--publicKeyPath", pubKeyPath.toString(), - "--privateKeyPath", priKeyPath.toString(), - "--licenseFile", licenseSpecFile.toString()); - License outputLicense = License.fromSource(new BytesArray(output.getBytes(StandardCharsets.UTF_8)), XContentType.JSON); + String output = execute( + "--publicKeyPath", + pubKeyPath.toString(), + "--privateKeyPath", + priKeyPath.toString(), + "--licenseFile", + licenseSpecFile.toString()); + final BytesArray bytes = new BytesArray(output.getBytes(StandardCharsets.UTF_8)); + License outputLicense = License.fromSource(bytes, XContentType.JSON); TestUtils.assertLicenseSpec(inputLicenseSpec, outputLicense); } + } diff --git a/license-tools/src/test/java/org/elasticsearch/license/licensor/tools/LicenseVerificationToolTests.java b/license-tools/src/test/java/org/elasticsearch/license/licensor/tools/LicenseVerificationToolTests.java index 66e2d6eef64..b6dea94ba0d 100644 --- a/license-tools/src/test/java/org/elasticsearch/license/licensor/tools/LicenseVerificationToolTests.java +++ b/license-tools/src/test/java/org/elasticsearch/license/licensor/tools/LicenseVerificationToolTests.java @@ -5,10 +5,6 @@ */ package org.elasticsearch.license.licensor.tools; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; - import org.elasticsearch.cli.Command; import org.elasticsearch.cli.CommandTestCase; import org.elasticsearch.cli.ExitCodes; @@ -18,6 +14,10 @@ import org.elasticsearch.license.License; import org.elasticsearch.license.licensor.TestUtils; import org.junit.Before; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; + public class LicenseVerificationToolTests extends CommandTestCase { protected Path pubKeyPath = null; protected Path priKeyPath = null; @@ -36,9 +36,9 @@ public class LicenseVerificationToolTests extends CommandTestCase { public void testMissingKeyPath() throws Exception { Path pub = createTempDir().resolve("pub"); - UserException e = expectThrows(UserException.class, () -> { - execute("--publicKeyPath", pub.toString()); - }); + UserException e = expectThrows( + UserException.class, + () -> execute("--publicKeyPath", pub.toString())); assertTrue(e.getMessage(), e.getMessage().contains("pub does not exist")); assertEquals(ExitCodes.USAGE, e.exitCode); } @@ -47,36 +47,53 @@ public class LicenseVerificationToolTests extends CommandTestCase { UserException e = expectThrows(UserException.class, () -> { execute("--publicKeyPath", pubKeyPath.toString()); }); - assertTrue(e.getMessage(), e.getMessage().contains("Must specify either --license or --licenseFile")); + assertTrue( + e.getMessage(), + e.getMessage().contains("Must specify either --license or --licenseFile")); assertEquals(ExitCodes.USAGE, e.exitCode); } public void testBrokenLicense() throws Exception { - License signedLicense = TestUtils.generateSignedLicense(TimeValue.timeValueHours(1), pubKeyPath, priKeyPath); + final TimeValue oneHour = TimeValue.timeValueHours(1); + License signedLicense = TestUtils.generateSignedLicense(oneHour, pubKeyPath, priKeyPath); License tamperedLicense = License.builder() .fromLicenseSpec(signedLicense, signedLicense.signature()) .expiryDate(signedLicense.expiryDate() + randomIntBetween(1, 1000)).build(); - UserException e = expectThrows(UserException.class, () -> { - execute("--publicKeyPath", pubKeyPath.toString(), - "--license", TestUtils.dumpLicense(tamperedLicense)); - }); + UserException e = expectThrows( + UserException.class, + () -> execute( + "--publicKeyPath", + pubKeyPath.toString(), + "--license", + TestUtils.dumpLicense(tamperedLicense))); assertEquals("Invalid License!", e.getMessage()); assertEquals(ExitCodes.DATA_ERROR, e.exitCode); } public void testLicenseSpecString() throws Exception { - License signedLicense = TestUtils.generateSignedLicense(TimeValue.timeValueHours(1), pubKeyPath, priKeyPath); - String output = execute("--publicKeyPath", pubKeyPath.toString(), - "--license", TestUtils.dumpLicense(signedLicense)); + final TimeValue oneHour = TimeValue.timeValueHours(1); + License signedLicense = TestUtils.generateSignedLicense(oneHour, pubKeyPath, priKeyPath); + String output = execute( + "--publicKeyPath", + pubKeyPath.toString(), + "--license", + TestUtils.dumpLicense(signedLicense)); assertFalse(output, output.isEmpty()); } public void testLicenseSpecFile() throws Exception { - License signedLicense = TestUtils.generateSignedLicense(TimeValue.timeValueHours(1), pubKeyPath, priKeyPath); + final TimeValue oneHour = TimeValue.timeValueHours(1); + License signedLicense = TestUtils.generateSignedLicense(oneHour, pubKeyPath, priKeyPath); Path licenseSpecFile = createTempFile(); - Files.write(licenseSpecFile, TestUtils.dumpLicense(signedLicense).getBytes(StandardCharsets.UTF_8)); - String output = execute("--publicKeyPath", pubKeyPath.toString(), - "--licenseFile", licenseSpecFile.toString()); + Files.write( + licenseSpecFile, + TestUtils.dumpLicense(signedLicense).getBytes(StandardCharsets.UTF_8)); + String output = execute( + "--publicKeyPath", + pubKeyPath.toString(), + "--licenseFile", + licenseSpecFile.toString()); assertFalse(output, output.isEmpty()); } + }