Skip to content

Commit a643e38

Browse files
author
Bryan Worrell
committed
Added IdentityFactory to stix.common.identity
1 parent 5850064 commit a643e38

6 files changed

Lines changed: 27 additions & 79 deletions

File tree

stix/common/identity.py

Lines changed: 8 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
# external
55
from mixbox import fields
6+
from mixbox import entities
67
from mixbox.cache import Cached
78

89
# internal
@@ -11,6 +12,13 @@
1112
from stix.bindings.stix_common import IdentityType
1213

1314

15+
class IdentityFactory(entities.EntityFactory):
16+
@classmethod
17+
def entity_class(cls, key):
18+
import stix.extensions.identity.ciq_identity_3_0 # noqa
19+
return stix.lookup_extension(key, default=Identity)
20+
21+
1422
class Identity(Cached, stix.Entity):
1523
_binding = common_binding
1624
_namespace = 'http://stix.mitre.org/common-1'
@@ -29,52 +37,6 @@ def __init__(self, id_=None, idref=None, name=None, related_identities=None):
2937
self.name = name
3038
self.related_identities = related_identities
3139

32-
@staticmethod
33-
def lookup_class(xsi_type):
34-
if not xsi_type:
35-
raise ValueError("xsi:type is required")
36-
37-
return stix.lookup_extension(xsi_type)
38-
39-
@classmethod
40-
def from_obj(cls, obj, partial=None):
41-
import stix.extensions.identity.ciq_identity_3_0 # noqa
42-
43-
if not obj:
44-
return None
45-
46-
if not partial:
47-
klass = stix.lookup_extension(obj, default=cls)
48-
partial = klass.from_obj(obj, partial=klass())
49-
else:
50-
partial.id_ = obj.id
51-
partial.idref = obj.idref
52-
partial.name = obj.Name
53-
partial.related_identities = RelatedIdentities.from_obj(obj.Related_Identities)
54-
55-
return partial
56-
57-
@classmethod
58-
def from_dict(cls, cls_dict, partial=None):
59-
import stix.extensions.identity.ciq_identity_3_0 # noqa
60-
61-
if not cls_dict:
62-
return None
63-
64-
get = cls_dict.get
65-
66-
if not partial:
67-
klass = stix.lookup_extension(get('xsi:type'), default=cls)
68-
partial = klass.from_dict(cls_dict, klass())
69-
else:
70-
partial.name = get('name')
71-
partial.id_ = get('id')
72-
partial.idref = get('idref')
73-
partial.related_identities = \
74-
RelatedIdentities.from_dict(get('related_identities'))
75-
76-
return partial
77-
7840

7941
# We can't import RelatedIdentity until we have defined the Identity class.
8042
from stix.common.related import RelatedIdentity

stix/common/information_source.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# relative
1414
from .vocabs import VocabString
1515
from .references import References
16-
from .identity import Identity
16+
from .identity import Identity, IdentityFactory
1717
from .structured_text import StructuredTextList
1818

1919

@@ -22,7 +22,7 @@ class InformationSource(stix.Entity):
2222
_binding_class = stix_common_binding.InformationSourceType
2323
_namespace = 'http://stix.mitre.org/common-1'
2424

25-
identity = fields.TypedField("Identity", Identity)
25+
identity = fields.TypedField("Identity", type_=Identity, factory=IdentityFactory)
2626
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)

stix/extensions/identity/ciq_identity_3_0.py

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -65,45 +65,34 @@ def specification(self, value):
6565

6666
self._specification = value
6767

68-
def to_obj(self, return_obj=None, ns_info=None):
69-
if not return_obj:
70-
return_obj = self._binding_class()
71-
72-
super(CIQIdentity3_0Instance, self).to_obj(return_obj)
73-
74-
# return_obj.id = self.id_
75-
# return_obj.idref = self.idref_
76-
return_obj.xsi_type = self._XSI_TYPE
68+
def to_obj(self, ns_info=None):
69+
obj = super(CIQIdentity3_0Instance, self).to_obj()
70+
obj.xsi_type = self._XSI_TYPE
7771

7872
if self.roles:
7973
for role in self.roles:
80-
return_obj.add_Role(role)
74+
obj.add_Role(role)
8175

8276
if self.specification:
83-
return_obj.Specification = self.specification.to_obj(ns_info=ns_info)
77+
obj.Specification = self.specification.to_obj(ns_info=ns_info)
8478

85-
return return_obj
79+
return obj
8680

8781
@classmethod
88-
def from_obj(cls, obj, return_obj=None):
89-
if obj is None:
90-
return None
91-
if not return_obj:
92-
return_obj = cls()
93-
94-
super(CIQIdentity3_0Instance, cls).from_obj(obj, return_obj)
82+
def from_obj(cls, cls_obj):
83+
obj = super(CIQIdentity3_0Instance, cls).from_obj(cls_obj)
9584

9685
roles = obj.Role
9786
specification = obj.Specification
9887

9988
if roles:
10089
for role in roles:
101-
return_obj.add_role(role)
90+
obj.add_role(role)
10291

10392
if specification is not None:
104-
return_obj.specification = STIXCIQIdentity3_0.from_obj(specification)
93+
obj.specification = STIXCIQIdentity3_0.from_obj(specification)
10594

106-
return return_obj
95+
return obj
10796

10897
def to_dict(self):
10998
d = super(CIQIdentity3_0Instance, self).to_dict()

stix/incident/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import stix
66
import stix.bindings.incident as incident_binding
77
from stix.common import vocabs
8-
from stix.common import (Identity, Statement, VocabString, InformationSource,
9-
Confidence)
8+
from stix.common import Statement, VocabString, InformationSource, Confidence
9+
from stix.common.identity import Identity, IdentityFactory
1010
from stix.common.related import (GenericRelationshipList, RelatedIndicator,
1111
RelatedThreatActor, RelatedTTP, RelatedObservable, RelatedIncident,
1212
RelatedPackageRefs)
@@ -47,7 +47,7 @@ class Incident(stix.BaseCoreComponent):
4747

4848
status = fields.TypedField("Status", vocabs.IncidentStatus)
4949
time = fields.TypedField("Time", Time)
50-
victims = fields.TypedField("Victim", Identity, multiple=True, key_name="victims")
50+
victims = fields.TypedField("Victim", Identity, factory=IdentityFactory, multiple=True, key_name="victims")
5151
attributed_threat_actors = fields.TypedField("Attributed_Threat_Actors", type_="stix.incident.AttributedThreatActors")
5252
related_indicators = fields.TypedField("Related_Indicators", type_="stix.incident.RelatedIndicators")
5353
related_observables = fields.TypedField("Related_Observables", type_="stix.incident.RelatedObservables")

stix/indicator/indicator.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99

1010
# internal
1111
import stix
12-
from stix.common import (Identity, InformationSource, VocabString, Confidence,
13-
RelatedTTP, Statement, CampaignRef)
12+
from stix.common.identity import Identity
13+
from stix.common import (InformationSource, VocabString, Confidence, RelatedTTP,
14+
Statement, CampaignRef)
1415
from stix.common.related import (GenericRelationshipList, RelatedCOA,
1516
RelatedIndicator, RelatedCampaignRef, RelatedPackageRefs)
1617
from stix.common.vocabs import IndicatorType

stix/ttp/resource.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,6 @@ class Personas(stix.EntityList):
105105
_binding = ttp_binding
106106
_binding_class = _binding.PersonasType
107107
_binding_var = "Persona"
108-
_inner_name = "personas"
109-
_dict_as_list = True
110108

111109
def _fix_value(self, value):
112110
return Identity(name=value)
@@ -118,5 +116,3 @@ class Tools(stix.EntityList):
118116
_binding = ttp_binding
119117
_binding_class = _binding.ToolsType
120118
_binding_var = "Tool"
121-
_inner_name = "tools"
122-
_dict_as_list = True

0 commit comments

Comments
 (0)