From 58f79fadb6000de6a9b101e680bd00b6dfc675c0 Mon Sep 17 00:00:00 2001 From: "opensearch-trigger-bot[bot]" <98922864+opensearch-trigger-bot[bot]@users.noreply.github.com> Date: Thu, 25 Apr 2024 14:30:15 -0700 Subject: [PATCH] Wrapped URI syntax exception in IllegalArgument exception. (#645) (#646) (cherry picked from commit 0aae343fddd9fe181a7936b8fb885d2a69866e75) Signed-off-by: AWSHurneyt Signed-off-by: github-actions[bot] Co-authored-by: github-actions[bot] --- .../alerting/model/ClusterMetricsInput.kt | 12 +++++-- .../model/ClusterMetricsInputTests.kt | 36 +++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/org/opensearch/commons/alerting/model/ClusterMetricsInput.kt b/src/main/kotlin/org/opensearch/commons/alerting/model/ClusterMetricsInput.kt index 061326f6..a11214e6 100644 --- a/src/main/kotlin/org/opensearch/commons/alerting/model/ClusterMetricsInput.kt +++ b/src/main/kotlin/org/opensearch/commons/alerting/model/ClusterMetricsInput.kt @@ -158,7 +158,11 @@ data class ClusterMetricsInput( return if (url.isEmpty()) { constructUrlFromInputs() } else { - URIBuilder(url).build() + try { + URIBuilder(url).build() + } catch (e: URISyntaxException) { + throw IllegalArgumentException("Invalid URL syntax.") + } } } @@ -243,7 +247,11 @@ data class ClusterMetricsInput( .setHost(SUPPORTED_HOST) .setPort(SUPPORTED_PORT) .setPath(path + pathParams) - uriBuilder.build() + try { + uriBuilder.build() + } catch (e: URISyntaxException) { + throw IllegalArgumentException("Invalid URL syntax.") + } } } diff --git a/src/test/kotlin/org/opensearch/commons/alerting/model/ClusterMetricsInputTests.kt b/src/test/kotlin/org/opensearch/commons/alerting/model/ClusterMetricsInputTests.kt index 0e739e7f..6d1c1055 100644 --- a/src/test/kotlin/org/opensearch/commons/alerting/model/ClusterMetricsInputTests.kt +++ b/src/test/kotlin/org/opensearch/commons/alerting/model/ClusterMetricsInputTests.kt @@ -555,4 +555,40 @@ class ClusterMetricsInputTests { ) } } + + @Test + fun `test url field contains invalid characters`() { + // GIVEN + path = "" + pathParams = "" + url = "http://localhost:9200/${ILLEGAL_PATH_PARAMETER_CHARACTERS.joinToString("")}" + + // WHEN + THEN + assertFailsWith("Invalid URL syntax.") { + ClusterMetricsInput( + path = path, + pathParams = pathParams, + url = url, + clusters = listOf() + ) + } + } + + @Test + fun `test URI fields provided and url contains invalid characters`() { + // GIVEN + path = "/_cluster/health" + pathParams = "index1,index2,index3,index4,index5" + url = "http://localhost:9200/${ILLEGAL_PATH_PARAMETER_CHARACTERS.joinToString("")}" + + // WHEN + THEN + assertFailsWith("Invalid URL syntax.") { + ClusterMetricsInput( + path = path, + pathParams = pathParams, + url = url, + clusters = listOf() + ) + } + } }