8
8
9
9
import org .apache .http .entity .ContentType ;
10
10
import org .apache .http .entity .StringEntity ;
11
+ import org .elasticsearch .ElasticsearchException ;
11
12
import org .elasticsearch .ElasticsearchStatusException ;
12
13
import org .elasticsearch .action .admin .cluster .repositories .put .PutRepositoryRequest ;
13
- import org .elasticsearch .action .admin .cluster .snapshots .delete .DeleteSnapshotRequest ;
14
+ import org .elasticsearch .action .admin .cluster .snapshots .get .GetSnapshotsRequest ;
15
+ import org .elasticsearch .action .admin .cluster .snapshots .get .GetSnapshotsResponse ;
14
16
import org .elasticsearch .client .Node ;
15
17
import org .elasticsearch .client .Request ;
16
18
import org .elasticsearch .client .RequestOptions ;
19
21
import org .elasticsearch .client .RestClient ;
20
22
import org .elasticsearch .client .RestClientBuilder ;
21
23
import org .elasticsearch .client .RestHighLevelClient ;
24
+ import org .elasticsearch .client .core .AcknowledgedResponse ;
22
25
import org .elasticsearch .client .slm .DeleteSnapshotLifecyclePolicyRequest ;
23
26
import org .elasticsearch .client .slm .ExecuteSnapshotLifecyclePolicyRequest ;
24
27
import org .elasticsearch .client .slm .ExecuteSnapshotLifecyclePolicyResponse ;
28
+ import org .elasticsearch .client .slm .ExecuteSnapshotLifecycleRetentionRequest ;
25
29
import org .elasticsearch .client .slm .GetSnapshotLifecyclePolicyRequest ;
26
30
import org .elasticsearch .client .slm .PutSnapshotLifecyclePolicyRequest ;
27
31
import org .elasticsearch .client .slm .SnapshotLifecyclePolicy ;
38
42
import org .elasticsearch .common .xcontent .support .XContentMapValues ;
39
43
import org .elasticsearch .repositories .fs .FsRepository ;
40
44
import org .elasticsearch .rest .RestStatus ;
45
+ import org .elasticsearch .snapshots .SnapshotState ;
41
46
import org .elasticsearch .test .rest .ESRestTestCase ;
42
47
import org .elasticsearch .xpack .core .ilm .DeleteAction ;
43
48
import org .elasticsearch .xpack .core .ilm .LifecycleAction ;
56
61
import static java .util .Collections .singletonMap ;
57
62
import static org .elasticsearch .common .xcontent .XContentFactory .jsonBuilder ;
58
63
import static org .elasticsearch .xpack .core .security .authc .support .UsernamePasswordToken .basicAuthHeaderValue ;
64
+ import static org .hamcrest .Matchers .containsString ;
59
65
import static org .hamcrest .Matchers .equalTo ;
60
- import static org .hamcrest .Matchers .is ;
61
66
62
67
public class PermissionsIT extends ESRestTestCase {
63
68
@@ -144,14 +149,15 @@ public void testCanManageIndexWithNoPermissions() throws Exception {
144
149
}
145
150
146
151
public void testSLMWithPermissions () throws Exception {
152
+ String repo = "my_repository" ;
147
153
createIndexAsAdmin ("index" , Settings .builder ().put ("index.number_of_replicas" , 0 ).build (), "" );
148
154
149
155
// Set up two roles and users, one for reading SLM, another for managing SLM
150
156
Request roleRequest = new Request ("PUT" , "/_security/role/slm-read" );
151
157
roleRequest .setJsonEntity ("{ \" cluster\" : [\" read_slm\" ] }" );
152
158
assertOK (adminClient ().performRequest (roleRequest ));
153
159
roleRequest = new Request ("PUT" , "/_security/role/slm-manage" );
154
- roleRequest .setJsonEntity ("{ \" cluster\" : [\" manage_slm\" , \" create_snapshot \" ]," +
160
+ roleRequest .setJsonEntity ("{ \" cluster\" : [\" manage_slm\" , \" cluster:admin/repository/* \" , \" cluster:admin/snapshot/* \" ]," +
155
161
"\" indices\" : [{ \" names\" : [\" .slm-history*\" ],\" privileges\" : [\" all\" ] }] }" );
156
162
assertOK (adminClient ().performRequest (roleRequest ));
157
163
@@ -181,7 +187,7 @@ public void testSLMWithPermissions() throws Exception {
181
187
182
188
Settings .Builder settingsBuilder = Settings .builder ().put ("location" , "." );
183
189
repoRequest .settings (settingsBuilder );
184
- repoRequest .name ("my_repository" );
190
+ repoRequest .name (repo );
185
191
repoRequest .type (FsRepository .TYPE );
186
192
org .elasticsearch .action .support .master .AcknowledgedResponse response =
187
193
hlAdminClient .snapshot ().createRepository (repoRequest , RequestOptions .DEFAULT );
@@ -190,7 +196,8 @@ public void testSLMWithPermissions() throws Exception {
190
196
Map <String , Object > config = new HashMap <>();
191
197
config .put ("indices" , Collections .singletonList ("index" ));
192
198
SnapshotLifecyclePolicy policy = new SnapshotLifecyclePolicy (
193
- "policy_id" , "name" , "1 2 3 * * ?" , "my_repository" , config , SnapshotRetentionConfiguration .EMPTY );
199
+ "policy_id" , "name" , "1 2 3 * * ?" , repo , config ,
200
+ new SnapshotRetentionConfiguration (TimeValue .ZERO , null , null ));
194
201
PutSnapshotLifecyclePolicyRequest request = new PutSnapshotLifecyclePolicyRequest (policy );
195
202
196
203
expectThrows (ElasticsearchStatusException .class ,
@@ -208,25 +215,47 @@ public void testSLMWithPermissions() throws Exception {
208
215
209
216
ExecuteSnapshotLifecyclePolicyResponse executeResp =
210
217
adminHLRC .indexLifecycle ().executeSnapshotLifecyclePolicy (executeRequest , RequestOptions .DEFAULT );
218
+ final String snapName = executeResp .getSnapshotName ();
211
219
212
- DeleteSnapshotLifecyclePolicyRequest deleteRequest = new DeleteSnapshotLifecyclePolicyRequest ("policy_id" );
220
+ assertBusy (() -> {
221
+ try {
222
+ logger .info ("--> checking for snapshot to be created" );
223
+ GetSnapshotsRequest getSnaps = new GetSnapshotsRequest (repo );
224
+ getSnaps .snapshots (new String []{snapName });
225
+ GetSnapshotsResponse getResp = adminHLRC .snapshot ().get (getSnaps , RequestOptions .DEFAULT );
226
+ assertThat (getResp .getSnapshots ().get (0 ).state (), equalTo (SnapshotState .SUCCESS ));
227
+ } catch (ElasticsearchException e ) {
228
+ fail ("expected snapshot to exist but it does not: " + e .getDetailedMessage ());
229
+ }
230
+ });
231
+
232
+ ExecuteSnapshotLifecycleRetentionRequest executeRetention = new ExecuteSnapshotLifecycleRetentionRequest ();
213
233
expectThrows (ElasticsearchStatusException .class , () ->
214
- readHlrc .indexLifecycle ().deleteSnapshotLifecyclePolicy ( deleteRequest , RequestOptions .DEFAULT ));
234
+ readHlrc .indexLifecycle ().executeSnapshotLifecycleRetention ( executeRetention , RequestOptions .DEFAULT ));
215
235
216
- adminHLRC .indexLifecycle ().deleteSnapshotLifecyclePolicy (deleteRequest , RequestOptions .DEFAULT );
236
+ AcknowledgedResponse retentionResp =
237
+ adminHLRC .indexLifecycle ().executeSnapshotLifecycleRetention (executeRetention , RequestOptions .DEFAULT );
238
+ assertTrue (retentionResp .isAcknowledged ());
217
239
218
- // Delete snapshot to clean up and make sure it's not on-going.
219
- // This is inside an assertBusy because the snapshot may not
220
- // yet exist (in which case it throws an error)
221
240
assertBusy (() -> {
222
241
try {
223
- DeleteSnapshotRequest delReq = new DeleteSnapshotRequest ("my_repository" , executeResp .getSnapshotName ());
224
- hlAdminClient .snapshot ().delete (delReq , RequestOptions .DEFAULT );
225
- } catch (ElasticsearchStatusException e ) {
226
- fail ("got exception: " + e );
242
+ logger .info ("--> checking for snapshot to be deleted" );
243
+ GetSnapshotsRequest getSnaps = new GetSnapshotsRequest (repo );
244
+ getSnaps .snapshots (new String []{snapName });
245
+ GetSnapshotsResponse getResp = adminHLRC .snapshot ().get (getSnaps , RequestOptions .DEFAULT );
246
+ assertThat (getResp .getSnapshots ().size (), equalTo (0 ));
247
+ } catch (ElasticsearchException e ) {
248
+ // great, we want it to not exist
249
+ assertThat (e .getDetailedMessage (), containsString ("snapshot_missing_exception" ));
227
250
}
228
251
});
229
252
253
+ DeleteSnapshotLifecyclePolicyRequest deleteRequest = new DeleteSnapshotLifecyclePolicyRequest ("policy_id" );
254
+ expectThrows (ElasticsearchStatusException .class , () ->
255
+ readHlrc .indexLifecycle ().deleteSnapshotLifecyclePolicy (deleteRequest , RequestOptions .DEFAULT ));
256
+
257
+ adminHLRC .indexLifecycle ().deleteSnapshotLifecyclePolicy (deleteRequest , RequestOptions .DEFAULT );
258
+
230
259
hlAdminClient .close ();
231
260
readHlrc .close ();
232
261
adminHLRC .close ();
0 commit comments