-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathUrlDownloadSource.java
More file actions
126 lines (111 loc) · 4.53 KB
/
UrlDownloadSource.java
File metadata and controls
126 lines (111 loc) · 4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package org.opensearch.securityanalytics.threatIntel.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;
import org.opensearch.core.xcontent.ToXContent;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.core.xcontent.XContentParser;
import java.io.IOException;
import java.net.URL;
import java.util.Locale;
/**
* This is a Threat Intel Source config where the iocs are downloaded from the URL
*/
public class UrlDownloadSource extends Source implements Writeable, ToXContent {
private static final Logger log = LogManager.getLogger(UrlDownloadSource.class);
public static final String URL_FIELD = "url";
public static final String FEED_FORMAT_FIELD = "feed_format";
public static final String HAS_CSV_HEADER_FIELD = "has_csv_header_field";
public static final String CSV_IOC_VALUE_COLUMN_NUM_FIELD = "csv_ioc_value_colum_num";
public static final String SOURCE_NAME = "URL_DOWNLOAD";
private final URL url;
private final String feedFormat;
private final Boolean hasCsvHeader;
private final Integer csvIocValueColumnNo;
public UrlDownloadSource(URL url, String feedFormat, Boolean hasCsvHeader, Integer csvIocValueColumnNo) {
this.url = url;
this.feedFormat = feedFormat;
this.hasCsvHeader = hasCsvHeader;
this.csvIocValueColumnNo = csvIocValueColumnNo;
}
public UrlDownloadSource(StreamInput sin) throws IOException {
this(
new URL(sin.readString()),
sin.readString(),
sin.readOptionalBoolean(),
sin.readOptionalInt()
);
}
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeString(url.toString());
out.writeString(feedFormat);
out.writeOptionalBoolean(hasCsvHeader);
out.writeOptionalInt(csvIocValueColumnNo);
}
@Override
String name() {
return SOURCE_NAME;
}
public URL getUrl() {
return url;
}
public static UrlDownloadSource parse(XContentParser xcp) throws IOException {
URL url = null;
String feedFormat = null;
Boolean hasCsvHeader = false;
Integer csvIocValueColumnNo = null;
while (xcp.nextToken() != XContentParser.Token.END_OBJECT) {
String fieldName = xcp.currentName();
xcp.nextToken();
switch (fieldName) {
case URL_FIELD:
String urlString = xcp.text();
url = new URL(urlString);
String protocol = url.getProtocol().toLowerCase(Locale.ROOT);
if (!"http".equals(protocol) && !"https".equals(protocol)) {
log.error("Unsupported protocol [{}]. Only http and https are allowed. Url:{}", protocol, urlString);
throw new IOException("Unsupported protocol [" + protocol + "]. Only http and https are allowed.");
}
break;
case FEED_FORMAT_FIELD:
feedFormat = xcp.text();
break;
case HAS_CSV_HEADER_FIELD:
hasCsvHeader = xcp.booleanValue();
break;
case CSV_IOC_VALUE_COLUMN_NUM_FIELD:
if (xcp.currentToken() == null)
xcp.skipChildren();
else
csvIocValueColumnNo = xcp.intValue();
break;
default:
xcp.skipChildren();
}
}
return new UrlDownloadSource(url, feedFormat, hasCsvHeader, csvIocValueColumnNo);
}
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
return builder.startObject()
.startObject(URL_DOWNLOAD_FIELD)
.field(URL_FIELD, url.toString())
.field(FEED_FORMAT_FIELD, feedFormat)
.field(HAS_CSV_HEADER_FIELD, hasCsvHeader)
.field(CSV_IOC_VALUE_COLUMN_NUM_FIELD, csvIocValueColumnNo)
.endObject()
.endObject();
}
public String getFeedFormat() {
return feedFormat;
}
public boolean hasCsvHeader() {
return hasCsvHeader;
}
public Integer getCsvIocValueColumnNo() {
return csvIocValueColumnNo;
}
}