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

Change doc level query name validation #630

Merged
merged 6 commits into from
Apr 12, 2024

Conversation

jowg-amazon
Copy link
Collaborator

@jowg-amazon jowg-amazon commented Apr 5, 2024

Description

Changes the doc level query name validation to restrict the name from 0-256 characters, similar to the validation done on the front end

Name validation regex modified from https://github.com/opensearch-project/alerting/blob/d2a590e744c920820536efc5e1a88a4185eeed74/alerting/src/main/kotlin/org/opensearch/alerting/util/IndexUtils.kt#L29

Issues Resolved

[List any issues this PR will resolve]

Check List

  • New functionality includes testing.
    • All tests pass
  • New functionality has been documented.
    • New functionality has javadoc added
  • Commits are signed per the DCO using --signoff

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

Signed-off-by: jowg-amazon <jowg@amazon.com>
Signed-off-by: jowg-amazon <jowg@amazon.com>
Copy link

codecov bot commented Apr 5, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 73.91%. Comparing base (3f5421f) to head (150413f).
Report is 1 commits behind head on main.

❗ Current head 150413f differs from pull request most recent head 6904220. Consider uploading reports for the commit 6904220 to get more accurate results

Additional details and impacted files
@@             Coverage Diff              @@
##               main     #630      +/-   ##
============================================
- Coverage     73.91%   73.91%   -0.01%     
  Complexity      891      891              
============================================
  Files           133      133              
  Lines          5870     5873       +3     
  Branches        717      718       +1     
============================================
+ Hits           4339     4341       +2     
  Misses         1219     1219              
- Partials        312      313       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@@ -160,15 +161,20 @@ data class DocLevelQuery(
}

// TODO: add test for this
Copy link

Choose a reason for hiding this comment

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

Do you want to address this as part of this PR?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This PR modifies the existing test in DocLevelMonitorInputTests, should we add more testing for this? If not I will remove this comment.

fun `test create Doc Level Query with invalid name length`() {

Choose a reason for hiding this comment

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

If there is no test for the part of the code that the TODO was added for, then let's add one?

Copy link
Collaborator Author

@jowg-amazon jowg-amazon Apr 10, 2024

Choose a reason for hiding this comment

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

These two unit tests should be sufficient to test the two validation functions. I think the TODO may have been left in after the tests were created. These two tests create an invalid doc level query so that validation for a doc level query name and doc level query tag exceptions are caught.

  1. fun `test create Doc Level Query with invalid name length`() {
  2. fun `test create Doc Level Query with invalid characters for tags`() {

Will take out the TODO comment

@@ -80,6 +80,7 @@ data class DocLevelQuery(
const val QUERY_FIELD_NAMES_FIELD = "query_field_names"
const val NO_ID = ""
val INVALID_CHARACTERS: List<String> = listOf(" ", "[", "]", "{", "}", "(", ")")
val QUERY_NAME_REGEX = "^.{0,256}$".toRegex() // regex to restrict string length 256 chars
Copy link

Choose a reason for hiding this comment

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

Doubt: How did we decide to cap the length at 256?

Copy link
Collaborator

Choose a reason for hiding this comment

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

+1

Copy link
Collaborator

Choose a reason for hiding this comment

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

Another comment - this regex differs from the one in alerting: https://github.com/opensearch-project/alerting/pull/1506/files#diff-c8b7d067c22deb1581efc7a7286769d1c24d1a96146f8a4ecb92ef0dec16c44aR53

What's the reason for a difference?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I chose 256 as the cap based on security analytics front end validation for a rule name in hopes of remaining somewhat consistent:
https://github.com/opensearch-project/security-analytics-dashboards-plugin/blob/3e01e1631fa8aaff4e0968c68424a81abff03981/public/utils/validation.ts#L14

I had to change the length to 0-256 because when security analytics creates findings from bucket level monitors, it will create a doc level query with an empty name.
https://github.com/opensearch-project/security-analytics/blob/0507239054d238dac1e9cf53cfde488aeb4be1c2/src/main/java/org/opensearch/securityanalytics/findings/FindingsService.java#L302

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The regex is different from alerting and security analytics because when security analytics converts the prepackaged sigma rules into doc level queries, there are some existing prepackaged rules that have some of the special characters like ( so we can't include all the validation at the doc level query level.

engechas
engechas previously approved these changes Apr 5, 2024
Copy link
Collaborator

@engechas engechas left a comment

Choose a reason for hiding this comment

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

Curious on the 256 length limit as well but seems fine to me regardless so approving

repeat(256) {
stringBuilder.append("a")
}
val badString = stringBuilder.toString() + "b".repeat(256)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Minor: Looks like this is testing a string of length 512. Would be better to test at the limit (257) imo

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

changed testing to 257 chars

@@ -80,6 +80,7 @@ data class DocLevelQuery(
const val QUERY_FIELD_NAMES_FIELD = "query_field_names"
const val NO_ID = ""
val INVALID_CHARACTERS: List<String> = listOf(" ", "[", "]", "{", "}", "(", ")")
val QUERY_NAME_REGEX = "^.{0,256}$".toRegex() // regex to restrict string length 256 chars
Copy link
Collaborator

Choose a reason for hiding this comment

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

+1

Signed-off-by: jowg-amazon <jowg@amazon.com>
engechas
engechas previously approved these changes Apr 10, 2024
Signed-off-by: jowg-amazon <jowg@amazon.com>
engechas
engechas previously approved these changes Apr 10, 2024
Signed-off-by: jowg-amazon <jowg@amazon.com>
Assertions.fail("Expecting an illegal argument exception")
} catch (e: IllegalArgumentException) {
Assertions.assertEquals(
"The query name, $emptyString, can be max 256 characters.",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Minor: the error message should say between 1-256 characters

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

Signed-off-by: jowg-amazon <jowg@amazon.com>
@jowg-amazon jowg-amazon merged commit 9beae87 into opensearch-project:main Apr 12, 2024
9 checks passed
opensearch-trigger-bot bot pushed a commit that referenced this pull request Apr 12, 2024
* change validation and tests

Signed-off-by: jowg-amazon <jowg@amazon.com>

* change regex to 0-256 chars

Signed-off-by: jowg-amazon <jowg@amazon.com>

* moved validation to common utils and fix test

Signed-off-by: jowg-amazon <jowg@amazon.com>

* remove TODO

Signed-off-by: jowg-amazon <jowg@amazon.com>

* ensure name is at lest 1 char

Signed-off-by: jowg-amazon <jowg@amazon.com>

* change error message

Signed-off-by: jowg-amazon <jowg@amazon.com>

---------

Signed-off-by: jowg-amazon <jowg@amazon.com>
(cherry picked from commit 9beae87)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
opensearch-trigger-bot bot pushed a commit that referenced this pull request Apr 12, 2024
* change validation and tests

Signed-off-by: jowg-amazon <jowg@amazon.com>

* change regex to 0-256 chars

Signed-off-by: jowg-amazon <jowg@amazon.com>

* moved validation to common utils and fix test

Signed-off-by: jowg-amazon <jowg@amazon.com>

* remove TODO

Signed-off-by: jowg-amazon <jowg@amazon.com>

* ensure name is at lest 1 char

Signed-off-by: jowg-amazon <jowg@amazon.com>

* change error message

Signed-off-by: jowg-amazon <jowg@amazon.com>

---------

Signed-off-by: jowg-amazon <jowg@amazon.com>
(cherry picked from commit 9beae87)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
jowg-amazon pushed a commit that referenced this pull request Apr 13, 2024
* change validation and tests



* change regex to 0-256 chars



* moved validation to common utils and fix test



* remove TODO



* ensure name is at lest 1 char



* change error message



---------


(cherry picked from commit 9beae87)

Signed-off-by: jowg-amazon <jowg@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>
jowg-amazon pushed a commit that referenced this pull request Apr 13, 2024
* change validation and tests



* change regex to 0-256 chars



* moved validation to common utils and fix test



* remove TODO



* ensure name is at lest 1 char



* change error message



---------


(cherry picked from commit 9beae87)

Signed-off-by: jowg-amazon <jowg@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>
opensearch-trigger-bot bot pushed a commit that referenced this pull request Apr 19, 2024
* change validation and tests

Signed-off-by: jowg-amazon <jowg@amazon.com>

* change regex to 0-256 chars

Signed-off-by: jowg-amazon <jowg@amazon.com>

* moved validation to common utils and fix test

Signed-off-by: jowg-amazon <jowg@amazon.com>

* remove TODO

Signed-off-by: jowg-amazon <jowg@amazon.com>

* ensure name is at lest 1 char

Signed-off-by: jowg-amazon <jowg@amazon.com>

* change error message

Signed-off-by: jowg-amazon <jowg@amazon.com>

---------

Signed-off-by: jowg-amazon <jowg@amazon.com>
(cherry picked from commit 9beae87)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants