77package org .elasticsearch .test ;
88
99import org .apache .http .HttpHost ;
10+ import org .elasticsearch .ResourceAlreadyExistsException ;
1011import org .elasticsearch .action .admin .cluster .node .info .NodeInfo ;
1112import org .elasticsearch .action .admin .cluster .node .info .NodesInfoResponse ;
1213import org .elasticsearch .action .admin .cluster .node .info .PluginsAndModules ;
14+ import org .elasticsearch .action .admin .indices .create .CreateIndexRequest ;
15+ import org .elasticsearch .action .admin .indices .delete .DeleteIndexRequest ;
16+ import org .elasticsearch .action .admin .indices .get .GetIndexRequest ;
17+ import org .elasticsearch .action .admin .indices .get .GetIndexResponse ;
18+ import org .elasticsearch .action .support .ActiveShardCount ;
19+ import org .elasticsearch .action .support .IndicesOptions ;
1320import org .elasticsearch .client .RequestOptions ;
1421import org .elasticsearch .client .RestClient ;
1522import org .elasticsearch .client .RestClientBuilder ;
2734import org .elasticsearch .license .LicenseSettings ;
2835import org .elasticsearch .plugins .Plugin ;
2936import org .elasticsearch .xpack .core .security .authc .support .Hasher ;
37+ import org .elasticsearch .xpack .core .security .authc .support .UsernamePasswordToken ;
3038import org .elasticsearch .xpack .core .security .test .TestRestrictedIndices ;
3139import org .elasticsearch .xpack .security .LocalStateSecurity ;
3240import org .elasticsearch .xpack .security .support .SecurityMigrations ;
4553import java .util .stream .Collectors ;
4654
4755import static org .elasticsearch .test .SecuritySettingsSourceField .TEST_PASSWORD_SECURE_STRING ;
56+ import static org .elasticsearch .test .hamcrest .ElasticsearchAssertions .assertAcked ;
4857import static org .elasticsearch .xpack .core .security .authc .support .UsernamePasswordToken .basicAuthHeaderValue ;
4958import static org .elasticsearch .xpack .security .support .SecurityIndexManager .getMigrationVersionFromIndexMetadata ;
59+ import static org .elasticsearch .xpack .security .support .SecuritySystemIndices .SECURITY_MAIN_ALIAS ;
5060import static org .hamcrest .Matchers .hasItem ;
61+ import static org .hamcrest .Matchers .is ;
5162
5263/**
5364 * A test that starts a single node with security enabled. This test case allows for customization
@@ -82,6 +93,13 @@ public static void destroyDefaultSettings() {
8293 tearDownRestClient ();
8394 }
8495
96+ @ Override
97+ public void setUp () throws Exception {
98+ super .setUp ();
99+ deleteSecurityIndexIfExists ();
100+ createSecurityIndexWithWaitForActiveShards ();
101+ }
102+
85103 @ Override
86104 public void tearDown () throws Exception {
87105 awaitSecurityMigration ();
@@ -100,7 +118,7 @@ private boolean isMigrationComplete(ClusterState state) {
100118 return getMigrationVersionFromIndexMetadata (indexMetadata ) == SecurityMigrations .MIGRATIONS_BY_VERSION .lastKey ();
101119 }
102120
103- private void awaitSecurityMigration () {
121+ protected void awaitSecurityMigration () {
104122 final var latch = new CountDownLatch (1 );
105123 ClusterService clusterService = getInstanceFromNode (ClusterService .class );
106124 clusterService .addListener ((event ) -> {
@@ -362,4 +380,40 @@ private static RestClient createRestClient(
362380 }
363381 return builder .build ();
364382 }
383+
384+ protected void deleteSecurityIndexIfExists () {
385+ // delete the security index, if it exist
386+ GetIndexRequest getIndexRequest = new GetIndexRequest (TEST_REQUEST_TIMEOUT );
387+ getIndexRequest .indices (SECURITY_MAIN_ALIAS );
388+ getIndexRequest .indicesOptions (IndicesOptions .lenientExpandOpen ());
389+ GetIndexResponse getIndexResponse = client ().admin ().indices ().getIndex (getIndexRequest ).actionGet ();
390+ if (getIndexResponse .getIndices ().length > 0 ) {
391+ assertThat (getIndexResponse .getIndices ().length , is (1 ));
392+ assertThat (getIndexResponse .getIndices ()[0 ], is (TestRestrictedIndices .INTERNAL_SECURITY_MAIN_INDEX_7 ));
393+
394+ // Security migration needs to finish before deleting the index
395+ awaitSecurityMigration ();
396+ DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest (getIndexResponse .getIndices ());
397+ assertAcked (client ().admin ().indices ().delete (deleteIndexRequest ).actionGet ());
398+ }
399+ }
400+
401+ protected void createSecurityIndexWithWaitForActiveShards () {
402+ final Client client = client ().filterWithHeader (
403+ Collections .singletonMap (
404+ "Authorization" ,
405+ UsernamePasswordToken .basicAuthHeaderValue (
406+ SecuritySettingsSource .ES_TEST_ROOT_USER ,
407+ SecuritySettingsSourceField .TEST_PASSWORD_SECURE_STRING
408+ )
409+ )
410+ );
411+ CreateIndexRequest createIndexRequest = new CreateIndexRequest (SECURITY_MAIN_ALIAS ).waitForActiveShards (ActiveShardCount .ALL )
412+ .masterNodeTimeout (TEST_REQUEST_TIMEOUT );
413+ try {
414+ client .admin ().indices ().create (createIndexRequest ).actionGet ();
415+ } catch (ResourceAlreadyExistsException e ) {
416+ logger .info ("Security index already exists, ignoring." , e );
417+ }
418+ }
365419}
0 commit comments