forked from opensearch-project/index-management
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSecurityRestTestCase.kt
556 lines (478 loc) · 22.7 KB
/
SecurityRestTestCase.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/
package org.opensearch.indexmanagement
import org.apache.hc.core5.http.ContentType
import org.apache.hc.core5.http.HttpHeaders
import org.apache.hc.core5.http.io.entity.StringEntity
import org.apache.hc.core5.http.message.BasicHeader
import org.opensearch.client.Request
import org.opensearch.client.Response
import org.opensearch.client.ResponseException
import org.opensearch.client.RestClient
import org.opensearch.common.Strings
import org.opensearch.common.settings.Settings
import org.opensearch.common.xcontent.XContentType
import org.opensearch.indexmanagement.indexstatemanagement.IndexStateManagementRestTestCase
import org.opensearch.indexmanagement.indexstatemanagement.model.ManagedIndexConfig
import org.opensearch.indexmanagement.indexstatemanagement.model.Policy
import org.opensearch.indexmanagement.indexstatemanagement.resthandler.RestExplainAction
import org.opensearch.indexmanagement.indexstatemanagement.settings.ManagedIndexSettings
import org.opensearch.indexmanagement.indexstatemanagement.toJsonString
import org.opensearch.indexmanagement.indexstatemanagement.util.INDEX_NUMBER_OF_REPLICAS
import org.opensearch.indexmanagement.indexstatemanagement.util.INDEX_NUMBER_OF_SHARDS
import org.opensearch.indexmanagement.rollup.RollupRestTestCase
import org.opensearch.indexmanagement.rollup.model.Rollup
import org.opensearch.indexmanagement.rollup.model.RollupMetadata
import org.opensearch.indexmanagement.rollup.toJsonString
import org.opensearch.indexmanagement.spi.indexstatemanagement.model.ManagedIndexMetaData
import org.opensearch.indexmanagement.transform.TransformRestTestCase
import org.opensearch.indexmanagement.transform.model.Transform
import org.opensearch.indexmanagement.transform.toJsonString
import org.opensearch.rest.RestRequest
import org.opensearch.rest.RestStatus
import org.opensearch.test.OpenSearchTestCase
import java.util.Locale
abstract class SecurityRestTestCase : IndexManagementRestTestCase() {
private object RollupRestTestCaseSecurityExtension : RollupRestTestCase() {
fun createRollupExt(
rollup: Rollup,
rollupId: String,
refresh: Boolean,
client: RestClient
) = super.createRollup(rollup, rollupId, refresh, client)
fun updateRollupStartTimeExt(update: Rollup, desiredStartTimeMillis: Long? = null) =
super.updateRollupStartTime(update, desiredStartTimeMillis)
fun getRollupMetadataExt(
metadataId: String,
refresh: Boolean = true,
header: BasicHeader = BasicHeader(HttpHeaders.CONTENT_TYPE, "application/json"),
) = super.getRollupMetadata(metadataId, refresh, header)
fun getRollupExt(
rollupId: String,
header: BasicHeader = BasicHeader(HttpHeaders.CONTENT_TYPE, "application/json"),
) = super.getRollup(rollupId, header)
fun putDateDocumentInSourceIndexExt(rollup: Rollup) = super.putDateDocumentInSourceIndex(rollup)
fun createRollupMappingStringExt(rollup: Rollup): String = super.createRollupMappingString(rollup)
}
private object IndexStateManagementRestTestCaseExt : IndexStateManagementRestTestCase() {
fun createPolicyExt(
policy: Policy,
policyId: String = OpenSearchTestCase.randomAlphaOfLength(10),
refresh: Boolean = true,
client: RestClient?,
) = super.createPolicy(policy, policyId, refresh, client)
fun createPolicyJsonExt(
policyString: String,
policyId: String,
refresh: Boolean = true,
client: RestClient
) = super.createPolicyJson(policyString, policyId, refresh, client)
fun updateManagedIndexConfigStartTimeExt(update: ManagedIndexConfig, desiredStartTimeMillis: Long? = null, retryOnConflict: Int = 0) = super.updateManagedIndexConfigStartTime(update, desiredStartTimeMillis, retryOnConflict)
fun getExistingManagedIndexConfigExt(index: String) = super.getExistingManagedIndexConfig(index)
fun createIndexExt(
index: String = randomAlphaOfLength(10).lowercase(Locale.ROOT),
policyID: String? = randomAlphaOfLength(10),
alias: String? = null,
replicas: String? = null,
shards: String? = null,
mapping: String = "",
settings: Settings? = null,
): Pair<String, String?> {
return super.createIndex(index, policyID, alias, replicas, shards, mapping, settings)
}
fun getExplainManagedIndexMetaDataExt(indexName: String, userClient: RestClient? = null): ManagedIndexMetaData {
return super.getExplainManagedIndexMetaData(indexName, userClient)
}
}
private object TransformRestTestCaseExt : TransformRestTestCase() {
fun updateTransformStartTimeExt(update: Transform, desiredStartTimeMillis: Long? = null) =
super.updateTransformStartTime(update, desiredStartTimeMillis)
fun createTransformExt(
transform: Transform,
transformId: String = randomAlphaOfLength(10),
refresh: Boolean = true,
client: RestClient? = null,
) = super.createTransform(transform, transformId, refresh, client)
fun getTransformExt(
transformId: String,
header: BasicHeader = BasicHeader(HttpHeaders.CONTENT_TYPE, "application/json"),
userClient: RestClient? = null
) = super.getTransform(transformId, header, userClient)
fun getTransformMetadataExt(metadataId: String) = super.getTransformMetadata(metadataId)
}
protected fun updateClusterSetting(key: String, value: String, escapeValue: Boolean = true) {
val formattedValue = if (escapeValue) "\"$value\"" else value
val request = """
{
"persistent": {
"$key": $formattedValue
}
}
""".trimIndent()
val res = client().makeRequest(
"PUT", "_cluster/settings", emptyMap(),
StringEntity(request, ContentType.APPLICATION_JSON)
)
assertEquals("Request failed", RestStatus.OK, res.restStatus())
}
protected fun createRollup(
rollup: Rollup,
client: RestClient
): Rollup {
return RollupRestTestCaseSecurityExtension.createRollupExt(rollup, rollup.id, true, client)
}
protected fun createRollupAndCheckStatus(
rollup: Rollup,
expectedStatus: RestStatus,
client: RestClient
): Response {
val request = Request("PUT", "${IndexManagementPlugin.ROLLUP_JOBS_BASE_URI}/${rollup.id}?refresh=true")
request.setJsonEntity(rollup.toJsonString())
return executeRequest(request, expectedStatus, client)
}
protected fun updateRollupStartTime(update: Rollup, desiredStartTimeMillis: Long? = null) {
RollupRestTestCaseSecurityExtension.updateRollupStartTimeExt(update, desiredStartTimeMillis)
}
protected fun getRollupMetadata(
metadataId: String,
refresh: Boolean = true,
header: BasicHeader = BasicHeader(HttpHeaders.CONTENT_TYPE, "application/json"),
): RollupMetadata {
return RollupRestTestCaseSecurityExtension.getRollupMetadataExt(metadataId, refresh, header)
}
protected fun getRollup(
rollupId: String,
header: BasicHeader = BasicHeader(HttpHeaders.CONTENT_TYPE, "application/json"),
): Rollup {
return RollupRestTestCaseSecurityExtension.getRollupExt(rollupId, header)
}
protected fun deleteRollup(rollupId: String, client: RestClient, expectedStatus: RestStatus) {
val request = Request(RestRequest.Method.DELETE.name, "${IndexManagementPlugin.ROLLUP_JOBS_BASE_URI}/$rollupId")
executeRequest(request, expectedStatus, client)
}
protected fun createRollupSourceIndex(rollup: Rollup, settings: Settings = Settings.EMPTY) {
createIndex(rollup.sourceIndex, settings, createRollupMappingString(rollup))
}
protected fun putDateDocumentInSourceIndex(rollup: Rollup) {
RollupRestTestCaseSecurityExtension.putDateDocumentInSourceIndexExt(rollup)
}
private fun createRollupMappingString(rollup: Rollup): String {
return RollupRestTestCaseSecurityExtension.createRollupMappingStringExt(rollup)
}
protected fun updateManagedIndexConfigStartTime(update: ManagedIndexConfig, desiredStartTimeMillis: Long? = null, retryOnConflict: Int = 0) {
return IndexStateManagementRestTestCaseExt.updateManagedIndexConfigStartTimeExt(update, desiredStartTimeMillis, retryOnConflict)
}
protected fun createPolicy(
policy: Policy,
policyId: String = OpenSearchTestCase.randomAlphaOfLength(10),
refresh: Boolean = true,
client: RestClient?,
): Policy {
return IndexStateManagementRestTestCaseExt.createPolicyExt(policy, policyId, refresh, client)
}
protected fun managedIndexExplainAllAsMap(
client: RestClient?,
): Map<*, *> {
val request = Request("GET", "${RestExplainAction.EXPLAIN_BASE_URI}")
return entityAsMap(executeRequest(request, RestStatus.OK, client!!))
}
protected fun getExistingManagedIndexConfig(index: String) = IndexStateManagementRestTestCaseExt.getExistingManagedIndexConfigExt(index)
protected fun createPolicyJson(
policyString: String,
policyId: String,
refresh: Boolean = true,
client: RestClient
): Response {
return IndexStateManagementRestTestCaseExt.createPolicyJsonExt(policyString, policyId, refresh, client)
}
protected fun deletePolicy(policyId: String, client: RestClient, expectedStatus: RestStatus) {
val request = Request("DELETE", "${IndexManagementPlugin.POLICY_BASE_URI}/$policyId")
executeRequest(request, expectedStatus, client)
}
protected fun addPolicyToIndex(index: String, policyId: String, expectedStatus: RestStatus, client: RestClient) {
val body = """
{
"policy_id": "$policyId"
}
""".trimIndent()
val request = Request("POST", "/_opendistro/_ism/add/$index")
request.setJsonEntity(body)
executeRequest(request, expectedStatus, client)
}
protected fun getExplainManagedIndexMetaData(indexName: String, userClient: RestClient? = null): ManagedIndexMetaData {
return IndexStateManagementRestTestCaseExt.getExplainManagedIndexMetaDataExt(indexName, userClient)
}
protected fun createIndex(indexName: String, sourceIndexMappingString: String?, client: RestClient) {
val waitForActiveShards = if (isMultiNode) "all" else "1"
val builtSettings = Settings.builder().let {
it.putNull(ManagedIndexSettings.ROLLOVER_ALIAS.key)
it.put(INDEX_NUMBER_OF_REPLICAS, "1")
it.put(INDEX_NUMBER_OF_SHARDS, "1")
it.put("index.write.wait_for_active_shards", waitForActiveShards)
it
}.build()
val request = Request("PUT", "/$indexName")
var entity = "{\"settings\": " + Strings.toString(XContentType.JSON, builtSettings)
if (sourceIndexMappingString != null) {
entity = "$entity,\"mappings\" : {$sourceIndexMappingString}"
}
entity = "$entity}"
request.setJsonEntity(entity)
client.performRequest(request)
}
fun createIndex(
index: String = randomAlphaOfLength(10).lowercase(Locale.ROOT),
policyID: String? = randomAlphaOfLength(10),
alias: String? = null,
replicas: String? = null,
shards: String? = null,
mapping: String = "",
settings: Settings? = null,
): Pair<String, String?> {
return IndexStateManagementRestTestCaseExt.createIndexExt(index, policyID, alias, replicas, shards, mapping, settings)
}
protected fun checkPolicies(
userClient: RestClient,
user: String,
expectedNumberOfPolicies: Int?,
expectedStatus: RestStatus = RestStatus.OK
): Response? {
val response = executeRequest(request = Request(RestRequest.Method.GET.name, IndexManagementPlugin.POLICY_BASE_URI), expectedStatus, userClient)
assertEquals(
"User $user not able to see all policies", expectedNumberOfPolicies, response.asMap()["total_policies"]
)
return response
}
protected fun checkIndexAccess(index: String, userClient: RestClient, expectedStatus: RestStatus): Response {
val request = Request(RestRequest.Method.GET.name, "$index/_search")
return executeRequest(request, expectedStatus, userClient)
}
protected fun checkPolicyGet(policyId: String, userClient: RestClient, expectedStatus: RestStatus): Response {
val request = Request(RestRequest.Method.GET.name, IndexManagementPlugin.POLICY_BASE_URI + "/$policyId")
return executeRequest(request, expectedStatus, userClient)
}
protected fun checkRollupExplain(rollupId: String, userClient: RestClient, expectedStatus: RestStatus) {
val request = Request(RestRequest.Method.GET.name, "${IndexManagementPlugin.ROLLUP_JOBS_BASE_URI}/$rollupId/_explain")
executeRequest(request, expectedStatus, userClient)
}
protected fun checkRollupGet(rollupId: String, userClient: RestClient, expectedStatus: RestStatus) {
val request = Request(RestRequest.Method.GET.name, "${IndexManagementPlugin.ROLLUP_JOBS_BASE_URI}/$rollupId")
executeRequest(request, expectedStatus, userClient)
}
protected fun updateTransformStartTime(update: Transform, desiredStartTimeMillis: Long? = null) =
TransformRestTestCaseExt.updateTransformStartTimeExt(update, desiredStartTimeMillis)
protected fun createTransform(
transform: Transform,
transformId: String = randomAlphaOfLength(10),
refresh: Boolean = true,
client: RestClient? = null,
) = TransformRestTestCaseExt.createTransformExt(transform, transformId, refresh, client)
protected fun createTransformAndCheckStatus(
transform: Transform,
expectedStatus: RestStatus,
client: RestClient
): Response {
val request = Request(RestRequest.Method.PUT.name, "${IndexManagementPlugin.TRANSFORM_BASE_URI}/${transform.id}?refresh=true")
request.setJsonEntity(transform.toJsonString())
return executeRequest(request, expectedStatus, client)
}
protected fun createPolicyAndCheckStatus(
policy: Policy,
expectedStatus: RestStatus,
client: RestClient
): Response {
val request = Request("PUT", "${IndexManagementPlugin.POLICY_BASE_URI}/${policy.id}?refresh=true")
request.setJsonEntity(policy.toJsonString())
return executeRequest(request, expectedStatus, client)
}
protected fun getTransform(
transformId: String,
header: BasicHeader = BasicHeader(HttpHeaders.CONTENT_TYPE, "application/json"),
client: RestClient
) = TransformRestTestCaseExt.getTransformExt(transformId, header, client)
protected fun getTransformMetadata(metadataId: String) = TransformRestTestCaseExt.getTransformMetadataExt(metadataId)
protected fun checkTransformExplain(transformId: String, userClient: RestClient, expectedStatus: RestStatus): Response {
val request = Request(RestRequest.Method.GET.name, IndexManagementPlugin.TRANSFORM_BASE_URI + "/$transformId/_explain")
return executeRequest(request, expectedStatus, userClient)
}
protected fun checkTransformGet(transformId: String, userClient: RestClient, expectedStatus: RestStatus): Response {
val request = Request(RestRequest.Method.GET.name, IndexManagementPlugin.TRANSFORM_BASE_URI + "/$transformId")
return executeRequest(request, expectedStatus, userClient)
}
protected fun stopTransform(transformId: String, client: RestClient, expectedStatus: RestStatus) {
val request = Request(RestRequest.Method.POST.name, "${IndexManagementPlugin.TRANSFORM_BASE_URI}/$transformId/_stop")
executeRequest(request, expectedStatus, client)
}
protected fun deleteTransform(transformId: String, client: RestClient, expectedStatus: RestStatus) {
val request = Request("DELETE", "${IndexManagementPlugin.TRANSFORM_BASE_URI}/$transformId")
executeRequest(request, expectedStatus, client)
}
protected fun checkTransformPreview(userClient: RestClient, expectedStatus: RestStatus): Response {
val request = Request(RestRequest.Method.GET.name, "${IndexManagementPlugin.TRANSFORM_BASE_URI}/_preview")
return executeRequest(request, expectedStatus, userClient)
}
private fun executeRequest(
request: Request,
expectedRestStatus: RestStatus? = null,
client: RestClient
): Response {
val response = try {
client.performRequest(request)
} catch (exception: ResponseException) {
exception.response
}
if (expectedRestStatus != null) {
assertEquals(expectedRestStatus.status, response.statusLine.statusCode)
}
return response
}
protected fun createUser(name: String, pwd: String = "Test123!", backendRoles: List<String> = listOf()) {
val backendRolesStr = backendRoles.joinToString { "\"$it\"" }
val json = """
{
"password": "$pwd",
"backend_roles": [$backendRolesStr],
"attributes":{}
}
""".trimIndent()
val request = Request(RestRequest.Method.PUT.name, "_plugins/_security/api/internalusers/$name")
request.setJsonEntity(json)
executeRequest(request, RestStatus.CREATED, client())
}
protected fun createUserWithCustomRole(
user: String,
password: String,
role: String,
clusterPermissions: List<String> = emptyList(),
indexPermissions: List<String> = emptyList(),
backendRoles: List<String> = emptyList(),
indexPatterns: List<String> = emptyList()
) {
createUser(user, password, backendRoles)
createRole(role, clusterPermissions, indexPermissions, indexPatterns)
assignRoleToUsers(role, listOf(user))
}
protected fun createRole(
name: String,
clusterPermissions: List<String>,
indexPermissions: List<String>,
indexPatterns: List<String>,
) {
val response = try {
client().performRequest(Request("GET", "/_plugins/_security/api/roles/$name"))
} catch (ex: ResponseException) {
ex.response
}
// If role already exist, do nothing
if (response.statusLine.statusCode == RestStatus.OK.status) {
return
}
val request = Request("PUT", "/_plugins/_security/api/roles/$name")
val indexPatternsStr = indexPatterns.joinToString { "\"$it\"" }
val clusterPermissionsStr = clusterPermissions.joinToString { "\"$it\"" }
val indexPermissionsStr = indexPermissions.joinToString { "\"$it\"" }
val entity = """
{
"cluster_permissions": [$clusterPermissionsStr],
"index_permissions": [
{
"fls": [],
"masked_fields": [],
"allowed_actions": [$indexPermissionsStr],
"index_patterns": [$indexPatternsStr]
}
],
"tenant_permissions": []
}
""".trimIndent()
request.setJsonEntity(entity)
executeRequest(request, RestStatus.CREATED, client())
}
protected fun assignRoleToUsers(role: String, users: List<String>) {
val request = Request("PUT", "/_plugins/_security/api/rolesmapping/$role")
val usersStr = users.joinToString { "\"$it\"" }
val entity = """
{
"backend_roles": [],
"hosts": [],
"users": [$usersStr]
}
""".trimIndent()
request.setJsonEntity(entity)
client().performRequest(request)
}
protected fun setFilterByBackendRole(filter: Boolean) {
val setting = """
{
"persistent": {
"plugins.index_management.filter_by_backend_roles": "$filter"
}
}
""".trimIndent()
val request = Request(RestRequest.Method.PUT.name, "_cluster/settings")
request.setJsonEntity(setting)
executeRequest(request, RestStatus.OK, client())
}
protected fun deleteUser(name: String) {
executeRequest(request = Request(RestRequest.Method.DELETE.name, "/_plugins/_security/api/internalusers/$name"), client = client())
}
protected fun deleteRole(name: String) {
executeRequest(request = Request(RestRequest.Method.DELETE.name, "/_plugins/_security/api/roles/$name"), client = client())
}
protected fun deleteIndexByName(index: String) {
executeRequest(request = Request(RestRequest.Method.DELETE.name, "/$index"), client = client())
}
protected fun validateSourceIndex(indexName: String) {
if (!indexExists(indexName)) {
generateNYCTaxiData(indexName)
assertIndexExists(indexName)
}
}
protected fun createReplicaCountTestPolicyRequest(priority: Int, indexPattern: String?): String {
return """
{
"policy": {
"description": "test policy",
"default_state": "start",
"states": [
{
"name": "start",
"actions": [
{
"replica_count": {
"number_of_replicas": 5
}
}
],
"transitions": []
}
],
"ism_template": {
"index_patterns": ["$indexPattern"],
"priority": $priority
}
}
}
""".trimIndent()
}
companion object {
const val AIRLINE_POLICY = "airline-policy"
const val AVAILABILITY_POLICY = "availability-policy"
const val AIRLINE_INDEX = "airline-1"
const val AVAILABILITY_INDEX = "availability-1"
const val PHONE_OPERATOR = "phone_operator"
const val HELPDESK = "helpdesk_staff"
const val HELPDESK_ROLE = "helpdesk_role"
const val PHONE_OPERATOR_ROLE = "phone_operator_role"
const val AIRLINE_INDEX_PATTERN = "airline-*"
const val AVAILABILITY_INDEX_PATTERN = "availability-*"
}
}