Skip to content

Commit

Permalink
Replace JavaVersion with Runtime.Version (#85361)
Browse files Browse the repository at this point in the history
Since Java 9, the JDK has provided a means of parsing Java versions and
getting the current Java version. That class obviates the need for the
JavaVersion class of Elasticsearch. This commit removes the JavaVersion
class in favor of Runtime.Version.

Note that most of the changes here simply removed logic around
versioning because this change is intended only for the master branch,
where Java 17 is required.
  • Loading branch information
rjernst committed Mar 28, 2022
1 parent 121d17c commit 0832232
Show file tree
Hide file tree
Showing 36 changed files with 58 additions and 903 deletions.
27 changes: 4 additions & 23 deletions libs/core/src/main/java/org/elasticsearch/jdk/JarHell.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.elasticsearch.core.SuppressForbidden;

import java.io.IOException;
import java.lang.Runtime.Version;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
Expand Down Expand Up @@ -224,39 +225,19 @@ private static void checkManifest(Manifest manifest, Path jar) {
// give a nice error if jar requires a newer java version
String targetVersion = manifest.getMainAttributes().getValue("X-Compile-Target-JDK");
if (targetVersion != null) {
checkVersionFormat(targetVersion);
checkJavaVersion(jar.toString(), targetVersion);
}
}

public static void checkVersionFormat(String targetVersion) {
if (JavaVersion.isValid(targetVersion) == false) {
throw new IllegalStateException(
String.format(
Locale.ROOT,
"version string must be a sequence of nonnegative decimal integers separated by \".\"'s and may have "
+ "leading zeros but was %s",
targetVersion
)
);
}
}

/**
* Checks that the java specification version {@code targetVersion}
* required by {@code resource} is compatible with the current installation.
*/
public static void checkJavaVersion(String resource, String targetVersion) {
JavaVersion version = JavaVersion.parse(targetVersion);
if (JavaVersion.current().compareTo(version) < 0) {
Version version = Version.parse(targetVersion);
if (Runtime.version().compareTo(version) < 0) {
throw new IllegalStateException(
String.format(
Locale.ROOT,
"%s requires Java %s:, your system: %s",
resource,
targetVersion,
JavaVersion.current().toString()
)
String.format(Locale.ROOT, "%s requires Java %s:, your system: %s", resource, targetVersion, Runtime.version().toString())
);
}
}
Expand Down
122 changes: 0 additions & 122 deletions libs/core/src/main/java/org/elasticsearch/jdk/JavaVersion.java

This file was deleted.

48 changes: 12 additions & 36 deletions libs/core/src/test/java/org/elasticsearch/jdk/JarHellTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.elasticsearch.test.ESTestCase;

import java.io.IOException;
import java.lang.Runtime.Version;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -27,6 +28,9 @@
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;

public class JarHellTests extends ESTestCase {

URL makeJar(Path dir, String name, Manifest manifest, String... files) throws IOException {
Expand Down Expand Up @@ -132,25 +136,21 @@ public void testXmlBeansLeniency() throws Exception {

public void testRequiredJDKVersionTooOld() throws Exception {
Path dir = createTempDir();
List<Integer> current = JavaVersion.current().getVersion();
List<Integer> current = Runtime.version().version();
List<Integer> target = new ArrayList<>(current.size());
for (int i = 0; i < current.size(); i++) {
target.add(current.get(i) + 1);
}
JavaVersion targetVersion = JavaVersion.parse(Strings.collectionToDelimitedString(target, "."));
Version targetVersion = Version.parse(Strings.collectionToDelimitedString(target, "."));

Manifest manifest = new Manifest();
Attributes attributes = manifest.getMainAttributes();
attributes.put(Attributes.Name.MANIFEST_VERSION, "1.0.0");
attributes.put(new Attributes.Name("X-Compile-Target-JDK"), targetVersion.toString());
Set<URL> jars = Collections.singleton(makeJar(dir, "foo.jar", manifest, "Foo.class"));
try {
JarHell.checkJarHell(jars, logger::debug);
fail("did not get expected exception");
} catch (IllegalStateException e) {
assertTrue(e.getMessage().contains("requires Java " + targetVersion.toString()));
assertTrue(e.getMessage().contains("your system: " + JavaVersion.current().toString()));
}
var e = expectThrows(IllegalStateException.class, () -> JarHell.checkJarHell(jars, logger::debug));
assertThat(e.getMessage(), containsString("requires Java " + targetVersion));
assertThat(e.getMessage(), containsString("your system: " + Runtime.version().toString()));
}

public void testBadJDKVersionInJar() throws Exception {
Expand All @@ -160,18 +160,8 @@ public void testBadJDKVersionInJar() throws Exception {
attributes.put(Attributes.Name.MANIFEST_VERSION, "1.0.0");
attributes.put(new Attributes.Name("X-Compile-Target-JDK"), "bogus");
Set<URL> jars = Collections.singleton(makeJar(dir, "foo.jar", manifest, "Foo.class"));
try {
JarHell.checkJarHell(jars, logger::debug);
fail("did not get expected exception");
} catch (IllegalStateException e) {
assertTrue(
e.getMessage()
.equals(
"version string must be a sequence of nonnegative decimal integers separated "
+ "by \".\"'s and may have leading zeros but was bogus"
)
);
}
var e = expectThrows(IllegalArgumentException.class, () -> JarHell.checkJarHell(jars, logger::debug));
assertThat(e.getMessage(), equalTo("Invalid version string: 'bogus'"));
}

public void testRequiredJDKVersionIsOK() throws Exception {
Expand All @@ -184,24 +174,10 @@ public void testRequiredJDKVersionIsOK() throws Exception {
JarHell.checkJarHell(jars, logger::debug);
}

public void testValidVersions() {
String[] versions = new String[] { "1.7", "1.7.0", "0.1.7", "1.7.0.80" };
for (String version : versions) {
try {
JarHell.checkVersionFormat(version);
} catch (IllegalStateException e) {
fail(version + " should be accepted as a valid version format");
}
}
}

public void testInvalidVersions() {
String[] versions = new String[] { "", "1.7.0_80", "1.7." };
for (String version : versions) {
try {
JarHell.checkVersionFormat(version);
fail("\"" + version + "\"" + " should be rejected as an invalid version format");
} catch (IllegalStateException e) {}
expectThrows(IllegalArgumentException.class, () -> JarHell.checkJavaVersion("foo", version));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

package org.elasticsearch.common.ssl;

import org.elasticsearch.jdk.JavaVersion;

import java.nio.file.Path;
import java.security.KeyStore;
import java.util.Arrays;
Expand Down Expand Up @@ -66,48 +64,6 @@ public abstract class SslConfigurationLoader {
: Arrays.asList("TLSv1.2", "TLSv1.1")
);

private static final List<String> JDK11_CIPHERS = List.of(
// TLSv1.3 cipher has PFS, AEAD, hardware support
"TLS_AES_256_GCM_SHA384",
"TLS_AES_128_GCM_SHA256",

// PFS, AEAD, hardware support
"TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384",
"TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256",

// PFS, AEAD, hardware support
"TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384",
"TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256",

// PFS, hardware support
"TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384",
"TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256",

// PFS, hardware support
"TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384",
"TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256",

// PFS, hardware support
"TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA",
"TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA",

// PFS, hardware support
"TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA",
"TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA",

// AEAD, hardware support
"TLS_RSA_WITH_AES_256_GCM_SHA384",
"TLS_RSA_WITH_AES_128_GCM_SHA256",

// hardware support
"TLS_RSA_WITH_AES_256_CBC_SHA256",
"TLS_RSA_WITH_AES_128_CBC_SHA256",

// hardware support
"TLS_RSA_WITH_AES_256_CBC_SHA",
"TLS_RSA_WITH_AES_128_CBC_SHA"
);

private static final List<String> JDK12_CIPHERS = List.of(
// TLSv1.3 cipher has PFS, AEAD, hardware support
"TLS_AES_256_GCM_SHA384",
Expand Down Expand Up @@ -157,9 +113,7 @@ public abstract class SslConfigurationLoader {
"TLS_RSA_WITH_AES_128_CBC_SHA"
);

static final List<String> DEFAULT_CIPHERS = JavaVersion.current().compareTo(JavaVersion.parse("12")) > -1
? JDK12_CIPHERS
: JDK11_CIPHERS;
static final List<String> DEFAULT_CIPHERS = JDK12_CIPHERS;
private static final char[] EMPTY_PASSWORD = new char[0];

private final String settingPrefix;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.elasticsearch.common.settings.MockSecureSettings;
import org.elasticsearch.common.settings.SecureString;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.jdk.JavaVersion;
import org.elasticsearch.test.ESTestCase;

import java.nio.file.Path;
Expand All @@ -27,7 +26,6 @@
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;

public class SslConfigurationLoaderTests extends ESTestCase {
Expand Down Expand Up @@ -223,16 +221,8 @@ public void testLoadKeysFromJKS() {
}

public void testChaCha20InCiphersOnJdk12Plus() {
assumeTrue("Test is only valid on JDK 12+ JVM", JavaVersion.current().compareTo(JavaVersion.parse("12")) > -1);
assertThat(SslConfigurationLoader.DEFAULT_CIPHERS, hasItem("TLS_CHACHA20_POLY1305_SHA256"));
assertThat(SslConfigurationLoader.DEFAULT_CIPHERS, hasItem("TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256"));
assertThat(SslConfigurationLoader.DEFAULT_CIPHERS, hasItem("TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256"));
}

public void testChaCha20NotInCiphersOnPreJdk12() {
assumeTrue("Test is only valid on pre JDK 12 JVM", JavaVersion.current().compareTo(JavaVersion.parse("12")) < 0);
assertThat(SslConfigurationLoader.DEFAULT_CIPHERS, not(hasItem("TLS_CHACHA20_POLY1305_SHA256")));
assertThat(SslConfigurationLoader.DEFAULT_CIPHERS, not(hasItem("TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256")));
assertThat(SslConfigurationLoader.DEFAULT_CIPHERS, not(hasItem("TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
package org.elasticsearch.painless;

import org.apache.lucene.util.Constants;
import org.elasticsearch.jdk.JavaVersion;
import org.hamcrest.Matcher;

import java.lang.invoke.MethodHandle;
Expand All @@ -31,11 +30,7 @@ protected String valueCtorCall(String valueType, int size) {

@Override
protected Matcher<String> outOfBoundsExceptionMessageMatcher(int index, int size) {
if (JavaVersion.current().compareTo(JavaVersion.parse("11")) < 0) {
return equalTo(Integer.toString(index));
} else {
return equalTo("Index " + Integer.toString(index) + " out of bounds for length " + Integer.toString(size));
}
return equalTo("Index " + Integer.toString(index) + " out of bounds for length " + Integer.toString(size));
}

public void testArrayLengthHelper() throws Throwable {
Expand Down

0 comments on commit 0832232

Please sign in to comment.