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

[Backport 2.x] Added validations for indexPattern in autofollow API #1416

Merged
Merged
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 @@ -14,6 +14,7 @@ package org.opensearch.replication.action.autofollow
import org.opensearch.replication.action.index.ReplicateIndexRequest
import org.opensearch.replication.metadata.store.KEY_SETTINGS
import org.opensearch.replication.util.ValidationUtil.validateName
import org.opensearch.replication.util.ValidationUtil.validatePattern
import org.opensearch.action.ActionRequestValidationException
import org.opensearch.action.support.master.AcknowledgedRequest
import org.opensearch.core.ParseField
Expand Down Expand Up @@ -113,8 +114,11 @@ class UpdateAutoFollowPatternRequest: AcknowledgedRequest<UpdateAutoFollowPatter
if(pattern != null) {
validationException.addValidationError("Unexpected pattern")
}
} else if(pattern == null) {
validationException.addValidationError("Missing pattern")
} else {
if(pattern == null)
validationException.addValidationError("Missing pattern")
else
validatePattern(pattern, validationException)
}

return if(validationException.validationErrors().isEmpty()) return null else validationException
Expand Down
20 changes: 20 additions & 0 deletions src/main/kotlin/org/opensearch/replication/util/ValidationUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,26 @@ object ValidationUtil {
validationException.addValidationError("Value $name must not start with '.'")
}

/**
* Validate the pattern against the rules that we have for indexPattern name.
*/

fun validatePattern(pattern: String?, validationException: ValidationException) {

if (!Strings.validFileNameExcludingAstrix(pattern))
validationException.addValidationError("Autofollow pattern: $pattern must not contain the following characters ${Strings.INVALID_FILENAME_CHARS}")

if (pattern.isNullOrEmpty() == true)
validationException.addValidationError("Autofollow pattern: $pattern must not be empty")

if ((pattern?.contains("#") ?: false)|| (pattern?.contains(":") ?: false))
validationException.addValidationError("Autofollow pattern: $pattern must not contain '#' or ':'")

if ((pattern?.startsWith('_') ?: false) || (pattern?.startsWith('-') ?: false))
validationException.addValidationError("Autofollow pattern: $pattern must not start with '_' or '-'")

}

/**
* validate leader index version for compatibility
* If on higher version - Replication will not be allowed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,51 @@ class UpdateAutoFollowPatternIT: MultiClusterRestTestCase() {
return getReplicationTaskList(clusterName, IndexReplicationExecutor.TASK_NAME + "*")
}

fun `test auto follow should fail on indexPattern validation failure`() {
val followerClient = getClientForCluster(FOLLOWER)
createConnectionBetweenClusters(FOLLOWER, LEADER, connectionAlias)
assertPatternValidation(followerClient, "testPattern,",
"Autofollow pattern: testPattern, must not contain the following characters")
assertPatternValidation(followerClient, "testPat?",
"Autofollow pattern: testPat? must not contain the following characters")
assertPatternValidation(followerClient, "test#",
"Autofollow pattern: test# must not contain '#' or ':'")
assertPatternValidation(followerClient, "test:",
"Autofollow pattern: test: must not contain '#' or ':'")
assertPatternValidation(followerClient, "_test",
"Autofollow pattern: _test must not start with '_' or '-'")
assertPatternValidation(followerClient, "-leader",
"Autofollow pattern: -leader must not start with '_' or '-'")
assertPatternValidation(followerClient, "",
"Autofollow pattern: must not be empty")

}
private fun assertPatternValidation(followerClient: RestHighLevelClient, pattern: String,
errorMsg: String) {
Assertions.assertThatThrownBy {
followerClient.updateAutoFollowPattern(connectionAlias, indexPatternName, pattern)
}.isInstanceOf(ResponseException::class.java)
.hasMessageContaining(errorMsg)
}

fun `test auto follow should succeed on valid indexPatterns`() {
val followerClient = getClientForCluster(FOLLOWER)
createConnectionBetweenClusters(FOLLOWER, LEADER, connectionAlias)
assertValidPatternValidation(followerClient, "test-leader")
assertValidPatternValidation(followerClient, "test*")
assertValidPatternValidation(followerClient, "leader-*")
assertValidPatternValidation(followerClient, "leader_test")
assertValidPatternValidation(followerClient, "Leader_Test-*")
assertValidPatternValidation(followerClient, "Leader_*")

}

private fun assertValidPatternValidation(followerClient: RestHighLevelClient, pattern: String) {
Assertions.assertThatCode {
followerClient.updateAutoFollowPattern(connectionAlias, indexPatternName, pattern)
}.doesNotThrowAnyException()
}

fun createDummyConnection(fromClusterName: String, connectionName: String="source") {
val fromCluster = getNamedCluster(fromClusterName)
val persistentConnectionRequest = Request("PUT", "_cluster/settings")
Expand Down
Loading