Skip to content

Commit

Permalink
Wrapping exceptions.
Browse files Browse the repository at this point in the history
Signed-off-by: AWSHurneyt <[email protected]>
  • Loading branch information
AWSHurneyt committed Jul 18, 2024
1 parent 8af8542 commit 163b9e4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.opensearch.core.xcontent.XContentParserUtils;
import org.opensearch.securityanalytics.commons.model.IOCType;
import org.opensearch.securityanalytics.commons.model.STIX2;
import org.opensearch.securityanalytics.util.SecurityAnalyticsException;
import org.opensearch.securityanalytics.util.XContentUtils;

import java.io.IOException;
Expand Down Expand Up @@ -205,7 +206,13 @@ public static STIX2IOC parse(XContentParser xcp, String id, Long version) throws
name = xcp.text();
break;
case TYPE_FIELD:
type = new IOCType(xcp.text());
String typeString = xcp.text();
try {;
type = new IOCType(typeString);
} catch (Exception e) {
logger.error("Could not determine IOC type '{}':", typeString, e);
throw SecurityAnalyticsException.wrap(e);
}
break;
case VALUE_FIELD:
value = xcp.text();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

package org.opensearch.securityanalytics.model;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.core.common.io.stream.StreamOutput;
import org.opensearch.core.common.io.stream.Writeable;
Expand All @@ -14,6 +16,7 @@
import org.opensearch.core.xcontent.XContentParserUtils;
import org.opensearch.securityanalytics.commons.model.IOCType;
import org.opensearch.securityanalytics.commons.model.STIX2;
import org.opensearch.securityanalytics.util.SecurityAnalyticsException;

import java.io.IOException;
import java.time.Instant;
Expand All @@ -25,6 +28,8 @@
* A data transfer object for the [STIX2IOC] data model.
*/
public class STIX2IOCDto implements Writeable, ToXContentObject {
private static final Logger logger = LogManager.getLogger(STIX2IOCDto.class);

private String id;
private String name;
private IOCType type;
Expand Down Expand Up @@ -175,7 +180,13 @@ public static STIX2IOCDto parse(XContentParser xcp, String id, Long version) thr
name = xcp.text();
break;
case STIX2.TYPE_FIELD:
type = new IOCType(xcp.text());
String typeString = xcp.text();
try {;
type = new IOCType(typeString);
} catch (Exception e) {
logger.error("Could not determine IOC type '{}':", typeString, e);
throw SecurityAnalyticsException.wrap(e);
}
break;
case STIX2.VALUE_FIELD:
value = xcp.text();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import com.amazonaws.AmazonServiceException;
import com.amazonaws.SdkClientException;
import com.google.common.collect.ImmutableList;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import org.apache.logging.log4j.LogManager;
Expand Down Expand Up @@ -58,6 +59,7 @@
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;
Expand All @@ -72,6 +74,7 @@
public class STIX2IOCFetchService {
private final Logger log = LogManager.getLogger(STIX2IOCFetchService.class);
private final String ENDPOINT_CONFIG_PATH = "/threatIntelFeed/internalAuthEndpoint.txt";
private final int MAX_REGION_LENGTH = 20;

private Client client;
private ClusterService clusterService;
Expand Down Expand Up @@ -108,7 +111,7 @@ public void onlyIndexIocs(SATIFSourceConfig saTifSourceConfig,
feedStore.indexIocs(stix2IOCList);
} catch (Exception e) {
log.error("Failed to index IOCs from source config", e);
listener.onFailure(e);
listener.onFailure(SecurityAnalyticsException.wrap(e));
}
}

Expand All @@ -122,15 +125,15 @@ public void downloadAndIndexIOCs(SATIFSourceConfig saTifSourceConfig, ActionList
s3Connector.load(consumer);
} catch (Exception e) {
log.error("Failed to download IOCs.", e);
listener.onFailure(e);
listener.onFailure(SecurityAnalyticsException.wrap(e));
return;
}

try {
consumer.flushIOCs();
} catch (Exception e) {
log.error("Failed to flush IOCs queue.", e);
listener.onFailure(e);
listener.onFailure(SecurityAnalyticsException.wrap(e));
}
}

Expand Down Expand Up @@ -222,7 +225,18 @@ private void validateS3ConnectorConfig(S3ConnectorConfig s3ConnectorConfig) {
}

if (s3ConnectorConfig.getRegion() == null || s3ConnectorConfig.getRegion().isEmpty()) {
throw new IllegalArgumentException("Region is required.");
throw SecurityAnalyticsException.wrap(new IllegalArgumentException("Region is required."));
}

if (s3ConnectorConfig.getRegion().length() > MAX_REGION_LENGTH) {
String error = String.format(
"[%s] field contains %s characters. Max character length is %s.",
S3Source.REGION_FIELD,
s3ConnectorConfig.getRegion().length(),
MAX_REGION_LENGTH
);
log.error(error);
throw SecurityAnalyticsException.wrap(new IllegalArgumentException(error));
}
}

Expand Down Expand Up @@ -264,13 +278,13 @@ public void downloadFromUrlAndIndexIOCs(SATIFSourceConfig saTifSourceConfig, Act
}
} catch (Exception e) {
log.error("Failed to download the IoCs in CSV format for source " + saTifSourceConfig.getId());
listener.onFailure(e);
listener.onFailure(SecurityAnalyticsException.wrap(e));
return;
}
break;
default:
log.error("unsupported feed format for url download:" + source.getFeedFormat());
listener.onFailure(new UnsupportedOperationException("unsupported feed format for url download:" + source.getFeedFormat()));
listener.onFailure(SecurityAnalyticsException.wrap(new UnsupportedOperationException("unsupported feed format for url download:" + source.getFeedFormat())));
}
}

Expand Down

0 comments on commit 163b9e4

Please sign in to comment.