浏览代码

Check stable plugin version at install and load time (#91780)

For a node to install a stable plugin, the node's Elasticsearch version must have the same major version as the Elasticsearch libraries against which the stable plugin was built. Additionally, a node will reject stable plugins that have been built with a future version of Elasticsearch.

* Add version checks for stable plugins
* Update docs/changelog/91780.yaml
William Brafford 2 年之前
父节点
当前提交
0367e07ef1

+ 5 - 0
docs/changelog/91780.yaml

@@ -0,0 +1,5 @@
+pr: 91780
+summary: Check stable plugin version at install and load time
+area: Infra/Plugins
+type: enhancement
+issues: []

+ 24 - 1
server/src/main/java/org/elasticsearch/plugins/PluginsUtils.java

@@ -77,7 +77,30 @@ public class PluginsUtils {
      * Verify the given plugin is compatible with the current Elasticsearch installation.
      */
     public static void verifyCompatibility(PluginDescriptor info) {
-        if (info.getElasticsearchVersion().equals(Version.CURRENT) == false) {
+        if (info.isStable()) {
+            if (info.getElasticsearchVersion().major != Version.CURRENT.major) {
+                throw new IllegalArgumentException(
+                    "Stable Plugin ["
+                        + info.getName()
+                        + "] was built for Elasticsearch major version "
+                        + info.getElasticsearchVersion().major
+                        + " but version "
+                        + Version.CURRENT
+                        + " is running"
+                );
+            }
+            if (info.getElasticsearchVersion().after(Version.CURRENT)) {
+                throw new IllegalArgumentException(
+                    "Stable Plugin ["
+                        + info.getName()
+                        + "] was built for Elasticsearch version "
+                        + info.getElasticsearchVersion()
+                        + " but earlier version "
+                        + Version.CURRENT
+                        + " is running"
+                );
+            }
+        } else if (info.getElasticsearchVersion().equals(Version.CURRENT) == false) {
             throw new IllegalArgumentException(
                 "Plugin ["
                     + info.getName()

+ 41 - 28
server/src/test/java/org/elasticsearch/plugins/PluginsUtilsTests.java

@@ -368,40 +368,35 @@ public class PluginsUtilsTests extends ESTestCase {
         assertThat(e.getCause().getMessage(), containsString("DummyClass1"));
     }
 
-    public void testIncompatibleElasticsearchVersion() throws Exception {
-        PluginDescriptor info = new PluginDescriptor(
-            "my_plugin",
-            "desc",
-            "1.0",
-            Version.fromId(6000099),
-            "1.8",
-            "FakePlugin",
-            null,
-            Collections.emptyList(),
-            false,
-            false,
-            false,
-            false
+    public void testStableEarlierElasticsearchVersion() throws Exception {
+        PluginDescriptor info = getPluginDescriptorForVersion(Version.fromId(Version.CURRENT.id + 1), "1.8", true);
+        IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> PluginsUtils.verifyCompatibility(info));
+        assertThat(
+            e.getMessage(),
+            containsString(
+                "was built for Elasticsearch version "
+                    + Version.fromId(Version.CURRENT.id + 1)
+                    + " but earlier version "
+                    + Version.CURRENT
+                    + " is running"
+            )
         );
+    }
+
+    public void testStableIncompatibleElasticsearchVersion() throws Exception {
+        PluginDescriptor info = getPluginDescriptorForVersion(Version.fromId(6000099), "1.8", true);
+        IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> PluginsUtils.verifyCompatibility(info));
+        assertThat(e.getMessage(), containsString("was built for Elasticsearch major version 6"));
+    }
+
+    public void testIncompatibleElasticsearchVersion() throws Exception {
+        PluginDescriptor info = getPluginDescriptorForVersion(Version.fromId(6000099), "1.8", false);
         IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> PluginsUtils.verifyCompatibility(info));
         assertThat(e.getMessage(), containsString("was built for Elasticsearch version 6.0.0"));
     }
 
     public void testIncompatibleJavaVersion() throws Exception {
-        PluginDescriptor info = new PluginDescriptor(
-            "my_plugin",
-            "desc",
-            "1.0",
-            Version.CURRENT,
-            "1000",
-            "FakePlugin",
-            null,
-            Collections.emptyList(),
-            false,
-            false,
-            false,
-            false
-        );
+        PluginDescriptor info = getPluginDescriptorForVersion(Version.CURRENT, "1000", false);
         IllegalStateException e = expectThrows(IllegalStateException.class, () -> PluginsUtils.verifyCompatibility(info));
         assertThat(e.getMessage(), containsString("my_plugin requires Java"));
     }
@@ -434,4 +429,22 @@ public class PluginsUtilsTests extends ESTestCase {
         assertThat(PluginsUtils.findPluginDirs(plugins), containsInAnyOrder(fake));
     }
 
+    private static PluginDescriptor getPluginDescriptorForVersion(Version id, String javaVersion, boolean isStable) {
+        PluginDescriptor info = new PluginDescriptor(
+            "my_plugin",
+            "desc",
+            "1.0",
+            id,
+            javaVersion,
+            "FakePlugin",
+            null,
+            Collections.emptyList(),
+            false,
+            false,
+            false,
+            isStable
+        );
+        return info;
+    }
+
 }