18
18
import org .elasticsearch .action .termvectors .MultiTermVectorsResponse ;
19
19
import org .elasticsearch .action .termvectors .TermVectorsAction ;
20
20
import org .elasticsearch .client .Requests ;
21
+ import org .elasticsearch .core .TimeValue ;
21
22
import org .elasticsearch .index .IndexNotFoundException ;
22
23
import org .elasticsearch .search .SearchHit ;
23
24
import org .elasticsearch .test .SecurityIntegTestCase ;
26
27
import java .util .ArrayList ;
27
28
import java .util .List ;
28
29
29
- import static org .apache .lucene .util .LuceneTestCase .AwaitsFix ;
30
30
import static org .elasticsearch .test .SecurityTestsUtils .assertAuthorizationExceptionDefaultUsers ;
31
31
import static org .elasticsearch .test .SecurityTestsUtils .assertThrowsAuthorizationExceptionDefaultUsers ;
32
32
import static org .elasticsearch .test .hamcrest .ElasticsearchAssertions .assertNoSearchHits ;
35
35
import static org .hamcrest .core .IsInstanceOf .instanceOf ;
36
36
import static org .hamcrest .number .OrderingComparison .greaterThan ;
37
37
38
- @ AwaitsFix (bugUrl = "https://github.com/elastic/elasticsearch/issues/53340" )
39
38
public class ReadActionsTests extends SecurityIntegTestCase {
40
39
41
40
@ Override
@@ -53,156 +52,166 @@ public void testSearchForAll() {
53
52
//index1 is not authorized and referred to through wildcard
54
53
createIndicesWithRandomAliases ("test1" , "test2" , "test3" , "index1" );
55
54
56
- SearchResponse searchResponse = client (). prepareSearch (). get ();
55
+ SearchResponse searchResponse = trySearch ();
57
56
assertReturnedIndices (searchResponse , "test1" , "test2" , "test3" );
58
57
}
59
58
60
59
public void testSearchForWildcard () {
61
60
//index1 is not authorized and referred to through wildcard
62
61
createIndicesWithRandomAliases ("test1" , "test2" , "test3" , "index1" );
63
62
64
- SearchResponse searchResponse = client (). prepareSearch ( "*" ). get ( );
63
+ SearchResponse searchResponse = trySearch ( "*" );
65
64
assertReturnedIndices (searchResponse , "test1" , "test2" , "test3" );
66
65
}
67
66
68
67
public void testSearchNonAuthorizedWildcard () {
69
68
//wildcard doesn't match any authorized index
70
69
createIndicesWithRandomAliases ("test1" , "test2" , "index1" , "index2" );
71
- assertNoSearchHits (client (). prepareSearch ( "index*" ). get ( ));
70
+ assertNoSearchHits (trySearch ( "index*" ));
72
71
}
73
72
74
73
public void testSearchNonAuthorizedWildcardDisallowNoIndices () {
75
74
//wildcard doesn't match any authorized index
76
75
createIndicesWithRandomAliases ("test1" , "test2" , "index1" , "index2" );
77
- IndexNotFoundException e = expectThrows (IndexNotFoundException .class , () -> client ().prepareSearch ("index*" )
78
- .setIndicesOptions (IndicesOptions .fromOptions (randomBoolean (), false , true , randomBoolean ())).get ());
76
+ IndexNotFoundException e = expectThrows (
77
+ IndexNotFoundException .class ,
78
+ () -> trySearch (IndicesOptions .fromOptions (randomBoolean (), false , true , randomBoolean ()), "index*" )
79
+ );
79
80
assertEquals ("no such index [index*]" , e .getMessage ());
80
81
}
81
82
82
83
public void testEmptyClusterSearchForAll () {
83
- assertNoSearchHits (client (). prepareSearch (). get ());
84
+ assertNoSearchHits (trySearch ());
84
85
}
85
86
86
87
public void testEmptyClusterSearchForAllDisallowNoIndices () {
87
- IndexNotFoundException e = expectThrows (IndexNotFoundException .class , () -> client ().prepareSearch ()
88
- .setIndicesOptions (IndicesOptions .fromOptions (randomBoolean (), false , true , randomBoolean ())).get ());
88
+ IndexNotFoundException e = expectThrows (
89
+ IndexNotFoundException .class ,
90
+ () -> trySearch (IndicesOptions .fromOptions (randomBoolean (), false , true , randomBoolean ()))
91
+ );
89
92
assertEquals ("no such index [[]]" , e .getMessage ());
90
93
}
91
94
92
95
public void testEmptyClusterSearchForWildcard () {
93
- SearchResponse searchResponse = client (). prepareSearch ( "*" ). get ( );
96
+ SearchResponse searchResponse = trySearch ( "*" );
94
97
assertNoSearchHits (searchResponse );
95
98
}
96
99
97
100
public void testEmptyClusterSearchForWildcardDisallowNoIndices () {
98
- IndexNotFoundException e = expectThrows (IndexNotFoundException .class , () -> client ().prepareSearch ("*" )
99
- .setIndicesOptions (IndicesOptions .fromOptions (randomBoolean (), false , true , randomBoolean ())).get ());
101
+ IndexNotFoundException e = expectThrows (
102
+ IndexNotFoundException .class ,
103
+ () -> trySearch (IndicesOptions .fromOptions (randomBoolean (), false , true , randomBoolean ()), "*" )
104
+ );
100
105
assertEquals ("no such index [*]" , e .getMessage ());
101
106
}
102
107
103
108
public void testEmptyAuthorizedIndicesSearchForAll () {
104
109
createIndicesWithRandomAliases ("index1" , "index2" );
105
- assertNoSearchHits (client (). prepareSearch (). get ());
110
+ assertNoSearchHits (trySearch ());
106
111
}
107
112
108
113
public void testEmptyAuthorizedIndicesSearchForAllDisallowNoIndices () {
109
114
createIndicesWithRandomAliases ("index1" , "index2" );
110
- IndexNotFoundException e = expectThrows (IndexNotFoundException .class , () -> client ().prepareSearch ()
111
- .setIndicesOptions (IndicesOptions .fromOptions (randomBoolean (), false , true , randomBoolean ())).get ());
115
+ IndexNotFoundException e = expectThrows (
116
+ IndexNotFoundException .class ,
117
+ () -> trySearch (IndicesOptions .fromOptions (randomBoolean (), false , true , randomBoolean ()))
118
+ );
112
119
assertEquals ("no such index [[]]" , e .getMessage ());
113
120
}
114
121
115
122
public void testEmptyAuthorizedIndicesSearchForWildcard () {
116
123
createIndicesWithRandomAliases ("index1" , "index2" );
117
- assertNoSearchHits (client (). prepareSearch ( "*" ). get ( ));
124
+ assertNoSearchHits (trySearch ( "*" ));
118
125
}
119
126
120
127
public void testEmptyAuthorizedIndicesSearchForWildcardDisallowNoIndices () {
121
128
createIndicesWithRandomAliases ("index1" , "index2" );
122
- IndexNotFoundException e = expectThrows (IndexNotFoundException .class , () -> client ().prepareSearch ("*" )
123
- .setIndicesOptions (IndicesOptions .fromOptions (randomBoolean (), false , true , randomBoolean ())).get ());
129
+ IndexNotFoundException e = expectThrows (
130
+ IndexNotFoundException .class ,
131
+ () -> trySearch (IndicesOptions .fromOptions (randomBoolean (), false , true , randomBoolean ()), "*" )
132
+ );
124
133
assertEquals ("no such index [*]" , e .getMessage ());
125
134
}
126
135
127
136
public void testExplicitNonAuthorizedIndex () {
128
137
createIndicesWithRandomAliases ("test1" , "test2" , "index1" );
129
- assertThrowsAuthorizationExceptionDefaultUsers (client (). prepareSearch ("test*" , "index1" ):: get , SearchAction .NAME );
138
+ assertThrowsAuthorizationExceptionDefaultUsers (() -> trySearch ("test*" , "index1" ), SearchAction .NAME );
130
139
}
131
140
132
141
public void testIndexNotFound () {
133
142
createIndicesWithRandomAliases ("test1" , "test2" , "index1" );
134
- assertThrowsAuthorizationExceptionDefaultUsers (client (). prepareSearch ("missing" ):: get , SearchAction .NAME );
143
+ assertThrowsAuthorizationExceptionDefaultUsers (() -> trySearch ("missing" ), SearchAction .NAME );
135
144
}
136
145
137
146
public void testIndexNotFoundIgnoreUnavailable () {
138
147
IndicesOptions indicesOptions = IndicesOptions .lenientExpandOpen ();
139
148
createIndicesWithRandomAliases ("test1" , "test2" , "index1" );
140
149
141
150
String index = randomFrom ("test1" , "test2" );
142
- assertReturnedIndices (client (). prepareSearch ( "missing" , index ). setIndicesOptions ( indicesOptions ). get ( ), index );
151
+ assertReturnedIndices (trySearch ( indicesOptions , "missing" , index ), index );
143
152
144
- assertReturnedIndices (client (). prepareSearch ( "missing" , "test*" ). setIndicesOptions ( indicesOptions ). get ( ), "test1" , "test2" );
153
+ assertReturnedIndices (trySearch ( indicesOptions , "missing" , "test*" ), "test1" , "test2" );
145
154
146
- assertReturnedIndices (client (). prepareSearch ( "missing_*" , "test*" ). setIndicesOptions ( indicesOptions ). get ( ), "test1" , "test2" );
155
+ assertReturnedIndices (trySearch ( indicesOptions , "missing_*" , "test*" ), "test1" , "test2" );
147
156
148
157
//an unauthorized index is the same as a missing one
149
- assertNoSearchHits (client (). prepareSearch ( "missing" ). setIndicesOptions ( indicesOptions ). get ( ));
158
+ assertNoSearchHits (trySearch ( indicesOptions , "missing" ));
150
159
151
- assertNoSearchHits (client (). prepareSearch ( "index1" ). setIndicesOptions ( indicesOptions ). get ( ));
160
+ assertNoSearchHits (trySearch ( indicesOptions , "index1" ));
152
161
153
- assertNoSearchHits (client (). prepareSearch ( "missing" , "index1" ). setIndicesOptions ( indicesOptions ). get ( ));
162
+ assertNoSearchHits (trySearch ( indicesOptions , "missing" , "index1" ));
154
163
155
- assertNoSearchHits (client (). prepareSearch ( "does_not_match_any_*" ). setIndicesOptions ( indicesOptions ). get ( ));
164
+ assertNoSearchHits (trySearch ( indicesOptions , "does_not_match_any_*" ));
156
165
157
- assertNoSearchHits (client (). prepareSearch ( "does_not_match_any_*" , "index1" ). setIndicesOptions ( indicesOptions ). get ( ));
166
+ assertNoSearchHits (trySearch ( indicesOptions , "does_not_match_any_*" , "index1" ));
158
167
159
- assertNoSearchHits (client (). prepareSearch ( "index*" ). setIndicesOptions ( indicesOptions ). get ( ));
168
+ assertNoSearchHits (trySearch ( indicesOptions , "index*" ));
160
169
161
- assertNoSearchHits (client (). prepareSearch ( "index*" , "missing" ). setIndicesOptions ( indicesOptions ). get ( ));
170
+ assertNoSearchHits (trySearch ( indicesOptions , "index*" , "missing" ));
162
171
}
163
172
164
173
public void testExplicitExclusion () {
165
174
//index1 is not authorized and referred to through wildcard, test2 is excluded
166
175
createIndicesWithRandomAliases ("test1" , "test2" , "test3" , "index1" );
167
176
168
- SearchResponse searchResponse = client (). prepareSearch ( "*" , "-test2" ). get ( );
177
+ SearchResponse searchResponse = trySearch ( "*" , "-test2" );
169
178
assertReturnedIndices (searchResponse , "test1" , "test3" );
170
179
}
171
180
172
181
public void testWildcardExclusion () {
173
182
//index1 is not authorized and referred to through wildcard, test2 is excluded
174
183
createIndicesWithRandomAliases ("test1" , "test2" , "test21" , "test3" , "index1" );
175
184
176
- SearchResponse searchResponse = client (). prepareSearch ( "*" , "-test2*" ). get ( );
185
+ SearchResponse searchResponse = trySearch ( "*" , "-test2*" );
177
186
assertReturnedIndices (searchResponse , "test1" , "test3" );
178
187
}
179
188
180
189
public void testInclusionAndWildcardsExclusion () {
181
190
//index1 is not authorized and referred to through wildcard, test111 and test112 are excluded
182
191
createIndicesWithRandomAliases ("test1" , "test10" , "test111" , "test112" , "test2" , "index1" );
183
192
184
- SearchResponse searchResponse = client (). prepareSearch ( "test1*" , "index*" , "-test11*" ). get ( );
193
+ SearchResponse searchResponse = trySearch ( "test1*" , "index*" , "-test11*" );
185
194
assertReturnedIndices (searchResponse , "test1" , "test10" );
186
195
}
187
196
188
197
public void testExplicitAndWildcardsInclusionAndWildcardExclusion () {
189
198
//index1 is not authorized and referred to through wildcard, test111 and test112 are excluded
190
199
createIndicesWithRandomAliases ("test1" , "test10" , "test111" , "test112" , "test2" , "index1" );
191
200
192
- SearchResponse searchResponse = client (). prepareSearch ( "test2" , "test11*" , "index*" , "-test2*" ). get ( );
201
+ SearchResponse searchResponse = trySearch ( "test2" , "test11*" , "index*" , "-test2*" );
193
202
assertReturnedIndices (searchResponse , "test111" , "test112" );
194
203
}
195
204
196
205
public void testExplicitAndWildcardInclusionAndExplicitExclusions () {
197
206
//index1 is not authorized and referred to through wildcard, test111 and test112 are excluded
198
207
createIndicesWithRandomAliases ("test1" , "test10" , "test111" , "test112" , "test2" , "index1" );
199
208
200
- SearchResponse searchResponse = client (). prepareSearch ( "test10" , "test11*" , "index*" , "-test111" , "-test112" ). get ( );
209
+ SearchResponse searchResponse = trySearch ( "test10" , "test11*" , "index*" , "-test111" , "-test112" );
201
210
assertReturnedIndices (searchResponse , "test10" );
202
211
}
203
212
204
213
public void testMissingDateMath () {
205
- expectThrows (IndexNotFoundException .class , () -> client (). prepareSearch ( "<logstash-{now/M}>" ). get ( ));
214
+ expectThrows (IndexNotFoundException .class , () -> trySearch ( "<logstash-{now/M}>" ));
206
215
}
207
216
208
217
public void testMultiSearchUnauthorizedIndex () {
@@ -397,6 +406,14 @@ public void testMultiTermVectors() {
397
406
assertThat (response .getResponses ()[4 ].getFailure ().getCause (), instanceOf (IndexNotFoundException .class ));
398
407
}
399
408
409
+ private SearchResponse trySearch (String ... indices ) {
410
+ return client ().prepareSearch (indices ).get (TimeValue .timeValueSeconds (20 ));
411
+ }
412
+
413
+ private SearchResponse trySearch (IndicesOptions options , String ... indices ) {
414
+ return client ().prepareSearch (indices ).setIndicesOptions (options ).get (TimeValue .timeValueSeconds (20 ));
415
+ }
416
+
400
417
private static void assertReturnedIndices (SearchResponse searchResponse , String ... indices ) {
401
418
List <String > foundIndices = new ArrayList <>();
402
419
for (SearchHit searchHit : searchResponse .getHits ().getHits ()) {
0 commit comments