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

WFCORE-7065 Migrate JBoss Modules module identifier validator from org.wildfly:wildfly-clustering-common #6257

Merged
merged 1 commit into from
Nov 26, 2024
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 @@ -3802,4 +3802,7 @@ OperationFailedRuntimeException capabilityAlreadyRegisteredInContext(String capa
@Message(id = 515, value = "The system property '%s' is deprecated and may be removed in a future version, " +
"attribute '%s' on resource '%s' should be used instead.")
void systemPropertyDeprecated(String systemProperty, String attribute, String address);

@Message(id = 516, value = "Parameter %s specifies an invalid module name: %s")
OperationFailedException invalidModuleNameParameter(String parameterName, String moduleName);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright The WildFly Authors
* SPDX-License-Identifier: Apache-2.0
*/

package org.jboss.as.controller.operations.validation;

import java.util.function.Predicate;
import java.util.regex.Pattern;

import org.jboss.as.controller.OperationFailedException;
import org.jboss.as.controller.logging.ControllerLogger;
import org.jboss.dmr.ModelNode;
import org.jboss.dmr.ModelType;

/**
* Validates that a given parameter is a syntactically valid module name within JBoss Modules.
* N.B. This does not validate that the module actually exists, i.e. can be loaded.
*/
public class ModuleNameValidator extends ModelTypeValidator {
public static final ParameterValidator INSTANCE = new ModuleNameValidator();
// Ensure module name is valid with filesystem module repository, permitting deprecated slot, if present
private static final Predicate<String> MODULE_NAME_TESTER = Pattern.compile("(?:^\\w+|\\w+\\.\\w+|\\w+\\Q\\:\\E\\w+)+(?:\\:(?:\\w+|\\w+\\.\\w+))?$").asMatchPredicate();
Copy link
Collaborator

Choose a reason for hiding this comment

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

@ropalka Does the JBoss Module expose any API to validate a module identifier? Maybe it could be useful to have something there


private ModuleNameValidator() {
super(ModelType.STRING);
}

@Override
public void validateParameter(String parameterName, ModelNode value) throws OperationFailedException {
super.validateParameter(parameterName, value);
if (value.isDefined()) {
String moduleName = value.asString();
if (!MODULE_NAME_TESTER.test(moduleName)) {
throw ControllerLogger.MGMT_OP_LOGGER.invalidModuleNameParameter(parameterName, moduleName);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright The WildFly Authors
* SPDX-License-Identifier: Apache-2.0
*/

package org.jboss.as.controller.operation.validation;

import org.jboss.as.controller.OperationFailedException;
import org.jboss.as.controller.operations.validation.ModuleNameValidator;
import org.jboss.as.controller.operations.validation.ParameterValidator;
import org.jboss.dmr.ModelNode;
import org.junit.Assert;
import org.junit.Test;

/**
* Unit test for ModuleNameValidator
*/
public class ModuleNameValidatorTestCase {

@Test
public void test() throws OperationFailedException {
ParameterValidator validator = ModuleNameValidator.INSTANCE;

validator.validateParameter("valid", new ModelNode("org.jboss.modules"));
validator.validateParameter("valid", new ModelNode("org.jboss.modules:main"));
validator.validateParameter("valid", new ModelNode("org.jboss.modules:1.9"));
validator.validateParameter("escaped", new ModelNode("org.jboss.modules.foo\\:bar:main"));

Assert.assertThrows(OperationFailedException.class, () -> validator.validateParameter("invalid", new ModelNode(".foo.bar")));
Assert.assertThrows(OperationFailedException.class, () -> validator.validateParameter("invalid", new ModelNode("foo..bar")));
Assert.assertThrows(OperationFailedException.class, () -> validator.validateParameter("invalid", new ModelNode("foo.bar.")));
Assert.assertThrows(OperationFailedException.class, () -> validator.validateParameter("invalid", new ModelNode("foo.bar:")));
Assert.assertThrows(OperationFailedException.class, () -> validator.validateParameter("invalid", new ModelNode("foo:bar:main")));
}
}
Loading