Skip to content

Commit

Permalink
test fix
Browse files Browse the repository at this point in the history
  • Loading branch information
lezzago committed Mar 8, 2022
1 parent 0868314 commit 0dfd0a5
Showing 1 changed file with 79 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,26 +69,29 @@ class SecureMonitorRestApiIT : AlertingRestTestCase() {
val user = "userOne"
var userClient: RestClient? = null

@Before
fun create() {

if (userClient == null) {
createUser(user, user, arrayOf())
userClient = SecureRestClientBuilder(clusterHosts.toTypedArray(), isHttps(), user, user).setSocketTimeout(60000).build()
}
}

@After
fun cleanup() {

userClient?.close()
deleteUser(user)
}
// @Before
// fun create() {
//
// if (userClient == null) {
// createUser(user, user, arrayOf())
// userClient = SecureRestClientBuilder(clusterHosts.toTypedArray(), isHttps(), user, user).setSocketTimeout(60000).build()
// }
// }
//
// @After
// fun cleanup() {
//
// userClient?.close()
// deleteUser(user)
// }

// Create Monitor related security tests

fun `test create monitor with an user with alerting role`() {

createUser(user, user, arrayOf())
val userClient = SecureRestClientBuilder(clusterHosts.toTypedArray(), isHttps(), user, user).setSocketTimeout(60000).build()

createUserWithTestData(user, TEST_HR_INDEX, TEST_HR_ROLE, TEST_HR_BACKEND_ROLE)
createUserRolesMapping(ALERTING_FULL_ACCESS_ROLE, arrayOf(user))
try {
Expand All @@ -107,6 +110,8 @@ class SecureMonitorRestApiIT : AlertingRestTestCase() {
} finally {
deleteRoleAndRoleMapping(TEST_HR_ROLE)
}
userClient.close()
deleteUser(user)
}

/*
Expand Down Expand Up @@ -220,6 +225,8 @@ class SecureMonitorRestApiIT : AlertingRestTestCase() {

fun `test create monitor with an user without index read role`() {

createUser(user, user, arrayOf())
val userClient = SecureRestClientBuilder(clusterHosts.toTypedArray(), isHttps(), user, user).setSocketTimeout(60000).build()
createUserWithTestDataAndCustomRole(
user,
TEST_HR_INDEX,
Expand All @@ -243,6 +250,8 @@ class SecureMonitorRestApiIT : AlertingRestTestCase() {
} finally {
deleteRoleAndRoleMapping(TEST_HR_ROLE)
}
userClient.close()
deleteUser(user)
}

fun `test create monitor with disable filter by`() {
Expand All @@ -254,6 +263,9 @@ class SecureMonitorRestApiIT : AlertingRestTestCase() {
}

fun `test get monitor with an user with get monitor role`() {
createUser(user, user, arrayOf())
val userClient = SecureRestClientBuilder(clusterHosts.toTypedArray(), isHttps(), user, user).setSocketTimeout(60000).build()

createUserWithTestDataAndCustomRole(
user,
TEST_HR_INDEX,
Expand All @@ -275,6 +287,8 @@ class SecureMonitorRestApiIT : AlertingRestTestCase() {
} finally {
deleteRoleAndRoleMapping(TEST_HR_ROLE)
}
userClient.close()
deleteUser(user)
}

/*
Expand Down Expand Up @@ -417,6 +431,9 @@ class SecureMonitorRestApiIT : AlertingRestTestCase() {

disableFilterBy()

createUser(user, user, arrayOf())
val userClient = SecureRestClientBuilder(clusterHosts.toTypedArray(), isHttps(), user, user).setSocketTimeout(60000).build()

// creates monitor as "admin" user.
val monitor = createRandomMonitor(true)
val search = SearchSourceBuilder().query(QueryBuilders.termQuery("_id", monitor.id)).toString()
Expand Down Expand Up @@ -457,12 +474,17 @@ class SecureMonitorRestApiIT : AlertingRestTestCase() {
} finally {
deleteRoleMapping(ALERTING_FULL_ACCESS_ROLE)
}
userClient.close()
deleteUser(user)
}

fun `test query monitors with enable filter by`() {

enableFilterBy()

createUser(user, user, arrayOf())
val userClient = SecureRestClientBuilder(clusterHosts.toTypedArray(), isHttps(), user, user).setSocketTimeout(60000).build()

// creates monitor as "admin" user.
val monitor = createRandomMonitor(true)
val search = SearchSourceBuilder().query(QueryBuilders.termQuery("_id", monitor.id)).toString()
Expand Down Expand Up @@ -503,9 +525,14 @@ class SecureMonitorRestApiIT : AlertingRestTestCase() {
} finally {
deleteRoleMapping(ALERTING_FULL_ACCESS_ROLE)
}
userClient.close()
deleteUser(user)
}

fun `test execute monitor with an user with execute monitor access`() {
createUser(user, user, arrayOf())
val userClient = SecureRestClientBuilder(clusterHosts.toTypedArray(), isHttps(), user, user).setSocketTimeout(60000).build()

createUserWithTestDataAndCustomRole(
user,
TEST_HR_INDEX,
Expand All @@ -526,6 +553,8 @@ class SecureMonitorRestApiIT : AlertingRestTestCase() {
} finally {
deleteRoleAndRoleMapping(TEST_HR_ROLE)
}
userClient.close()
deleteUser(user)
}

/*
Expand Down Expand Up @@ -558,6 +587,9 @@ class SecureMonitorRestApiIT : AlertingRestTestCase() {
*/

fun `test delete monitor with an user with delete monitor access`() {
createUser(user, user, arrayOf())
val userClient = SecureRestClientBuilder(clusterHosts.toTypedArray(), isHttps(), user, user).setSocketTimeout(60000).build()

createUserWithTestDataAndCustomRole(
user,
TEST_HR_INDEX,
Expand All @@ -580,9 +612,14 @@ class SecureMonitorRestApiIT : AlertingRestTestCase() {
} finally {
deleteRoleAndRoleMapping(TEST_HR_ROLE)
}
userClient.close()
deleteUser(user)
}

fun `test delete monitor with an user without delete monitor access`() {
createUser(user, user, arrayOf())
val userClient = SecureRestClientBuilder(clusterHosts.toTypedArray(), isHttps(), user, user).setSocketTimeout(60000).build()

createUserWithTestDataAndCustomRole(
user,
TEST_HR_INDEX,
Expand All @@ -607,10 +644,15 @@ class SecureMonitorRestApiIT : AlertingRestTestCase() {
} finally {
deleteRoleAndRoleMapping(TEST_HR_ROLE)
}
userClient.close()
deleteUser(user)
}

fun `test query all alerts in all states with disabled filter by`() {

createUser(user, user, arrayOf())
val userClient = SecureRestClientBuilder(clusterHosts.toTypedArray(), isHttps(), user, user).setSocketTimeout(60000).build()

disableFilterBy()
putAlertMappings()
val monitor = createRandomMonitor(refresh = true)
Expand Down Expand Up @@ -643,10 +685,15 @@ class SecureMonitorRestApiIT : AlertingRestTestCase() {
} finally {
deleteRoleMapping(ALERTING_FULL_ACCESS_ROLE)
}
userClient.close()
deleteUser(user)
}

fun `test query all alerts in all states with filter by`() {

createUser(user, user, arrayOf())
val userClient = SecureRestClientBuilder(clusterHosts.toTypedArray(), isHttps(), user, user).setSocketTimeout(60000).build()

enableFilterBy()
putAlertMappings()
val adminUser = User(ADMIN, listOf(ADMIN), listOf(ALL_ACCESS_ROLE), listOf())
Expand Down Expand Up @@ -680,10 +727,15 @@ class SecureMonitorRestApiIT : AlertingRestTestCase() {
} finally {
deleteRoleMapping(ALERTING_FULL_ACCESS_ROLE)
}
userClient.close()
deleteUser(user)
}

fun `test get alerts with an user with get alerts role`() {

createUser(user, user, arrayOf())
val userClient = SecureRestClientBuilder(clusterHosts.toTypedArray(), isHttps(), user, user).setSocketTimeout(60000).build()

putAlertMappings()
val ackAlertsUser = User(ADMIN, listOf(ADMIN), listOf(ALERTING_GET_ALERTS_ACCESS), listOf())
var monitor = createRandomMonitor(refresh = true).copy(user = ackAlertsUser)
Expand Down Expand Up @@ -714,12 +766,17 @@ class SecureMonitorRestApiIT : AlertingRestTestCase() {
} finally {
deleteRoleAndRoleMapping(TEST_HR_ROLE)
}
userClient.close()
deleteUser(user)
}

// Execute Monitor related security tests

fun `test execute monitor with elevate permissions`() {

createUser(user, user, arrayOf())
val userClient = SecureRestClientBuilder(clusterHosts.toTypedArray(), isHttps(), user, user).setSocketTimeout(60000).build()

val action = randomAction(template = randomTemplateScript("Hello {{ctx.monitor.name}}"), destinationId = createDestination().id)
val inputs = listOf(
SearchInput(
Expand All @@ -746,10 +803,15 @@ class SecureMonitorRestApiIT : AlertingRestTestCase() {
} finally {
deleteRoleMapping(ALERTING_FULL_ACCESS_ROLE)
}
userClient.close()
deleteUser(user)
}

fun `test admin all access with enable filter by`() {

createUser(user, user, arrayOf())
val userClient = SecureRestClientBuilder(clusterHosts.toTypedArray(), isHttps(), user, user).setSocketTimeout(60000).build()

enableFilterBy()
createUserWithTestData(user, TEST_HR_INDEX, TEST_HR_ROLE, TEST_HR_BACKEND_ROLE)
createUserRolesMapping(ALERTING_FULL_ACCESS_ROLE, arrayOf(user))
Expand Down Expand Up @@ -803,6 +865,8 @@ class SecureMonitorRestApiIT : AlertingRestTestCase() {
} finally {
deleteRoleAndRoleMapping(TEST_HR_ROLE)
}
userClient.close()
deleteUser(user)
}

/*
Expand Down

0 comments on commit 0dfd0a5

Please sign in to comment.