1414
1515import org .apache .http .util .EntityUtils ;
1616import org .elasticsearch .client .Request ;
17+ import org .elasticsearch .client .RequestOptions ;
18+ import org .elasticsearch .client .Response ;
19+ import org .elasticsearch .client .WarningsHandler ;
20+ import org .elasticsearch .common .settings .Settings ;
21+ import org .elasticsearch .common .util .concurrent .ThreadContext ;
22+ import org .elasticsearch .core .Nullable ;
1723import org .elasticsearch .test .cluster .ElasticsearchCluster ;
1824import org .elasticsearch .test .cluster .FeatureFlag ;
1925import org .elasticsearch .test .cluster .local .distribution .DistributionType ;
2531import org .junit .rules .TestRule ;
2632
2733import java .io .IOException ;
34+ import java .nio .charset .StandardCharsets ;
35+ import java .util .ArrayList ;
36+ import java .util .Base64 ;
37+ import java .util .HashSet ;
2838import java .util .List ;
2939import java .util .Map ;
3040import java .util .Objects ;
3141import java .util .concurrent .TimeUnit ;
3242
33- import static org .hamcrest .Matchers .contains ;
43+ import static org .hamcrest .Matchers .is ;
3444
3545public class FullClusterRestartIT extends ParameterizedFullClusterRestartTestCase {
3646
3747 private static final boolean useFixture = Boolean .getBoolean ("geoip_use_service" ) == false ;
3848
39- private static GeoIpHttpFixture fixture = new GeoIpHttpFixture (useFixture );
49+ private static final GeoIpHttpFixture fixture = new GeoIpHttpFixture (useFixture );
4050
41- private static ElasticsearchCluster cluster = ElasticsearchCluster .local ()
51+ // e.g. use ./gradlew -Dtests.jvm.argline="-Dgeoip_test_with_security=false" ":modules:ingest-geoip:qa:full-cluster-restart:check"
52+ // to set this to false, if you so desire
53+ private static final boolean useSecurity = Boolean .parseBoolean (System .getProperty ("geoip_test_with_security" , "true" ));
54+
55+ private static final ElasticsearchCluster cluster = ElasticsearchCluster .local ()
4256 .distribution (DistributionType .DEFAULT )
4357 .version (getOldClusterTestVersion ())
4458 .nodes (2 )
4559 .setting ("ingest.geoip.downloader.endpoint" , () -> fixture .getAddress (), s -> useFixture )
46- .setting ("xpack.security.enabled" , "false" )
60+ .setting ("xpack.security.enabled" , useSecurity ? "true" : "false" )
4761 .feature (FeatureFlag .TIME_SERIES_MODE )
4862 .build ();
4963
64+ @ Override
65+ protected Settings restClientSettings () {
66+ Settings settings = super .restClientSettings ();
67+ if (useSecurity ) {
68+ String token = "Basic " + Base64 .getEncoder ().encodeToString ("test_user:x-pack-test-password" .getBytes (StandardCharsets .UTF_8 ));
69+ settings = Settings .builder ().put (settings ).put (ThreadContext .PREFIX + ".Authorization" , token ).build ();
70+ }
71+ return settings ;
72+ }
73+
5074 @ ClassRule
5175 public static TestRule ruleChain = RuleChain .outerRule (fixture ).around (cluster );
5276
@@ -60,6 +84,9 @@ protected ElasticsearchCluster getUpgradeCluster() {
6084 }
6185
6286 public void testGeoIpSystemFeaturesMigration () throws Exception {
87+ final List <String > maybeSecurityIndex = useSecurity ? List .of (".security-7" ) : List .of ();
88+ final List <String > maybeSecurityIndexReindexed = useSecurity ? List .of (".security-7-reindexed-for-10" ) : List .of ();
89+
6390 if (isRunningAgainstOldCluster ()) {
6491 Request enableDownloader = new Request ("PUT" , "/_cluster/settings" );
6592 enableDownloader .setJsonEntity ("""
@@ -86,23 +113,36 @@ public void testGeoIpSystemFeaturesMigration() throws Exception {
86113 assertBusy (() -> testDatabasesLoaded (), 30 , TimeUnit .SECONDS );
87114
88115 // the geoip index should be created
89- assertBusy (() -> testCatIndices (".geoip_databases" ));
116+ assertBusy (() -> testCatIndices (List . of ( ".geoip_databases" ), maybeSecurityIndex ));
90117 assertBusy (() -> testIndexGeoDoc ());
118+
119+ // before the upgrade, Kibana should work
120+ assertBusy (() -> testGetStarAsKibana (List .of ("my-index-00001" ), maybeSecurityIndex ));
91121 } else {
122+ // after the upgrade, but before the migration, Kibana should work
123+ assertBusy (() -> testGetStarAsKibana (List .of ("my-index-00001" ), maybeSecurityIndex ));
124+
125+ // migrate the system features and give the cluster a moment to settle
92126 Request migrateSystemFeatures = new Request ("POST" , "/_migration/system_features" );
93127 assertOK (client ().performRequest (migrateSystemFeatures ));
128+ ensureHealth (request -> request .addParameter ("wait_for_status" , "yellow" ));
94129
95- assertBusy (() -> testCatIndices (".geoip_databases-reindexed-for-10" , "my-index-00001" ));
130+ assertBusy (() -> testCatIndices (List . of ( ".geoip_databases-reindexed-for-10" , "my-index-00001" ), maybeSecurityIndexReindexed ));
96131 assertBusy (() -> testIndexGeoDoc ());
97132
133+ // after the migration, Kibana should work
134+ if (useSecurity == false ) { // BUT IT DOESN'T if security is enabled
135+ assertBusy (() -> testGetStarAsKibana (List .of ("my-index-00001" ), maybeSecurityIndexReindexed ));
136+ }
137+
98138 Request disableDownloader = new Request ("PUT" , "/_cluster/settings" );
99139 disableDownloader .setJsonEntity ("""
100140 {"persistent": {"ingest.geoip.downloader.enabled": false}}
101141 """ );
102142 assertOK (client ().performRequest (disableDownloader ));
103143
104144 // the geoip index should be deleted
105- assertBusy (() -> testCatIndices ("my-index-00001" ));
145+ assertBusy (() -> testCatIndices (List . of ( "my-index-00001" ), maybeSecurityIndexReindexed ));
106146
107147 Request enableDownloader = new Request ("PUT" , "/_cluster/settings" );
108148 enableDownloader .setJsonEntity ("""
@@ -114,7 +154,7 @@ public void testGeoIpSystemFeaturesMigration() throws Exception {
114154 assertBusy (() -> testDatabasesLoaded (), 30 , TimeUnit .SECONDS );
115155
116156 // the geoip index should be recreated
117- assertBusy (() -> testCatIndices (".geoip_databases" , "my-index-00001" ));
157+ assertBusy (() -> testCatIndices (List . of ( ".geoip_databases" , "my-index-00001" ), maybeSecurityIndexReindexed ));
118158 assertBusy (() -> testIndexGeoDoc ());
119159 }
120160 }
@@ -146,11 +186,17 @@ private void testDatabasesLoaded() throws IOException {
146186 }
147187 }
148188
149- private void testCatIndices (String ... indexNames ) throws IOException {
189+ private void testCatIndices (List < String > indexNames , @ Nullable List < String > additionalIndexNames ) throws IOException {
150190 Request catIndices = new Request ("GET" , "_cat/indices/*?s=index&h=index&expand_wildcards=all" );
151191 String response = EntityUtils .toString (client ().performRequest (catIndices ).getEntity ());
152192 List <String > indices = List .of (response .trim ().split ("\\ s+" ));
153- assertThat (indices , contains (indexNames ));
193+
194+ if (additionalIndexNames != null && additionalIndexNames .isEmpty () == false ) {
195+ indexNames = new ArrayList <>(indexNames ); // recopy into a mutable list
196+ indexNames .addAll (additionalIndexNames );
197+ }
198+
199+ assertThat (new HashSet <>(indices ), is (new HashSet <>(indexNames )));
154200 }
155201
156202 private void testIndexGeoDoc () throws IOException {
@@ -165,4 +211,23 @@ private void testIndexGeoDoc() throws IOException {
165211 assertNull (doc .evaluate ("_source.tags" ));
166212 assertEquals ("Sweden" , doc .evaluate ("_source.geo.country_name" ));
167213 }
214+
215+ private void testGetStarAsKibana (List <String > indexNames , @ Nullable List <String > additionalIndexNames ) throws IOException {
216+ Request getStar = new Request ("GET" , "*?expand_wildcards=all" );
217+ getStar .setOptions (
218+ RequestOptions .DEFAULT .toBuilder ()
219+ .addHeader ("X-elastic-product-origin" , "kibana" )
220+ .setWarningsHandler (WarningsHandler .PERMISSIVE ) // we don't care about warnings, just errors
221+ );
222+ Response response = client ().performRequest (getStar );
223+ assertOK (response );
224+
225+ if (additionalIndexNames != null && additionalIndexNames .isEmpty () == false ) {
226+ indexNames = new ArrayList <>(indexNames ); // recopy into a mutable list
227+ indexNames .addAll (additionalIndexNames );
228+ }
229+
230+ Map <String , Object > map = responseAsMap (response );
231+ assertThat (map .keySet (), is (new HashSet <>(indexNames )));
232+ }
168233}
0 commit comments