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

Fix KapuaNamedEntity name validation #2796

Merged
merged 10 commits into from
Dec 18, 2019
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

import org.eclipse.kapua.KapuaIllegalArgumentException;
import org.eclipse.kapua.KapuaIllegalNullArgumentException;
import org.eclipse.kapua.model.KapuaNamedEntity;
import org.eclipse.kapua.model.KapuaNamedEntityCreator;

import javax.annotation.Nullable;
import javax.validation.constraints.NotNull;
Expand Down Expand Up @@ -214,7 +216,7 @@ public static void numRange(long value, long minValue, long maxValue, String arg
* @param argumentName The argument name with will be used in the exception
* @throws KapuaIllegalArgumentException If the given {@link String} excedees the given length limits.
*/
public static void lengthRange(@NotNull String value, @Nullable Long minLength, @Nullable Long maxLength, @NotNull String argumentName) throws KapuaIllegalArgumentException {
public static void lengthRange(@NotNull String value, @Nullable Integer minLength, @Nullable Integer maxLength, @NotNull String argumentName) throws KapuaIllegalArgumentException {

if (minLength != null && value.length() < minLength) {
throw new KapuaIllegalArgumentException(argumentName, "Value less than allowed min length. Min length is " + minLength);
Expand All @@ -224,4 +226,49 @@ public static void lengthRange(@NotNull String value, @Nullable Long minLength,
throw new KapuaIllegalArgumentException(argumentName, "Value over than allowed max length. Max length is " + maxLength);
}
}

/**
* Comprehensive validation method for the {@link KapuaNamedEntity#getName()} or {@link KapuaNamedEntityCreator#getName()} fields.
* <p>
* Same as {@link #validateEntityName(String, Integer, Integer, String)} but assumes default minimum length of 3 chars and maximum length of 255 chars.
*
* @param name The value to validate. Usually would be the {@link KapuaNamedEntity#getName()} or {@link KapuaNamedEntityCreator#getName()}, but other values could be provided
* @param argumentName The name of the argumento to bundle with the {@link KapuaIllegalArgumentException}
* @throws KapuaIllegalNullArgumentException If the given value to validate is {@code null}.
* @throws KapuaIllegalArgumentException If other validations fails.
* @see ArgumentValidator#notEmptyOrNull(String, String)
* @see ArgumentValidator#lengthRange(String, Integer, Integer, String)
* @see ArgumentValidator#match(String, ValidationRegex, String)
* @since 1.2.0
*/
public static void validateEntityName(@Nullable String name, @NotNull String argumentName) throws KapuaIllegalNullArgumentException, KapuaIllegalArgumentException {
validateEntityName(name, 3, 255, argumentName);
}

/**
* Comprehensive validation method for the {@link KapuaNamedEntity#getName()} or {@link KapuaNamedEntityCreator#getName()} fields.
* <p>
* It invokes in sequence the three {@link ArgumentValidator} validation methods, using the provided parameters.
* <ul>
* <li>{@link #notEmptyOrNull(String, String)}</li>
* <li>{@link #lengthRange(String, Integer, Integer, String)}</li>
* <li>{@link #match(String, ValidationRegex, String)} with {@link CommonsValidationRegex#NAME_SPACE_REGEXP} </li>
* </ul>
*
* @param name The value to validate. Usually would be the {@link KapuaNamedEntity#getName()} or {@link KapuaNamedEntityCreator#getName()}, but other values could be provided
* @param minLength The minimum length of the field. If {@code null} the minLength validation is skipped
* @param maxLength The maximum length of the field. If {@code null} the maxLength validation is skipped
* @param argumentName The name of the argumento to bundle with the {@link KapuaIllegalArgumentException}
* @throws KapuaIllegalNullArgumentException If the given value to validate is {@code null}.
* @throws KapuaIllegalArgumentException If other validations fails.
* @see ArgumentValidator#notEmptyOrNull(String, String)
* @see ArgumentValidator#lengthRange(String, Integer, Integer, String)
* @see ArgumentValidator#match(String, ValidationRegex, String)
* @since 1.2.0
*/
public static void validateEntityName(@Nullable String name, @Nullable Integer minLength, @Nullable Integer maxLength, @NotNull String argumentName) throws KapuaIllegalNullArgumentException, KapuaIllegalArgumentException {
notEmptyOrNull(name, argumentName);
lengthRange(name, minLength, maxLength, argumentName);
match(name, CommonsValidationRegex.NAME_SPACE_REGEXP, argumentName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@ public void createBody() {
FormPanel groupFormPanel = new FormPanel(FORM_LABEL_WIDTH);
groupNameField = new KapuaTextField<String>();
groupNameField.setAllowBlank(false);
groupNameField.setMinLength(3);
groupNameField.setMaxLength(255);
groupNameField.setFieldLabel("* " + MSGS.dialogAddFieldName());
groupNameField.setValidator(new TextFieldValidator(groupNameField, FieldType.NAME));
groupNameField.setValidator(new TextFieldValidator(groupNameField, FieldType.NAME_SPACE));
groupNameField.setToolTip(MSGS.dialogAddFieldNameTooltip());
groupFormPanel.add(groupNameField);

Expand All @@ -71,7 +72,7 @@ public void createBody() {
public void validateGroups() {
if (groupNameField.getValue() == null) {
ConsoleInfo.display("Error", CONSOLE_MSGS.allFieldsRequired());
}
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public RoleAddDialog(GwtSession currentSession) {
public void validateRoles() {
if (roleNameField.getValue() == null) {
ConsoleInfo.display("Error", CONSOLE_MSGS.allFieldsRequired());
}
}
}

@Override
Expand Down Expand Up @@ -117,9 +117,10 @@ public void createBody() {
// Name
roleNameField = new KapuaTextField<String>();
roleNameField.setAllowBlank(false);
roleNameField.setMinLength(3);
roleNameField.setMaxLength(255);
roleNameField.setFieldLabel("* " + MSGS.dialogAddFieldName());
roleNameField.setValidator(new TextFieldValidator(roleNameField, FieldType.NAME));
roleNameField.setValidator(new TextFieldValidator(roleNameField, FieldType.NAME_SPACE));
roleNameField.setToolTip(MSGS.dialogAddFieldNameTooltip());
roleFormPanel.add(roleNameField);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,11 @@ public void createBody() {

name = new KapuaTextField<String>();
name.setAllowBlank(false);
name.setMinLength(3);
name.setMaxLength(255);
name.setName("name");
name.setFieldLabel("* " + JOB_MSGS.dialogAddFieldName());
name.setValidator(new TextFieldValidator(name, FieldType.NAME));
name.setValidator(new TextFieldValidator(name, FieldType.NAME_SPACE));
name.setToolTip(JOB_MSGS.dialogAddFieldNameTooltip());
jobFormPanel.add(name);

Expand All @@ -74,7 +75,7 @@ public void createBody() {
public void validateJob() {
if (name.getValue() == null) {
ConsoleInfo.display("Error", CONSOLE_MSGS.allFieldsRequired());
}
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.eclipse.kapua.app.console.module.api.client.util.FailureHandler;
import org.eclipse.kapua.app.console.module.api.client.util.validator.AfterDateValidator;
import org.eclipse.kapua.app.console.module.api.client.util.validator.BeforeDateValidator;
import org.eclipse.kapua.app.console.module.api.client.util.validator.TextFieldValidator;
import org.eclipse.kapua.app.console.module.api.shared.model.session.GwtSession;
import org.eclipse.kapua.app.console.module.job.client.messages.ConsoleJobMessages;
import org.eclipse.kapua.app.console.module.job.shared.model.scheduler.GwtTrigger;
Expand Down Expand Up @@ -138,7 +139,9 @@ public void handleEvent(BaseEvent be) {
};

triggerName.setAllowBlank(false);
triggerName.setMinLength(3);
triggerName.setMaxLength(255);
triggerName.setValidator(new TextFieldValidator(triggerName, TextFieldValidator.FieldType.NAME_SPACE));
triggerName.setFieldLabel("* " + JOB_MSGS.dialogAddScheduleScheduleNameLabel());
triggerName.setToolTip(JOB_MSGS.dialogAddScheduleNameTooltip());
mainPanel.add(triggerName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.eclipse.kapua.app.console.module.api.client.util.DialogUtils;
import org.eclipse.kapua.app.console.module.api.client.util.FailureHandler;
import org.eclipse.kapua.app.console.module.api.client.util.KapuaSafeHtmlUtils;
import org.eclipse.kapua.app.console.module.api.client.util.validator.TextFieldValidator;
import org.eclipse.kapua.app.console.module.api.shared.model.session.GwtSession;
import org.eclipse.kapua.app.console.module.job.client.messages.ConsoleJobMessages;
import org.eclipse.kapua.app.console.module.job.shared.model.GwtJobStep;
Expand Down Expand Up @@ -98,7 +99,10 @@ public JobStepAddDialog(GwtSession currentSession, String jobId) {
this.jobId = jobId;

jobStepName = new KapuaTextField<String>();
jobStepName.setMinLength(3);
jobStepName.setMaxLength(255);
jobStepName.setValidator(new TextFieldValidator(jobStepName, TextFieldValidator.FieldType.NAME_SPACE));

jobStepDescription = new KapuaTextField<String>();
jobStepDefinitionCombo = new ComboBox<GwtJobStepDefinition>();
jobStepPropertiesFieldSet = new FieldSet();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public void createBody() {
tagNameField = new KapuaTextField<String>();
tagNameField.setAllowBlank(false);
tagNameField.setFieldLabel("* " + MSGS.dialogAddFieldName());
tagNameField.setValidator(new TextFieldValidator(tagNameField, FieldType.NAME));
tagNameField.setValidator(new TextFieldValidator(tagNameField, FieldType.NAME_SPACE));
tagNameField.setMinLength(3);
tagNameField.setMaxLength(255);
tagFormPanel.add(tagNameField);

Expand All @@ -70,7 +71,7 @@ public void createBody() {
public void validateTags() {
if (tagNameField.getValue() == null) {
ConsoleInfo.display("Error", CONSOLE_MSGS.allFieldsRequired());
}
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,9 @@ Feature: Job service CRUD tests
| boolean | infiniteChildEntities | true |
| integer | maxNumberChildEntities | 5 |
Given A job creator with an empty name
Given I expect the exception "KapuaIllegalNullArgumentException" with the text "name"
When I create a new job entity from the existing creator
Then No exception was thrown
When I search for the job in the database
Then The job entity matches the creator
Then An exception was thrown
Then I logout

Scenario: Job with a duplicate name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,14 @@ Feature: Trigger service tests

Scenario: Adding "Device Connect" Schedule With Min Length Name
Login as kapua-sys user and create a job with name job0.
Add a new schedule with one character long name.
Add a new schedule with three character long name.
As this is the defined limit for the min number of characters
for the schedule name, no errors should be thrown.

Given I login as user with name "kapua-sys" and password "kapua-password"
And I create a job with the name "job0"
When I found trigger properties with name "Device Connect"
And A regular trigger creator with the name "w"
And A regular trigger creator with the name "www"
And The trigger is set to start today at 10:00.
And I create a new trigger from the existing creator with previously defined date properties
And I logout
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ public EndpointInfo create(EndpointInfoCreator endpointInfoCreator)

ArgumentValidator.notEmptyOrNull(endpointInfoCreator.getSchema(), "endpointInfoCreator.schema");
ArgumentValidator.match(endpointInfoCreator.getSchema(), CommonsValidationRegex.URI_SCHEME, "endpointInfoCreator.schema");
ArgumentValidator.lengthRange(endpointInfoCreator.getSchema(), 1L, 64L, "endpointInfoCreator.schema");
ArgumentValidator.lengthRange(endpointInfoCreator.getSchema(), 1, 64, "endpointInfoCreator.schema");

ArgumentValidator.notEmptyOrNull(endpointInfoCreator.getDns(), "endpointInfoCreator.dns");
ArgumentValidator.match(endpointInfoCreator.getDns(), CommonsValidationRegex.URI_DNS, "endpointInfoCreator.dns");
ArgumentValidator.lengthRange(endpointInfoCreator.getDns(), 1L, 1024L, "endpointInfoCreator.dns");
ArgumentValidator.lengthRange(endpointInfoCreator.getDns(), 1, 1024, "endpointInfoCreator.dns");

ArgumentValidator.notNegative(endpointInfoCreator.getPort(), "endpointInfoCreator.port");
ArgumentValidator.numRange(endpointInfoCreator.getPort(), 1, 65535, "endpointInfoCreator.port");
Expand Down Expand Up @@ -109,11 +109,11 @@ public EndpointInfo update(EndpointInfo endpointInfo) throws KapuaException {

ArgumentValidator.notEmptyOrNull(endpointInfo.getSchema(), "endpointInfo.schema");
ArgumentValidator.match(endpointInfo.getSchema(), CommonsValidationRegex.URI_SCHEME, "endpointInfo.schema");
ArgumentValidator.lengthRange(endpointInfo.getSchema(), 1L, 64L, "endpointInfo.schema");
ArgumentValidator.lengthRange(endpointInfo.getSchema(), 1, 64, "endpointInfo.schema");

ArgumentValidator.notEmptyOrNull(endpointInfo.getDns(), "endpointInfo.dns");
ArgumentValidator.match(endpointInfo.getDns(), CommonsValidationRegex.URI_DNS, "endpointInfo.dns");
ArgumentValidator.lengthRange(endpointInfo.getDns(), 1L, 1024L, "endpointInfo.dns");
ArgumentValidator.lengthRange(endpointInfo.getDns(), 1, 1024, "endpointInfo.dns");

ArgumentValidator.notNegative(endpointInfo.getPort(), "endpointInfo.port");
ArgumentValidator.numRange(endpointInfo.getPort(), 1, 65535, "endpointInfo.port");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public Job create(JobCreator creator) throws KapuaException {
// Argument validation
ArgumentValidator.notNull(creator, "jobCreator");
ArgumentValidator.notNull(creator.getScopeId(), "jobCreator.scopeId");
ArgumentValidator.notNull(creator.getName(), "jobCreator.name");
ArgumentValidator.validateEntityName(creator.getName(), "jobCreator.name");

//
// Check access
Expand Down Expand Up @@ -106,7 +106,7 @@ public Job update(Job job) throws KapuaException {
// Argument Validation
ArgumentValidator.notNull(job, "job");
ArgumentValidator.notNull(job.getScopeId(), "job.scopeId");
ArgumentValidator.notNull(job.getName(), "job.name");
ArgumentValidator.validateEntityName(job.getName(), "job.name");

//
// Check access
Expand Down Expand Up @@ -195,7 +195,7 @@ public void deleteForced(KapuaId scopeId, KapuaId jobId) throws KapuaException {

//
// Private methods
//
//

/**
* Deletes the {@link Job} like {@link #delete(KapuaId, KapuaId)}.
Expand Down Expand Up @@ -249,7 +249,7 @@ private void deleteInternal(KapuaId scopeId, KapuaId jobId, boolean forced) thro
//
// Do delete
try {
KapuaSecurityUtils.doPrivileged(() -> jobEngineService.cleanJobData(scopeId, jobId));
KapuaSecurityUtils.doPrivileged(() -> jobEngineService.cleanJobData(scopeId, jobId));
} catch (Exception e) {
if (forced) {
LOG.warn("Error while cleaning Job data. Ignoring exception since delete is forced! Error: {}", e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public JobStepDefinition create(JobStepDefinitionCreator creator) throws KapuaEx
ArgumentValidator.notNull(creator, "stepDefinitionCreator");
ArgumentValidator.notNull(creator.getScopeId(), "stepDefinitionCreator.scopeId");
ArgumentValidator.notNull(creator.getStepType(), "stepDefinitionCreator.stepType");
ArgumentValidator.notEmptyOrNull(creator.getName(), "stepDefinitionCreator.name");
ArgumentValidator.validateEntityName(creator.getName(), "stepDefinitionCreator.name");
ArgumentValidator.notEmptyOrNull(creator.getProcessorName(), "stepDefinitionCreator.processorName");

//
Expand All @@ -80,7 +80,7 @@ public JobStepDefinition update(JobStepDefinition jobStepDefinition) throws Kapu
ArgumentValidator.notNull(jobStepDefinition, "stepDefinition");
ArgumentValidator.notNull(jobStepDefinition.getScopeId(), "stepDefinition.scopeId");
ArgumentValidator.notNull(jobStepDefinition.getStepType(), "jobStepDefinition.stepType");
ArgumentValidator.notEmptyOrNull(jobStepDefinition.getName(), "jobStepDefinition.name");
ArgumentValidator.validateEntityName(jobStepDefinition.getName(), "jobStepDefinition.name");
ArgumentValidator.notEmptyOrNull(jobStepDefinition.getProcessorName(), "jobStepDefinition.processorName");

//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
import org.eclipse.kapua.KapuaEntityUniquenessException;
import org.eclipse.kapua.KapuaException;
import org.eclipse.kapua.commons.configuration.AbstractKapuaConfigurableResourceLimitedService;
import org.eclipse.kapua.model.query.SortOrder;
import org.eclipse.kapua.commons.util.ArgumentValidator;
import org.eclipse.kapua.locator.KapuaLocator;
import org.eclipse.kapua.locator.KapuaProvider;
import org.eclipse.kapua.model.domain.Actions;
import org.eclipse.kapua.model.id.KapuaId;
import org.eclipse.kapua.model.query.KapuaQuery;
import org.eclipse.kapua.model.query.SortOrder;
import org.eclipse.kapua.model.query.predicate.AttributePredicate.Operator;
import org.eclipse.kapua.service.authorization.AuthorizationService;
import org.eclipse.kapua.service.authorization.permission.PermissionFactory;
Expand Down Expand Up @@ -71,8 +71,7 @@ public JobStep create(JobStepCreator jobStepCreator) throws KapuaException {
// Argument validation
ArgumentValidator.notNull(jobStepCreator, "jobStepCreator");
ArgumentValidator.notNull(jobStepCreator.getScopeId(), "jobStepCreator.scopeId");
ArgumentValidator.notEmptyOrNull(jobStepCreator.getName(), "jobStepCreator.name");
ArgumentValidator.numRange(jobStepCreator.getName().length(), 1, 255, "jobStepCreator.name");
ArgumentValidator.validateEntityName(jobStepCreator.getName(), "jobStepCreator.name");
ArgumentValidator.notNull(jobStepCreator.getJobStepDefinitionId(), "jobStepCreator.stepDefinitionId");

if (jobStepCreator.getDescription() != null) {
Expand Down Expand Up @@ -152,7 +151,7 @@ public JobStep update(JobStep jobStep) throws KapuaException {
// Argument validation
ArgumentValidator.notNull(jobStep, "jobStep");
ArgumentValidator.notNull(jobStep.getScopeId(), "jobStep.scopeId");
ArgumentValidator.notNull(jobStep.getName(), "jobStep.name");
ArgumentValidator.validateEntityName(jobStep.getName(), "jobStep.name");
ArgumentValidator.notNull(jobStep.getJobStepDefinitionId(), "jobStep.stepDefinitionId");
if (jobStep.getDescription() != null) {
ArgumentValidator.numRange(jobStep.getDescription().length(), 0, 8192, "jobStep.description");
Expand Down
Loading