Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Input Validation for Monitor Fields #771

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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 @@ -75,14 +75,22 @@ fun validateIamRoleArn(roleArn: String) {
require(roleArnRegex.matcher(roleArn).find()) { "Invalid AWS role ARN: $roleArn " }
}

fun isValidName(name: String): Boolean {
fun isValidQueryName(name: String): Boolean {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a short doc comment that explains this function validates the doc level monitor DocLevelQuery name specifically? We use the term "query" in many places, so a quick comment can help prevent confusion.
https://github.com/opensearch-project/common-utils/blob/main/src/main/kotlin/org/opensearch/commons/alerting/model/DocLevelQuery.kt#L16

// Regex to restrict string so that it cannot start with [_, -, +],
// contain two consecutive periods or contain invalid chars
val regex = Regex("""^(?![_\-\+])(?!.*\.\.)[^$INVALID_NAME_CHARS]+$""")

return name.matches(regex)
}

fun isValidName(name: String): Boolean {
// Start with letter or underscore
// Followed by letters, numbers, underscore or hyphen
// Total length between 4 and 50 characters
val regex = Regex("^[a-zA-Z_][a-zA-Z0-9_-]{3,49}$")
return name.matches(regex)
}

fun getInvalidNameChars(): String {
return INVALID_NAME_CHARS
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.commons.utils

import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test

class ValidationHelpers {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: Could you rename the test suite and it file to something like ValiationHelpersTests? That will follow the naming pattern we've used for other unit test suites.
https://github.com/opensearch-project/common-utils/blob/main/src/test/kotlin/org/opensearch/commons/alerting/MonitorTests.kt

@Test
fun `test valid names`() {
/* Start with letter or underscore
Followed by letters, numbers, underscore or hyphen
Total length between 4 and 50 characters */

assertTrue(isValidName("valid"))
assertTrue(isValidName("Valid_name"))
assertTrue(isValidName("valid_name_123"))
assertTrue(isValidName("_validName"))
assertTrue(isValidName("valid_name-123"))
assertTrue(isValidName("_123_valid_name"))

// Boundary value tests
assertTrue(isValidName("qwer"))
assertTrue(isValidName("q".repeat(50)))
assertTrue(isValidName("____"))
assertTrue(isValidName("a-b-"))
}

@Test
fun `test invalid names`() {
// Invalid starting characters
assertFalse(isValidName("123name"))
assertFalse(isValidName("-name"))
assertFalse(isValidName("1name"))

// Should not have invalid characters
assertFalse(isValidName("invalid@name"))
assertFalse(isValidName("invalid name"))
assertFalse(isValidName("invalid#name"))
assertFalse(isValidName("invalid.name"))
assertFalse(isValidName(" "))

// Should not allow special characters other than _ and -
assertFalse(isValidName("name!"))
assertFalse(isValidName("name*"))
assertFalse(isValidName("name$"))
assertFalse(isValidName("name%"))

// Boundary value tests
assertFalse(isValidName("abc"))
assertFalse(isValidName("a".repeat(51)))
}
}
Loading