Skip to content

Commit

Permalink
add validation for source config and allow null to be read in parser
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 25, 2024
1 parent d5c8f7a commit ca9fa6b
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,19 @@ public class SourceConfigDtoValidator {
public List<String> validateSourceConfigDto(SATIFSourceConfigDto sourceConfigDto) {
List<String> 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()) {
Expand All @@ -34,34 +46,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");
}
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;
}
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;
} 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;
} 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"));
}
}

0 comments on commit ca9fa6b

Please sign in to comment.