Skip to content

Commit

Permalink
Only check plugin version compatibility for semantically versioned bu…
Browse files Browse the repository at this point in the history
…ilds
  • Loading branch information
williamrandolph committed Oct 18, 2023
1 parent 6ae25de commit 34ecda5
Showing 1 changed file with 48 additions and 50 deletions.
98 changes: 48 additions & 50 deletions server/src/main/java/org/elasticsearch/plugins/PluginsUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Function;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -79,72 +80,69 @@ public static List<Path> findPluginDirs(final Path rootPath) throws IOException
* Verify the given plugin is compatible with the current Elasticsearch installation.
*/
public static void verifyCompatibility(PluginDescriptor info) {
boolean buildSupportsStablePlugins;
SemanticVersion currentElasticsearchVersion = null;
try {
currentElasticsearchVersion = SemanticVersion.create(Build.current().version());
buildSupportsStablePlugins = true;
} catch (IllegalArgumentException e) {
buildSupportsStablePlugins = false;
}
// stable plugins can run on the exact version they're built with even if it's not semantic
if (info.isStable() && buildSupportsStablePlugins) {
SemanticVersion pluginElasticsearchVersion;
try {
pluginElasticsearchVersion = SemanticVersion.create(info.getElasticsearchVersion());
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException(
"Stable Plugin ["
+ info.getName()
+ "] was built for non-semantic Elasticsearch version "
+ info.getElasticsearchVersion()
+ " and cannot run on version "
+ Build.current().version()
);
}
boolean hasSemanticVersion = SemanticVersion.semanticPattern.matcher(Build.current().version()).matches();
// If we're not on a semantic version, assume plugins are compatible
if (hasSemanticVersion) {
SemanticVersion currentElasticsearchVersion = SemanticVersion.create(Build.current().version());
if (info.isStable()) {
SemanticVersion pluginElasticsearchVersion;
try {
pluginElasticsearchVersion = SemanticVersion.create(info.getElasticsearchVersion());
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException(
"Stable Plugin ["
+ info.getName()
+ "] was built for non-semantic Elasticsearch version "
+ info.getElasticsearchVersion()
+ " and cannot run on version "
+ Build.current().version()
);
}

// case: Major version mismatch
if (pluginElasticsearchVersion.major != currentElasticsearchVersion.major) {
throw new IllegalArgumentException(
"Stable Plugin ["
+ info.getName()
+ "] was built for Elasticsearch major version "
+ pluginElasticsearchVersion.major
+ " but version "
+ Build.current().version()
+ " is running"
);
}
// case: Major version mismatch
if (pluginElasticsearchVersion.major != currentElasticsearchVersion.major) {
throw new IllegalArgumentException(
"Stable Plugin ["
+ info.getName()
+ "] was built for Elasticsearch major version "
+ pluginElasticsearchVersion.major
+ " but version "
+ Build.current().version()
+ " is running"
);
}

// case: stable plugin from the future
if (pluginElasticsearchVersion.after(currentElasticsearchVersion)) {
// case: stable plugin from the future
if (pluginElasticsearchVersion.after(currentElasticsearchVersion)) {
throw new IllegalArgumentException(
"Stable Plugin ["
+ info.getName()
+ "] was built for Elasticsearch version "
+ info.getElasticsearchVersion()
+ " but earlier version "
+ Build.current().version()
+ " is running"
);
}
} else if (info.getElasticsearchVersion().equals(currentElasticsearchVersion.toString()) == false) {
throw new IllegalArgumentException(
"Stable Plugin ["
"Plugin ["
+ info.getName()
+ "] was built for Elasticsearch version "
+ info.getElasticsearchVersion()
+ " but earlier version "
+ " but version "
+ Build.current().version()
+ " is running"
);
}
} else if (info.getElasticsearchVersion().equals(currentElasticsearchVersion.toString()) == false) {
throw new IllegalArgumentException(
"Plugin ["
+ info.getName()
+ "] was built for Elasticsearch version "
+ info.getElasticsearchVersion()
+ " but version "
+ Build.current().version()
+ " is running"
);
}
JarHell.checkJavaVersion(info.getName(), info.getJavaVersion());
}

private record SemanticVersion(int major, int minor, int bugfix, String suffix) {
static final Pattern semanticPattern = Pattern.compile("^(\\d+)\\.(\\d+)\\.(\\d+)(\\D?.*)$");
static SemanticVersion create(String version) {
Matcher matcher = Pattern.compile("^(\\d+)\\.(\\d+)\\.(\\d+)(\\D?.*)$").matcher(version);
Matcher matcher = semanticPattern.matcher(version);
if (matcher.matches() == false) {
throw new IllegalArgumentException("Cannot be parsed as a semantic version: " + version);
}
Expand Down

0 comments on commit 34ecda5

Please sign in to comment.