Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add validation for threat intel source config #1393

Merged
merged 3 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a follow-up item, let's add validation for the refresh frequency.

List<String> errorMsgs = new ArrayList<>();

if (sourceConfigDto.getIocTypes().isEmpty()) {
if (sourceConfigDto.getName() == null || sourceConfigDto.getName().isEmpty()) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added validation for names based on how frontend currently validates the name

errorMsgs.add("Name must not be empty");
}

if (sourceConfigDto.getFormat() == null || sourceConfigDto.getFormat().isEmpty()) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may need to add length validation for this as well. Unless this gets validated somewhere downstream, the config could be created via backend API with something like 300k characters; which could disrupt functionality.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added length validation

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