Skip to content

Commit 5aeee79

Browse files
authored
Fix assertSecurityIndexActive to actually wait (#112924) (#113372)
One of the test util assertion methods `assertSecurityIndexActive` currently completes successfully if the security index does not exist or if it's unavailable, which is not what the method name implies. This PR fixes the `assertBusy` call to properly wait for the security index.
1 parent c3a2b19 commit 5aeee79

File tree

7 files changed

+21
-18
lines changed

7 files changed

+21
-18
lines changed

x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/test/NativeRealmIntegTestCase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public abstract class NativeRealmIntegTestCase extends SecurityIntegTestCase {
3636

3737
@Before
3838
public void ensureNativeStoresStarted() throws Exception {
39-
assertSecurityIndexActive();
39+
createSecurityIndexWithWaitForActiveShards();
4040
if (shouldSetReservedUserPasswords()) {
4141
setupReservedPasswords();
4242
}

x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authc/ApiKeyIntegTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ protected boolean addMockHttpTransport() {
191191

192192
@Before
193193
public void waitForSecurityIndexWritable() throws Exception {
194-
assertSecurityIndexActive();
194+
createSecurityIndexWithWaitForActiveShards();
195195
}
196196

197197
@After

x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authc/TokenAuthIntegTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,7 @@ public void testAuthenticateWithWrongToken() throws Exception {
783783

784784
@Before
785785
public void waitForSecurityIndexWritable() throws Exception {
786-
assertSecurityIndexActive();
786+
createSecurityIndexWithWaitForActiveShards();
787787
}
788788

789789
@After

x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authz/SecurityScrollTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
public class SecurityScrollTests extends SecurityIntegTestCase {
3232

3333
public void testScrollIsPerUser() throws Exception {
34-
assertSecurityIndexActive();
34+
createSecurityIndexWithWaitForActiveShards();
3535
new PutRoleRequestBuilder(client()).name("scrollable")
3636
.addIndices(new String[] { randomAlphaOfLengthBetween(4, 12) }, new String[] { "read" }, null, null, null, randomBoolean())
3737
.get();

x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/support/SecurityIndexManagerIntegTests.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
public class SecurityIndexManagerIntegTests extends SecurityIntegTestCase {
4747

4848
public void testConcurrentOperationsTryingToCreateSecurityIndexAndAlias() throws Exception {
49-
assertSecurityIndexActive();
5049
final int processors = Runtime.getRuntime().availableProcessors();
5150
final int numThreads = Math.min(50, scaledRandomIntBetween((processors + 1) / 2, 4 * processors)); // up to 50 threads
5251
final int maxNumRequests = 50 / numThreads; // bound to a maximum of 50 requests
@@ -111,7 +110,7 @@ public void testOnIndexAvailableForSearchIndexCompletesWithinTimeout() throws Ex
111110
// pick longer wait than in the assertBusy that waits for below to ensure index has had enough time to initialize
112111
securityIndexManager.onIndexAvailableForSearch((ActionListener<Void>) future, TimeValue.timeValueSeconds(40));
113112

114-
createSecurityIndex();
113+
createSecurityIndexWithWaitForActiveShards();
115114

116115
assertBusy(
117116
() -> assertThat(securityIndexManager.isAvailable(SecurityIndexManager.Availability.SEARCH_SHARDS), is(true)),
@@ -126,7 +125,7 @@ public void testOnIndexAvailableForSearchIndexCompletesWithinTimeout() throws Ex
126125

127126
@SuppressWarnings("unchecked")
128127
public void testOnIndexAvailableForSearchIndexAlreadyAvailable() throws Exception {
129-
createSecurityIndex();
128+
createSecurityIndexWithWaitForActiveShards();
130129

131130
final SecurityIndexManager securityIndexManager = internalCluster().getInstances(NativePrivilegeStore.class)
132131
.iterator()

x-pack/plugin/security/src/test/java/org/elasticsearch/test/SecurityIntegTestCase.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
1515
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
1616
import org.elasticsearch.action.admin.indices.get.GetIndexResponse;
17+
import org.elasticsearch.action.support.ActiveShardCount;
1718
import org.elasticsearch.action.support.IndicesOptions;
1819
import org.elasticsearch.client.RequestOptions;
1920
import org.elasticsearch.client.internal.Client;
@@ -381,6 +382,12 @@ protected Function<Client, Client> getClientWrapper() {
381382
return client -> (client instanceof NodeClient) ? client.filterWithHeader(headers) : client;
382383
}
383384

385+
/**
386+
* Waits for security index to become available. Note that you must ensure index creation was triggered before calling this method,
387+
* by calling one of the resource creation APIs (e.g., creating a user).
388+
* If you use {@link #createSecurityIndexWithWaitForActiveShards()} to create the index it's not necessary to call
389+
* {@link #assertSecurityIndexActive} since the create method ensures the index is active.
390+
*/
384391
public void assertSecurityIndexActive() throws Exception {
385392
assertSecurityIndexActive(cluster());
386393
}
@@ -391,14 +398,10 @@ public void assertSecurityIndexActive(TestCluster testCluster) throws Exception
391398
ClusterState clusterState = client.admin().cluster().prepareState(TEST_REQUEST_TIMEOUT).setLocal(true).get().getState();
392399
assertFalse(clusterState.blocks().hasGlobalBlock(GatewayService.STATE_NOT_RECOVERED_BLOCK));
393400
Index securityIndex = resolveSecurityIndex(clusterState.metadata());
394-
// TODO this is a bug -- since we are not tripping assertions here, this will complete successfully even if the security
395-
// index does not exist
396-
if (securityIndex != null) {
397-
IndexRoutingTable indexRoutingTable = clusterState.routingTable().index(securityIndex);
398-
if (indexRoutingTable != null) {
399-
assertTrue(indexRoutingTable.allPrimaryShardsActive());
400-
}
401-
}
401+
assertNotNull(securityIndex);
402+
IndexRoutingTable indexRoutingTable = clusterState.routingTable().index(securityIndex);
403+
assertNotNull(indexRoutingTable);
404+
assertTrue(indexRoutingTable.allPrimaryShardsActive());
402405
}, 30L, TimeUnit.SECONDS);
403406
}
404407
}
@@ -424,7 +427,7 @@ protected void deleteSecurityIndex() {
424427
}
425428
}
426429

427-
protected void createSecurityIndex() {
430+
protected void createSecurityIndexWithWaitForActiveShards() {
428431
final Client client = client().filterWithHeader(
429432
Collections.singletonMap(
430433
"Authorization",
@@ -434,7 +437,8 @@ protected void createSecurityIndex() {
434437
)
435438
)
436439
);
437-
CreateIndexRequest createIndexRequest = new CreateIndexRequest(SECURITY_MAIN_ALIAS);
440+
CreateIndexRequest createIndexRequest = new CreateIndexRequest(SECURITY_MAIN_ALIAS).waitForActiveShards(ActiveShardCount.ALL)
441+
.masterNodeTimeout(TEST_REQUEST_TIMEOUT);
438442
client.admin().indices().create(createIndexRequest).actionGet();
439443
}
440444

x-pack/qa/third-party/active-directory/src/test/java/org/elasticsearch/xpack/security/authc/ldap/AbstractAdLdapRealmTestCase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ protected Settings buildRealmSettings(
181181

182182
@Before
183183
public void setupRoleMappings() throws Exception {
184-
assertSecurityIndexActive();
184+
createSecurityIndexWithWaitForActiveShards();
185185

186186
List<String> content = getRoleMappingContent(RoleMappingEntry::nativeContent);
187187
if (content.isEmpty()) {

0 commit comments

Comments
 (0)