Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,10 @@ data class ClusterMetricsInput(
}

if (apiType.isNotEmpty() && path.isNotEmpty()) {
val formattedPath = path.trim('/')
val derivedType = ClusterMetricType.values()
.filter { it != ClusterMetricType.BLANK }
.find { path.startsWith(it.prependPath) || path.startsWith(it.defaultPath) }
.find { formattedPath.startsWith(it.prependPath.trim('/')) || formattedPath.startsWith(it.defaultPath.trim('/')) }

require(derivedType != null && derivedType.name == apiType) {
"The provided api_type [$apiType] does not match the path [$path]."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -630,4 +630,84 @@ class ClusterMetricsInputTests {
val input = ClusterMetricsInput.parseInner(xcp)
assertEquals(ClusterMetricsInput.ClusterMetricType.CAT_INDICES, input.clusterMetricType)
}

@Test
fun `test parseInner accepts matching api_type and path without leading slash`() {
val inputJson = """
{"uri":{"api_type":"CAT_INDICES","path":"_cat/indices","path_params":"","url":"","clusters":[]}}
""".trimIndent()
val xcp = org.opensearch.common.xcontent.json.JsonXContent.jsonXContent
.createParser(
org.opensearch.core.xcontent.NamedXContentRegistry.EMPTY,
org.opensearch.core.xcontent.DeprecationHandler.THROW_UNSUPPORTED_OPERATION,
inputJson
)
xcp.nextToken()
xcp.nextToken()
xcp.nextToken()

val input = ClusterMetricsInput.parseInner(xcp)
assertEquals(ClusterMetricsInput.ClusterMetricType.CAT_INDICES, input.clusterMetricType)
}

@Test
fun `test parseInner rejects mismatched api_type and path without leading slash`() {
val inputJson = """
{"uri":{"api_type":"CLUSTER_STATS","path":"_cat/indices","path_params":"","url":"","clusters":[]}}
""".trimIndent()
val xcp = org.opensearch.common.xcontent.json.JsonXContent.jsonXContent
.createParser(
org.opensearch.core.xcontent.NamedXContentRegistry.EMPTY,
org.opensearch.core.xcontent.DeprecationHandler.THROW_UNSUPPORTED_OPERATION,
inputJson
)
xcp.nextToken()
xcp.nextToken()
xcp.nextToken()

assertFailsWith<IllegalArgumentException>("The provided api_type") {
ClusterMetricsInput.parseInner(xcp)
}
}

@Test
fun `test parseInner accepts matching api_type with path containing trailing slash`() {
val inputJson = """
{"uri":{"api_type":"CLUSTER_HEALTH","path":"/_cluster/health/","path_params":"","url":"","clusters":[]}}
""".trimIndent()
val xcp = org.opensearch.common.xcontent.json.JsonXContent.jsonXContent
.createParser(
org.opensearch.core.xcontent.NamedXContentRegistry.EMPTY,
org.opensearch.core.xcontent.DeprecationHandler.THROW_UNSUPPORTED_OPERATION,
inputJson
)
xcp.nextToken()
xcp.nextToken()
xcp.nextToken()

val input = ClusterMetricsInput.parseInner(xcp)
assertEquals(ClusterMetricsInput.ClusterMetricType.CLUSTER_HEALTH, input.clusterMetricType)
}

@Test
fun `test parseInner rejects slash-only path with api_type`() {
// path "/" enters validation since path.isNotEmpty(),
// but trimmed path doesn't match any ClusterMetricType
val inputJson = """
{"uri":{"api_type":"CAT_INDICES","path":"/","path_params":"","url":"http://localhost:9200/_cluster/health","clusters":[]}}
""".trimIndent()
val xcp = org.opensearch.common.xcontent.json.JsonXContent.jsonXContent
.createParser(
org.opensearch.core.xcontent.NamedXContentRegistry.EMPTY,
org.opensearch.core.xcontent.DeprecationHandler.THROW_UNSUPPORTED_OPERATION,
inputJson
)
xcp.nextToken()
xcp.nextToken()
xcp.nextToken()

assertFailsWith<IllegalArgumentException>("The provided api_type") {
ClusterMetricsInput.parseInner(xcp)
}
}
}
Loading