Skip to content

Commit

Permalink
Backport for 1373 to 2.x (#1416)
Browse files Browse the repository at this point in the history
  • Loading branch information
skumarp7 authored Jul 29, 2024
1 parent 15ce58e commit 3c1cbdd
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
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

0 comments on commit 3c1cbdd

Please sign in to comment.