From ca9fa6b6935744bac8bfbd4e436ca40b084fcb82 Mon Sep 17 00:00:00 2001 From: Joanne Wang Date: Fri, 25 Oct 2024 07:17:14 -0700 Subject: [PATCH] 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