From 29c64d83e1c1f8994069697a35fcfc823e10655f Mon Sep 17 00:00:00 2001 From: Colin Goodheart-Smithe Date: Tue, 11 Apr 2017 08:30:59 +0100 Subject: [PATCH] Removes version 2.x constants from Version (elastic/x-pack-elasticsearch#1022) Original commit: elastic/x-pack-elasticsearch@39ef909aec7a2145ad0c2c4c8d8fa5b94aee24aa --- .../support/IndexLifecycleManager.java | 24 ++++----- ...urityIndexBackwardsCompatibilityTests.java | 5 -- .../extensions/XPackExtensionInfoTests.java | 4 +- ...ingIndicesBackwardsCompatibilityTests.java | 23 +-------- .../exporter/MonitoringDocTests.java | 2 +- .../cluster/ClusterInfoResolverTests.java | 2 +- .../SecurityLifecycleServiceTests.java | 18 +++---- .../authc/esnative/ReservedRealmTests.java | 3 +- ...herIndicesBackwardsCompatibilityTests.java | 51 ++++++++++--------- 9 files changed, 53 insertions(+), 79 deletions(-) diff --git a/plugin/src/main/java/org/elasticsearch/xpack/security/support/IndexLifecycleManager.java b/plugin/src/main/java/org/elasticsearch/xpack/security/support/IndexLifecycleManager.java index f32ea3f6134..0398eca0700 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/security/support/IndexLifecycleManager.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/security/support/IndexLifecycleManager.java @@ -5,17 +5,6 @@ */ package org.elasticsearch.xpack.security.support; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; -import java.util.concurrent.CopyOnWriteArrayList; -import java.util.concurrent.atomic.AtomicBoolean; -import java.util.concurrent.atomic.AtomicReference; -import java.util.function.Predicate; -import java.util.regex.Pattern; - import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.message.ParameterizedMessage; import org.apache.logging.log4j.util.Supplier; @@ -46,6 +35,16 @@ import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.xpack.security.InternalClient; import org.elasticsearch.xpack.template.TemplateUtils; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicReference; +import java.util.function.Predicate; +import java.util.regex.Pattern; + import static org.elasticsearch.common.xcontent.XContentHelper.convertToMap; /** @@ -269,8 +268,7 @@ public class IndexLifecycleManager extends AbstractComponent { Map meta = (Map) mappingMetaData.sourceAsMap().get("_meta"); if (meta == null) { - // something pre-5.0, but we don't know what. Use 2.3.0 as a placeholder for "old" - return Version.V_2_3_0; + throw new IllegalStateException("Cannot read security-version string"); } return Version.fromString((String) meta.get(SECURITY_VERSION_STRING)); } catch (IOException e) { diff --git a/plugin/src/test/java/org/elasticsearch/OldSecurityIndexBackwardsCompatibilityTests.java b/plugin/src/test/java/org/elasticsearch/OldSecurityIndexBackwardsCompatibilityTests.java index 3a6ea2a7ccf..6e2362bde80 100644 --- a/plugin/src/test/java/org/elasticsearch/OldSecurityIndexBackwardsCompatibilityTests.java +++ b/plugin/src/test/java/org/elasticsearch/OldSecurityIndexBackwardsCompatibilityTests.java @@ -70,11 +70,6 @@ import static org.hamcrest.Matchers.hasSize; **/ public class OldSecurityIndexBackwardsCompatibilityTests extends AbstractOldXPackIndicesBackwardsCompatibilityTestCase { - @Override - protected boolean shouldTestVersion(Version version) { - return version.onOrAfter(Version.V_2_3_0); // native realm only supported from 2.3.0 on - } - protected void checkVersion(Version version) throws Exception { // wait for service to start SecurityClient securityClient = new SecurityClient(client()); diff --git a/plugin/src/test/java/org/elasticsearch/xpack/extensions/XPackExtensionInfoTests.java b/plugin/src/test/java/org/elasticsearch/xpack/extensions/XPackExtensionInfoTests.java index 8851e0787d7..6894ad853f1 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/extensions/XPackExtensionInfoTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/extensions/XPackExtensionInfoTests.java @@ -138,11 +138,11 @@ public class XPackExtensionInfoTests extends ESTestCase { "description", "fake desc", "name", "my_extension", "version", "1.0", - "xpack.version", Version.V_2_0_0.toString()); + "xpack.version", Version.V_5_0_0.toString()); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> { XPackExtensionInfo.readFromProperties(extensionDir); }); - assertTrue(e.getMessage().contains("Was designed for version [2.0.0]")); + assertTrue(e.getMessage().contains("Was designed for version [5.0.0]")); } public void testReadFromPropertiesJvmMissingClassname() throws Exception { diff --git a/plugin/src/test/java/org/elasticsearch/xpack/monitoring/OldMonitoringIndicesBackwardsCompatibilityTests.java b/plugin/src/test/java/org/elasticsearch/xpack/monitoring/OldMonitoringIndicesBackwardsCompatibilityTests.java index facf8ecc70f..25002f80b27 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/monitoring/OldMonitoringIndicesBackwardsCompatibilityTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/monitoring/OldMonitoringIndicesBackwardsCompatibilityTests.java @@ -6,6 +6,7 @@ package org.elasticsearch.xpack.monitoring; import com.carrotsearch.randomizedtesting.annotations.ThreadLeakLingering; + import org.apache.lucene.util.LuceneTestCase; import org.elasticsearch.AbstractOldXPackIndicesBackwardsCompatibilityTestCase; import org.elasticsearch.Version; @@ -42,7 +43,6 @@ import java.util.concurrent.TimeUnit; import static org.elasticsearch.common.unit.TimeValue.timeValueSeconds; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; -import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount; import static org.hamcrest.Matchers.allOf; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.greaterThan; @@ -127,22 +127,6 @@ public class OldMonitoringIndicesBackwardsCompatibilityTests extends AbstractOld assertThat(clusterHealth.getIndices().get(expectedIndex).getActivePrimaryShards(), equalTo(numShards.numPrimaries)); }); - if (version.before(Version.V_2_3_0)) { - /* We can't do anything with indexes created before 2.3 so we just assert that we didn't delete them or do - anything otherwise crazy. */ - SearchResponse response = client().prepareSearch(".marvel-es-data").get(); - // 2.0.x didn't index the nodes info - long expectedEsData = version.before(Version.V_2_1_0) ? 1 : 2; - assertHitCount(response, expectedEsData); - response = client().prepareSearch(".marvel-es-*").get(); - assertThat(response.getHits().getTotalHits(), greaterThanOrEqualTo(20L)); - return; - } - - /* Indexes created from 2.3 onwards get aliased to the place they'd be if they were created in 5.0 so queries should just work. - * Monitoring doesn't really have a Java API so we can't test that, but we can test that we write the data we expected to - * write. */ - SearchResponse firstIndexStats = search(indexPattern, IndexStatsMonitoringDoc.TYPE, greaterThanOrEqualTo(10L)); @@ -295,9 +279,6 @@ public class OldMonitoringIndicesBackwardsCompatibilityTests extends AbstractOld } private void checkSourceNode(final Version version, Map element) { - if (version.onOrAfter(Version.V_2_3_0)) { - // The source_node field has been added in v2.3.0 - assertThat(element, hasKey("source_node")); - } + assertThat(element, hasKey("source_node")); } } diff --git a/plugin/src/test/java/org/elasticsearch/xpack/monitoring/exporter/MonitoringDocTests.java b/plugin/src/test/java/org/elasticsearch/xpack/monitoring/exporter/MonitoringDocTests.java index 2970a6f904d..449cdb1f926 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/monitoring/exporter/MonitoringDocTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/monitoring/exporter/MonitoringDocTests.java @@ -25,7 +25,7 @@ public class MonitoringDocTests extends ESTestCase { String nodeId = randomAlphaOfLength(5); TransportAddress address = buildNewFakeTransportAddress(); - Version version = randomFrom(Version.V_2_4_1, Version.V_5_0_1, Version.CURRENT); + Version version = randomFrom(Version.V_5_0_1, Version.V_5_3_0_UNRELEASED, Version.CURRENT); String name = randomBoolean() ? randomAlphaOfLength(5) : ""; Map attributes = new HashMap<>(); diff --git a/plugin/src/test/java/org/elasticsearch/xpack/monitoring/resolver/cluster/ClusterInfoResolverTests.java b/plugin/src/test/java/org/elasticsearch/xpack/monitoring/resolver/cluster/ClusterInfoResolverTests.java index da159dac731..3c424600afd 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/monitoring/resolver/cluster/ClusterInfoResolverTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/monitoring/resolver/cluster/ClusterInfoResolverTests.java @@ -47,7 +47,7 @@ public class ClusterInfoResolverTests extends MonitoringIndexNameResolverTestCas Math.abs(randomLong()), new DiscoveryNode("id", buildNewFakeTransportAddress(), emptyMap(), emptySet(), Version.CURRENT), randomAlphaOfLength(5), - randomFrom(Version.V_2_0_0, Version.CURRENT).toString(), + randomFrom(Version.V_5_0_0, Version.CURRENT).toString(), licenseBuilder.build(), Collections.singletonList(new MonitoringFeatureSet.Usage(randomBoolean(), randomBoolean(), emptyMap())), new ClusterStatsResponse( diff --git a/plugin/src/test/java/org/elasticsearch/xpack/security/SecurityLifecycleServiceTests.java b/plugin/src/test/java/org/elasticsearch/xpack/security/SecurityLifecycleServiceTests.java index 8fcfb5d9064..bfe0ef36267 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/security/SecurityLifecycleServiceTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/security/SecurityLifecycleServiceTests.java @@ -5,11 +5,6 @@ */ package org.elasticsearch.xpack.security; -import java.io.IOException; -import java.util.Map; -import java.util.concurrent.CopyOnWriteArrayList; -import java.util.concurrent.atomic.AtomicReference; - import org.elasticsearch.ElasticsearchException; import org.elasticsearch.Version; import org.elasticsearch.action.Action; @@ -49,6 +44,11 @@ import org.junit.After; import org.junit.Before; import org.mockito.Mockito; +import java.io.IOException; +import java.util.Map; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.atomic.AtomicReference; + import static org.elasticsearch.xpack.security.SecurityLifecycleService.SECURITY_INDEX_NAME; import static org.elasticsearch.xpack.security.SecurityLifecycleService.SECURITY_TEMPLATE_NAME; import static org.elasticsearch.xpack.security.SecurityLifecycleService.securityIndexMappingAndTemplateSufficientToRead; @@ -312,13 +312,13 @@ public class SecurityLifecycleServiceTests extends ESTestCase { assertFalse(securityIndex.checkMappingVersion(Version.V_5_0_0::after)); } - public void testMissingVersionMappingIsIdentifiedAsNotUpToDate() throws IOException { + public void testMissingVersionMappingThrowsError() throws IOException { String templateString = "/missing-version-" + SECURITY_TEMPLATE_NAME + ".json"; ClusterState.Builder clusterStateBuilder = createClusterStateWithMapping(templateString); final ClusterState clusterState = clusterStateBuilder.build(); - assertFalse(securityIndexMappingAndTemplateUpToDate(clusterState, logger)); - assertFalse(securityIndexMappingAndTemplateSufficientToRead(clusterState, logger)); - checkMappingUpdateWorkCorrectly(clusterStateBuilder, Version.V_2_3_0); + IllegalStateException exception = expectThrows(IllegalStateException.class, + () -> securityIndexMappingAndTemplateUpToDate(clusterState, logger)); + assertEquals(exception.getMessage(), "Cannot read security-version string"); } public void testMissingIndexIsIdentifiedAsUpToDate() throws IOException { diff --git a/plugin/src/test/java/org/elasticsearch/xpack/security/authc/esnative/ReservedRealmTests.java b/plugin/src/test/java/org/elasticsearch/xpack/security/authc/esnative/ReservedRealmTests.java index 54a14e1a0f6..e4df6e1e91d 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/security/authc/esnative/ReservedRealmTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/security/authc/esnative/ReservedRealmTests.java @@ -12,6 +12,7 @@ import org.elasticsearch.action.support.PlainActionFuture; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; import org.elasticsearch.env.Environment; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.XPackSettings; import org.elasticsearch.xpack.security.SecurityLifecycleService; import org.elasticsearch.xpack.security.authc.esnative.NativeUsersStore.ReservedUserInfo; @@ -24,7 +25,6 @@ import org.elasticsearch.xpack.security.user.ElasticUser; import org.elasticsearch.xpack.security.user.KibanaUser; import org.elasticsearch.xpack.security.user.LogstashSystemUser; import org.elasticsearch.xpack.security.user.User; -import org.elasticsearch.test.ESTestCase; import org.junit.Before; import org.mockito.ArgumentCaptor; @@ -356,7 +356,6 @@ public class ReservedRealmTests extends ESTestCase { } private void verifyVersionPredicate(String principal, Predicate versionPredicate) { - assertThat(versionPredicate.test(Version.V_2_4_3), is(false)); assertThat(versionPredicate.test(Version.V_5_0_0_rc1), is(false)); switch (principal) { case LogstashSystemUser.NAME: diff --git a/plugin/src/test/java/org/elasticsearch/xpack/watcher/OldWatcherIndicesBackwardsCompatibilityTests.java b/plugin/src/test/java/org/elasticsearch/xpack/watcher/OldWatcherIndicesBackwardsCompatibilityTests.java index 2aed1312172..a338ded23f0 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/watcher/OldWatcherIndicesBackwardsCompatibilityTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/watcher/OldWatcherIndicesBackwardsCompatibilityTests.java @@ -94,31 +94,32 @@ public class OldWatcherIndicesBackwardsCompatibilityTests extends AbstractOldXPa indexPayload = (Map) actions.get("index_payload"); assertEquals(timeout, indexPayload.get("throttle_period_in_millis")); - if (version.onOrAfter(Version.V_2_3_0)) { - /* Fetch a watch with a funny timeout to verify loading fractional time values. This watch is only built in >= 2.3 because - * email attachments aren't supported before that. */ - bwcWatch = watcherClient.prepareGetWatch("bwc_funny_timeout").get(); - assertTrue(bwcWatch.isFound()); - assertNotNull(bwcWatch.getSource()); - source = bwcWatch.getSource().getAsMap(); - actions = (Map) source.get("actions"); - Map work = (Map) actions.get("work"); - Map email = (Map) work.get("email"); - Map attachments = (Map) email.get("attachments"); - Map attachment = (Map) attachments.get("test_report.pdf"); - Map http = (Map) attachment.get("http"); - Map request = (Map) http.get("request"); - assertEquals(timeout, request.get("read_timeout_millis")); - assertEquals("https", request.get("scheme")); - assertEquals("example.com", request.get("host")); - assertEquals("{{ctx.metadata.report_url}}", request.get("path")); - assertEquals(8443, request.get("port")); - Map auth = (Map) request.get("auth"); - Map basic = (Map) auth.get("basic"); - assertThat(basic, hasEntry("username", "Aladdin")); - // password doesn't come back because it is hidden - assertThat(basic, not(hasKey("password"))); - } + /* + * Fetch a watch with a funny timeout to verify loading fractional time + * values. This watch is only built in >= 2.3 because email attachments + * aren't supported before that. + */ + bwcWatch = watcherClient.prepareGetWatch("bwc_funny_timeout").get(); + assertTrue(bwcWatch.isFound()); + assertNotNull(bwcWatch.getSource()); + source = bwcWatch.getSource().getAsMap(); + actions = (Map) source.get("actions"); + Map work = (Map) actions.get("work"); + Map email = (Map) work.get("email"); + Map attachments = (Map) email.get("attachments"); + Map attachment = (Map) attachments.get("test_report.pdf"); + Map http = (Map) attachment.get("http"); + Map request = (Map) http.get("request"); + assertEquals(timeout, request.get("read_timeout_millis")); + assertEquals("https", request.get("scheme")); + assertEquals("example.com", request.get("host")); + assertEquals("{{ctx.metadata.report_url}}", request.get("path")); + assertEquals(8443, request.get("port")); + Map auth = (Map) request.get("auth"); + Map basic = (Map) auth.get("basic"); + assertThat(basic, hasEntry("username", "Aladdin")); + // password doesn't come back because it is hidden + assertThat(basic, not(hasKey("password"))); String watchHistoryPattern = version.onOrAfter(Version.V_5_0_0_alpha1) ? ".watcher-history*" : ".watch_history*"; SearchResponse history = client().prepareSearch(watchHistoryPattern).get();