浏览代码

Prevent upgrades to 8.0 from 7.non-last (#82321)

* Prevent upgrades to 8.0 from 7.non-last

This PR introduces a check to prevent upgrading
to 8.0 without first upgrading to 7.last.

Closes #81865

Co-authored-by: David Turner <david.turner@elastic.co>
Nikola Grcevski 3 年之前
父节点
当前提交
979e17aa3b

+ 6 - 0
docs/changelog/82321.yaml

@@ -0,0 +1,6 @@
+pr: 82321
+summary: Prevent upgrades to 8.0 from 7.non-last
+area: Infra/Core
+type: enhancement
+issues:
+ - 81865

+ 10 - 1
server/src/internalClusterTest/java/org/elasticsearch/env/NodeEnvironmentIT.java

@@ -136,7 +136,16 @@ public class NodeEnvironmentIT extends ESIntegTestCase {
         );
         );
         assertThat(
         assertThat(
             illegalStateException.getMessage(),
             illegalStateException.getMessage(),
-            allOf(startsWith("cannot upgrade a node from version ["), endsWith("] directly to version [" + Version.CURRENT + "]"))
+            allOf(
+                startsWith("cannot upgrade a node from version ["),
+                endsWith(
+                    "] directly to version ["
+                        + Version.CURRENT
+                        + "], upgrade to version ["
+                        + Version.CURRENT.minimumCompatibilityVersion()
+                        + "] first."
+                )
+            )
         );
         );
     }
     }
 
 

+ 11 - 6
server/src/main/java/org/elasticsearch/env/NodeMetadata.java

@@ -92,14 +92,19 @@ public final class NodeMetadata {
     }
     }
 
 
     public NodeMetadata upgradeToCurrentVersion() {
     public NodeMetadata upgradeToCurrentVersion() {
-        if (nodeVersion.equals(Version.V_EMPTY)) {
-            assert Version.CURRENT.major <= Version.V_7_0_0.major + 1 : "version is required in the node metadata from v9 onwards";
-            return new NodeMetadata(nodeId, Version.CURRENT, Version.V_EMPTY);
-        }
+        assert (nodeVersion.equals(Version.V_EMPTY) == false) || (Version.CURRENT.major <= Version.V_7_0_0.major + 1)
+            : "version is required in the node metadata from v9 onwards";
 
 
-        if (nodeVersion.before(Version.CURRENT.minimumIndexCompatibilityVersion())) {
+        if (nodeVersion.before(Version.CURRENT.minimumCompatibilityVersion())) {
             throw new IllegalStateException(
             throw new IllegalStateException(
-                "cannot upgrade a node from version [" + nodeVersion + "] directly to version [" + Version.CURRENT + "]"
+                "cannot upgrade a node from version ["
+                    + nodeVersion
+                    + "] directly to version ["
+                    + Version.CURRENT
+                    + "], "
+                    + "upgrade to version ["
+                    + Version.CURRENT.minimumCompatibilityVersion()
+                    + "] first."
             );
             );
         }
         }
 
 

+ 23 - 7
server/src/test/java/org/elasticsearch/env/NodeMetadataTests.java

@@ -73,7 +73,7 @@ public class NodeMetadataTests extends ESTestCase {
         final NodeMetadata nodeMetadata = new NodeMetadata(
         final NodeMetadata nodeMetadata = new NodeMetadata(
             nodeId,
             nodeId,
             randomValueOtherThanMany(
             randomValueOtherThanMany(
-                v -> v.after(Version.CURRENT) || v.before(Version.CURRENT.minimumIndexCompatibilityVersion()),
+                v -> v.after(Version.CURRENT) || v.before(Version.CURRENT.minimumCompatibilityVersion()),
                 this::randomVersion
                 this::randomVersion
             )
             )
         ).upgradeToCurrentVersion();
         ).upgradeToCurrentVersion();
@@ -83,9 +83,15 @@ public class NodeMetadataTests extends ESTestCase {
 
 
     public void testUpgradesMissingVersion() {
     public void testUpgradesMissingVersion() {
         final String nodeId = randomAlphaOfLength(10);
         final String nodeId = randomAlphaOfLength(10);
-        final NodeMetadata nodeMetadata = new NodeMetadata(nodeId, Version.V_EMPTY).upgradeToCurrentVersion();
-        assertThat(nodeMetadata.nodeVersion(), equalTo(Version.CURRENT));
-        assertThat(nodeMetadata.nodeId(), equalTo(nodeId));
+
+        final IllegalStateException illegalStateException = expectThrows(
+            IllegalStateException.class,
+            () -> new NodeMetadata(nodeId, Version.V_EMPTY).upgradeToCurrentVersion()
+        );
+        assertThat(
+            illegalStateException.getMessage(),
+            startsWith("cannot upgrade a node from version [" + Version.V_EMPTY + "] directly to version [" + Version.CURRENT + "]")
+        );
     }
     }
 
 
     public void testDoesNotUpgradeFutureVersion() {
     public void testDoesNotUpgradeFutureVersion() {
@@ -106,13 +112,23 @@ public class NodeMetadataTests extends ESTestCase {
         );
         );
         assertThat(
         assertThat(
             illegalStateException.getMessage(),
             illegalStateException.getMessage(),
-            allOf(startsWith("cannot upgrade a node from version ["), endsWith("] directly to version [" + Version.CURRENT + "]"))
+            allOf(
+                startsWith("cannot upgrade a node from version ["),
+                endsWith(
+                    "] directly to version ["
+                        + Version.CURRENT
+                        + "], upgrade to version ["
+                        + Version.CURRENT.minimumCompatibilityVersion()
+                        + "] first."
+                )
+            )
         );
         );
     }
     }
 
 
     public void testUpgradeMarksPreviousVersion() {
     public void testUpgradeMarksPreviousVersion() {
         final String nodeId = randomAlphaOfLength(10);
         final String nodeId = randomAlphaOfLength(10);
-        final Version version = VersionUtils.randomVersionBetween(random(), Version.V_7_3_0, Version.V_7_16_0);
+        final Version version = VersionUtils.randomVersionBetween(random(), Version.CURRENT.minimumCompatibilityVersion(), Version.V_8_0_0);
+
         final NodeMetadata nodeMetadata = new NodeMetadata(nodeId, version).upgradeToCurrentVersion();
         final NodeMetadata nodeMetadata = new NodeMetadata(nodeId, version).upgradeToCurrentVersion();
         assertThat(nodeMetadata.nodeVersion(), equalTo(Version.CURRENT));
         assertThat(nodeMetadata.nodeVersion(), equalTo(Version.CURRENT));
         assertThat(nodeMetadata.previousNodeVersion(), equalTo(version));
         assertThat(nodeMetadata.previousNodeVersion(), equalTo(version));
@@ -123,6 +139,6 @@ public class NodeMetadataTests extends ESTestCase {
     }
     }
 
 
     public static Version tooOldVersion() {
     public static Version tooOldVersion() {
-        return Version.fromId(between(1, Version.CURRENT.minimumIndexCompatibilityVersion().id - 1));
+        return Version.fromId(between(1, Version.CURRENT.minimumCompatibilityVersion().id - 1));
     }
     }
 }
 }

+ 12 - 3
x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/SecurityImplicitBehaviorBootstrapCheckTests.java

@@ -26,7 +26,10 @@ import static org.hamcrest.Matchers.is;
 public class SecurityImplicitBehaviorBootstrapCheckTests extends AbstractBootstrapCheckTestCase {
 public class SecurityImplicitBehaviorBootstrapCheckTests extends AbstractBootstrapCheckTestCase {
 
 
     public void testFailureUpgradeFrom7xWithImplicitSecuritySettings() throws Exception {
     public void testFailureUpgradeFrom7xWithImplicitSecuritySettings() throws Exception {
-        final Version previousVersion = VersionUtils.randomVersionBetween(random(), Version.V_7_2_0, Version.V_7_16_0);
+        final Version previousVersion = randomValueOtherThan(
+            Version.V_8_0_0,
+            () -> VersionUtils.randomVersionBetween(random(), Version.CURRENT.minimumCompatibilityVersion(), Version.V_8_0_0)
+        );
         NodeMetadata nodeMetadata = new NodeMetadata(randomAlphaOfLength(10), previousVersion);
         NodeMetadata nodeMetadata = new NodeMetadata(randomAlphaOfLength(10), previousVersion);
         nodeMetadata = nodeMetadata.upgradeToCurrentVersion();
         nodeMetadata = nodeMetadata.upgradeToCurrentVersion();
         BootstrapCheck.BootstrapCheckResult result = new SecurityImplicitBehaviorBootstrapCheck(nodeMetadata).check(
         BootstrapCheck.BootstrapCheckResult result = new SecurityImplicitBehaviorBootstrapCheck(nodeMetadata).check(
@@ -52,7 +55,10 @@ public class SecurityImplicitBehaviorBootstrapCheckTests extends AbstractBootstr
     }
     }
 
 
     public void testUpgradeFrom7xWithImplicitSecuritySettingsOnGoldPlus() throws Exception {
     public void testUpgradeFrom7xWithImplicitSecuritySettingsOnGoldPlus() throws Exception {
-        final Version previousVersion = VersionUtils.randomVersionBetween(random(), Version.V_7_2_0, Version.V_7_16_0);
+        final Version previousVersion = randomValueOtherThan(
+            Version.V_8_0_0,
+            () -> VersionUtils.randomVersionBetween(random(), Version.CURRENT.minimumCompatibilityVersion(), Version.V_8_0_0)
+        );
         NodeMetadata nodeMetadata = new NodeMetadata(randomAlphaOfLength(10), previousVersion);
         NodeMetadata nodeMetadata = new NodeMetadata(randomAlphaOfLength(10), previousVersion);
         nodeMetadata = nodeMetadata.upgradeToCurrentVersion();
         nodeMetadata = nodeMetadata.upgradeToCurrentVersion();
         BootstrapCheck.BootstrapCheckResult result = new SecurityImplicitBehaviorBootstrapCheck(nodeMetadata).check(
         BootstrapCheck.BootstrapCheckResult result = new SecurityImplicitBehaviorBootstrapCheck(nodeMetadata).check(
@@ -62,7 +68,10 @@ public class SecurityImplicitBehaviorBootstrapCheckTests extends AbstractBootstr
     }
     }
 
 
     public void testUpgradeFrom7xWithExplicitSecuritySettings() throws Exception {
     public void testUpgradeFrom7xWithExplicitSecuritySettings() throws Exception {
-        final Version previousVersion = VersionUtils.randomVersionBetween(random(), Version.V_7_2_0, Version.V_7_16_0);
+        final Version previousVersion = randomValueOtherThan(
+            Version.V_8_0_0,
+            () -> VersionUtils.randomVersionBetween(random(), Version.CURRENT.minimumCompatibilityVersion(), Version.V_8_0_0)
+        );
         NodeMetadata nodeMetadata = new NodeMetadata(randomAlphaOfLength(10), previousVersion);
         NodeMetadata nodeMetadata = new NodeMetadata(randomAlphaOfLength(10), previousVersion);
         nodeMetadata = nodeMetadata.upgradeToCurrentVersion();
         nodeMetadata = nodeMetadata.upgradeToCurrentVersion();
         BootstrapCheck.BootstrapCheckResult result = new SecurityImplicitBehaviorBootstrapCheck(nodeMetadata).check(
         BootstrapCheck.BootstrapCheckResult result = new SecurityImplicitBehaviorBootstrapCheck(nodeMetadata).check(