Skip to content

Commit

Permalink
Add validation for threat intel source config (#1393)
Browse files Browse the repository at this point in the history
* add validation for source config and allow null to be read in parser

Signed-off-by: Joanne Wang <[email protected]>

* add parsing tests

Signed-off-by: Joanne Wang <[email protected]>

* add additional validation

Signed-off-by: Joanne Wang <[email protected]>

---------

Signed-off-by: Joanne Wang <[email protected]>
(cherry picked from commit 364f42d)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
github-actions[bot] committed Oct 30, 2024
1 parent 3a5cbe7 commit 9692350
Show file tree
Hide file tree
Showing 6 changed files with 201 additions and 43 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,7 +22,34 @@ public class SourceConfigDtoValidator {
public List<String> validateSourceConfigDto(SATIFSourceConfigDto sourceConfigDto) {
List<String> errorMsgs = new ArrayList<>();

if (sourceConfigDto.getIocTypes().isEmpty()) {
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");

Check warning on line 35 in src/main/java/org/opensearch/securityanalytics/threatIntel/common/SourceConfigDtoValidator.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/threatIntel/common/SourceConfigDtoValidator.java#L35

Added line #L35 was not covered by tests
}

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");

Check warning on line 41 in src/main/java/org/opensearch/securityanalytics/threatIntel/common/SourceConfigDtoValidator.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/threatIntel/common/SourceConfigDtoValidator.java#L41

Added line #L41 was not covered by tests
}

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

Check warning on line 45 in src/main/java/org/opensearch/securityanalytics/threatIntel/common/SourceConfigDtoValidator.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/threatIntel/common/SourceConfigDtoValidator.java#L45

Added line #L45 was not covered by tests
}

if (sourceConfigDto.getSource() == null) {
errorMsgs.add("Source must not be empty");
}

if (sourceConfigDto.getIocTypes() == null || sourceConfigDto.getIocTypes().isEmpty()) {
errorMsgs.add("Must specify at least one IOC type");
} else {
for (String s: sourceConfigDto.getIocTypes()) {
Expand All @@ -34,34 +59,41 @@ public List<String> validateSourceConfigDto(SATIFSourceConfigDto sourceConfigDto
}
}

switch (sourceConfigDto.getType()) {
case IOC_UPLOAD:
if (sourceConfigDto.isEnabled()) {
errorMsgs.add("Job Scheduler cannot be enabled for IOC_UPLOAD type");
}
if (sourceConfigDto.getSchedule() != null) {
errorMsgs.add("Cannot pass in schedule for IOC_UPLOAD type");
}
if (sourceConfigDto.getSource() != null && sourceConfigDto.getSource() instanceof IocUploadSource == false) {
errorMsgs.add("Source must be IOC_UPLOAD type");
}
break;
case S3_CUSTOM:
if (sourceConfigDto.getSchedule() == null) {
errorMsgs.add("Must pass in schedule for S3_CUSTOM type");
}
if (sourceConfigDto.getSource() != null && sourceConfigDto.getSource() instanceof S3Source == false) {
errorMsgs.add("Source must be S3_CUSTOM type");
}
break;
case URL_DOWNLOAD:
if (sourceConfigDto.getSchedule() == null) {
errorMsgs.add("Must pass in schedule for URL_DOWNLOAD source type");
}
if (sourceConfigDto.getSource() != null && sourceConfigDto.getSource() instanceof UrlDownloadSource == false) {
errorMsgs.add("Source must be URL_DOWNLOAD source type");
}
break;
if (sourceConfigDto.getType() == null) {
errorMsgs.add("Type must not be empty");
} else {
switch (sourceConfigDto.getType()) {
case IOC_UPLOAD:
if (sourceConfigDto.isEnabled()) {
errorMsgs.add("Job Scheduler cannot be enabled for IOC_UPLOAD type");

Check warning on line 68 in src/main/java/org/opensearch/securityanalytics/threatIntel/common/SourceConfigDtoValidator.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/threatIntel/common/SourceConfigDtoValidator.java#L68

Added line #L68 was not covered by tests
}
if (sourceConfigDto.getSchedule() != null) {
errorMsgs.add("Cannot pass in schedule for IOC_UPLOAD type");

Check warning on line 71 in src/main/java/org/opensearch/securityanalytics/threatIntel/common/SourceConfigDtoValidator.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/threatIntel/common/SourceConfigDtoValidator.java#L71

Added line #L71 was not covered by tests
}
if (sourceConfigDto.getSource() != null && sourceConfigDto.getSource() instanceof IocUploadSource == false) {
errorMsgs.add("Source must be IOC_UPLOAD type");

Check warning on line 74 in src/main/java/org/opensearch/securityanalytics/threatIntel/common/SourceConfigDtoValidator.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/threatIntel/common/SourceConfigDtoValidator.java#L74

Added line #L74 was not covered by tests
}
if (sourceConfigDto.getSource() instanceof IocUploadSource && ((IocUploadSource) sourceConfigDto.getSource()).getIocs() == null) {
errorMsgs.add("Ioc list must include at least one ioc");

Check warning on line 77 in src/main/java/org/opensearch/securityanalytics/threatIntel/common/SourceConfigDtoValidator.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/threatIntel/common/SourceConfigDtoValidator.java#L77

Added line #L77 was not covered by tests
}
break;
case S3_CUSTOM:
if (sourceConfigDto.getSchedule() == null) {
errorMsgs.add("Must pass in schedule for S3_CUSTOM type");

Check warning on line 82 in src/main/java/org/opensearch/securityanalytics/threatIntel/common/SourceConfigDtoValidator.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/threatIntel/common/SourceConfigDtoValidator.java#L82

Added line #L82 was not covered by tests
}
if (sourceConfigDto.getSource() != null && sourceConfigDto.getSource() instanceof S3Source == false) {
errorMsgs.add("Source must be S3_CUSTOM type");

Check warning on line 85 in src/main/java/org/opensearch/securityanalytics/threatIntel/common/SourceConfigDtoValidator.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/threatIntel/common/SourceConfigDtoValidator.java#L85

Added line #L85 was not covered by tests
}
break;
case URL_DOWNLOAD:
if (sourceConfigDto.getSchedule() == null) {
errorMsgs.add("Must pass in schedule for URL_DOWNLOAD source type");

Check warning on line 90 in src/main/java/org/opensearch/securityanalytics/threatIntel/common/SourceConfigDtoValidator.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/threatIntel/common/SourceConfigDtoValidator.java#L90

Added line #L90 was not covered by tests
}
if (sourceConfigDto.getSource() != null && sourceConfigDto.getSource() instanceof UrlDownloadSource == false) {
errorMsgs.add("Source must be URL_DOWNLOAD source type");

Check warning on line 93 in src/main/java/org/opensearch/securityanalytics/threatIntel/common/SourceConfigDtoValidator.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/threatIntel/common/SourceConfigDtoValidator.java#L93

Added line #L93 was not covered by tests
}
break;
}
}
return errorMsgs;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public class SATIFSourceConfig implements TIFSourceConfig, Writeable, ScheduledJ

public SATIFSourceConfig(String id, Long version, String name, String format, SourceConfigType type, String description, User createdByUser, Instant createdAt, Source source,
Instant enabledTime, Instant lastUpdateTime, Schedule schedule, TIFJobState state, RefreshType refreshType, Instant lastRefreshedTime, User lastRefreshedUser,
Boolean isEnabled, IocStoreConfig iocStoreConfig, List<String> iocTypes, boolean enabledForScan) {
boolean isEnabled, IocStoreConfig iocStoreConfig, List<String> iocTypes, boolean enabledForScan) {
this.id = id == null ? UUIDs.base64UUID() : id;
this.version = version != null ? version : NO_VERSION;
this.name = name;
Expand Down Expand Up @@ -289,7 +289,7 @@ public static SATIFSourceConfig parse(XContentParser xcp, String id, Long versio
RefreshType refreshType = null;
Instant lastRefreshedTime = null;
User lastRefreshedUser = null;
Boolean isEnabled = null;
boolean isEnabled = true;
boolean enabledForScan = true;
IocStoreConfig iocStoreConfig = null;
List<String> iocTypes = new ArrayList<>();
Expand All @@ -303,16 +303,28 @@ public static SATIFSourceConfig parse(XContentParser xcp, String id, Long versio
case SOURCE_CONFIG_FIELD:
break;
case NAME_FIELD:
name = xcp.text();
if (xcp.currentToken() == XContentParser.Token.VALUE_NULL) {
name = null;
} else {
name = xcp.text();
}
break;
case VERSION_FIELD:
version = xcp.longValue();
break;
case FORMAT_FIELD:
format = xcp.text();
if (xcp.currentToken() == XContentParser.Token.VALUE_NULL) {
format = null;
} else {
format = xcp.text();
}
break;
case TYPE_FIELD:
sourceConfigType = toSourceConfigType(xcp.text());
if (xcp.currentToken() == XContentParser.Token.VALUE_NULL) {
sourceConfigType = null;

Check warning on line 324 in src/main/java/org/opensearch/securityanalytics/threatIntel/model/SATIFSourceConfig.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/threatIntel/model/SATIFSourceConfig.java#L324

Added line #L324 was not covered by tests
} else {
sourceConfigType = toSourceConfigType(xcp.text());
}
break;
case DESCRIPTION_FIELD:
if (xcp.currentToken() == XContentParser.Token.VALUE_NULL) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ private List<STIX2IOCDto> convertToIocDtos(List<STIX2IOC> stix2IocList) {

public SATIFSourceConfigDto(String id, Long version, String name, String format, SourceConfigType type, String description, User createdByUser, Instant createdAt, Source source,
Instant enabledTime, Instant lastUpdateTime, Schedule schedule, TIFJobState state, RefreshType refreshType, Instant lastRefreshedTime, User lastRefreshedUser,
Boolean isEnabled, List<String> iocTypes, boolean enabledForScan) {
boolean isEnabled, List<String> iocTypes, boolean enabledForScan) {
this.id = id == null ? UUIDs.base64UUID() : id;
this.version = version != null ? version : NO_VERSION;
this.name = name;
Expand Down Expand Up @@ -314,7 +314,7 @@ public static SATIFSourceConfigDto parse(XContentParser xcp, String id, Long ver
RefreshType refreshType = null;
Instant lastRefreshedTime = null;
User lastRefreshedUser = null;
Boolean isEnabled = null;
boolean isEnabled = true;
List<String> iocTypes = new ArrayList<>();
boolean enabledForScan = true;

Expand All @@ -326,13 +326,25 @@ public static SATIFSourceConfigDto parse(XContentParser xcp, String id, Long ver
case SOURCE_CONFIG_FIELD:
break;
case NAME_FIELD:
name = xcp.text();
if (xcp.currentToken() == XContentParser.Token.VALUE_NULL) {
name = null;
} else {
name = xcp.text();
}
break;
case FORMAT_FIELD:
format = xcp.text();
if (xcp.currentToken() == XContentParser.Token.VALUE_NULL) {
format = null;
} else {
format = xcp.text();
}
break;
case TYPE_FIELD:
sourceConfigType = toSourceConfigType(xcp.text());
if (xcp.currentToken() == XContentParser.Token.VALUE_NULL) {
sourceConfigType = null;

Check warning on line 344 in src/main/java/org/opensearch/securityanalytics/threatIntel/model/SATIFSourceConfigDto.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/threatIntel/model/SATIFSourceConfigDto.java#L344

Added line #L344 was not covered by tests
} else {
sourceConfigType = toSourceConfigType(xcp.text());
}
break;
case DESCRIPTION_FIELD:
if (xcp.currentToken() == XContentParser.Token.VALUE_NULL) {
Expand Down Expand Up @@ -426,7 +438,6 @@ public static SATIFSourceConfigDto parse(XContentParser xcp, String id, Long ver
case ENABLED_FIELD:
isEnabled = xcp.booleanValue();
break;

case ENABLED_FOR_SCAN_FIELD:
enabledForScan = xcp.booleanValue();
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package org.opensearch.securityanalytics.action;

import org.junit.Assert;
import org.opensearch.action.ActionRequestValidationException;
import org.opensearch.common.io.stream.BytesStreamOutput;
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.rest.RestRequest;
Expand Down Expand Up @@ -33,4 +34,40 @@ public void testTIFSourceConfigPostRequest() throws IOException {
Assert.assertEquals(RestRequest.Method.POST, newRequest.getMethod());
Assert.assertNotNull(newRequest.getTIFConfigDto());
}

public void testValidateSourceConfigPostRequest() {
// Source config with invalid: name, format, source, ioc type, source config type
SATIFSourceConfigDto saTifSourceConfigDto = new SATIFSourceConfigDto(
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
false,
null,
true
);
String id = saTifSourceConfigDto.getId();
SAIndexTIFSourceConfigRequest request = new SAIndexTIFSourceConfigRequest(id, RestRequest.Method.POST, saTifSourceConfigDto);
Assert.assertNotNull(request);

ActionRequestValidationException exception = request.validate();
assertEquals(5, exception.validationErrors().size());
assertTrue(exception.validationErrors().contains("Name must not be empty"));
assertTrue(exception.validationErrors().contains("Format must not be empty"));
assertTrue(exception.validationErrors().contains("Source must not be empty"));
assertTrue(exception.validationErrors().contains("Must specify at least one IOC type"));
assertTrue(exception.validationErrors().contains("Type must not be empty"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.jobscheduler.spi.schedule.IntervalSchedule;
import org.opensearch.securityanalytics.threatIntel.common.SourceConfigType;
import org.opensearch.securityanalytics.threatIntel.model.S3Source;
import org.opensearch.securityanalytics.threatIntel.model.SATIFSourceConfigDto;
import org.opensearch.test.OpenSearchTestCase;

import java.io.IOException;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.List;

import static org.opensearch.securityanalytics.TestHelpers.randomSATIFSourceConfigDto;

Expand All @@ -36,6 +40,34 @@ public void testParseFunction() throws IOException {
assertEqualsSaTifSourceConfigDtos(saTifSourceConfigDto, newSaTifSourceConfigDto);
}

public void testParseFunctionWithNullValues() throws IOException {
// Source config with invalid name and format
SATIFSourceConfigDto saTifSourceConfigDto = new SATIFSourceConfigDto(
"randomId",
null,
null,
null,
SourceConfigType.S3_CUSTOM,
null,
null,
null,
new S3Source("bucket", "objectkey", "region", "rolearn"),
null,
null,
new org.opensearch.jobscheduler.spi.schedule.IntervalSchedule(Instant.now(), 1, ChronoUnit.DAYS),
null,
null,
null,
null,
true,
List.of("ip"),
true
);
String json = toJsonString(saTifSourceConfigDto);
SATIFSourceConfigDto newSaTifSourceConfigDto = SATIFSourceConfigDto.parse(getParser(json), saTifSourceConfigDto.getId(), null);
assertEqualsSaTifSourceConfigDtos(saTifSourceConfigDto, newSaTifSourceConfigDto);
}

public XContentParser getParser(String xc) throws IOException {
XContentParser parser = XContentType.JSON.xContent().createParser(xContentRegistry(), LoggingDeprecationHandler.INSTANCE, xc);
parser.nextToken();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.jobscheduler.spi.schedule.IntervalSchedule;
import org.opensearch.securityanalytics.commons.model.IOCType;
import org.opensearch.securityanalytics.threatIntel.common.SourceConfigType;
import org.opensearch.securityanalytics.threatIntel.model.DefaultIocStoreConfig;
import org.opensearch.securityanalytics.threatIntel.model.S3Source;
import org.opensearch.securityanalytics.threatIntel.model.SATIFSourceConfig;
import org.opensearch.test.OpenSearchTestCase;

import java.io.IOException;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.List;

import static org.opensearch.securityanalytics.TestHelpers.randomSATIFSourceConfig;

Expand All @@ -37,6 +42,35 @@ public void testParseFunction() throws IOException {
assertEqualsSaTifSourceConfigs(saTifSourceConfig, newSaTifSourceConfig);
}

public void testParseFunctionWithNullValues() throws IOException {
// Source config with invalid name and format
SATIFSourceConfig saTifSourceConfig = new SATIFSourceConfig(
null,
null,
null,
null,
SourceConfigType.S3_CUSTOM,
null,
null,
null,
new S3Source("bucket", "objectkey", "region", "rolearn"),
null,
null,
new org.opensearch.jobscheduler.spi.schedule.IntervalSchedule(Instant.now(), 1, ChronoUnit.DAYS),
null,
null,
null,
null,
true,
new DefaultIocStoreConfig(List.of(new DefaultIocStoreConfig.IocToIndexDetails(new IOCType(IOCType.DOMAIN_NAME_TYPE), "indexPattern", "writeIndex"))),
List.of("ip"),
true
);
String json = toJsonString(saTifSourceConfig);
SATIFSourceConfig newSaTifSourceConfig = SATIFSourceConfig.parse(getParser(json), saTifSourceConfig.getId(), null);
assertEqualsSaTifSourceConfigs(saTifSourceConfig, newSaTifSourceConfig);
}

public XContentParser getParser(String xc) throws IOException {
XContentParser parser = XContentType.JSON.xContent().createParser(xContentRegistry(), LoggingDeprecationHandler.INSTANCE, xc);
parser.nextToken();
Expand Down

0 comments on commit 9692350

Please sign in to comment.