1010package org .elasticsearch .upgrades ;
1111
1212import com .carrotsearch .randomizedtesting .annotations .Name ;
13+
1314import org .elasticsearch .client .Request ;
1415import org .elasticsearch .core .SuppressForbidden ;
1516import org .elasticsearch .test .XContentTestUtils ;
@@ -90,20 +91,26 @@ public void checkVersions() {
9091 );
9192 }
9293
93- protected static void waitForSecurityMigrationCompletion () throws Exception {
94+ private static void waitForSecurityMigrationCompletionIfIndexExists () throws Exception {
9495 final Request request = new Request ("GET" , "_cluster/state/metadata/.security-7" );
9596 assertBusy (() -> {
9697 Map <String , Object > indices = new XContentTestUtils .JsonMapView (entityAsMap (client ().performRequest (request ))).get (
9798 "metadata.indices"
9899 );
99100 assertNotNull (indices );
100- // JsonMapView doesn't support . prefixed indices (splits on .)
101- @ SuppressWarnings ("unchecked" )
102- String responseVersion = new XContentTestUtils .JsonMapView ((Map <String , Object >) indices .get (".security-7" )).get (
103- "migration_version.version"
104- );
105- assertNotNull (responseVersion );
106- assertTrue (Integer .parseInt (responseVersion ) >= ROLE_MAPPINGS_CLEANUP_MIGRATION_VERSION );
101+ // If the security index exists, migration needs to happen. There is a bug in pre cluster state role mappings code that tries
102+ // to write file based role mappings before security index manager state is recovered, this makes it look like the security
103+ // index is outdated (isIndexUpToDate == false). Because we can't rely on the index being there for old versions, this check
104+ // is needed.
105+ if (indices .containsKey (".security-7" )) {
106+ // JsonMapView doesn't support . prefixed indices (splits on .)
107+ @ SuppressWarnings ("unchecked" )
108+ String responseVersion = new XContentTestUtils .JsonMapView ((Map <String , Object >) indices .get (".security-7" )).get (
109+ "migration_version.version"
110+ );
111+ assertNotNull (responseVersion );
112+ assertTrue (Integer .parseInt (responseVersion ) >= ROLE_MAPPINGS_CLEANUP_MIGRATION_VERSION );
113+ }
107114 });
108115 }
109116
@@ -123,7 +130,7 @@ public void testRoleMappingsAppliedOnUpgrade() throws Exception {
123130 ).get ("metadata.role_mappings.role_mappings" );
124131 assertThat (clusterStateRoleMappings , is (not (nullValue ())));
125132 assertThat (clusterStateRoleMappings .size (), equalTo (1 ));
126- waitForSecurityMigrationCompletion ();
133+ waitForSecurityMigrationCompletionIfIndexExists ();
127134 assertThat (
128135 entityAsMap (client ().performRequest (new Request ("GET" , "/_security/role_mapping" ))).keySet (),
129136 contains ("everyone_kibana-read-only-operator-mapping" )
0 commit comments