Skip to content

Commit

Permalink
change validation message and move validation loc
Browse files Browse the repository at this point in the history
Signed-off-by: Joanne Wang <[email protected]>
  • Loading branch information
jowg-amazon committed Apr 8, 2024
1 parent cd50847 commit b490cf7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import org.opensearch.commons.alerting.model.DocumentLevelTrigger
import org.opensearch.commons.alerting.model.Monitor
import org.opensearch.commons.alerting.model.QueryLevelTrigger
import org.opensearch.commons.alerting.model.ScheduledJob
import org.opensearch.commons.utils.getInvalidNameChars
import org.opensearch.commons.utils.isValidName
import org.opensearch.core.rest.RestStatus
import org.opensearch.core.xcontent.ToXContent
import org.opensearch.core.xcontent.XContentParser.Token
Expand All @@ -47,11 +49,6 @@ private val log = LogManager.getLogger(RestIndexMonitorAction::class.java)
*/
class RestIndexMonitorAction : BaseRestHandler() {

// allowed characters [- : , ( ) [ ] ' _]
private val allowedChars = "-:,\\(\\)\\[\\]\'_"
// regex to restrict string to alphanumeric and allowed chars, must be between 0 - 256 characters
val regex = "[\\w\\s$allowedChars]{0,256}"

override fun getName(): String {
return "index_monitor_action"
}
Expand Down Expand Up @@ -122,8 +119,8 @@ class RestIndexMonitorAction : BaseRestHandler() {
if (it !is DocumentLevelTrigger) {
throw IllegalArgumentException("Illegal trigger type, ${it.javaClass.name}, for document level monitor")
}
validateDocLevelQueryName(monitor)
}
validateDocLevelQueryName(monitor)
}
}

Expand All @@ -144,10 +141,10 @@ class RestIndexMonitorAction : BaseRestHandler() {
private fun validateDocLevelQueryName(monitor: Monitor) {
monitor.inputs.filterIsInstance<DocLevelMonitorInput>().forEach { docLevelMonitorInput ->
docLevelMonitorInput.queries.forEach { dlq ->
if (!dlq.name.matches(Regex(regex))) {
if (!isValidName(dlq.name)) {
throw IllegalArgumentException(
"Doc level query name, ${dlq.name}, may only contain alphanumeric values and " +
"these special characters: ${allowedChars.replace("\\","")}"
"Doc level query name may not start with [_, +, -], contain '..', or contain: " +
getInvalidNameChars().replace("\\", "")
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import org.opensearch.commons.alerting.model.Monitor
import org.opensearch.commons.alerting.model.QueryLevelTrigger
import org.opensearch.commons.alerting.model.ScheduledJob
import org.opensearch.commons.alerting.model.SearchInput
import org.opensearch.commons.utils.getInvalidNameChars
import org.opensearch.core.common.bytes.BytesReference
import org.opensearch.core.rest.RestStatus
import org.opensearch.core.xcontent.ToXContent
Expand Down Expand Up @@ -1289,7 +1290,7 @@ class MonitorRestApiIT : AlertingRestTestCase() {

fun `test creating and updating a document monitor with invalid query name`() {
// creating a monitor with an invalid query name
val invalidQueryName = "Invalid @ query ! name"
val invalidQueryName = "_Invalid .. query ! n>ame"
val queries = listOf(randomDocLevelQuery(name = invalidQueryName))
val randomDocLevelMonitorInput = randomDocLevelMonitorInput(queries = queries)
val inputs = listOf(randomDocLevelMonitorInput)
Expand All @@ -1301,13 +1302,14 @@ class MonitorRestApiIT : AlertingRestTestCase() {
fail("Doc level monitor with invalid query name should be rejected")
} catch (e: ResponseException) {
assertEquals("Unexpected status", RestStatus.BAD_REQUEST, e.response.restStatus())
val expectedMessage = "Doc level query name, $invalidQueryName, may only contain alphanumeric values"
val expectedMessage = "Doc level query name may not start with [_, +, -], contain '..', or contain: " +
getInvalidNameChars().replace("\\", "")
e.message?.let { assertTrue(it.contains(expectedMessage)) }
}

// successfully creating monitor with valid query name
val testIndex = createTestIndex()
val docQuery = DocLevelQuery(query = "test_field:\"us-west-2\"", name = "valid name", fields = listOf())
val docQuery = DocLevelQuery(query = "test_field:\"us-west-2\"", name = "valid (name)", fields = listOf())
val docLevelInput = DocLevelMonitorInput("description", listOf(testIndex), listOf(docQuery))

monitor = createMonitor(randomDocumentLevelMonitor(inputs = listOf(docLevelInput), triggers = listOf(trigger)))
Expand All @@ -1324,7 +1326,8 @@ class MonitorRestApiIT : AlertingRestTestCase() {
fail("Doc level monitor with invalid query name should be rejected")
} catch (e: ResponseException) {
assertEquals("Unexpected status", RestStatus.BAD_REQUEST, e.response.restStatus())
val expectedMessage = "Doc level query name, $invalidQueryName, may only contain alphanumeric values"
val expectedMessage = "Doc level query name may not start with [_, +, -], contain '..', or contain: " +
getInvalidNameChars().replace("\\", "")
e.message?.let { assertTrue(it.contains(expectedMessage)) }
}
}
Expand Down

0 comments on commit b490cf7

Please sign in to comment.