Skip to content

Commit 72e53e1

Browse files
author
Bryan Worrell
committed
This includes all the changes from the msd_features branch. I just
set this up because the branch was out of date and the merge was going to be pretty painful. * Added unit tests for the add_related_observable() and add_related_indicator() methods.
1 parent 61f5072 commit 72e53e1

4 files changed

Lines changed: 139 additions & 11 deletions

File tree

docs/api/coa/coa.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Classes
99
-------
1010

1111
.. autoclass:: CourseOfAction
12-
:show-inheritance:
12+
:show-inheritance:
1313
:members:
1414

1515
.. autoclass:: RelatedCOAs

stix/incident/__init__.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,86 @@ def coa_taken(self, value):
220220
def add_coa_taken(self, value):
221221
self.coa_taken.append(value)
222222

223+
@property
224+
def related_indicators(self):
225+
return self._related_indicators
226+
227+
@related_indicators.setter
228+
def related_indicators(self, value):
229+
self._set_var(RelatedIndicators, related_indicators=value)
230+
231+
def add_related_indicator(self, value):
232+
"""Adds an Related Indicator to the ``related_indicators`` list
233+
property of this :class:`Incident`.
234+
235+
The `indicator` parameter must be an instance of
236+
:class:`.RelatedIndicator` or :class:`Indicator`.
237+
238+
If the `indicator` parameter is ``None``, no item wil be added to the
239+
``related_indicators`` list property.
240+
241+
Calling this method is the same as calling ``append()`` on the
242+
``related_indicators`` property.
243+
244+
See Also:
245+
The :class:`RelatedIndicators` documentation.
246+
247+
Note:
248+
If the `indicator` parameter is not an instance of
249+
:class:`.RelatedIndicator` an attempt will be
250+
made to convert it to one.
251+
252+
Args:
253+
indicator: An instance of :class:`Indicator` or
254+
:class:`.RelatedIndicator`.
255+
256+
Raises:
257+
ValueError: If the `indicator` parameter cannot be converted into
258+
an instance of :class:`.RelatedIndicator`
259+
260+
"""
261+
self.related_indicators.append(value)
262+
263+
@property
264+
def related_observables(self):
265+
return self._related_observables
266+
267+
@related_observables.setter
268+
def related_observables(self, value):
269+
self._set_var(RelatedObservables, related_observables=value)
270+
271+
def add_related_observable(self, value):
272+
"""Adds a Related Observable to the ``related_observables`` list
273+
property of this :class:`Incident`.
274+
275+
The `observable` parameter must be an instance of
276+
:class:`.RelatedObservable` or :class:`Observable`.
277+
278+
If the `observable` parameter is ``None``, no item will be added to the
279+
``related_observables`` list property.
280+
281+
Calling this method is the same as calling ``append()`` on the
282+
``related_observables`` property.
283+
284+
See Also:
285+
The :class:`RelatedObservables` documentation.
286+
287+
Note:
288+
If the `observable` parameter is not an instance of
289+
:class:`.RelatedObservable` an attempt will be
290+
made to convert it to one.
291+
292+
Args:
293+
observable: An instance of :class:`Observable` or
294+
:class:`.RelatedObservable`.
295+
296+
Raises:
297+
ValueError: If the `value` parameter cannot be converted into
298+
an instance of :class:`.RelatedObservable`
299+
300+
"""
301+
self.related_observables.append(value)
302+
223303
def to_obj(self, return_obj=None, ns_info=None):
224304
if not return_obj:
225305
return_obj = self._binding_class()

stix/incident/time.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,21 @@ class Time(stix.Entity):
1111
_binding_class = _binding.TimeType
1212
_namespace = "http://stix.mitre.org/Incident-1"
1313

14-
def __init__(self):
15-
self.first_malicious_action = None
16-
self.initial_compromise = None
17-
self.first_data_exfiltration = None
18-
self.incident_discovery = None
19-
self.incident_opened = None
20-
self.containment_achieved = None
21-
self.restoration_achieved = None
22-
self.incident_reported = None
23-
self.incident_closed = None
14+
def __init__(self, first_malicious_action=None, initial_compromise=None,
15+
first_data_exfiltration=None, incident_discovery=None,
16+
incident_opened=None, containment_achieved=None,
17+
restoration_achieved=None, incident_reported=None,
18+
incident_closed=None):
19+
20+
self.first_malicious_action = first_malicious_action
21+
self.initial_compromise = initial_compromise
22+
self.first_data_exfiltration = first_data_exfiltration
23+
self.incident_discovery = incident_discovery
24+
self.incident_opened = incident_opened
25+
self.containment_achieved = containment_achieved
26+
self.restoration_achieved = restoration_achieved
27+
self.incident_reported = incident_reported
28+
self.incident_closed = incident_closed
2429

2530
@property
2631
def first_malicious_action(self):

stix/test/incident_test.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,49 @@ def test_description_output(self):
465465
xml = s.getvalue()
466466
self.assertTrue("A Description" in xml, "Description not exported")
467467

468+
def test_add_related_observable(self):
469+
from cybox.core import Observable
470+
from stix.common.related import RelatedObservable
471+
472+
i = self.klass()
473+
474+
self.assertEqual(0, len(i.related_observables))
475+
i.add_related_observable(Observable())
476+
self.assertEqual(1, len(i.related_observables))
477+
478+
479+
related = RelatedObservable(Observable())
480+
i.add_related_observable(related)
481+
self.assertEqual(2, len(i.related_observables))
482+
483+
# Test that this fails
484+
self.assertRaises(
485+
ValueError,
486+
i.add_related_observable,
487+
"THIS SHOULD FAIL"
488+
)
489+
490+
def test_add_related_indicator(self):
491+
from stix.indicator import Indicator
492+
from stix.common.related import RelatedIndicator
493+
494+
i = self.klass()
495+
496+
self.assertEqual(0, len(i.related_indicators))
497+
i.add_related_indicator(Indicator())
498+
self.assertEqual(1, len(i.related_indicators))
499+
500+
related = RelatedIndicator(Indicator())
501+
i.add_related_indicator(related)
502+
self.assertEqual(2, len(i.related_indicators))
503+
504+
# Test that this fails
505+
self.assertRaises(
506+
ValueError,
507+
i.add_related_indicator,
508+
"THIS SHOULD FAIL"
509+
)
510+
468511

469512

470513
if __name__ == "__main__":

0 commit comments

Comments
 (0)