added queue name and topic validation

This commit is contained in:
YevhenBondarenko 2022-06-28 14:54:38 +02:00
parent dadaa524df
commit 68f0388723
6 changed files with 34 additions and 5 deletions

View File

@ -22,11 +22,17 @@ import org.thingsboard.server.common.data.SearchTextBasedWithAdditionalInfo;
import org.thingsboard.server.common.data.id.QueueId; import org.thingsboard.server.common.data.id.QueueId;
import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.tenant.profile.TenantProfileQueueConfiguration; import org.thingsboard.server.common.data.tenant.profile.TenantProfileQueueConfiguration;
import org.thingsboard.server.common.data.validation.Length;
import org.thingsboard.server.common.data.validation.NoXss;
@Data @Data
public class Queue extends SearchTextBasedWithAdditionalInfo<QueueId> implements HasName, HasTenantId { public class Queue extends SearchTextBasedWithAdditionalInfo<QueueId> implements HasName, HasTenantId {
private TenantId tenantId; private TenantId tenantId;
@NoXss
@Length(fieldName = "name")
private String name; private String name;
@NoXss
@Length(fieldName = "topic")
private String topic; private String topic;
private int pollInterval; private int pollInterval;
private int partitions; private int partitions;

View File

@ -29,6 +29,8 @@ import org.thingsboard.server.dao.queue.QueueDao;
import org.thingsboard.server.dao.service.DataValidator; import org.thingsboard.server.dao.service.DataValidator;
import org.thingsboard.server.dao.tenant.TbTenantProfileCache; import org.thingsboard.server.dao.tenant.TbTenantProfileCache;
import java.util.regex.Pattern;
@Component @Component
public class QueueValidator extends DataValidator<Queue> { public class QueueValidator extends DataValidator<Queue> {
@ -38,6 +40,8 @@ public class QueueValidator extends DataValidator<Queue> {
@Autowired @Autowired
private TbTenantProfileCache tenantProfileCache; private TbTenantProfileCache tenantProfileCache;
private final Pattern queueTopicPattern = Pattern.compile("^[a-zA-Z0-9_.\\-]+$");
@Override @Override
protected void validateCreate(TenantId tenantId, Queue queue) { protected void validateCreate(TenantId tenantId, Queue queue) {
if (queueDao.findQueueByTenantIdAndName(tenantId, queue.getName()) != null) { if (queueDao.findQueueByTenantIdAndName(tenantId, queue.getName()) != null) {
@ -76,8 +80,14 @@ public class QueueValidator extends DataValidator<Queue> {
if (StringUtils.isEmpty(queue.getName())) { if (StringUtils.isEmpty(queue.getName())) {
throw new DataValidationException("Queue name should be specified!"); throw new DataValidationException("Queue name should be specified!");
} }
if (StringUtils.isBlank(queue.getTopic())) { if (!queueTopicPattern.matcher(queue.getName()).matches()) {
throw new DataValidationException("Queue topic should be non empty and without spaces!"); throw new DataValidationException("Queue name contains a character other than ASCII alphanumerics, '.', '_' and '-'!");
}
if (StringUtils.isEmpty(queue.getTopic())) {
throw new DataValidationException("Queue topic should be specified!");
}
if (!queueTopicPattern.matcher(queue.getTopic()).matches()) {
throw new DataValidationException("Queue topic contains a character other than ASCII alphanumerics, '.', '_' and '-'!");
} }
if (queue.getPollInterval() < 1) { if (queue.getPollInterval() < 1) {
throw new DataValidationException("Queue poll interval should be more then 0!"); throw new DataValidationException("Queue poll interval should be more then 0!");

View File

@ -34,6 +34,7 @@ import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.regex.Pattern;
@Component @Component
public class TenantProfileDataValidator extends DataValidator<TenantProfile> { public class TenantProfileDataValidator extends DataValidator<TenantProfile> {
@ -45,6 +46,8 @@ public class TenantProfileDataValidator extends DataValidator<TenantProfile> {
@Lazy @Lazy
private TenantProfileService tenantProfileService; private TenantProfileService tenantProfileService;
private final Pattern queueTopicPattern = Pattern.compile("^[a-zA-Z0-9_.\\-]+$");
@Override @Override
protected void validateDataImpl(TenantId tenantId, TenantProfile tenantProfile) { protected void validateDataImpl(TenantId tenantId, TenantProfile tenantProfile) {
if (StringUtils.isEmpty(tenantProfile.getName())) { if (StringUtils.isEmpty(tenantProfile.getName())) {
@ -110,8 +113,14 @@ public class TenantProfileDataValidator extends DataValidator<TenantProfile> {
if (StringUtils.isEmpty(queue.getName())) { if (StringUtils.isEmpty(queue.getName())) {
throw new DataValidationException("Queue name should be specified!"); throw new DataValidationException("Queue name should be specified!");
} }
if (StringUtils.isBlank(queue.getTopic())) { if (!queueTopicPattern.matcher(queue.getName()).matches()) {
throw new DataValidationException("Queue topic should be non empty and without spaces!"); throw new DataValidationException("Queue name contains a character other than ASCII alphanumerics, '.', '_' and '-'!");
}
if (StringUtils.isEmpty(queue.getTopic())) {
throw new DataValidationException("Queue topic should be specified!");
}
if (!queueTopicPattern.matcher(queue.getTopic()).matches()) {
throw new DataValidationException("Queue topic contains a character other than ASCII alphanumerics, '.', '_' and '-'!");
} }
if (queue.getPollInterval() < 1) { if (queue.getPollInterval() < 1) {
throw new DataValidationException("Queue poll interval should be more then 0!"); throw new DataValidationException("Queue poll interval should be more then 0!");

View File

@ -26,6 +26,9 @@
<mat-error *ngIf="queueFormGroup.get('name').hasError('unique')"> <mat-error *ngIf="queueFormGroup.get('name').hasError('unique')">
{{ 'queue.name-unique' | translate }} {{ 'queue.name-unique' | translate }}
</mat-error> </mat-error>
<mat-error *ngIf="queueFormGroup.get('name').hasError('pattern')">
{{ 'queue.name-pattern' | translate }}
</mat-error>
</mat-form-field> </mat-form-field>
<mat-accordion class="queue-strategy" [multi]="true"> <mat-accordion class="queue-strategy" [multi]="true">
<mat-expansion-panel [expanded]="true"> <mat-expansion-panel [expanded]="true">

View File

@ -99,7 +99,7 @@ export class QueueFormComponent implements ControlValueAccessor, OnInit, OnDestr
ngOnInit() { ngOnInit() {
this.queueFormGroup = this.fb.group( this.queueFormGroup = this.fb.group(
{ {
name: ['', [Validators.required]], name: ['', [Validators.required, Validators.pattern(/^[a-zA-Z0-9_.\-]+$/)]],
pollInterval: [25, [Validators.min(1), Validators.required]], pollInterval: [25, [Validators.min(1), Validators.required]],
partitions: [10, [Validators.min(1), Validators.required]], partitions: [10, [Validators.min(1), Validators.required]],
consumerPerPartition: [false, []], consumerPerPartition: [false, []],

View File

@ -2928,6 +2928,7 @@
"name": "Name", "name": "Name",
"name-required": "Queue name is required!", "name-required": "Queue name is required!",
"name-unique": "Queue name is not unique!", "name-unique": "Queue name is not unique!",
"name-pattern": "Queue name contains a character other than ASCII alphanumerics, '.', '_' and '-'!",
"queue-required": "Queue is required!", "queue-required": "Queue is required!",
"topic-required": "Queue topic is required!", "topic-required": "Queue topic is required!",
"poll-interval-required": "Poll interval is required!", "poll-interval-required": "Poll interval is required!",