-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy pathTransformSecurityBehaviorIT.kt
299 lines (257 loc) · 12 KB
/
TransformSecurityBehaviorIT.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
/*
* 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.junit.After
import org.junit.Before
import org.opensearch.client.RestClient
import org.opensearch.commons.rest.SecureRestClientBuilder
import org.opensearch.core.rest.RestStatus
import org.opensearch.indexmanagement.common.model.dimension.Terms
import org.opensearch.indexmanagement.indexstatemanagement.settings.ManagedIndexSettings
import org.opensearch.indexmanagement.transform.model.Transform
import org.opensearch.indexmanagement.transform.model.TransformMetadata
import org.opensearch.indexmanagement.transform.randomTransform
import org.opensearch.jobscheduler.spi.schedule.IntervalSchedule
import org.opensearch.test.junit.annotations.TestLogging
import java.time.Instant
import java.time.temporal.ChronoUnit
@TestLogging("level:DEBUG", reason = "Debug for tests.")
class TransformSecurityBehaviorIT : SecurityRestTestCase() {
private val password = "TestpgfhertergGd435AASA123!"
private val superTransformUser = "john"
private var superUserClient: RestClient? = null
private val testUser = "testUser"
private val testRole = "test_role"
var testUserClient: RestClient? = null
@Before
fun setupUsersAndRoles() {
updateClusterSetting(ManagedIndexSettings.JITTER.key, "0.0", false)
// Init super transform user
val helpdeskClusterPermissions =
listOf(
STOP_TRANSFORM,
EXPLAIN_INDEX,
TRANSFORM_ACTION,
GET_TRANSFORM,
EXPLAIN_TRANSFORM,
START_TRANSFORM,
DELETE_TRANSFORM,
HEALTH,
GET_TRANSFORMS,
)
val indexPermissions =
listOf(
MANAGED_INDEX,
CREATE_INDEX,
WRITE_INDEX,
BULK_WRITE_INDEX,
GET_INDEX_MAPPING,
SEARCH_INDEX,
PUT_INDEX_MAPPING,
)
// In this test suite case john is a "super-user" which has all relevant privileges
createUser(superTransformUser, password, listOf(HELPDESK))
createRole(HELPDESK_ROLE, helpdeskClusterPermissions, indexPermissions, listOf(AIRLINE_INDEX_PATTERN))
assignRoleToUsers(HELPDESK_ROLE, listOf(superTransformUser))
superUserClient =
SecureRestClientBuilder(
clusterHosts.toTypedArray(),
isHttps(),
superTransformUser,
password,
).setSocketTimeout(60000).setConnectionRequestTimeout(180000)
.build()
}
@After
fun cleanup() {
// Remove super user
superUserClient?.close()
deleteUser(superTransformUser)
deleteRole(HELPDESK_ROLE)
// Remove test user
testUserClient?.close()
deleteUser(testUser)
deleteRole(testRole)
deleteIndexByName(".opendistro-ism-config")
}
fun `test transform successful execution`() {
// Prepare index data
val sourceIndex = AIRLINE_INDEX
val targetIndex = "${AIRLINE_INDEX}_target_index"
validateSourceIndex(sourceIndex)
createTestUserWithRole(emptyList(), emptyList())
testUserClient =
SecureRestClientBuilder(clusterHosts.toTypedArray(), isHttps(), testUser, password).setSocketTimeout(60000)
.setConnectionRequestTimeout(180000)
.build()
try {
val transform = createSimpleTransform(sourceIndex, targetIndex)
// Create transform with user that has cluster privilege
createTransform(transform, transform.id, true, superUserClient)
updateTransformStartTime(transform)
waitFor {
assertTrue("Target transform index was not created", indexExists(transform.targetIndex))
checkTransformExplain(transform.id, superUserClient!!, RestStatus.OK)
checkTransformExplain(transform.id, testUserClient!!, RestStatus.FORBIDDEN)
}
// Check if transform was executed correctly
waitFor {
val job = getTransform(transformId = transform.id, client = superUserClient!!)
assertNotNull("Transform job doesn't have metadata set", job.metadataId)
val transformMetadata = getTransformMetadata(job.metadataId!!)
assertEquals("Transform has not finished", TransformMetadata.Status.FINISHED, transformMetadata.status)
}
} finally {
deleteIndexByName(sourceIndex)
deleteIndexByName(targetIndex)
}
}
fun `test failed transform creation user missing cluster privileges`() {
// Prepare index data
val sourceIndex = AIRLINE_INDEX
validateSourceIndex(sourceIndex)
createTestUserWithRole(emptyList(), emptyList())
testUserClient =
SecureRestClientBuilder(clusterHosts.toTypedArray(), isHttps(), testUser, password).setSocketTimeout(60000)
.setConnectionRequestTimeout(180000)
.build()
try {
val transform = randomTransform()
// Assert that client without appropriate cluster privilege can't create transform
createTransformAndCheckStatus(transform, RestStatus.FORBIDDEN, testUserClient!!)
} finally {
deleteIndexByName(sourceIndex)
}
}
fun `test failed transform execution user missing index access`() {
// Prepare index data
val sourceIndex = "source_index"
val targetIndex = "target_index"
validateSourceIndex(sourceIndex)
try {
val transform = createSimpleTransform(sourceIndex, targetIndex)
createTransform(transform, transform.id, true, superUserClient)
// Verify that user can access the transform
checkTransformExplain(transform.id, superUserClient!!, RestStatus.OK)
// Create transform with user that has cluster privilege
updateTransformStartTime(transform)
waitFor {
val job = getTransform(transformId = transform.id, client = superUserClient!!)
assertNotNull("Transform job doesn't have metadata set", job.metadataId)
val transformMetadata = getTransformMetadata(job.metadataId!!)
assertEquals("Transform finished", TransformMetadata.Status.FAILED, transformMetadata.status)
assertFalse("Target transform index was created", indexExists(transform.targetIndex))
}
} finally {
deleteIndexByName(sourceIndex)
}
}
fun `test transform access`() {
// Prepare index data
val sourceIndex = AIRLINE_INDEX
val targetIndex = "${AIRLINE_INDEX}_target_index"
validateSourceIndex(sourceIndex)
// Create client without rollup roles assigned
createTestUserWithRole(emptyList(), emptyList())
testUserClient =
SecureRestClientBuilder(clusterHosts.toTypedArray(), isHttps(), testUser, password).setSocketTimeout(60000)
.setConnectionRequestTimeout(180000)
.build()
try {
val transform = createSimpleTransform(sourceIndex, targetIndex)
// User without cluster privileges can't create transform
createTransformAndCheckStatus(transform, RestStatus.FORBIDDEN, testUserClient!!)
createTransformAndCheckStatus(transform, RestStatus.CREATED, superUserClient!!)
// User with transform explain can access transform job
checkTransformExplain(transform.id, superUserClient!!, RestStatus.OK)
checkTransformGet(transform.id, superUserClient!!, RestStatus.OK)
// User without transform explain will receive FORBIDDEN
checkTransformExplain(transform.id, testUserClient!!, RestStatus.FORBIDDEN)
checkTransformGet(transform.id, testUserClient!!, RestStatus.FORBIDDEN)
// Assign transform related privileges to non transform user and check if he can access the transform
assignRoleToUsers(HELPDESK_ROLE, listOf(testUser, superTransformUser))
checkTransformExplain(transform.id, testUserClient!!, RestStatus.OK)
checkTransformGet(transform.id, testUserClient!!, RestStatus.OK)
} finally {
deleteIndexByName(sourceIndex)
deleteIndexByName(targetIndex)
}
}
fun `test delete transform`() {
createTestUserWithRole(listOf(GET_TRANSFORM), listOf(GET_INDEX_MAPPING, SEARCH_INDEX))
testUserClient =
SecureRestClientBuilder(clusterHosts.toTypedArray(), isHttps(), testUser, password).setSocketTimeout(60000)
.setConnectionRequestTimeout(180000)
.build()
val targetIndex = "$AIRLINE_INDEX-target"
try {
val transform = createSimpleTransform(AIRLINE_INDEX, targetIndex)
createTransform(transform, transform.id, true, superUserClient!!)
checkTransformGet(transform.id, superUserClient!!, RestStatus.OK)
checkTransformGet(transform.id, testUserClient!!, RestStatus.OK)
deleteTransform(transform.id, testUserClient!!, RestStatus.FORBIDDEN)
// Stop transform in order to delete it
stopTransform(transform.id, superUserClient!!, RestStatus.OK)
deleteTransform(transform.id, superUserClient!!, RestStatus.OK)
checkTransformGet(transform.id, superUserClient!!, RestStatus.NOT_FOUND)
} finally {
deleteIndexByName(AIRLINE_INDEX)
}
}
fun `test stop transform`() {
createTestUserWithRole(listOf(GET_TRANSFORM), listOf(GET_INDEX_MAPPING, SEARCH_INDEX))
testUserClient =
SecureRestClientBuilder(clusterHosts.toTypedArray(), isHttps(), testUser, password).setSocketTimeout(60000)
.setConnectionRequestTimeout(180000)
.build()
val targetIndex = "$AIRLINE_INDEX-target"
try {
val transform = createSimpleTransform(AIRLINE_INDEX, targetIndex)
createTransform(transform, transform.id, true, superUserClient!!)
checkTransformGet(transform.id, superUserClient!!, RestStatus.OK)
checkTransformGet(transform.id, testUserClient!!, RestStatus.OK)
stopTransform(transform.id, testUserClient!!, RestStatus.FORBIDDEN)
stopTransform(transform.id, superUserClient!!, RestStatus.OK)
assignRoleToUsers(HELPDESK_ROLE, listOf(testUser, superTransformUser))
stopTransform(transform.id, testUserClient!!, RestStatus.OK)
} finally {
deleteIndexByName(AIRLINE_INDEX)
}
}
private fun createSimpleTransform(
sourceIndex: String,
targetIndex: String,
): Transform = Transform(
id = "id_1",
schemaVersion = 1L,
enabled = true,
enabledAt = Instant.now(),
updatedAt = Instant.now(),
jobSchedule = IntervalSchedule(Instant.now(), 1, ChronoUnit.MINUTES),
description = "test transform",
metadataId = null,
sourceIndex = sourceIndex,
targetIndex = targetIndex,
roles = emptyList(),
pageSize = 100,
groups =
listOf(
Terms(sourceField = "store_and_fwd_flag", targetField = "flag"),
),
)
private fun createTestUserWithRole(clusterPermissions: List<String>, indexPermissions: List<String>) {
val testBackendRole = testRole + "_backend"
// In this test suite case john is a "super-user" which has all relevant privileges
createUser(testUser, password, listOf(testBackendRole))
createRole(testRole, clusterPermissions, indexPermissions, listOf(AIRLINE_INDEX_PATTERN))
assignRoleToUsers(testRole, listOf(testUser))
}
}