Skip to content

Commit

Permalink
add additional validation
Browse files Browse the repository at this point in the history
Signed-off-by: Joanne Wang <[email protected]>
  • Loading branch information
jowg-amazon committed Oct 29, 2024
1 parent 0bb0651 commit ec8e5d9
Showing 1 changed file with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@

package org.opensearch.securityanalytics.threatIntel.common;

import org.opensearch.securityanalytics.commons.model.IOC;
import org.opensearch.securityanalytics.commons.model.IOCType;
import org.opensearch.securityanalytics.threatIntel.model.IocUploadSource;
import org.opensearch.securityanalytics.threatIntel.model.S3Source;
import org.opensearch.securityanalytics.threatIntel.model.SATIFSourceConfigDto;
import org.opensearch.securityanalytics.threatIntel.model.UrlDownloadSource;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.regex.Pattern;

/**
* Source config dto validator
Expand All @@ -24,12 +22,27 @@ public class SourceConfigDtoValidator {
public List<String> validateSourceConfigDto(SATIFSourceConfigDto sourceConfigDto) {
List<String> errorMsgs = new ArrayList<>();

String nameRegex = "^[a-zA-Z0-9 _-]{1,128}$";
Pattern namePattern = Pattern.compile(nameRegex);

int MAX_RULE_DESCRIPTION_LENGTH = 65535;
String descriptionRegex = "^.{0," + MAX_RULE_DESCRIPTION_LENGTH + "}$";
Pattern descriptionPattern = Pattern.compile(descriptionRegex);

if (sourceConfigDto.getName() == null || sourceConfigDto.getName().isEmpty()) {
errorMsgs.add("Name must not be empty");
} else if (sourceConfigDto.getName() != null && namePattern.matcher(sourceConfigDto.getName()).matches() == false) {
errorMsgs.add("Name must be less than 128 characters and only consist of upper and lowercase letters, numbers 0-9, hyphens, spaces, and underscores");
}

if (sourceConfigDto.getFormat() == null || sourceConfigDto.getFormat().isEmpty()) {
errorMsgs.add("Format must not be empty");
} else if (sourceConfigDto.getFormat() != null && sourceConfigDto.getFormat().length() > 50) {
errorMsgs.add("Format must be 50 characters or less");
}

if (sourceConfigDto.getDescription() != null && descriptionPattern.matcher(sourceConfigDto.getDescription()).matches() == false) {
errorMsgs.add("Description must be " + MAX_RULE_DESCRIPTION_LENGTH + " characters or less");
}

if (sourceConfigDto.getSource() == null) {
Expand Down

0 comments on commit ec8e5d9

Please sign in to comment.