-
Notifications
You must be signed in to change notification settings - Fork 506
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add name validation to task definitions and workflow definitions (#315)
- Loading branch information
1 parent
8fb219f
commit 4ae5b18
Showing
15 changed files
with
151 additions
and
12 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
common/src/main/java/com/netflix/conductor/common/constraints/ValidNameConstraint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright 2020 Conductor Authors. | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* <p> | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
package com.netflix.conductor.common.constraints; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
|
||
import jakarta.validation.Constraint; | ||
import jakarta.validation.ConstraintValidator; | ||
import jakarta.validation.ConstraintValidatorContext; | ||
import jakarta.validation.Payload; | ||
|
||
import static java.lang.annotation.ElementType.FIELD; | ||
|
||
/** | ||
* This constraint class validates following things. | ||
* | ||
* <ul> | ||
* <li>1. Name is valid or not | ||
* </ul> | ||
*/ | ||
@Documented | ||
@Constraint(validatedBy = ValidNameConstraint.NameValidator.class) | ||
@Target({FIELD}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface ValidNameConstraint { | ||
|
||
String message() default ""; | ||
|
||
Class<?>[] groups() default {}; | ||
|
||
Class<? extends Payload>[] payload() default {}; | ||
|
||
class NameValidator implements ConstraintValidator<ValidNameConstraint, String> { | ||
|
||
private static final String NAME_PATTERN = "^[A-Za-z0-9_<>{}#\\s-]+$"; | ||
public static final String INVALID_NAME_MESSAGE = | ||
"Allowed characters are alphanumeric, underscores, spaces, hyphens, and special characters like <, >, {, }, #"; | ||
|
||
@Value("${conductor.app.workflow.name-validation.enabled}") | ||
private boolean nameValidationEnabled; | ||
|
||
@Override | ||
public void initialize(ValidNameConstraint constraintAnnotation) {} | ||
|
||
@Override | ||
public boolean isValid(String name, ConstraintValidatorContext context) { | ||
boolean valid = name == null || !nameValidationEnabled || name.matches(NAME_PATTERN); | ||
if (!valid) { | ||
context.disableDefaultConstraintViolation(); | ||
context.buildConstraintViolationWithTemplate( | ||
"Invalid name '" + name + "'. " + INVALID_NAME_MESSAGE) | ||
.addConstraintViolation(); | ||
} | ||
return valid; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
common/src/test/java/com/netflix/conductor/common/constraints/NameValidatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright 2024 Conductor Authors. | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* <p> | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
package com.netflix.conductor.common.constraints; | ||
|
||
import org.junit.Test; | ||
import org.springframework.test.util.ReflectionTestUtils; | ||
|
||
import jakarta.validation.ConstraintValidatorContext; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class NameValidatorTest { | ||
@Test | ||
public void nameWithAllowedCharactersIsValid() { | ||
ValidNameConstraint.NameValidator nameValidator = new ValidNameConstraint.NameValidator(); | ||
assertTrue(nameValidator.isValid("workflowDef", null)); | ||
} | ||
|
||
@Test | ||
public void nonAllowedCharactersInNameIsInvalid() { | ||
ValidNameConstraint.NameValidator nameValidator = new ValidNameConstraint.NameValidator(); | ||
ConstraintValidatorContext context = mock(ConstraintValidatorContext.class); | ||
ConstraintValidatorContext.ConstraintViolationBuilder builder = | ||
mock(ConstraintValidatorContext.ConstraintViolationBuilder.class); | ||
when(context.buildConstraintViolationWithTemplate(anyString())).thenReturn(builder); | ||
|
||
ReflectionTestUtils.setField(nameValidator, "nameValidationEnabled", true); | ||
|
||
assertFalse(nameValidator.isValid("workflowDef@", context)); | ||
} | ||
|
||
// Null should be tested by @NotEmpty or @NotNull | ||
@Test | ||
public void nullIsValid() { | ||
ValidNameConstraint.NameValidator nameValidator = new ValidNameConstraint.NameValidator(); | ||
assertTrue(nameValidator.isValid(null, null)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
conductor.app.workflow.name-validation.enabled=true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters