Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* fixed security tests

Signed-off-by: Raj Chakravarthi <raj@icedome.ca>
(cherry picked from commit c51940f)
  • Loading branch information
raj-chak authored and lezzago committed Jun 8, 2023
1 parent 85b44b8 commit a02b933
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1134,10 +1134,11 @@ abstract class AlertingRestTestCase : ODFERestTestCase() {
client().performRequest(request)
}

fun createIndexRoleWithDocLevelSecurity(name: String, index: String, dlsQuery: String) {
fun createIndexRoleWithDocLevelSecurity(name: String, index: String, dlsQuery: String, clusterPermissions: String? = "") {
val request = Request("PUT", "/_plugins/_security/api/roles/$name")
var entity = "{\n" +
"\"cluster_permissions\": [\n" +
"\"$clusterPermissions\"\n" +
"],\n" +
"\"index_permissions\": [\n" +
"{\n" +
Expand Down Expand Up @@ -1198,10 +1199,10 @@ abstract class AlertingRestTestCase : ODFERestTestCase() {
user: String,
index: String,
role: String,
backendRole: String,
backendRoles: List<String>,
clusterPermissions: String?
) {
createUser(user, user, arrayOf(backendRole))
createUser(user, user, backendRoles.toTypedArray())
createTestIndex(index)
createCustomIndexRole(role, index, clusterPermissions)
createUserRolesMapping(role, arrayOf(user))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class SecureDestinationRestApiIT : AlertingRestTestCase() {
}
}

val user = "userOne"
val user = "userA"
var userClient: RestClient? = null

@Before
Expand Down Expand Up @@ -139,7 +139,7 @@ class SecureDestinationRestApiIT : AlertingRestTestCase() {
user,
TEST_HR_INDEX,
TEST_HR_ROLE,
TEST_HR_BACKEND_ROLE,
listOf(TEST_HR_BACKEND_ROLE),
getClusterPermissionsFromCustomRole(ALERTING_GET_DESTINATION_ACCESS)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ import org.junit.After
import org.junit.Before
import org.junit.BeforeClass
import org.opensearch.alerting.ALERTING_GET_EMAIL_ACCOUNT_ACCESS
import org.opensearch.alerting.ALERTING_NO_ACCESS_ROLE
import org.opensearch.alerting.ALERTING_SEARCH_EMAIL_ACCOUNT_ACCESS
import org.opensearch.alerting.AlertingPlugin
import org.opensearch.alerting.AlertingRestTestCase
import org.opensearch.alerting.TEST_HR_BACKEND_ROLE
import org.opensearch.alerting.TEST_HR_INDEX
import org.opensearch.alerting.TEST_HR_ROLE
import org.opensearch.alerting.makeRequest
import org.opensearch.client.ResponseException
import org.opensearch.client.RestClient
import org.opensearch.commons.rest.SecureRestClientBuilder
import org.opensearch.rest.RestStatus
Expand Down Expand Up @@ -50,7 +52,7 @@ class SecureEmailAccountRestApiIT : AlertingRestTestCase() {
}
}

val user = "userOne"
val user = "userB"
var userClient: RestClient? = null

@Before
Expand All @@ -76,7 +78,7 @@ class SecureEmailAccountRestApiIT : AlertingRestTestCase() {
user,
TEST_HR_INDEX,
TEST_HR_ROLE,
TEST_HR_BACKEND_ROLE,
listOf(TEST_HR_BACKEND_ROLE),
getClusterPermissionsFromCustomRole(ALERTING_GET_EMAIL_ACCOUNT_ACCESS)
)

Expand Down Expand Up @@ -105,7 +107,7 @@ class SecureEmailAccountRestApiIT : AlertingRestTestCase() {
user,
TEST_HR_INDEX,
TEST_HR_ROLE,
TEST_HR_BACKEND_ROLE,
listOf(TEST_HR_BACKEND_ROLE),
getClusterPermissionsFromCustomRole(ALERTING_SEARCH_EMAIL_ACCOUNT_ACCESS)
)

Expand All @@ -126,18 +128,16 @@ class SecureEmailAccountRestApiIT : AlertingRestTestCase() {

/*
TODO: https://github.com/opensearch-project/alerting/issues/300
*/
fun `test get email accounts with an user without get email account role`() {
createUserWithTestDataAndCustomRole(
user,
TEST_HR_INDEX,
TEST_HR_ROLE,
TEST_HR_BACKEND_ROLE,
listOf(TEST_HR_BACKEND_ROLE),
getClusterPermissionsFromCustomRole(ALERTING_NO_ACCESS_ROLE)
)
val emailAccount = createRandomEmailAccountWithGivenName(true, randomAlphaOfLength(5))
try {
userClient?.makeRequest(
"GET",
Expand All @@ -155,19 +155,15 @@ class SecureEmailAccountRestApiIT : AlertingRestTestCase() {
deleteRoleAndRoleMapping(TEST_HR_ROLE)
}
}
fun `test search email accounts with an user without search email account role`() {
createUserWithTestDataAndCustomRole(
user,
TEST_HR_INDEX,
TEST_HR_ROLE,
TEST_HR_BACKEND_ROLE,
listOf(TEST_HR_BACKEND_ROLE),
getClusterPermissionsFromCustomRole(ALERTING_NO_ACCESS_ROLE)
)
createRandomEmailAccountWithGivenName(true, randomAlphaOfLength(5))
try {
userClient?.makeRequest(
"POST",
Expand All @@ -182,6 +178,4 @@ class SecureEmailAccountRestApiIT : AlertingRestTestCase() {
deleteRoleAndRoleMapping(TEST_HR_ROLE)
}
}
*/
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class SecureEmailGroupsRestApiIT : AlertingRestTestCase() {
}
}

val user = "userOne"
val user = "userC"
var userClient: RestClient? = null

@Before
Expand All @@ -78,7 +78,7 @@ class SecureEmailGroupsRestApiIT : AlertingRestTestCase() {
user,
TEST_HR_INDEX,
TEST_HR_ROLE,
TEST_HR_BACKEND_ROLE,
listOf(TEST_HR_BACKEND_ROLE),
getClusterPermissionsFromCustomRole(ALERTING_GET_EMAIL_GROUP_ACCESS)
)

Expand All @@ -105,7 +105,7 @@ class SecureEmailGroupsRestApiIT : AlertingRestTestCase() {
user,
TEST_HR_INDEX,
TEST_HR_ROLE,
TEST_HR_BACKEND_ROLE,
listOf(TEST_HR_BACKEND_ROLE),
getClusterPermissionsFromCustomRole(ALERTING_SEARCH_EMAIL_GROUP_ACCESS)
)

Expand Down
Loading

0 comments on commit a02b933

Please sign in to comment.