Skip to content

Commit 09f9832

Browse files
author
Bryan Worrell
committed
More TypedFields updates. All data marking code is broken at the moment. Considering a EntityFactory class.
1 parent c3b3380 commit 09f9832

11 files changed

Lines changed: 81 additions & 248 deletions

File tree

stix/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,8 +370,8 @@ class BaseCoreComponent(Cached, Entity):
370370
title = fields.TypedField("Title")
371371
id_ = fields.IdField("id")
372372
idref = fields.IdrefField("idref")
373-
descriptions = fields.TypedField("Description", type_="stix.common.StructuredTextList", key_name="description")
374-
short_descriptions = fields.TypedField("Short_Description", type_="stix.common.StructuredTextList", key_name="short_description")
373+
descriptions = fields.TypedField("Description", type_="stix.common.StructuredTextList")
374+
short_descriptions = fields.TypedField("Short_Description", type_="stix.common.StructuredTextList")
375375
version = fields.TypedField("version", preset_hook=_validate_version)
376376
timestamp = fields.DateTimeField("timestamp")
377377
handling = fields.TypedField("Handling", type_="stix.data_marking.Marking")

stix/campaign/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ class Campaign(stix.BaseCoreComponent):
112112
_ALL_VERSIONS = ("1.0", "1.0.1", "1.1", "1.1.1", "1.2")
113113
_ID_PREFIX = 'campaign'
114114

115-
descriptions = fields.TypedField("Description", StructuredTextList, key_name="description")
115+
descriptions = fields.TypedField("Description", StructuredTextList)
116116
activity = fields.TypedField("Activity", Activity, multiple=True)
117117
associated_campaigns = fields.TypedField("Associated_Campaigns", AssociatedCampaigns)
118118
attribution = fields.TypedField("Attribution", Attribution, multiple=True)

stix/common/activity.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Activity(stix.Entity):
1717
_namespace = 'http://stix.mitre.org/common-1'
1818

1919
date_time = fields.TypedField("Date_Time", DateTimeWithPrecision)
20-
descriptions = fields.TypedField("Description", StructuredTextList, key_name="description")
20+
descriptions = fields.TypedField("Description", StructuredTextList)
2121

2222
def __init__(self):
2323
super(Activity, self).__init__()

stix/common/confidence.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Confidence(stix.Entity):
1919
_binding_class = common_binding.ConfidenceType
2020

2121
value = fields.TypedField("Value", VocabString)
22-
descriptions = fields.TypedField("Description", StructuredTextList, key_name="description")
22+
descriptions = fields.TypedField("Description", StructuredTextList)
2323
timestamp = fields.DateTimeField("timestamp")
2424
timestamp_precision = fields.TypedField("timestamp_precision", preset_hook=validate_precision)
2525
source = fields.TypedField("Source", type_="stix.common.InformationSource")

stix/common/information_source.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class InformationSource(stix.Entity):
2323
_namespace = 'http://stix.mitre.org/common-1'
2424

2525
identity = fields.TypedField("Identity", Identity)
26-
descriptions = fields.TypedField("Description", StructuredTextList, key_name="description")
26+
descriptions = fields.TypedField("Description", StructuredTextList)
2727
contributing_sources = fields.TypedField("Contributing_Sources", type_="stix.common.information_source.ContributingSources")
2828
time = fields.TypedField("Time", cybox.common.Time)
2929
roles = fields.TypedField("Role", VocabString, multiple=True, key_name="roles")

stix/common/tools.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class ToolInformation(stix.Entity, cybox.common.ToolInformation):
1919
_binding_class = common_binding.ToolInformationType
2020

2121
title = fields.TypedField("Title")
22-
short_descriptions = fields.TypedField("Short_Description", StructuredTextList, key_name="short_description")
22+
short_descriptions = fields.TypedField("Short_Description", StructuredTextList)
2323

2424
def __init__(self, title=None, short_description=None, tool_name=None, tool_vendor=None):
2525
super(ToolInformation, self).__init__(tool_name=tool_name, tool_vendor=tool_vendor)

stix/core/stix_header.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ class STIXHeader(stix.Entity):
3838

3939
title = fields.TypedField("Title", preset_hook=deprecated.field)
4040
package_intents = VocabField("Package_Intent", PackageIntent, multiple=True, preset_hook=deprecated.field)
41-
descriptions = fields.TypedField("Description", type_=StructuredTextList, key_name="description", preset_hook=deprecated.field)
42-
short_descriptions = fields.TypedField("Short_Description", type_=StructuredTextList, key_name="short_description", preset_hook=deprecated.field)
41+
descriptions = fields.TypedField("Description", type_=StructuredTextList, preset_hook=deprecated.field)
42+
short_descriptions = fields.TypedField("Short_Description", type_=StructuredTextList, preset_hook=deprecated.field)
4343
handling = fields.TypedField("Handling", Marking)
4444
information_source = fields.TypedField("Information_Source", InformationSource)
4545
profiles = fields.TypedField("Profiles", Profiles)

stix/data_marking.py

Lines changed: 67 additions & 192 deletions
Original file line numberDiff line numberDiff line change
@@ -13,193 +13,31 @@
1313
import stix.bindings.data_marking as stix_data_marking_binding
1414

1515

16-
class Marking(stix.Entity):
17-
_binding = stix_data_marking_binding
18-
_binding_class = stix_data_marking_binding.MarkingType
19-
_namespace = 'http://data-marking.mitre.org/Marking-1'
20-
21-
def __init__(self, markings=None):
22-
self.markings = _MarkingSpecifications(markings)
23-
24-
@property
25-
def markings(self):
26-
return self._markings
27-
28-
@markings.setter
29-
def markings(self, value):
30-
self._markings = _MarkingSpecifications(value)
31-
32-
def add_marking(self, value):
33-
self._markings.append(value)
34-
35-
def to_obj(self, ns_info=None):
36-
obj = super(Marking, self).to_obj(ns_info=ns_info)
37-
38-
if self.markings:
39-
obj.Marking = self.markings.to_obj(ns_info=ns_info)
40-
41-
return obj
42-
43-
def to_list(self):
44-
return self.markings.to_list() if self.markings else []
45-
46-
@classmethod
47-
def from_obj(cls, obj):
48-
if not obj:
49-
return None
50-
51-
obj = super(Marking, cls).from_obj(obj)
52-
obj.markings = _MarkingSpecifications.from_obj(obj.Marking)
53-
return obj
54-
55-
@classmethod
56-
def from_list(cls, markings_list):
57-
if not markings_list:
58-
return None
59-
60-
obj = cls()
61-
mlist = _MarkingSpecifications.from_list(markings_list)
62-
obj.markings = mlist
63-
return obj
64-
65-
to_dict = to_list
66-
from_dict = from_list
67-
68-
69-
class MarkingSpecification(Cached, stix.Entity):
70-
_binding = stix_data_marking_binding
71-
_binding_class = stix_data_marking_binding.MarkingSpecificationType
72-
_namespace = 'http://data-marking.mitre.org/Marking-1'
73-
74-
def __init__(self, controlled_structure=None, marking_structures=None):
75-
super(MarkingSpecification, self).__init__()
76-
77-
self.id_ = None
78-
self.idref = None
79-
self.version = None
80-
self.controlled_structure = controlled_structure
81-
self.marking_structures = _MarkingStructures(marking_structures)
82-
self.information_source = None
83-
84-
@property
85-
def information_source(self):
86-
return self._information_source
87-
88-
@information_source.setter
89-
def information_source(self, value):
90-
self._set_var(InformationSource, try_cast=False, information_source=value)
91-
92-
@property
93-
def marking_structures(self):
94-
return self._marking_structures
95-
96-
@marking_structures.setter
97-
def marking_structures(self, value):
98-
self._marking_structures = _MarkingStructures(value)
99-
100-
def to_obj(self, return_obj=None, ns_info=None):
101-
super(MarkingSpecification, self).to_obj(
102-
return_obj=return_obj,
103-
ns_info=ns_info
104-
)
105-
106-
obj = self._binding_class()
107-
108-
obj.id = self.id_
109-
obj.idref = self.idref
110-
obj.version = self.version
111-
obj.Controlled_Structure = self.controlled_structure
112-
obj.Marking_Structure = self.marking_structures.to_obj(ns_info=ns_info)
113-
114-
if self.information_source:
115-
obj.Information_Source = self.information_source.to_obj(ns_info=ns_info)
116-
117-
return obj
118-
119-
def to_dict(self):
120-
return super(MarkingSpecification, self).to_dict()
121-
122-
@classmethod
123-
def from_obj(cls, obj, return_obj=None):
124-
if not obj:
125-
return None
126-
if not return_obj:
127-
return_obj = cls()
128-
129-
return_obj.id_ = obj.id
130-
return_obj.idref = obj.idref
131-
return_obj.version = obj.version
132-
return_obj.controlled_structure = obj.Controlled_Structure
133-
return_obj.marking_structures = _MarkingStructures.from_obj(obj.Marking_Structure)
134-
return_obj.information_source = InformationSource.from_obj(obj.Information_Source)
135-
136-
return return_obj
137-
138-
@classmethod
139-
def from_dict(cls, d, return_obj=None):
140-
if not d:
141-
return None
142-
if not return_obj:
143-
return_obj = cls()
144-
145-
get = d.get # PEP8 line length fix
146-
return_obj.id_ = get('id')
147-
return_obj.idref = get('idref')
148-
return_obj.version = get('version')
149-
return_obj.controlled_structure = get('controlled_structure')
150-
return_obj.marking_structures = _MarkingStructures.from_dict(
151-
get('marking_structures')
152-
)
153-
return_obj.information_source = InformationSource.from_dict(
154-
get('information_source')
155-
)
156-
157-
return return_obj
158-
159-
16016
class MarkingStructure(Cached, stix.Entity):
16117
_binding = stix_data_marking_binding
16218
_binding_class = stix_data_marking_binding.MarkingStructureType
16319
_namespace = 'http://data-marking.mitre.org/Marking-1'
16420
_XSI_TYPE = None # overridden by subclasses
16521

22+
id_ = fields.IdField("id")
23+
idref = fields.IdrefField("idref")
24+
marking_model_name = fields.TypedField("marking_model_name")
25+
marking_mode_ref = fields.TypedField("marking_model_ref")
26+
16627
def __init__(self):
28+
super(MarkingStructure, self).__init__()
29+
16730
self.id_ = None
16831
self.idref = None
16932
self.marking_model_name = None
17033
self.marking_model_ref = None
17134

172-
def to_obj(self, return_obj=None, ns_info=None):
173-
super(MarkingStructure, self).to_obj(
174-
return_obj=return_obj,
175-
ns_info=ns_info
176-
)
177-
178-
if not return_obj:
179-
return_obj = self._binding_class()
180-
181-
return_obj.id = self.id_
182-
return_obj.idref = self.idref
183-
return_obj.marking_model_name = self.marking_model_name
184-
return_obj.marking_model_ref = self.marking_model_ref
185-
186-
return return_obj
187-
18835
def to_dict(self):
189-
d = {}
36+
d = super(MarkingStructure, self).to_dict()
19037

19138
if self._XSI_TYPE:
19239
d['xsi:type'] = self._XSI_TYPE
19340

194-
if self.id_:
195-
d['id'] = self.id_
196-
if self.idref:
197-
d['idref'] = self.idref
198-
if self.marking_model_name:
199-
d['marking_model_name'] = self.marking_model_name
200-
if self.marking_model_ref:
201-
d['marking_model_ref'] = self.marking_model_ref
202-
20341
return d
20442

20543
@staticmethod
@@ -210,58 +48,95 @@ def lookup_class(xsi_type):
21048
return stix.lookup_extension(xsi_type)
21149

21250
@classmethod
213-
def from_obj(cls, obj, return_obj=None):
51+
def from_obj(cls, cls_obj, partial=None):
21452
import stix.extensions.marking.tlp # noqa
21553
import stix.extensions.marking.simple_marking # noqa
21654
import stix.extensions.marking.terms_of_use_marking # noqa
21755

218-
if not obj:
56+
if not cls_obj:
21957
return None
22058

221-
if return_obj:
222-
m = return_obj
223-
m.id_ = obj.id
224-
m.idref = obj.idref
225-
m.marking_model_name = obj.marking_model_name
226-
m.marking_model_ref = obj.marking_model_ref
59+
if partial:
60+
m = partial
61+
m.id_ = cls_obj.id
62+
m.idref = cls_obj.idref
63+
m.marking_model_name = cls_obj.marking_model_name
64+
m.marking_model_ref = cls_obj.marking_model_ref
22765

22866
else:
229-
klass = stix.lookup_extension(obj, default=cls)
230-
m = klass.from_obj(obj, return_obj=klass())
67+
klass = stix.lookup_extension(cls_obj, default=cls)
68+
m = klass.from_obj(cls_obj, klass())
23169

23270
return m
23371

23472
@classmethod
235-
def from_dict(cls, d, return_obj=None):
73+
def from_dict(cls, cls_dict, partial=None):
23674
import stix.extensions.marking.tlp # noqa
23775
import stix.extensions.marking.simple_marking # noqa
23876
import stix.extensions.marking.terms_of_use_marking # noqa
239-
240-
if not d:
77+
78+
if not cls_dict:
24179
return None
24280

243-
get = d.get
244-
245-
if return_obj is not None:
246-
m = return_obj
81+
get = cls_dict.get
82+
83+
if partial is not None:
84+
m = partial
24785
m.id_ = get('id')
24886
m.idref = get('idref')
24987
m.marking_model_name = get('marking_model_name')
25088
m.marking_model_ref = get('marking_model_ref')
25189
else:
25290
klass = stix.lookup_extension(get('xsi:type'), default=cls)
253-
m = klass.from_dict(d, return_obj=klass())
91+
m = klass.from_dict(cls_dict, klass())
25492

25593
return m
25694

25795

258-
# Not Actual STIX Types!
259-
class _MarkingSpecifications(stix.TypedList):
96+
class MarkingSpecification(Cached, stix.Entity):
97+
_binding = stix_data_marking_binding
98+
_binding_class = stix_data_marking_binding.MarkingSpecificationType
99+
_namespace = 'http://data-marking.mitre.org/Marking-1'
100+
101+
id_ = fields.IdField("id")
102+
idref = fields.IdrefField("idref")
103+
version = fields.TypedField("version")
104+
controlled_structure = fields.TypedField("Controlled_Structure")
105+
marking_structures = fields.TypedField("Marking_Structure", MarkingStructure, multiple=True, key_name="marking_structures")
106+
information_source = fields.TypedField("Information_Source", InformationSource)
107+
108+
def __init__(self, controlled_structure=None, marking_structures=None):
109+
super(MarkingSpecification, self).__init__()
110+
111+
self.id_ = None
112+
self.idref = None
113+
self.version = None
114+
self.controlled_structure = controlled_structure
115+
self.marking_structures = marking_structures
116+
self.information_source = None
117+
118+
119+
class Marking(stix.EntityList):
120+
_binding = stix_data_marking_binding
121+
_binding_class = stix_data_marking_binding.MarkingType
122+
_namespace = 'http://data-marking.mitre.org/Marking-1'
260123
_contained_type = MarkingSpecification
124+
_binding_var = "Marking"
125+
126+
def __init__(self, markings=None):
127+
super(Marking, self).__init__(markings)
261128

129+
@property
130+
def markings(self):
131+
return self._inner
132+
133+
@markings.setter
134+
def markings(self, value):
135+
self._inner = []
136+
self.extend(value)
262137

263-
class _MarkingStructures(stix.TypedList):
264-
_contained_type = MarkingStructure
138+
def add_marking(self, value):
139+
self.markings.append(value)
265140

266141

267142
# Backwards compatibility

0 commit comments

Comments
 (0)