stixflayer 0.1.1

Fast, spec-correct STIX 2.1 parsing, validation, and serialization in Rust.
Documentation
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
//! Data structures and functions for implementing Extension Definition SMOs

use crate::{
    base::{CommonProperties, CommonPropertiesBuilder, Stix},
    error::{add_error, return_multiple_errors, CheckContext, Outcome, StixError as Error},
    relationship_objects::{Related, RelationshipObjectBuilder},
    types::{ExtensionType, ExternalReference, GranularMarking, Identified, Identifier, Timestamp},
    validation::validate_value,
};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use serde_with::skip_serializing_none;
use std::collections::BTreeMap;
use stixflayer_derive::StixProperties;

/// An Extension Definition Stix Meta Object (SMO).
///
/// The STIX Extension Definition object allows producers of threat intelligence to extend existing STIX objects or to create entirely new STIX objects in a standardized way.
/// This object contains detailed information about the extension and any additional properties and or objects that it defines. This extension mechanism **MUST NOT** be used
/// to redefine existing standardized objects or properties.
///
/// There are three ways to extend STIX using STIX Extensions.
///
/// 1. Define one or more new STIX Object types.
/// 2. Define additional properties for an existing STIX Object type as a nested property extension. This is typically done to represent a sub-component or module of one or
/// more STIX Object types.
/// 3. Define additional properties for an existing STIX Object type at the object's top-level. This can be done to represent properties that form an inherent part of the
/// definition of an object type.
///
/// When defining a STIX extension using the nested property extension mechanism the `extensions` property **MUST** include the extension definition's UUID that defines the extension
/// definition object and the `extension_type` property.
///
/// The three uses of this extension facility **MAY** be combined into a single Extension Definition object when appropriate.
///
/// For more information see <https://docs.oasis-open.org/cti/stix/v2.1/os/stix-v2.1-os.html#_32j232tfvtly>
#[skip_serializing_none]
#[derive(Clone, Debug, PartialEq, Eq, Default, Serialize, Deserialize, StixProperties)]
pub struct ExtensionDefinition {
    /// The STIX type of the object: `extension-definition`.
    #[serde(rename = "type")]
    pub object_type: String,
    /// Common STIX object properties
    #[serde(flatten)]
    pub common_properties: CommonProperties,
    /// A name used for display purposes during execution, development, or debugging.
    pub name: String,
    /// A detailed explanation of what data the extension conveys and how it is intended to be used.
    /// The `schema` property is the normative definition of the extension, and this property, if present, is for documentation purposes only.
    ///
    /// While the description property is optional this property **SHOULD** be populated.
    pub description: Option<String>,
    /// The normative definition of the extension, either as a URL or as plain text explaining the definition.
    ///
    /// A URL **SHOULD** point to a JSON schema or a location that contains information about the schema.
    pub schema: String,
    /// The version of this extension. Producers of STIX extensions are encouraged to follow standard semantic versioning procedures where the version number follows the
    /// pattern, MAJOR.MINOR.PATCH. This will allow consumers to distinguish between the three different levels of compatibility typically identified by such versioning strings.
    pub version: String,
    /// This property specifies one or more extension types contained within this extension.
    ///
    /// The values for this property **MUST** come from the `extension-type-enum` enumeration.
    ///
    /// When this property includes `toplevel-property-extension` then the `extension_properties` property **SHOULD** include one or more property names.
    pub extension_types: Vec<ExtensionType>,
    /// This property contains the list of new property names that are added to an object by an extension.
    ///
    /// This property **MUST** only be used when the `extension_types` property includes a value of `toplevel-property-extension`.
    /// In other words, when new properties are being added at the top-level of an existing object.
    pub extension_properties: Option<Vec<String>>,
}

impl ExtensionDefinition {
    /// Deserializes an ExtensionDefinition SMO from a JSON String.
    /// Checks that all fields conform to the STIX 2.1 standard.
    /// If the `allow_custom` flag is false, checks that there are no fields in the JSON String
    /// that are not in the Extension Definition SMO type definition.
    pub fn from_json(json: &str, allow_custom: bool) -> Result<Self, Error> {
        let value: serde_json::Value =
            serde_json::from_str(json).map_err(|e| Error::DeserializationError(e.to_string()))?;
        validate_value(value, allow_custom, true)
    }

    /// Returns `true` if this object's `revoked` property is set to `true`.
    pub fn is_revoked(&self) -> bool {
        matches!(self.common_properties.revoked, Some(true))
    }

    /// Create a builder for a sighting relationship with this object as the `sighting_of_ref`.
    pub fn add_sighting(self) -> Result<RelationshipObjectBuilder, Error> {
        let sighting_of_ref = self.get_id().to_owned();

        RelationshipObjectBuilder::new_sighting(sighting_of_ref)
    }
}

impl Identified for ExtensionDefinition {
    fn get_id(&self) -> &Identifier {
        &self.common_properties.id
    }
}

impl Related for ExtensionDefinition {
    fn add_relationship<T: Related + Identified>(
        self,
        target: T,
        relationship_type: String,
    ) -> Result<RelationshipObjectBuilder, Error> {
        let source_id = self.get_id().to_owned();
        let target_id = target.get_id().to_owned();

        RelationshipObjectBuilder::new(source_id, target_id, &relationship_type)
    }
}

crate::impl_custom_properties_holder!(ExtensionDefinition);

impl Stix for ExtensionDefinition {
    fn check(&self, ctx: &mut CheckContext) -> Result<(), Error> {
        ctx.set_object_id(Some(self.get_id().to_string()));
        let mut errors = Vec::new();

        // Check common properties
        add_error(&mut errors, self.common_properties.check(ctx));

        // Check that required common properties fields are present and prohibited common properties fields are absent

        // Check that the `spec_version` field exists for Extension Definition SMOs
        if self.common_properties.spec_version.is_none() {
            errors.push(Error::ValidationError(
                "Extension Definition SMOs must have a `spec_version` property.".to_string(),
            ));
        }
        // Check that the `created_by_ref` field exists for Extension Definition SMOs
        if self.common_properties.created_by_ref.is_none() {
            errors.push(Error::ValidationError(
                "Extension Definition SMOs must have a `created_by_ref` property.".to_string(),
            ));
        }
        // Check that the `created` field exists for Extension Definition SMOs
        if self.common_properties.created.is_none() {
            errors.push(Error::ValidationError(
                "Extension Definition SMOs must have a `created` property.".to_string(),
            ));
        }
        // Check that the `modified` field exists for Extension Definition SMOs
        if self.common_properties.modified.is_none() {
            errors.push(Error::ValidationError(
                "Extension Definition SMOs must have a `modified` property.".to_string(),
            ));
        }
        // Check that the `confidence` property is `None` for Extension Definition SMOs
        if self.common_properties.confidence.is_some() {
            errors.push(Error::ValidationError(
                "Extension Definition SMOs cannot have a `confidence` property.".to_string(),
            ));
        }
        // Check that the `lang` property is `None` for Extension Definition SMOs
        if self.common_properties.lang.is_some() {
            errors.push(Error::ValidationError(
                "Extension Definition SMOs cannot have a `lang` property.".to_string(),
            ));
        }
        // Check that the `defanged` property is `None` for Extension Definition SMOs
        if self.common_properties.defanged.is_some() {
            errors.push(Error::ValidationError(
                "Extension Definition SMOs cannot have a `defanged` property.".to_string(),
            ));
        }
        // // Check that the `extensions` property is `None` for Extension Definition SMOs
        // if self.common_properties.extensions.is_some() {
        //     errors.push(Error::ValidationError(
        //         "Extension Definition SMOs cannot have a `extensions` property.".to_string(),
        //     ));
        // }

        // Check specific ExtensionDefinition type constraints
        if self.object_type != "extension-definition" {
            errors.push(Error::ValidationError(
                "Extension Definition SMOs must have a type of 'extenion-definition'".to_string(),
            ));
        }

        if self.description.is_none() || self.description == Some("".to_string()) {
            errors.push(Error::ValidationError(
                "Extension Definition SMOs must have a `description` property.".to_string(),
            ));
        }

        if self
            .extension_types
            .contains(&ExtensionType::ToplevelPropertyExtension)
        {
            if self.extension_properties.is_none() {
                errors.push(Error::ValidationError(
                    "When `extension_types` includes `toplevel-property-extension`, `extension_properties` must include one or more property names.".to_string(),
                ));
            }
        } else if self.extension_properties.is_some() {
            errors.push(Error::ValidationError(
                "The `extension_properties` property must only be used when `extension_types` includes a value of `toplevel-property-extension`.".to_string(),
            ));
        }

        return_multiple_errors(errors)
    }
}

/// Builder struct for Extension Definition SMOs.
///
/// This follows the "Rust builder pattern," where we  use a `new()` function to construct a Builder
/// with a minimum set of required fields, then set additional fields with their own setter functions.
/// Once all fields have been set, the `build()` function will take all of the fields in the Builder
/// struct and use them to create the final `LanguageContent` struct.
///
/// Note: Because the `schema`, `version`, and `extension_types` properties are required for Extension Definition SMOs,
/// the `build()` method will error if those fields are not set before it is called.
#[derive(Clone, Debug)]
pub struct ExtensionDefinitionBuilder {
    /// Common STIX properties
    common_properties: CommonPropertiesBuilder,
    name: String,
    description: Option<String>,
    schema: Option<String>,
    version: Option<String>,
    extension_types: Option<Vec<ExtensionType>>,
    extension_properties: Option<Vec<String>>,
}

impl ExtensionDefinitionBuilder {
    // Creates a new STIX 2.1 `ExtensionDefinitionBuilder` with a given name
    ///
    /// Automatically generates an `id`
    ///
    /// Other fields are set to their Default (which is `None`` for optional fields)
    pub fn new(name: &str) -> Result<Self, Error> {
        // Build the common properties with generated and default values
        let common_properties =
            CommonPropertiesBuilder::new("extension-definition", "extension-definition")?;

        Ok(Self {
            common_properties,
            name: name.to_string(),
            description: Default::default(),
            schema: Default::default(),
            version: Default::default(),
            extension_types: Default::default(),
            extension_properties: Default::default(),
        })
    }

    /// Create a new STIX 2.1 `ExtensionDefinitionBuilder` by cloning the fields from an existing `ExtensionDefinition`
    /// When built, this will create `ExtensionDefinition` as a newer version of the original object.
    pub fn version(old: &ExtensionDefinition) -> Result<ExtensionDefinitionBuilder, Error> {
        if old.is_revoked() {
            return Err(Error::UnableToVersion(format!(
                "ExtensionDefinition {} is revoked. Versioning a revoked object is prohibited.",
                old.common_properties.id
            )));
        }

        let old_properties = old.common_properties.clone();
        let common_properties =
            CommonPropertiesBuilder::version("extension-definition", &old_properties)?;
        let name = old.name.clone();
        let description = old.description.clone();
        let schema = Some(old.schema.clone());
        let version = Some(old.version.clone());
        let extension_types = Some(old.extension_types.clone());
        let extension_properties = old.extension_properties.clone();

        Ok(ExtensionDefinitionBuilder {
            common_properties,
            name,
            description,
            schema,
            version,
            extension_types,
            extension_properties,
        })
    }

    /// Create a builder from an already-parsed ExtensionDefinition, preserving its
    /// `id`, `created`, `modified`, and `revoked` properties exactly. Unlike
    /// `version()`, this does not treat the object as the basis for a new version.
    pub fn from_parsed(old: &ExtensionDefinition) -> Result<Self, Error> {
        let old_properties = old.common_properties.clone();
        let common_properties =
            CommonPropertiesBuilder::from_existing("extension-definition", &old_properties)?;
        let name = old.name.clone();
        let description = old.description.clone();
        let schema = Some(old.schema.clone());
        let version = Some(old.version.clone());
        let extension_types = Some(old.extension_types.clone());
        let extension_properties = old.extension_properties.clone();

        Ok(ExtensionDefinitionBuilder {
            common_properties,
            name,
            description,
            schema,
            version,
            extension_types,
            extension_properties,
        })
    }

    // Setter functions for common properties

    /// Set the optional `created_by_ref` field for a Extension Definition SMO under construction
    /// This is only allowed when creating a new Extension Definition SMO, 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 Extension Definition SMO 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 Extension Definition SMO under construction
    pub fn confidence(mut self, confidence: u8) -> Self {
        self.common_properties = self.common_properties.clone().confidence(confidence);
        self
    }

    /// Set the optional `external_references` field for an Extension Definition SMO 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 Extension Definition SMO 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 Extension Definition SMO under construction.
    pub fn granular_markings(mut self, markings: Vec<GranularMarking>) -> Self {
        self.common_properties = self.common_properties.clone().granular_markings(markings);
        self
    }

    /// Set the `created` timestamp for an Extension Definition SMO under construction.
    pub fn created(mut self, created: Timestamp) -> Self {
        self.common_properties = self.common_properties.clone().created(created);
        self
    }

    /// Set the `modified` timestamp for an Extension Definition SMO under construction.
    pub fn modified(mut self, modified: Timestamp) -> Self {
        self.common_properties = self.common_properties.clone().modified(modified);
        self
    }

    // Setter functions for Extension Definition specific properties

    /// Set the optional `description` field for an Extension Definition SMO under construction
    pub fn description(mut self, description: String) -> Self {
        self.description = Some(description);
        self
    }

    /// Set the required `schema` field for an Extension Definition SMO under construction
    pub fn schema(mut self, schema: String) -> Self {
        self.schema = Some(schema);
        self
    }

    /// Set the required `version` field for an Extension Definition SMO under construction
    pub fn set_version(mut self, version: String) -> Self {
        self.version = Some(version);
        self
    }

    /// Set the required `extension_types` field for an Extension Definition SMO under construction
    pub fn extension_types(mut self, extension_types: Vec<ExtensionType>) -> Self {
        self.extension_types = Some(extension_types);
        self
    }

    /// Set the optional `extension_properties` field for an Extension Definition SMO under construction
    pub fn extension_properties(mut self, extension_properties: Vec<String>) -> Self {
        self.extension_properties = Some(extension_properties);
        self
    }

    /// Set custom properties for the extension definition under construction.
    pub fn custom_properties(mut self, custom_properties: BTreeMap<String, Value>) -> Self {
        self.common_properties = self
            .common_properties
            .clone()
            .custom_properties(custom_properties);
        self
    }

    /// Builds a new Extension Definition SMO without running `stix_check()`
    /// validation.
    ///
    /// This assembles the final `ExtensionDefinition` object 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<ExtensionDefinition, Error> {
        let mut errors = Vec::new();

        // Check that required fields are included before creating the object

        if self.schema.is_none() {
            errors.push(Error::MissingBuilderProperty {
                object_type: "extension-definition".to_string(),
                property: "schema".to_string(),
            })
        }

        if self.version.is_none() {
            errors.push(Error::MissingBuilderProperty {
                object_type: "extension-definition".to_string(),
                property: "version".to_string(),
            })
        }

        if self.extension_types.is_none() {
            errors.push(Error::MissingBuilderProperty {
                object_type: "extension-definition".to_string(),
                property: "extension_types".to_string(),
            })
        }

        return_multiple_errors(errors)?;

        let common_properties = self.common_properties.build();

        let name = self.name;
        let description = self.description;
        // PANIC: All unwraps are safe because we would have returned earlier if any of the Options were None
        let schema = self.schema.unwrap();
        let version = self.version.unwrap();
        let extension_types = self.extension_types.unwrap();
        let extension_properties = self.extension_properties;

        let extension_definition = ExtensionDefinition {
            object_type: "extension-definition".to_string(),
            common_properties,
            name,
            description,
            schema,
            version,
            extension_types,
            extension_properties,
        };

        Ok(extension_definition)
    }

    /// Builds a new Extension Definition SMO, using the information found in the
    /// `ExtensionDefinitionBuilder`.
    ///
    /// This assembles the final `ExtensionDefinition` object from the builder and
    /// then runs `stix_check()` on it.
    pub fn build(self) -> Result<ExtensionDefinition, 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<ExtensionDefinition> {
        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())
    }
}