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

[Backport 2.17] Add validation for threat intel source config #1408

Merged
merged 1 commit into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -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 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 @@
}
}

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 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 @@
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 @@
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 @@

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 @@
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 @@
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 @@
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