|
7 | 7 |
|
8 | 8 | package org.elasticsearch.xpack.security.authz; |
9 | 9 |
|
| 10 | +import org.elasticsearch.action.search.SearchRequest; |
| 11 | +import org.elasticsearch.cluster.metadata.DataStream; |
10 | 12 | import org.elasticsearch.common.Strings; |
11 | 13 | import org.elasticsearch.core.Tuple; |
12 | 14 | import org.elasticsearch.test.ESTestCase; |
| 15 | +import org.elasticsearch.transport.TransportRequest; |
13 | 16 | import org.elasticsearch.xpack.core.security.authc.Authentication; |
14 | 17 | import org.elasticsearch.xpack.core.security.authc.AuthenticationField; |
15 | 18 | import org.elasticsearch.xpack.core.security.authc.AuthenticationTestHelper; |
|
23 | 26 | import java.util.stream.IntStream; |
24 | 27 | import java.util.stream.Stream; |
25 | 28 |
|
| 29 | +import static org.hamcrest.Matchers.endsWith; |
26 | 30 | import static org.hamcrest.Matchers.equalTo; |
27 | 31 | import static org.mockito.Mockito.mock; |
28 | 32 | import static org.mockito.Mockito.when; |
@@ -233,6 +237,94 @@ public void testActionDeniedForCrossClusterAccessAuthentication() { |
233 | 237 | ); |
234 | 238 | } |
235 | 239 |
|
| 240 | + public void testActionDeniedWithFailuresAndCorrectActionIncludesFailuresMessage() { |
| 241 | + assumeTrue("failure store required", DataStream.isFailureStoreFeatureFlagEnabled()); |
| 242 | + |
| 243 | + Authentication authentication = AuthenticationTestHelper.builder().build(); |
| 244 | + |
| 245 | + final String action = "indices:data/read/" + randomAlphaOfLengthBetween(0, 8); |
| 246 | + SearchRequest request = mock(SearchRequest.class); |
| 247 | + for (List<String> requestedIndices : List.of( |
| 248 | + List.of(randomAlphaOfLength(5) + "::failures"), |
| 249 | + List.of(randomAlphaOfLength(5) + "::failures", randomAlphaOfLength(5)), |
| 250 | + List.of(randomAlphaOfLength(5), randomAlphaOfLength(5) + "::failures") |
| 251 | + )) { |
| 252 | + when(request.indices()).thenReturn(requestedIndices.toArray(new String[0])); |
| 253 | + assertThat( |
| 254 | + "for [" + Strings.collectionToCommaDelimitedString(requestedIndices) + "]", |
| 255 | + denialMessages.actionDenied(authentication, null, action, request, null), |
| 256 | + endsWith( |
| 257 | + "this action is granted by the index privileges [read,all] for data access, " |
| 258 | + + "or by [read_failure_store] for access with the [failures] selector" |
| 259 | + ) |
| 260 | + ); |
| 261 | + } |
| 262 | + } |
| 263 | + |
| 264 | + public void testActionDeniedWithNonMatchingActionFailuresOmitsFailuresMessage() { |
| 265 | + assumeTrue("failure store required", DataStream.isFailureStoreFeatureFlagEnabled()); |
| 266 | + |
| 267 | + Authentication authentication = AuthenticationTestHelper.builder().build(); |
| 268 | + |
| 269 | + // granted only by all, so selector message is omitted |
| 270 | + final String action = "indices:/some/action/" + randomAlphaOfLengthBetween(0, 8); |
| 271 | + SearchRequest request = mock(SearchRequest.class); |
| 272 | + for (List<String> requestedIndices : List.of( |
| 273 | + List.of(randomAlphaOfLength(5) + "::failures"), |
| 274 | + List.of(randomAlphaOfLength(5) + "::failures", randomAlphaOfLength(5)), |
| 275 | + List.of(randomAlphaOfLength(5), randomAlphaOfLength(5) + "::failures") |
| 276 | + )) { |
| 277 | + when(request.indices()).thenReturn(requestedIndices.toArray(new String[0])); |
| 278 | + assertThat( |
| 279 | + "for [" + Strings.collectionToCommaDelimitedString(requestedIndices) + "]", |
| 280 | + denialMessages.actionDenied(authentication, null, action, request, null), |
| 281 | + endsWith("this action is granted by the index privileges [all]") |
| 282 | + ); |
| 283 | + } |
| 284 | + } |
| 285 | + |
| 286 | + public void testActionDeniedWithoutFailuresOmitsFailuresMessage() { |
| 287 | + assumeTrue("failure store required", DataStream.isFailureStoreFeatureFlagEnabled()); |
| 288 | + |
| 289 | + Authentication authentication = AuthenticationTestHelper.builder().build(); |
| 290 | + |
| 291 | + final String action = "indices:data/read/" + randomAlphaOfLengthBetween(0, 8); |
| 292 | + SearchRequest request = mock(SearchRequest.class); |
| 293 | + for (List<String> requestedIndices : List.of( |
| 294 | + List.<String>of(), |
| 295 | + List.of(randomAlphaOfLength(5)), |
| 296 | + List.of(randomAlphaOfLength(5), randomAlphaOfLength(5)) |
| 297 | + )) { |
| 298 | + when(request.indices()).thenReturn(requestedIndices.toArray(new String[0])); |
| 299 | + assertThat( |
| 300 | + "for [" + Strings.collectionToCommaDelimitedString(requestedIndices) + "]", |
| 301 | + denialMessages.actionDenied(authentication, null, action, request, null), |
| 302 | + endsWith("this action is granted by the index privileges [read,all]") |
| 303 | + ); |
| 304 | + } |
| 305 | + } |
| 306 | + |
| 307 | + public void testActionDeniedWithoutIndicesOmitsFailuresMessage() { |
| 308 | + assumeTrue("failure store required", DataStream.isFailureStoreFeatureFlagEnabled()); |
| 309 | + |
| 310 | + Authentication authentication = AuthenticationTestHelper.builder().build(); |
| 311 | + |
| 312 | + final String action = "indices:data/read/" + randomAlphaOfLengthBetween(0, 8); |
| 313 | + // not an IndicesRequest |
| 314 | + TransportRequest request = mock(TransportRequest.class); |
| 315 | + for (List<String> requestedIndices : List.of( |
| 316 | + List.<String>of(), |
| 317 | + List.of(randomAlphaOfLength(5)), |
| 318 | + List.of(randomAlphaOfLength(5), randomAlphaOfLength(5)) |
| 319 | + )) { |
| 320 | + assertThat( |
| 321 | + "for [" + Strings.collectionToCommaDelimitedString(requestedIndices) + "]", |
| 322 | + denialMessages.actionDenied(authentication, null, action, request, null), |
| 323 | + endsWith("this action is granted by the index privileges [read,all]") |
| 324 | + ); |
| 325 | + } |
| 326 | + } |
| 327 | + |
236 | 328 | public void testSuccessfulAuthenticationDescription() { |
237 | 329 | final Authentication authentication1 = AuthenticationTestHelper.builder().realm().build(false); |
238 | 330 | assertThat( |
|
0 commit comments