-
Notifications
You must be signed in to change notification settings - Fork 93
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
[Backport 2.x] Added validation for the new clusters field. (#633) #636
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.commons.alerting.util | ||
|
||
import org.apache.logging.log4j.LogManager | ||
import org.opensearch.OpenSearchException | ||
import org.opensearch.OpenSearchSecurityException | ||
import org.opensearch.OpenSearchStatusException | ||
import org.opensearch.core.common.Strings | ||
import org.opensearch.core.rest.RestStatus | ||
import org.opensearch.index.IndexNotFoundException | ||
import org.opensearch.index.engine.VersionConflictEngineException | ||
import org.opensearch.indices.InvalidIndexNameException | ||
|
||
private val log = LogManager.getLogger(CommonUtilsException::class.java) | ||
|
||
class CommonUtilsException(message: String, val status: RestStatus, ex: Exception) : OpenSearchException(message, ex) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what benefit are we getting from removing the delegation of execption handling to the plugin? there is no business logic in common utils There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed |
||
|
||
override fun status(): RestStatus { | ||
return status | ||
} | ||
|
||
companion object { | ||
|
||
@JvmStatic | ||
fun wrap(ex: Exception): OpenSearchException { | ||
log.error("Common utils error: $ex") | ||
|
||
var friendlyMsg = "Unknown error" | ||
var status = RestStatus.INTERNAL_SERVER_ERROR | ||
when (ex) { | ||
is IndexNotFoundException -> { | ||
status = ex.status() | ||
friendlyMsg = "Configured indices are not found: ${ex.index}" | ||
} | ||
is OpenSearchSecurityException -> { | ||
status = ex.status() | ||
friendlyMsg = "User doesn't have permissions to execute this action. Contact administrator." | ||
} | ||
is OpenSearchStatusException -> { | ||
status = ex.status() | ||
friendlyMsg = ex.message as String | ||
} | ||
is IllegalArgumentException -> { | ||
status = RestStatus.BAD_REQUEST | ||
friendlyMsg = ex.message as String | ||
} | ||
is VersionConflictEngineException -> { | ||
status = ex.status() | ||
friendlyMsg = ex.message as String | ||
} | ||
is InvalidIndexNameException -> { | ||
status = RestStatus.BAD_REQUEST | ||
friendlyMsg = ex.message as String | ||
} | ||
else -> { | ||
if (!Strings.isNullOrEmpty(ex.message)) { | ||
friendlyMsg = ex.message as String | ||
} | ||
} | ||
} | ||
// Wrapping the origin exception as runtime to avoid it being formatted. | ||
// Currently, alerting-kibana is using `error.root_cause.reason` as text in the toast message. | ||
// Below logic is to set friendly message to error.root_cause.reason. | ||
return CommonUtilsException(friendlyMsg, status, Exception("${ex.javaClass.name}: ${ex.message}")) | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,24 @@ import java.time.Instant | |
|
||
class IndexUtils { | ||
companion object { | ||
/** | ||
* This regex asserts that the string: | ||
* The index does not start with an underscore _, hyphen -, or plus sign + | ||
* The index does not contain two consecutive periods (e.g., `..`) | ||
* The index does not contain any whitespace characters, commas, backslashes, forward slashes, asterisks, | ||
* question marks, double quotes, less than or greater than signs, pipes, colons, or periods. | ||
* The length of the index must be between 1 and 255 characters | ||
*/ | ||
val VALID_INDEX_NAME_REGEX = Regex("""^(?![_\-\+])(?!.*\.\.)[^\s,\\\/\*\?"<>|#:\.]{1,255}$""") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why aren't we using opensearch core's index name validation? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was just moved to common utils from alerting; it wasn't implemented as part of these changes. Core didn't have a regex for index names. This comment explains where the validation came from https://github.com/opensearch-project/alerting/pull/992/files#r1259175796 |
||
|
||
/** | ||
* This regex asserts that the string: | ||
* The index pattern can start with an optional period | ||
* The index pattern can contain lowercase letters, digits, underscores, hyphens, asterisks, and periods | ||
* The length of the index pattern must be between 1 and 255 characters | ||
*/ | ||
val INDEX_PATTERN_REGEX = Regex("""^(?=.{1,255}$)\.?[a-z0-9_\-\*\.]+$""") | ||
|
||
const val NO_SCHEMA_VERSION = 0 | ||
|
||
const val MONITOR_MAX_INPUTS = 1 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,24 @@ package org.opensearch.commons.utils | |
import java.net.URL | ||
import java.util.regex.Pattern | ||
|
||
/** | ||
* This regex asserts that the string: | ||
* Starts with a lowercase letter, or digit | ||
* Contains a sequence of characters followed by an optional colon and another sequence of characters | ||
* The sequences of characters can include lowercase letters, uppercase letters, digits, underscores, or hyphens | ||
* The total length of the string can range from 1 to 255 characters | ||
*/ | ||
val CLUSTER_NAME_REGEX = Regex("^(?=.{1,255}$)[a-z0-9]([a-zA-Z0-9_-]*:?[a-zA-Z0-9_-]*)$") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why are we not re-using cross cluster validation from core? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Discussed this in this comment |
||
|
||
/** | ||
* This regex asserts that the string: | ||
* Starts with a lowercase letter, digit, or asterisk | ||
* Contains a sequence of characters followed by an optional colon and another sequence of characters | ||
* The sequences of characters can include lowercase letters, uppercase letters, digits, underscores, asterisks, or hyphens | ||
* The total length of the string can range from 1 to 255 characters | ||
*/ | ||
val CLUSTER_PATTERN_REGEX = Regex("^(?=.{1,255}$)[a-z0-9*]([a-zA-Z0-9_*-]*:?[a-zA-Z0-9_*-]*)$") | ||
|
||
// Valid ID characters = (All Base64 chars + "_-") to support UUID format and Base64 encoded IDs | ||
private val VALID_ID_CHARS: Set<Char> = (('a'..'z') + ('A'..'Z') + ('0'..'9') + '+' + '/' + '_' + '-').toSet() | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
plz add error log
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why are we creating a common utils exception?
The plugin using common utils can handle the exception and wrap it as necessary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed CommonUtilsException in PR #639. Will cherry-pick that commit once that PR is merged.