From ca9fa6b6935744bac8bfbd4e436ca40b084fcb82 Mon Sep 17 00:00:00 2001 From: Joanne Wang Date: Fri, 25 Oct 2024 07:17:14 -0700 Subject: [PATCH 1/3] add validation for source config and allow null to be read in parser Signed-off-by: Joanne Wang --- .../common/SourceConfigDtoValidator.java | 77 ++++++++++++------- .../threatIntel/model/SATIFSourceConfig.java | 22 ++++-- .../model/SATIFSourceConfigDto.java | 23 ++++-- .../IndexTIFSourceConfigRequestTests.java | 37 +++++++++ 4 files changed, 119 insertions(+), 40 deletions(-) diff --git a/src/main/java/org/opensearch/securityanalytics/threatIntel/common/SourceConfigDtoValidator.java b/src/main/java/org/opensearch/securityanalytics/threatIntel/common/SourceConfigDtoValidator.java index 8001f37ea..80c56c46e 100644 --- a/src/main/java/org/opensearch/securityanalytics/threatIntel/common/SourceConfigDtoValidator.java +++ b/src/main/java/org/opensearch/securityanalytics/threatIntel/common/SourceConfigDtoValidator.java @@ -24,7 +24,19 @@ public class SourceConfigDtoValidator { public List validateSourceConfigDto(SATIFSourceConfigDto sourceConfigDto) { List errorMsgs = new ArrayList<>(); - if (sourceConfigDto.getIocTypes().isEmpty()) { + if (sourceConfigDto.getName() == null || sourceConfigDto.getName().isEmpty()) { + errorMsgs.add("Name must not be empty"); + } + + if (sourceConfigDto.getFormat() == null || sourceConfigDto.getFormat().isEmpty()) { + errorMsgs.add("Format must not be empty"); + } + + 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()) { @@ -34,34 +46,41 @@ public List 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"); + } + 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"); + } + if (sourceConfigDto.getSource() instanceof IocUploadSource && ((IocUploadSource) sourceConfigDto.getSource()).getIocs() == null) { + errorMsgs.add("Ioc list must include at least one ioc"); + } + 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; + } } return errorMsgs; } diff --git a/src/main/java/org/opensearch/securityanalytics/threatIntel/model/SATIFSourceConfig.java b/src/main/java/org/opensearch/securityanalytics/threatIntel/model/SATIFSourceConfig.java index 51b909334..2c634ce70 100644 --- a/src/main/java/org/opensearch/securityanalytics/threatIntel/model/SATIFSourceConfig.java +++ b/src/main/java/org/opensearch/securityanalytics/threatIntel/model/SATIFSourceConfig.java @@ -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 iocTypes, boolean enabledForScan) { + boolean isEnabled, IocStoreConfig iocStoreConfig, List iocTypes, boolean enabledForScan) { this.id = id == null ? UUIDs.base64UUID() : id; this.version = version != null ? version : NO_VERSION; this.name = name; @@ -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 iocTypes = new ArrayList<>(); @@ -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; + } else { + sourceConfigType = toSourceConfigType(xcp.text()); + } break; case DESCRIPTION_FIELD: if (xcp.currentToken() == XContentParser.Token.VALUE_NULL) { diff --git a/src/main/java/org/opensearch/securityanalytics/threatIntel/model/SATIFSourceConfigDto.java b/src/main/java/org/opensearch/securityanalytics/threatIntel/model/SATIFSourceConfigDto.java index 3ba64d47a..222a345ed 100644 --- a/src/main/java/org/opensearch/securityanalytics/threatIntel/model/SATIFSourceConfigDto.java +++ b/src/main/java/org/opensearch/securityanalytics/threatIntel/model/SATIFSourceConfigDto.java @@ -123,7 +123,7 @@ private List convertToIocDtos(List 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 iocTypes, boolean enabledForScan) { + boolean isEnabled, List iocTypes, boolean enabledForScan) { this.id = id == null ? UUIDs.base64UUID() : id; this.version = version != null ? version : NO_VERSION; this.name = name; @@ -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 iocTypes = new ArrayList<>(); boolean enabledForScan = true; @@ -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; + } else { + sourceConfigType = toSourceConfigType(xcp.text()); + } break; case DESCRIPTION_FIELD: if (xcp.currentToken() == XContentParser.Token.VALUE_NULL) { @@ -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; diff --git a/src/test/java/org/opensearch/securityanalytics/action/IndexTIFSourceConfigRequestTests.java b/src/test/java/org/opensearch/securityanalytics/action/IndexTIFSourceConfigRequestTests.java index b572d2ca6..e40516e25 100644 --- a/src/test/java/org/opensearch/securityanalytics/action/IndexTIFSourceConfigRequestTests.java +++ b/src/test/java/org/opensearch/securityanalytics/action/IndexTIFSourceConfigRequestTests.java @@ -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; @@ -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")); + } } \ No newline at end of file From 0bb0651282a11da0fb25ac6a0ba205c271a662e3 Mon Sep 17 00:00:00 2001 From: Joanne Wang Date: Tue, 29 Oct 2024 15:03:58 -0700 Subject: [PATCH 2/3] add parsing tests Signed-off-by: Joanne Wang --- .../model/SATIFSourceConfigDtoTests.java | 32 +++++++++++++++++ .../model/SATIFSourceConfigTests.java | 34 +++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/src/test/java/org/opensearch/securityanalytics/model/SATIFSourceConfigDtoTests.java b/src/test/java/org/opensearch/securityanalytics/model/SATIFSourceConfigDtoTests.java index c9215af5a..e5b2ed7c5 100644 --- a/src/test/java/org/opensearch/securityanalytics/model/SATIFSourceConfigDtoTests.java +++ b/src/test/java/org/opensearch/securityanalytics/model/SATIFSourceConfigDtoTests.java @@ -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; @@ -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(); diff --git a/src/test/java/org/opensearch/securityanalytics/model/SATIFSourceConfigTests.java b/src/test/java/org/opensearch/securityanalytics/model/SATIFSourceConfigTests.java index 2687907d1..8fa8ec395 100644 --- a/src/test/java/org/opensearch/securityanalytics/model/SATIFSourceConfigTests.java +++ b/src/test/java/org/opensearch/securityanalytics/model/SATIFSourceConfigTests.java @@ -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; @@ -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(); From ec8e5d986bce81fe0454ede93e844f21389cf001 Mon Sep 17 00:00:00 2001 From: Joanne Wang Date: Tue, 29 Oct 2024 16:23:05 -0700 Subject: [PATCH 3/3] add additional validation Signed-off-by: Joanne Wang --- .../common/SourceConfigDtoValidator.java | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/opensearch/securityanalytics/threatIntel/common/SourceConfigDtoValidator.java b/src/main/java/org/opensearch/securityanalytics/threatIntel/common/SourceConfigDtoValidator.java index 80c56c46e..4a5ab1446 100644 --- a/src/main/java/org/opensearch/securityanalytics/threatIntel/common/SourceConfigDtoValidator.java +++ b/src/main/java/org/opensearch/securityanalytics/threatIntel/common/SourceConfigDtoValidator.java @@ -5,7 +5,6 @@ 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; @@ -13,9 +12,8 @@ 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 @@ -24,12 +22,27 @@ public class SourceConfigDtoValidator { public List validateSourceConfigDto(SATIFSourceConfigDto sourceConfigDto) { List 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) {