Skip to content

Commit 80a04af

Browse files
authored
Fix CI check with security failing due to empty string in payload body (#1994)
* Fix CI check with security failing due to empty string in payload body Signed-off-by: Craig Perkins <[email protected]> * Abstract call Signed-off-by: Craig Perkins <[email protected]> * Remove Thread.sleep Signed-off-by: Craig Perkins <[email protected]> --------- Signed-off-by: Craig Perkins <[email protected]>
1 parent 87345c3 commit 80a04af

File tree

1 file changed

+120
-140
lines changed

1 file changed

+120
-140
lines changed

alerting/src/test/kotlin/org/opensearch/alerting/AlertingRestTestCase.kt

Lines changed: 120 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,7 @@ abstract class AlertingRestTestCase : ODFERestTestCase() {
176176
): MonitorV2 {
177177
// every random ppl monitor's query searches index TEST_INDEX_NAME
178178
// by default, so create that first before creating the monitor
179-
val indexExistsResponse = client().makeRequest("HEAD", TEST_INDEX_NAME)
180-
if (indexExistsResponse.restStatus() == RestStatus.NOT_FOUND) {
179+
if (!indexExists(TEST_INDEX_NAME)) {
181180
createIndex(TEST_INDEX_NAME, Settings.EMPTY, TEST_INDEX_MAPPINGS)
182181
}
183182

@@ -760,9 +759,9 @@ abstract class AlertingRestTestCase : ODFERestTestCase() {
760759
if (refresh) refreshIndex(indices)
761760

762761
val request = """
763-
{ "version" : true,
764-
"query": { "match_all": {} }
765-
}
762+
{ "version" : true,
763+
"query": { "match_all": {} }
764+
}
766765
""".trimIndent()
767766
val httpResponse = adminClient().makeRequest("GET", "/$indices/_search", StringEntity(request, APPLICATION_JSON))
768767
assertEquals("Search failed", RestStatus.OK, httpResponse.restStatus())
@@ -807,9 +806,9 @@ abstract class AlertingRestTestCase : ODFERestTestCase() {
807806
if (refresh) refreshIndex(indices)
808807

809808
val request = """
810-
{ "version" : true,
811-
"query": { "match_all": {} }
812-
}
809+
{ "version" : true,
810+
"query": { "match_all": {} }
811+
}
813812
""".trimIndent()
814813
val httpResponse = adminClient().makeRequest("GET", "/$indices/_search", StringEntity(request, APPLICATION_JSON))
815814
assertEquals("Search failed", RestStatus.OK, httpResponse.restStatus())
@@ -832,9 +831,9 @@ abstract class AlertingRestTestCase : ODFERestTestCase() {
832831
// If this is a test monitor (it doesn't have an ID) and no alerts will be saved for it.
833832
val searchParams = if (monitor.id != Monitor.NO_ID) mapOf("routing" to monitor.id) else mapOf()
834833
val request = """
835-
{ "version" : true,
836-
"query" : { "term" : { "${Alert.MONITOR_ID_FIELD}" : "${monitor.id}" } }
837-
}
834+
{ "version" : true,
835+
"query" : { "term" : { "${Alert.MONITOR_ID_FIELD}" : "${monitor.id}" } }
836+
}
838837
""".trimIndent()
839838
val httpResponse = adminClient().makeRequest("GET", "/$indices/_search", searchParams, StringEntity(request, APPLICATION_JSON))
840839
assertEquals("Search failed", RestStatus.OK, httpResponse.restStatus())
@@ -861,9 +860,9 @@ abstract class AlertingRestTestCase : ODFERestTestCase() {
861860
// If this is a test monitor (it doesn't have an ID) and no alerts will be saved for it.
862861
val searchParams = if (monitorV2Id != MonitorV2.NO_ID) mapOf("routing" to monitorV2Id) else mapOf()
863862
val request = """
864-
{ "version" : true,
865-
"query" : { "term" : { "${AlertV2.MONITOR_V2_ID_FIELD}" : "$monitorV2Id" } }
866-
}
863+
{ "version" : true,
864+
"query" : { "term" : { "${AlertV2.MONITOR_V2_ID_FIELD}" : "$monitorV2Id" } }
865+
}
867866
""".trimIndent()
868867
val httpResponse = adminClient().makeRequest("GET", "/$indices/_search", searchParams, StringEntity(request, APPLICATION_JSON))
869868
assertEquals("Search failed", RestStatus.OK, httpResponse.restStatus())
@@ -1039,9 +1038,9 @@ abstract class AlertingRestTestCase : ODFERestTestCase() {
10391038
protected fun searchMonitors(): SearchResponse {
10401039
var baseEndpoint = "${AlertingPlugin.MONITOR_BASE_URI}/_search?"
10411040
val request = """
1042-
{ "version" : true,
1043-
"query": { "match_all": {} }
1044-
}
1041+
{ "version" : true,
1042+
"query": { "match_all": {} }
1043+
}
10451044
""".trimIndent()
10461045
val httpResponse = adminClient().makeRequest("POST", baseEndpoint, StringEntity(request, APPLICATION_JSON))
10471046
assertEquals("Search failed", RestStatus.OK, httpResponse.restStatus())
@@ -1374,9 +1373,9 @@ abstract class AlertingRestTestCase : ODFERestTestCase() {
13741373
val testTime = DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(twoMinsAgo)
13751374
val testDoc = """
13761375
{
1377-
"test_strict_date_time": "$testTime",
1378-
"test_field": "$value",
1379-
"number": "$i"
1376+
"test_strict_date_time": "$testTime",
1377+
"test_field": "$value",
1378+
"number": "$i"
13801379
}
13811380
""".trimIndent()
13821381
// Indexing documents with deterministic doc id to allow for easy selected deletion during testing
@@ -1390,9 +1389,9 @@ abstract class AlertingRestTestCase : ODFERestTestCase() {
13901389
val testTime = DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(time)
13911390
val testDoc = """
13921391
{
1393-
"test_strict_date_time": "$testTime",
1394-
"test_field": "$value",
1395-
"number": "$i"
1392+
"test_strict_date_time": "$testTime",
1393+
"test_field": "$value",
1394+
"number": "$i"
13961395
}
13971396
""".trimIndent()
13981397
// Indexing documents with deterministic doc id to allow for easy selected deletion during testing
@@ -1409,9 +1408,9 @@ abstract class AlertingRestTestCase : ODFERestTestCase() {
14091408
val testTime = DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(time)
14101409
val testDoc = """
14111410
{
1412-
"test_strict_date_time": "$testTime",
1413-
"test_field": "$value",
1414-
"number": "$i"
1411+
"test_strict_date_time": "$testTime",
1412+
"test_field": "$value",
1413+
"number": "$i"
14151414
}
14161415
""".trimIndent()
14171416
// Indexing documents with deterministic doc id to allow for easy selected deletion during testing
@@ -1698,72 +1697,66 @@ abstract class AlertingRestTestCase : ODFERestTestCase() {
16981697
val customAttributesString = customAttributes.entries.joinToString(prefix = "{", separator = ", ", postfix = "}") {
16991698
"\"${it.key}\": \"${it.value}\""
17001699
}
1701-
var entity = " {\n" +
1702-
"\"password\": \"$password\",\n" +
1703-
"\"backend_roles\": [$broles],\n" +
1704-
"\"attributes\": $customAttributesString\n" +
1705-
"} "
1700+
var entity = """
1701+
{
1702+
"password": "$password",
1703+
"backend_roles": [$broles],
1704+
"attributes": $customAttributesString
1705+
}
1706+
""".trimIndent()
17061707
request.setJsonEntity(entity)
17071708
client().performRequest(request)
17081709
}
17091710

17101711
fun patchUserBackendRoles(name: String, backendRoles: Array<String>) {
17111712
val request = Request("PATCH", "/_plugins/_security/api/internalusers/$name")
17121713
val broles = backendRoles.joinToString { "\"$it\"" }
1713-
var entity = " [{\n" +
1714-
"\"op\": \"replace\",\n" +
1715-
"\"path\": \"/backend_roles\",\n" +
1716-
"\"value\": [$broles]\n" +
1717-
"}]"
1714+
var entity = """
1715+
[{
1716+
"op": "replace",
1717+
"path": "/backend_roles",
1718+
"value": [$broles]
1719+
}]
1720+
""".trimIndent()
17181721
request.setJsonEntity(entity)
17191722
client().performRequest(request)
17201723
}
17211724

17221725
fun createIndexRole(name: String, index: String) {
17231726
val request = Request("PUT", "/_plugins/_security/api/roles/$name")
1724-
var entity = "{\n" +
1725-
"\"cluster_permissions\": [\n" +
1726-
"],\n" +
1727-
"\"index_permissions\": [\n" +
1728-
"{\n" +
1729-
"\"index_patterns\": [\n" +
1730-
"\"$index\"\n" +
1731-
"],\n" +
1732-
"\"dls\": \"\",\n" +
1733-
"\"fls\": [],\n" +
1734-
"\"masked_fields\": [],\n" +
1735-
"\"allowed_actions\": [\n" +
1736-
"\"crud\"\n" +
1737-
"]\n" +
1738-
"}\n" +
1739-
"],\n" +
1740-
"\"tenant_permissions\": []\n" +
1741-
"}"
1727+
var entity = """
1728+
{
1729+
"cluster_permissions": [],
1730+
"index_permissions": [{
1731+
"index_patterns": ["$index"],
1732+
"dls": "",
1733+
"fls": [],
1734+
"masked_fields": [],
1735+
"allowed_actions": ["crud"]
1736+
}],
1737+
"tenant_permissions": []
1738+
}
1739+
""".trimIndent()
17421740
request.setJsonEntity(entity)
17431741
client().performRequest(request)
17441742
}
17451743

17461744
fun createCustomIndexRole(name: String, index: String, clusterPermissions: String?) {
17471745
val request = Request("PUT", "/_plugins/_security/api/roles/$name")
1748-
var entity = "{\n" +
1749-
"\"cluster_permissions\": [\n" +
1750-
"\"$clusterPermissions\"\n" +
1751-
"],\n" +
1752-
"\"index_permissions\": [\n" +
1753-
"{\n" +
1754-
"\"index_patterns\": [\n" +
1755-
"\"$index\"\n" +
1756-
"],\n" +
1757-
"\"dls\": \"\",\n" +
1758-
"\"fls\": [],\n" +
1759-
"\"masked_fields\": [],\n" +
1760-
"\"allowed_actions\": [\n" +
1761-
"\"crud\"\n" +
1762-
"]\n" +
1763-
"}\n" +
1764-
"],\n" +
1765-
"\"tenant_permissions\": []\n" +
1766-
"}"
1746+
val clusterPerms = if (clusterPermissions.isNullOrEmpty()) "[]" else "[\"$clusterPermissions\"]"
1747+
var entity = """
1748+
{
1749+
"cluster_permissions": $clusterPerms,
1750+
"index_permissions": [{
1751+
"index_patterns": ["$index"],
1752+
"dls": "",
1753+
"fls": [],
1754+
"masked_fields": [],
1755+
"allowed_actions": ["crud"]
1756+
}],
1757+
"tenant_permissions": []
1758+
}
1759+
""".trimIndent()
17671760
request.setJsonEntity(entity)
17681761
client().performRequest(request)
17691762
}
@@ -1776,50 +1769,39 @@ abstract class AlertingRestTestCase : ODFERestTestCase() {
17761769
Collectors.joining(",")
17771770
)
17781771

1779-
var entity = "{\n" +
1780-
"\"cluster_permissions\": [\n" +
1781-
"$clusterPermissionsStr\n" +
1782-
"],\n" +
1783-
"\"index_permissions\": [\n" +
1784-
"{\n" +
1785-
"\"index_patterns\": [\n" +
1786-
"\"$index\"\n" +
1787-
"],\n" +
1788-
"\"dls\": \"\",\n" +
1789-
"\"fls\": [],\n" +
1790-
"\"masked_fields\": [],\n" +
1791-
"\"allowed_actions\": [\n" +
1792-
"\"crud\"\n" +
1793-
"]\n" +
1794-
"}\n" +
1795-
"],\n" +
1796-
"\"tenant_permissions\": []\n" +
1797-
"}"
1772+
var entity = """
1773+
{
1774+
"cluster_permissions": [$clusterPermissionsStr],
1775+
"index_permissions": [{
1776+
"index_patterns": ["$index"],
1777+
"dls": "",
1778+
"fls": [],
1779+
"masked_fields": [],
1780+
"allowed_actions": ["crud"]
1781+
}],
1782+
"tenant_permissions": []
1783+
}
1784+
""".trimIndent()
17981785
request.setJsonEntity(entity)
17991786
client().performRequest(request)
18001787
}
18011788

18021789
fun createIndexRoleWithDocLevelSecurity(name: String, index: String, dlsQuery: String, clusterPermissions: String? = "") {
18031790
val request = Request("PUT", "/_plugins/_security/api/roles/$name")
1804-
var entity = "{\n" +
1805-
"\"cluster_permissions\": [\n" +
1806-
"\"$clusterPermissions\"\n" +
1807-
"],\n" +
1808-
"\"index_permissions\": [\n" +
1809-
"{\n" +
1810-
"\"index_patterns\": [\n" +
1811-
"\"$index\"\n" +
1812-
"],\n" +
1813-
"\"dls\": \"$dlsQuery\",\n" +
1814-
"\"fls\": [],\n" +
1815-
"\"masked_fields\": [],\n" +
1816-
"\"allowed_actions\": [\n" +
1817-
"\"crud\"\n" +
1818-
"]\n" +
1819-
"}\n" +
1820-
"],\n" +
1821-
"\"tenant_permissions\": []\n" +
1822-
"}"
1791+
val clusterPerms = if (clusterPermissions.isNullOrEmpty()) "[]" else "[\"$clusterPermissions\"]"
1792+
var entity = """
1793+
{
1794+
"cluster_permissions": $clusterPerms,
1795+
"index_permissions": [{
1796+
"index_patterns": ["$index"],
1797+
"dls": "$dlsQuery",
1798+
"fls": [],
1799+
"masked_fields": [],
1800+
"allowed_actions": ["crud"]
1801+
}],
1802+
"tenant_permissions": []
1803+
}
1804+
""".trimIndent()
18231805
request.setJsonEntity(entity)
18241806
client().performRequest(request)
18251807
}
@@ -1831,37 +1813,33 @@ abstract class AlertingRestTestCase : ODFERestTestCase() {
18311813
)
18321814

18331815
val request = Request("PUT", "/_plugins/_security/api/roles/$name")
1834-
var entity = "{\n" +
1835-
"\"cluster_permissions\": [\n" +
1836-
"$clusterPermissionsStr\n" +
1837-
"],\n" +
1838-
"\"index_permissions\": [\n" +
1839-
"{\n" +
1840-
"\"index_patterns\": [\n" +
1841-
"\"$index\"\n" +
1842-
"],\n" +
1843-
"\"dls\": \"$dlsQuery\",\n" +
1844-
"\"fls\": [],\n" +
1845-
"\"masked_fields\": [],\n" +
1846-
"\"allowed_actions\": [\n" +
1847-
"\"crud\"\n" +
1848-
"]\n" +
1849-
"}\n" +
1850-
"],\n" +
1851-
"\"tenant_permissions\": []\n" +
1852-
"}"
1816+
var entity = """
1817+
{
1818+
"cluster_permissions": [$clusterPermissionsStr],
1819+
"index_permissions": [{
1820+
"index_patterns": ["$index"],
1821+
"dls": "$dlsQuery",
1822+
"fls": [],
1823+
"masked_fields": [],
1824+
"allowed_actions": ["crud"]
1825+
}],
1826+
"tenant_permissions": []
1827+
}
1828+
""".trimIndent()
18531829
request.setJsonEntity(entity)
18541830
client().performRequest(request)
18551831
}
18561832

18571833
fun createUserRolesMapping(role: String, users: Array<String>) {
18581834
val request = Request("PUT", "/_plugins/_security/api/rolesmapping/$role")
18591835
val usersStr = users.joinToString { it -> "\"$it\"" }
1860-
var entity = "{ \n" +
1861-
" \"backend_roles\" : [ ],\n" +
1862-
" \"hosts\" : [ ],\n" +
1863-
" \"users\" : [$usersStr]\n" +
1864-
"}"
1836+
var entity = """
1837+
{
1838+
"backend_roles": [],
1839+
"hosts": [],
1840+
"users": [$usersStr]
1841+
}
1842+
""".trimIndent()
18651843
request.setJsonEntity(entity)
18661844
client().performRequest(request)
18671845
}
@@ -1872,11 +1850,13 @@ abstract class AlertingRestTestCase : ODFERestTestCase() {
18721850

18731851
val op = if (addUser) "add" else "remove"
18741852

1875-
val entity = "[{\n" +
1876-
" \"op\" : \"$op\",\n" +
1877-
" \"path\" : \"/users\",\n" +
1878-
" \"value\" : [$usersStr]\n" +
1879-
"}]"
1853+
val entity = """
1854+
[{
1855+
"op": "$op",
1856+
"path": "/users",
1857+
"value": [$usersStr]
1858+
}]
1859+
""".trimIndent()
18801860

18811861
request.setJsonEntity(entity)
18821862
client().performRequest(request)

0 commit comments

Comments
 (0)