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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
use crate::{
base::{CommonPropertiesBuilder, Stix},
error::{CheckContext, Outcome, StixError as Error},
relationship_objects::types::RelationshipType,
types::{
DictionaryValue, ExternalReference, GranularMarking, Identifier, StixDictionary, Timestamp,
},
};
use std::str::FromStr;
use crate::relationship_objects::{
Relationship, RelationshipObject, RelationshipObjectType, Sighting,
};
/// Creates a new STIX 2.1 `RelationshipObject` of the given type
#[derive(Clone, Debug)]
pub struct RelationshipObjectBuilder {
object_type: RelationshipObjectType,
common_properties: CommonPropertiesBuilder,
description: Option<String>,
}
impl RelationshipObjectBuilder {
/// Creates a new STIX 2.1 RelationshipObjectBuilder of the for the given relationship type between two object Identifiers
///
/// The `source`, `target`, and `relationship_type` fields must be lowercase, with words separated by `-`
///
/// Automatically generates an `id`
/// Other fields are set to their Default (which is `None`` for optional fields)
pub fn new(
source: Identifier,
target: Identifier,
relationship_type: &str,
) -> Result<RelationshipObjectBuilder, Error> {
// Get the relationship type if it is in the STIX list of relationship types, or else create a custom relationship type
let final_relationship_type =
RelationshipType::from_str(&relationship_type.replace("-", ""))
.unwrap_or_else(|_| RelationshipType::Custom(relationship_type.to_string()));
// Validate that the relationship type is allowed between the source and target (this will never return an error for a custom type)
final_relationship_type.validate(&source, &target)?;
// Build the common properties with generated and default values
let common_properties = CommonPropertiesBuilder::new("sro", "relationship")?;
// The initial inner `Relationship` struct will be created with default values other than the provided relationship information
Ok(RelationshipObjectBuilder {
object_type: RelationshipObjectType::Relationship(Relationship {
relationship_type: final_relationship_type,
source_ref: source,
target_ref: target,
start_time: Default::default(),
stop_time: Default::default(),
}),
common_properties,
description: Default::default(),
})
}
/// Creates a new STIX 2.1 RelationshipObjectBuilder for a sighting of an object Identifier
///
/// The `type` field must be lowercase, with words separated by `-`
///
/// Automatically generates an `id`
/// Other fields are set to their Default (which is `None`` for optional fields)
pub fn new_sighting(sighting_of_ref: Identifier) -> Result<RelationshipObjectBuilder, Error> {
let common_properties = CommonPropertiesBuilder::new("sro", "sighting")?;
// The initial inner `Sighting` struct will be created with default values other than the provided `sighting_of_ref`
Ok(RelationshipObjectBuilder {
object_type: RelationshipObjectType::Sighting(Sighting {
first_seen: Default::default(),
last_seen: Default::default(),
count: Default::default(),
sighting_of_ref,
observed_data_refs: Default::default(),
where_sighted_refs: Default::default(),
summary: Default::default(),
}),
common_properties,
description: Default::default(),
})
}
/// Create a new STIX 2.1 `RelationshipObjectBuilder` by cloning the fields from an existing `RelationshipObject`
/// When built, this will create `RelationshipObject` as a newer version of the original object.
pub fn version(old: &RelationshipObject) -> Result<RelationshipObjectBuilder, Error> {
if old.is_revoked() {
return Err(Error::UnableToVersion(format!(
"SRO {} is revoked. Versioning a revoked object is prohibited.",
old.common_properties.id
)));
}
let object_type = old.object_type.clone();
let description = old.description.clone();
let old_properties = old.common_properties.clone();
let common_properties = CommonPropertiesBuilder::version("sro", &old_properties)?;
Ok(RelationshipObjectBuilder {
object_type,
common_properties,
description,
})
}
/// Create a builder from an already-parsed SRO, preserving its `id`, `created`,
/// `modified`, and `revoked` properties exactly. Unlike `version()`, this does
/// not treat the object as the basis for a new version and does not reject
/// revoked objects.
pub fn from_parsed(old: &RelationshipObject) -> Result<RelationshipObjectBuilder, Error> {
let object_type = old.object_type.clone();
let description = old.description.clone();
let old_properties = old.common_properties.clone();
let common_properties = CommonPropertiesBuilder::from_existing("sro", &old_properties)?;
Ok(RelationshipObjectBuilder {
object_type,
common_properties,
description,
})
}
// Setter functions for optional properties common to both SRO types
/// Set the optional `created_by_ref` field for an SRO under construction.
/// This is only allowed when creating a new SRO, not when versioning an existing one,
/// as only the original creator of an object can version it.
pub fn created_by_ref(mut self, id: Identifier) -> Result<Self, Error> {
self.common_properties = self.common_properties.clone().created_by_ref(id)?;
Ok(self)
}
/// Set the optional `labels` field for an SRO under construction.
pub fn labels(mut self, labels: Vec<String>) -> Self {
self.common_properties = self.common_properties.clone().labels(labels);
self
}
/// Set the optional `confidence` field for an SRO under construction.
pub fn confidence(mut self, confidence: u8) -> Self {
self.common_properties = self.common_properties.clone().confidence(confidence);
self
}
/// Set the optional `lang` field for an SRO under construction.
/// If the language is English ("en"), this does not need to be set (but it can be if specificity is desired).
pub fn lang(mut self, language: String) -> Self {
self.common_properties = self.common_properties.clone().lang(language);
self
}
/// Set the optional `external_references` field for an SRO under construction.
pub fn external_references(mut self, references: Vec<ExternalReference>) -> Self {
self.common_properties = self
.common_properties
.clone()
.external_references(references);
self
}
/// Set the optional `object_marking_refs` field for an SRO under construction.
pub fn object_marking_refs(mut self, references: Vec<Identifier>) -> Self {
self.common_properties = self
.common_properties
.clone()
.object_marking_refs(references);
self
}
/// Set the optional `granular_markings` field for an SRO under construction.
pub fn granular_markings(mut self, markings: Vec<GranularMarking>) -> Self {
self.common_properties = self.common_properties.clone().granular_markings(markings);
self
}
/// Add an optional extension to the `extensions` field for an SRO under construction, creating the field if it does not exist
pub fn add_extension(
mut self,
key: &str,
extension: StixDictionary<DictionaryValue>,
) -> Result<Self, Error> {
self.common_properties = self
.common_properties
.clone()
.add_extension(key, extension)?;
Ok(self)
}
/// Set the optional `description` field for an SRO under construction
pub fn description(mut self, description: String) -> Self {
self.description = Some(description);
self
}
// Setter functions for properties that exist only for generic "relationship" SROs
// These functions will error if you try to set these properties for a "sighting"
/// Set the optional `start time` field for a generic SRO under construction.
pub fn start_time(mut self, datetime: &str) -> Result<Self, Error> {
if let RelationshipObjectType::Relationship(ref mut relationship) = self.object_type {
let start_time = Timestamp(datetime.parse().map_err(Error::DateTimeError)?);
relationship.start_time = Some(start_time);
return Ok(self);
}
Err(Error::IllegalBuilderProperty {
object: "SROs".to_string(),
object_type: self.object_type.to_string(),
field: "start time".to_string(),
})
}
/// Set the optional `stop time` field for a generic SRO under construction.
pub fn stop_time(mut self, datetime: &str) -> Result<Self, Error> {
if let RelationshipObjectType::Relationship(ref mut relationship) = self.object_type {
let stop_time = Timestamp(datetime.parse().map_err(Error::DateTimeError)?);
relationship.stop_time = Some(stop_time);
return Ok(self);
}
Err(Error::IllegalBuilderProperty {
object: "SROs".to_string(),
object_type: self.object_type.to_string(),
field: "stop time".to_string(),
})
}
// Setter functions for properties that exist only for "sighting" SROs
// These functions will error if you try the property for a a generic "relationship"
/// Set the optional `first_seen` field for a sightings SRO under construction.
pub fn first_seen(mut self, datetime: &str) -> Result<Self, Error> {
if let RelationshipObjectType::Sighting(ref mut sighting) = self.object_type {
let first_seen = Timestamp(datetime.parse().map_err(Error::DateTimeError)?);
sighting.first_seen = Some(first_seen);
return Ok(self);
}
Err(Error::IllegalBuilderProperty {
object: "SROs".to_string(),
object_type: self.object_type.to_string(),
field: "first seen".to_string(),
})
}
/// Set the optional `last_seen` field for a sightings SRO under construction.
pub fn last_seen(mut self, datetime: &str) -> Result<Self, Error> {
if let RelationshipObjectType::Sighting(ref mut sighting) = self.object_type {
let last_seen = Timestamp(datetime.parse().map_err(Error::DateTimeError)?);
sighting.last_seen = Some(last_seen);
return Ok(self);
}
Err(Error::IllegalBuilderProperty {
object: "SROs".to_string(),
object_type: self.object_type.to_string(),
field: "last seen".to_string(),
})
}
/// Set the optional `count` field for a sightings SRO under construction.
pub fn count(mut self, count: u64) -> Result<Self, Error> {
if let RelationshipObjectType::Sighting(ref mut sighting) = self.object_type {
sighting.count = Some(count);
return Ok(self);
}
Err(Error::IllegalBuilderProperty {
object: "SROs".to_string(),
object_type: self.object_type.to_string(),
field: "count".to_string(),
})
}
/// Set the optional `observed_data_refs` field for a sightings SRO under construction.
pub fn observed_data_refs(
mut self,
observed_data_refs: Vec<Identifier>,
) -> Result<Self, Error> {
if let RelationshipObjectType::Sighting(ref mut sighting) = self.object_type {
sighting.observed_data_refs = Some(observed_data_refs);
return Ok(self);
}
Err(Error::IllegalBuilderProperty {
object: "SROs".to_string(),
object_type: self.object_type.to_string(),
field: "observed data refs".to_string(),
})
}
/// Set the optional `where_sighted_refs` field for a sightings SRO under construction.
pub fn where_sighted_refs(
mut self,
where_sighted_refs: Vec<Identifier>,
) -> Result<Self, Error> {
if let RelationshipObjectType::Sighting(ref mut sighting) = self.object_type {
sighting.where_sighted_refs = Some(where_sighted_refs);
return Ok(self);
}
Err(Error::IllegalBuilderProperty {
object: "SROs".to_string(),
object_type: self.object_type.to_string(),
field: "where_sighted_refs".to_string(),
})
}
/// Set the optional `summary` field for a sightings SRO under construction to `Some(true)`.
pub fn set_summary(mut self) -> Result<Self, Error> {
if let RelationshipObjectType::Sighting(ref mut sighting) = self.object_type {
sighting.summary = Some(true);
return Ok(self);
}
Err(Error::IllegalBuilderProperty {
object: "SROs".to_string(),
object_type: self.object_type.to_string(),
field: "summary".to_string(),
})
}
/// Builds a new SRO without running `stix_check()` validation.
///
/// This assembles the final `RelationshipObject` from the builder, but skips
/// the object-specific `stix_check()`. It is intended for callers that have
/// already validated the object and only need its typed representation
/// (e.g. serialization).
pub fn build_no_validate(self) -> Result<RelationshipObject, Error> {
let common_properties = self.common_properties.build();
let sro = RelationshipObject {
object_type: self.object_type,
common_properties,
description: self.description,
};
Ok(sro)
}
/// Builds a new SRO, using the information found in the `RelationshipObjectBuilder`.
///
/// This assembles the final `RelationshipObject` from the builder and then
/// runs `stix_check()` on it.
pub fn build(self) -> Result<RelationshipObject, Error> {
let object = self.build_no_validate()?;
object.stix_check()?;
Ok(object)
}
/// Builds the object and returns its validation outcome, including warnings.
pub fn build_outcome(self) -> Outcome<RelationshipObject> {
let object = match self.build_no_validate() {
Ok(object) => object,
Err(error) => return Outcome::err(error, Vec::new()),
};
let mut ctx = CheckContext::new();
let result = object.check(&mut ctx).map(|()| object);
Outcome::from_result(result, ctx.into_warnings())
}
}