|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.elasticsearch.xpack.fleet; |
| 9 | + |
| 10 | +import org.apache.http.util.EntityUtils; |
| 11 | +import org.elasticsearch.client.Request; |
| 12 | +import org.elasticsearch.client.RequestOptions; |
| 13 | +import org.elasticsearch.client.Response; |
| 14 | +import org.elasticsearch.client.ResponseException; |
| 15 | +import org.elasticsearch.common.settings.Settings; |
| 16 | +import org.elasticsearch.common.util.concurrent.ThreadContext; |
| 17 | +import org.elasticsearch.test.SecuritySettingsSourceField; |
| 18 | +import org.elasticsearch.test.rest.ESRestTestCase; |
| 19 | + |
| 20 | +import java.util.Collections; |
| 21 | +import java.util.List; |
| 22 | + |
| 23 | +import static java.util.Collections.emptyList; |
| 24 | +import static java.util.Collections.singletonList; |
| 25 | +import static org.hamcrest.Matchers.allOf; |
| 26 | +import static org.hamcrest.Matchers.containsString; |
| 27 | +import static org.hamcrest.Matchers.is; |
| 28 | +import static org.hamcrest.Matchers.not; |
| 29 | + |
| 30 | +public class FleetDataStreamIT extends ESRestTestCase { |
| 31 | + |
| 32 | + static final String BASIC_AUTH_VALUE = basicAuthHeaderValue( |
| 33 | + "x_pack_rest_user", |
| 34 | + SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING |
| 35 | + ); |
| 36 | + |
| 37 | + @Override |
| 38 | + protected Settings restClientSettings() { |
| 39 | + // Note that we are superuser here but DO NOT provide a product origin |
| 40 | + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", BASIC_AUTH_VALUE).build(); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + protected Settings restAdminSettings() { |
| 45 | + // Note that we are both superuser here and provide a product origin |
| 46 | + return Settings.builder() |
| 47 | + .put(ThreadContext.PREFIX + ".Authorization", BASIC_AUTH_VALUE) |
| 48 | + .put(ThreadContext.PREFIX + ".X-elastic-product-origin", "fleet") |
| 49 | + .build(); |
| 50 | + } |
| 51 | + |
| 52 | + public void testAliasWithSystemDataStream() throws Exception { |
| 53 | + // Create a system data stream |
| 54 | + Request initialDocResponse = new Request("POST", ".fleet-actions-results/_doc"); |
| 55 | + initialDocResponse.setJsonEntity("{\"@timestamp\": 0}"); |
| 56 | + assertOK(adminClient().performRequest(initialDocResponse)); |
| 57 | + |
| 58 | + // Create a system index - this one has an alias |
| 59 | + Request sysIdxRequest = new Request("PUT", ".fleet-artifacts"); |
| 60 | + assertOK(adminClient().performRequest(sysIdxRequest)); |
| 61 | + |
| 62 | + // Create a regular index |
| 63 | + String regularIndex = "regular-idx"; |
| 64 | + String regularAlias = "regular-alias"; |
| 65 | + Request regularIdxRequest = new Request("PUT", regularIndex); |
| 66 | + regularIdxRequest.setJsonEntity("{\"aliases\": {\"" + regularAlias + "\": {}}}"); |
| 67 | + assertOK(client().performRequest(regularIdxRequest)); |
| 68 | + |
| 69 | + assertGetAliasAPIBehavesAsExpected(regularIndex, regularAlias); |
| 70 | + } |
| 71 | + |
| 72 | + public void testAliasWithSystemIndices() throws Exception { |
| 73 | + // Create a system index - this one has an alias |
| 74 | + Request sysIdxRequest = new Request("PUT", ".fleet-artifacts"); |
| 75 | + assertOK(adminClient().performRequest(sysIdxRequest)); |
| 76 | + |
| 77 | + // Create a regular index |
| 78 | + String regularIndex = "regular-idx"; |
| 79 | + String regularAlias = "regular-alias"; |
| 80 | + Request regularIdxRequest = new Request("PUT", regularIndex); |
| 81 | + regularIdxRequest.setJsonEntity("{\"aliases\": {\"" + regularAlias + "\": {}}}"); |
| 82 | + assertOK(client().performRequest(regularIdxRequest)); |
| 83 | + |
| 84 | + assertGetAliasAPIBehavesAsExpected(regularIndex, regularAlias); |
| 85 | + } |
| 86 | + |
| 87 | + private void assertGetAliasAPIBehavesAsExpected(String regularIndex, String regularAlias) throws Exception { |
| 88 | + // Get a non-system alias, should not warn or error |
| 89 | + { |
| 90 | + Request request = new Request("GET", "_alias/" + regularAlias); |
| 91 | + Response response = client().performRequest(request); |
| 92 | + assertOK(response); |
| 93 | + assertThat( |
| 94 | + EntityUtils.toString(response.getEntity()), |
| 95 | + allOf(containsString(regularAlias), containsString(regularIndex), not(containsString(".fleet-artifacts"))) |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + // Fully specify a regular index and alias, should not warn or error |
| 100 | + { |
| 101 | + Request request = new Request("GET", regularIndex + "/_alias/" + regularAlias); |
| 102 | + Response response = client().performRequest(request); |
| 103 | + assertOK(response); |
| 104 | + assertThat( |
| 105 | + EntityUtils.toString(response.getEntity()), |
| 106 | + allOf(containsString(regularAlias), containsString(regularIndex), not(containsString(".fleet-artifacts"))) |
| 107 | + ); |
| 108 | + } |
| 109 | + |
| 110 | + // The rest of these produce a warning |
| 111 | + RequestOptions consumeWarningsOptions = RequestOptions.DEFAULT.toBuilder() |
| 112 | + .setWarningsHandler( |
| 113 | + warnings -> Collections.singletonList( |
| 114 | + "this request accesses system indices: [.fleet-artifacts-7], but " |
| 115 | + + "in a future major version, direct access to system indices will be prevented by default" |
| 116 | + ).equals(warnings) == false |
| 117 | + ) |
| 118 | + .build(); |
| 119 | + |
| 120 | + // The base _alias route warns because there is a system index in the response |
| 121 | + { |
| 122 | + Request request = new Request("GET", "_alias"); |
| 123 | + request.setOptions(consumeWarningsOptions); // The result includes system indices, so we warn |
| 124 | + Response response = client().performRequest(request); |
| 125 | + assertOK(response); |
| 126 | + assertThat( |
| 127 | + EntityUtils.toString(response.getEntity()), |
| 128 | + allOf(containsString(regularAlias), containsString(regularIndex), not(containsString(".fleet-actions-results"))) |
| 129 | + ); |
| 130 | + } |
| 131 | + |
| 132 | + // Specify a system alias, should warn |
| 133 | + { |
| 134 | + Request request = new Request("GET", "_alias/.fleet-artifacts"); |
| 135 | + request.setOptions(consumeWarningsOptions); |
| 136 | + Response response = client().performRequest(request); |
| 137 | + assertOK(response); |
| 138 | + assertThat( |
| 139 | + EntityUtils.toString(response.getEntity()), |
| 140 | + allOf( |
| 141 | + containsString(".fleet-artifacts"), |
| 142 | + containsString(".fleet-artifacts-7"), |
| 143 | + not(containsString(regularAlias)), |
| 144 | + not(containsString(regularIndex)) |
| 145 | + ) |
| 146 | + ); |
| 147 | + } |
| 148 | + |
| 149 | + // Fully specify a system index and alias, should warn |
| 150 | + { |
| 151 | + Request request = new Request("GET", ".fleet-artifacts-7/_alias/.fleet-artifacts"); |
| 152 | + request.setOptions(consumeWarningsOptions); |
| 153 | + Response response = client().performRequest(request); |
| 154 | + assertOK(response); |
| 155 | + assertThat( |
| 156 | + EntityUtils.toString(response.getEntity()), |
| 157 | + allOf( |
| 158 | + containsString(".fleet-artifacts"), |
| 159 | + containsString(".fleet-artifacts-7"), |
| 160 | + not(containsString(regularAlias)), |
| 161 | + not(containsString(regularIndex)) |
| 162 | + ) |
| 163 | + ); |
| 164 | + } |
| 165 | + |
| 166 | + // Check an alias that doesn't exist |
| 167 | + { |
| 168 | + Request getAliasRequest = new Request("GET", "_alias/auditbeat-7.13.0"); |
| 169 | + try { |
| 170 | + client().performRequest(getAliasRequest); |
| 171 | + fail("this request should not succeed, as it is looking for an alias that does not exist"); |
| 172 | + } catch (ResponseException e) { |
| 173 | + assertThat(e.getResponse().getStatusLine().getStatusCode(), is(404)); |
| 174 | + assertThat( |
| 175 | + EntityUtils.toString(e.getResponse().getEntity()), |
| 176 | + not(containsString("use and access is reserved for system operations")) |
| 177 | + ); |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + // Specify a system data stream as an alias - should 404 |
| 182 | + { |
| 183 | + Request getAliasRequest = new Request("GET", "_alias/.fleet-actions-results"); |
| 184 | + try { |
| 185 | + client().performRequest(getAliasRequest); |
| 186 | + fail("this request should not succeed, as it is looking for an alias that does not exist"); |
| 187 | + } catch (ResponseException e) { |
| 188 | + assertThat(e.getResponse().getStatusLine().getStatusCode(), is(404)); |
| 189 | + assertThat( |
| 190 | + EntityUtils.toString(e.getResponse().getEntity()), |
| 191 | + not(containsString("use and access is reserved for system operations")) |
| 192 | + ); |
| 193 | + } |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + public void testCountWithSystemDataStream() throws Exception { |
| 198 | + assertThatAPIWildcardResolutionWorks(); |
| 199 | + |
| 200 | + // Create a system data stream |
| 201 | + Request initialDocResponse = new Request("POST", ".fleet-actions-results/_doc"); |
| 202 | + initialDocResponse.setJsonEntity("{\"@timestamp\": 0}"); |
| 203 | + assertOK(adminClient().performRequest(initialDocResponse)); |
| 204 | + assertThatAPIWildcardResolutionWorks(); |
| 205 | + |
| 206 | + // Create a system index - this one has an alias |
| 207 | + Request sysIdxRequest = new Request("PUT", ".fleet-artifacts"); |
| 208 | + assertOK(adminClient().performRequest(sysIdxRequest)); |
| 209 | + assertThatAPIWildcardResolutionWorks( |
| 210 | + singletonList( |
| 211 | + "this request accesses system indices: [.fleet-artifacts-7], but in a future major version, direct access to system" |
| 212 | + + " indices will be prevented by default" |
| 213 | + ) |
| 214 | + ); |
| 215 | + assertThatAPIWildcardResolutionWorks( |
| 216 | + singletonList( |
| 217 | + "this request accesses system indices: [.fleet-artifacts-7], but in a future major version, direct access to system" |
| 218 | + + " indices will be prevented by default" |
| 219 | + ), |
| 220 | + ".f*" |
| 221 | + ); |
| 222 | + |
| 223 | + // Create a regular index |
| 224 | + String regularIndex = "regular-idx"; |
| 225 | + String regularAlias = "regular-alias"; |
| 226 | + Request regularIdxRequest = new Request("PUT", regularIndex); |
| 227 | + regularIdxRequest.setJsonEntity("{\"aliases\": {\"" + regularAlias + "\": {}}}"); |
| 228 | + assertOK(client().performRequest(regularIdxRequest)); |
| 229 | + assertThatAPIWildcardResolutionWorks( |
| 230 | + singletonList( |
| 231 | + "this request accesses system indices: [.fleet-artifacts-7], but in a future major version, direct access to system" |
| 232 | + + " indices will be prevented by default" |
| 233 | + ) |
| 234 | + ); |
| 235 | + assertThatAPIWildcardResolutionWorks(emptyList(), "r*"); |
| 236 | + } |
| 237 | + |
| 238 | + private void assertThatAPIWildcardResolutionWorks() throws Exception { |
| 239 | + assertThatAPIWildcardResolutionWorks(emptyList(), null); |
| 240 | + } |
| 241 | + |
| 242 | + private void assertThatAPIWildcardResolutionWorks(List<String> warningsExpected) throws Exception { |
| 243 | + assertThatAPIWildcardResolutionWorks(warningsExpected, null); |
| 244 | + } |
| 245 | + |
| 246 | + private void assertThatAPIWildcardResolutionWorks(List<String> warningsExpected, String indexPattern) throws Exception { |
| 247 | + String path = indexPattern == null || indexPattern.isEmpty() ? "/_count" : "/" + indexPattern + "/_count"; |
| 248 | + Request countRequest = new Request("GET", path); |
| 249 | + if (warningsExpected.isEmpty() == false) { |
| 250 | + countRequest.setOptions( |
| 251 | + countRequest.getOptions().toBuilder().setWarningsHandler(warnings -> warningsExpected.equals(warnings) == false) |
| 252 | + ); |
| 253 | + } |
| 254 | + assertOK(client().performRequest(countRequest)); |
| 255 | + } |
| 256 | +} |
0 commit comments