-
Notifications
You must be signed in to change notification settings - Fork 25k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SQL: Handle uberjar scenario where the ES jdbc driver file is bundled in another jar #51856
Changes from 4 commits
a132138
77cb232
0035ed2
1423fda
2654304
aa653ef
d8f64b8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
|
||
import java.io.IOException; | ||
import java.net.URL; | ||
import java.net.URLConnection; | ||
import java.util.Collections; | ||
import java.util.Enumeration; | ||
import java.util.LinkedHashSet; | ||
|
@@ -86,26 +87,34 @@ static byte[] from(String ver) { | |
// This is similar to how Elasticsearch's Build class digs up its build information. | ||
// Since version info is not critical, the parsing is lenient | ||
URL url = Version.class.getProtectionDomain().getCodeSource().getLocation(); | ||
String urlStr = url.toString(); | ||
CURRENT = extractVersion(url); | ||
} | ||
|
||
static Version extractVersion(URL url) { | ||
String urlStr = url.toString(); | ||
byte maj = 0, min = 0, rev = 0; | ||
String ver = "Unknown"; | ||
String hash = ver; | ||
|
||
if (urlStr.endsWith(".jar")) { | ||
try (JarInputStream jar = new JarInputStream(url.openStream())) { | ||
Manifest manifest = jar.getManifest(); | ||
hash = manifest.getMainAttributes().getValue("Change"); | ||
ver = manifest.getMainAttributes().getValue("X-Compile-Elasticsearch-Version"); | ||
byte[] vers = from(ver); | ||
maj = vers[0]; | ||
min = vers[1]; | ||
rev = vers[2]; | ||
|
||
if (urlStr.endsWith(".jar") || urlStr.endsWith(".jar!/")) { | ||
try { | ||
URLConnection conn = url.openConnection(); | ||
conn.setUseCaches(false); | ||
|
||
try (JarInputStream jar = new JarInputStream(conn.getInputStream())) { | ||
Manifest manifest = jar.getManifest(); | ||
hash = manifest.getMainAttributes().getValue("Change"); | ||
ver = manifest.getMainAttributes().getValue("X-Compile-Elasticsearch-Version"); | ||
byte[] vers = from(ver); | ||
maj = vers[0]; | ||
min = vers[1]; | ||
rev = vers[2]; | ||
} | ||
} catch (Exception ex) { | ||
throw new IllegalArgumentException("Detected Elasticsearch JDBC jar but cannot retrieve its version", ex); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we add a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think it's needed to close the URLConnection (actually, the class doesn't have a method to close/terminate it), only its stream: https://docs.oracle.com/javase/tutorial/networking/urls/readingWriting.html |
||
} | ||
CURRENT = new Version(ver, hash, maj, min, rev); | ||
return new Version(ver, hash, maj, min, rev); | ||
} | ||
|
||
@Override | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,17 @@ | |
package org.elasticsearch.xpack.sql.client; | ||
|
||
import org.elasticsearch.test.ESTestCase; | ||
import org.elasticsearch.xpack.sql.client.Version; | ||
|
||
import java.io.BufferedInputStream; | ||
import java.io.IOException; | ||
import java.net.URL; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.StandardOpenOption; | ||
import java.util.jar.Attributes; | ||
import java.util.jar.JarEntry; | ||
import java.util.jar.JarOutputStream; | ||
import java.util.jar.Manifest; | ||
|
||
public class VersionTests extends ESTestCase { | ||
public void test70Version() { | ||
|
@@ -43,4 +53,44 @@ public void testInvalidVersion() { | |
IllegalArgumentException err = expectThrows(IllegalArgumentException.class, () -> Version.from("7.1")); | ||
assertEquals("Invalid version 7.1", err.getMessage()); | ||
} | ||
|
||
public void testVersionFromJarInJar() throws IOException { | ||
Path dir = createTempDir(); | ||
Path jarPath = dir.resolve("foo.jar"); // simulated uberjar containing the jdbc driver | ||
Path innerJarPath = dir.resolve("es-sql-jdbc.jar"); // simulated ES JDBC driver file | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given it's a test, this is completely optional: extract the |
||
|
||
Manifest jdbcJarManifest = new Manifest(); | ||
Attributes attributes = jdbcJarManifest.getMainAttributes(); | ||
attributes.put(Attributes.Name.MANIFEST_VERSION, "1.0.0"); | ||
attributes.put(new Attributes.Name("Change"), "abc"); | ||
attributes.put(new Attributes.Name("X-Compile-Elasticsearch-Version"), "1.2.3"); | ||
|
||
// create the jdbc driver file | ||
try (JarOutputStream jdbc = new JarOutputStream(Files.newOutputStream(innerJarPath, StandardOpenOption.CREATE), jdbcJarManifest)) {} | ||
|
||
// create the uberjar and embed the jdbc driver one into it | ||
try (BufferedInputStream in = new BufferedInputStream(Files.newInputStream(innerJarPath)); | ||
JarOutputStream out = new JarOutputStream(Files.newOutputStream(jarPath, StandardOpenOption.CREATE), new Manifest())) { | ||
JarEntry entry = new JarEntry("es-sql-jdbc.jar!/"); | ||
out.putNextEntry(entry); | ||
|
||
byte[] buffer = new byte[1024]; | ||
while (true) { | ||
int count = in.read(buffer); | ||
if (count == -1) { | ||
break; | ||
} | ||
out.write(buffer, 0, count); | ||
} | ||
} | ||
|
||
URL jarInJar = new URL("jar:" + jarPath.toUri().toURL().toString() + "!/es-sql-jdbc.jar!/"); | ||
|
||
Version version = Version.extractVersion(jarInJar); | ||
assertEquals(1, version.major); | ||
assertEquals(2, version.minor); | ||
assertEquals(3, version.revision); | ||
assertEquals("abc", version.hash); | ||
assertEquals("1.2.3", version.version); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know this is unmodified code, however, it's noteworthy that server/src/main/java/org/elasticsearch/Version.java parses ints, not bytes. Not sure if that's because it later does some int maths with the components, or because it reserves the possibility to use some tagging values at some point (like
7.2.222
for a special release). Just mentioning it, should we ever want to align this.