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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244


use std::collections::HashMap;

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use struct_metadata::Described;

use crate::{JsonMap, Sha256, ExpandingClassification, ElasticMeta, Sid, ClassificationString};


/// Model of Submission
#[derive(Serialize, Deserialize, Debug, Described, Clone)]
#[metadata_type(ElasticMeta)]
#[metadata(index=true, store=true)]
pub struct Submission {
    // pub archive_ts = odm.Optional(odm.Date(store=False, description="Archiving timestamp (Deprecated)"))
    /// Document is present in the malware archive
    pub archived: bool,
    /// Classification of the submission
    #[serde(flatten)]
    pub classification: ExpandingClassification,
    /// Total number of errors in the submission
    pub error_count: i32,
    /// List of error keys
    #[metadata(store=false)]
    pub errors: Vec<String>,
    /// Expiry timestamp
    #[metadata(store=false)]
    pub expiry_ts: Option<DateTime<Utc>>,
    /// Total number of files in the submission
    pub file_count: i32,
    /// List of files that were originally submitted
    // pub files: Vec<File>,
    pub file: File,
    /// Maximum score of all the files in the scan
    pub max_score: i32,
    /// Metadata associated to the submission
    #[metadata(store=false)]
    pub metadata: HashMap<String, String>,
    /// Submission parameter details
    pub params: SubmissionParams,
    /// List of result keys
    #[metadata(store=false)]
    pub results: Vec<String>,
    /// Submission ID
    #[metadata(copyto="__text__")]
    pub sid: Sid,
    /// Status of the submission
    pub state: SubmissionState,
    /// This document is going to be deleted as soon as it finishes
    pub to_be_deleted: bool,
    /// Submission-specific times
    pub times: Times,
    /// Malicious verdict details
    pub verdict: Verdict,
    /// Was loaded from the archive
    #[metadata(index=false)]
    pub from_archive: bool,

    /// the filescore key, used in deduplication. This is a non-unique key, that is
    /// shared by submissions that may be processed as duplicates.
    #[metadata(index=false, store=false)]
    pub scan_key: Option<String>,
}

/// Submission Parameters
#[derive(Serialize, Deserialize, Debug, Described, Clone)]
#[metadata_type(ElasticMeta)]
#[metadata(index=true, store=false)]
pub struct SubmissionParams {
    /// classification of the submission
    pub classification: ClassificationString,
    /// Should a deep scan be performed?
    pub deep_scan: bool,
    /// Description of the submission
    #[serde(skip_serializing_if = "Option::is_none")]
    #[metadata(store=true, copyto="__text__")]
    pub description: Option<String>,
    /// Should this submission generate an alert?
    pub generate_alert: bool,
    /// List of groups related to this scan
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub groups: Vec<String>,
    /// Ignore the cached service results?
    pub ignore_cache: bool,
    /// Should we ignore dynamic recursion prevention?
    pub ignore_dynamic_recursion_prevention: bool,
    /// Should we ignore filtering services?
    pub ignore_filtering: bool,
    /// Ignore the file size limits?
    pub ignore_size: bool,
    /// Exempt from being dropped by ingester?
    pub never_drop: bool,
    /// Is the file submitted already known to be malicious?
    pub malicious: bool,
    /// Max number of extracted files
    pub max_extracted: u32,
    /// Max number of supplementary files
    pub max_supplementary: u32,
    /// Priority of the scan
    pub priority: u16,
    /// Should the submission do extra profiling?
    pub profile: bool,
    /// Does this submission count against quota?
    pub quota_item: bool,
    /// Service selection
    pub services: ServiceSelection,
    /// Service-specific parameters
    #[metadata(index=false, store=false)]
    pub service_spec: HashMap<String, JsonMap>,
    /// User who submitted the file
    #[metadata(store=true, copyto="__text__")]
    pub submitter: String,
    /// Time, in days, to live for this submission
    pub ttl: u32,
    /// Type of submission
    #[serde(rename="type")]
    pub submission_type: String,
    /// Initialization for temporary submission data
    #[serde(skip_serializing_if = "Option::is_none")]
    #[metadata(index=false)]
    pub initial_data: Option<String>,
    /// Does the submission automatically goes into the archive when completed?
    pub auto_archive: bool,
    /// When the submission is archived, should we delete it from hot storage right away?
    pub delete_after_archive: bool
}

impl Default for SubmissionParams {
    fn default() -> Self {
        Self {
            classification: ClassificationString::new("".to_owned()).unwrap(),
            deep_scan: false,
            description: None,
            generate_alert: false,
            groups: vec![],
            ignore_cache: false,
            ignore_dynamic_recursion_prevention: false,
            ignore_filtering: false,
            ignore_size: false,
            never_drop: false,
            malicious: false,
            max_extracted: 100,
            max_supplementary: 100,
            priority: 100,
            profile: false,
            quota_item: false,
            services: Default::default(),
            service_spec: Default::default(),
            submitter: "USER".to_owned(),
            ttl: 30,
            submission_type: "USER".to_owned(),
            initial_data: None,
            auto_archive: false,
            delete_after_archive: false,
        }
    }
}


/// Service Selection Scheme
#[derive(Serialize, Deserialize, Default, Debug, Described, Clone)]
#[metadata_type(ElasticMeta)]
#[metadata(index=false, store=false)]
pub struct ServiceSelection {
    /// List of selected services
    #[serde(skip_serializing_if = "Option::is_none")]
    pub selected: Option<Vec<String>>,
    /// List of excluded services
    #[serde(skip_serializing_if = "Option::is_none")]
    pub excluded: Option<Vec<String>>,
    /// List of services to rescan when moving between systems
    #[serde(skip_serializing_if = "Option::is_none")]
    pub rescan: Option<Vec<String>>,
    /// Add to service selection when resubmitting
    #[serde(skip_serializing_if = "Option::is_none")]
    pub resubmit: Option<Vec<String>>,
}

/// Submission-Relevant Times
#[derive(Serialize, Deserialize, Debug, Described, Clone)]
#[metadata_type(ElasticMeta)]
#[metadata(index=true, store=true)]
pub struct Times {
    /// Date at which the submission finished scanning
    #[metadata(store=false)]
    pub completed: Option<DateTime<Utc>>,
    /// Date at which the submission started scanning
    pub submitted: DateTime<Utc>,
}


/// Submission Verdict
#[derive(Serialize, Deserialize, Debug, Described, Clone)]
#[metadata_type(ElasticMeta)]
#[metadata(index=true, store=false)]
pub struct Verdict {
    /// List of user that thinks this submission is malicious
    #[serde(default)]
    pub malicious: Vec<String>,
    /// List of user that thinks this submission is non-malicious
    #[serde(default)]
    pub non_malicious: Vec<String>,
}

#[derive(Serialize, Debug, PartialEq, Eq, strum::Display, Described, Clone, Copy)]
#[metadata_type(ElasticMeta)]
pub enum SubmissionState {
    Failed,
    Submitted,
    Completed,
}

impl<'de> Deserialize<'de> for SubmissionState {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>
    {
        let string = String::deserialize(deserializer)?;
        match string.to_lowercase().as_str() {
            "failed" => Ok(Self::Failed),
            "submitted" => Ok(Self::Submitted),
            "completed" => Ok(Self::Completed),
            _ => Err(serde::de::Error::custom("unparsable submission state")),
        }
    }
}


/// File Model of Submission
#[derive(Serialize, Deserialize, Debug, Described, Clone)]
#[metadata_type(ElasticMeta)]
#[metadata(index=true, store=false)]
pub struct File {
    /// Name of the file
    #[metadata(copyto="__text__")]
    pub name: String,
    /// Size of the file in bytes
    pub size: Option<u64>,
    /// SHA256 hash of the file
    #[metadata(copyto="__text__")]
    pub sha256: Sha256,
}