Skip to content

Commit 08b51de

Browse files
opensearch-trigger-bot[bot]github-actions[bot]Manaswini Ragamouni
authored
Validate api_type matches path in ClusterMetricsInput (#912) (#914)
When creating a cluster metrics monitor, the api_type field was silently ignored during deserialization. This allowed creating monitors with mismatched api_type and path fields, which then could not be deleted via the DeleteMonitor API. Added validation in parseInner to reject requests where the provided api_type does not match the API derived from the path. Resolves opensearch-project/alerting#1987 (cherry picked from commit 5103490) Signed-off-by: Manaswini Ragamouni <ragamanu@amazon.com> Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: Manaswini Ragamouni <ragamanu@amazon.com>
1 parent 5df25f5 commit 08b51de

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

src/main/kotlin/org/opensearch/commons/alerting/model/ClusterMetricsInput.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ data class ClusterMetricsInput(
123123
var path = ""
124124
var pathParams = ""
125125
var url = ""
126+
var apiType = ""
126127
val clusters = mutableListOf<String>()
127128

128129
XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, xcp.currentToken(), xcp)
@@ -131,6 +132,7 @@ data class ClusterMetricsInput(
131132
val fieldName = xcp.currentName()
132133
xcp.nextToken()
133134
when (fieldName) {
135+
API_TYPE_FIELD -> apiType = xcp.text()
134136
PATH_FIELD -> path = xcp.text()
135137
PATH_PARAMS_FIELD -> pathParams = xcp.text()
136138
URL_FIELD -> url = xcp.text()
@@ -144,6 +146,17 @@ data class ClusterMetricsInput(
144146
}
145147
}
146148
}
149+
150+
if (apiType.isNotEmpty() && path.isNotEmpty()) {
151+
val derivedType = ClusterMetricType.values()
152+
.filter { it != ClusterMetricType.BLANK }
153+
.find { path.startsWith(it.prependPath) || path.startsWith(it.defaultPath) }
154+
155+
require(derivedType != null && derivedType.name == apiType) {
156+
"The provided api_type [$apiType] does not match the path [$path]."
157+
}
158+
}
159+
147160
return ClusterMetricsInput(path, pathParams, url, clusters)
148161
}
149162
}

src/test/kotlin/org/opensearch/commons/alerting/model/ClusterMetricsInputTests.kt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,4 +591,43 @@ class ClusterMetricsInputTests {
591591
)
592592
}
593593
}
594+
595+
@Test
596+
fun `test parseInner rejects mismatched api_type and path`() {
597+
val inputJson = """
598+
{"uri":{"api_type":"CLUSTER_STATS","path":"/_cat/indices","path_params":"","url":"","clusters":[]}}
599+
""".trimIndent()
600+
val xcp = org.opensearch.common.xcontent.json.JsonXContent.jsonXContent
601+
.createParser(
602+
org.opensearch.core.xcontent.NamedXContentRegistry.EMPTY,
603+
org.opensearch.core.xcontent.DeprecationHandler.THROW_UNSUPPORTED_OPERATION,
604+
inputJson
605+
)
606+
xcp.nextToken() // START_OBJECT (root)
607+
xcp.nextToken() // FIELD_NAME "uri"
608+
xcp.nextToken() // START_OBJECT (uri)
609+
610+
assertFailsWith<IllegalArgumentException>("The provided api_type") {
611+
ClusterMetricsInput.parseInner(xcp)
612+
}
613+
}
614+
615+
@Test
616+
fun `test parseInner accepts matching api_type and path`() {
617+
val inputJson = """
618+
{"uri":{"api_type":"CAT_INDICES","path":"/_cat/indices","path_params":"","url":"","clusters":[]}}
619+
""".trimIndent()
620+
val xcp = org.opensearch.common.xcontent.json.JsonXContent.jsonXContent
621+
.createParser(
622+
org.opensearch.core.xcontent.NamedXContentRegistry.EMPTY,
623+
org.opensearch.core.xcontent.DeprecationHandler.THROW_UNSUPPORTED_OPERATION,
624+
inputJson
625+
)
626+
xcp.nextToken()
627+
xcp.nextToken()
628+
xcp.nextToken()
629+
630+
val input = ClusterMetricsInput.parseInner(xcp)
631+
assertEquals(ClusterMetricsInput.ClusterMetricType.CAT_INDICES, input.clusterMetricType)
632+
}
594633
}

0 commit comments

Comments
 (0)