Skip to content

Commit

Permalink
Merge pull request #362 from emmanvg/oasis-ciq-1.1.1
Browse files Browse the repository at this point in the history
update PersonName to allow xnl:Type (STIX 1.1.1)
  • Loading branch information
emmanvg authored Mar 9, 2020
2 parents 62f30da + 794eafb commit 26f588b
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 18 deletions.
87 changes: 82 additions & 5 deletions stix/extensions/identity/ciq_identity_3_0.py
Original file line number Diff line number Diff line change
Expand Up @@ -1028,13 +1028,53 @@ class PersonName(stix.Entity):
_namespace = XML_NS_XNL
XML_TAG = "{%s}PersonName" % _namespace

def __init__(self, name_elements=None):
TYPE_ALIAS = 'Alias'
TYPE_LEGAL_NAME = 'LegalName'
TYPE_KNOWN_AS = 'KnownAs'
TYPE_MAIDEN_NAME = 'MaidenName'
TYPE_FORMER_NAME = 'FormerName'
TYPE_COMMON_USE = 'CommonUse'
TYPE_NAME_AT_BIRTH = 'NameAtBirth'
TYPE_PREFERRED_NAME = 'PreferredName'
TYPE_OFFICIAL_NAME = 'OfficialName'
TYPE_UNOFFICIAL_NAME = 'UnofficialName'
TYPE_NICK_NAME = 'NickName'
TYPE_PET_NAME = 'PetName'

TYPES = (
TYPE_ALIAS,
TYPE_LEGAL_NAME,
TYPE_KNOWN_AS,
TYPE_MAIDEN_NAME,
TYPE_FORMER_NAME,
TYPE_COMMON_USE,
TYPE_NAME_AT_BIRTH,
TYPE_PREFERRED_NAME,
TYPE_OFFICIAL_NAME,
TYPE_UNOFFICIAL_NAME,
TYPE_NICK_NAME,
TYPE_PET_NAME,
)

def __init__(self, name_elements=None, type_=None):
self.name_elements = []
self.type_ = type_

if name_elements:
for name_element in name_elements:
self.add_name_element(name_element)

@property
def type_(self):
return self._type

@type_.setter
def type_(self, value):
if value and value not in self.TYPES:
raise ValueError('value must be one of %s: ' % (self.TYPES,))

self._type = value

def add_name_element(self, value):
if isinstance(value, string_types):
self.name_elements.append(PersonNameElement(value=value))
Expand All @@ -1050,6 +1090,9 @@ def to_obj(self, return_obj=None, ns_info=None):
root_tag = PersonName.XML_TAG
return_obj = et.Element(root_tag)

if self.type_:
return_obj.attrib['{%s}Type' % XML_NS_XNL] = self.type_

for name_element in self.name_elements:
return_obj.append(name_element.to_obj(ns_info=ns_info))

Expand All @@ -1063,6 +1106,8 @@ def from_obj(cls, obj, return_obj=None):
if not return_obj:
return_obj = cls()

return_obj.type_ = obj.attrib.get('{%s}Type' % XML_NS_XNL)

name_elements = obj.findall(PersonNameElement.XML_TAG)
if name_elements:
for name_element_obj in name_elements:
Expand All @@ -1074,6 +1119,9 @@ def from_obj(cls, obj, return_obj=None):
def to_dict(self):
d = {}

if self.type_:
d['type'] = self.type_

if self.name_elements:
for ne in self.name_elements:
d.setdefault('name_elements', []).append(ne.to_dict())
Expand All @@ -1088,6 +1136,8 @@ def from_dict(cls, dict_repr, return_obj=None):
if not return_obj:
return_obj = cls()

return_obj.type_ = dict_repr.get('type')

ne_dicts = dict_repr.get('name_elements', [])

for ne_dict in ne_dicts:
Expand All @@ -1100,11 +1150,40 @@ class OrganisationName(stix.Entity):
_namespace = XML_NS_XNL
XML_TAG = "{%s}OrganisationName" % _namespace

TYPE_LEGAL_NAME = 'LegalName'
TYPE_FORMER_NAME = 'FormerName'
TYPE_COMMON_USE = 'CommonUse'
TYPE_PUBLISHING_NAME = 'PublishingName'
TYPE_OFFICIAL_NAME = 'OfficialName'
TYPE_UNOFFICIAL_NAME = 'UnofficialName'
TYPE_UNDEFINED = 'Undefined'

TYPES = (
TYPE_LEGAL_NAME,
TYPE_FORMER_NAME,
TYPE_COMMON_USE,
TYPE_PUBLISHING_NAME,
TYPE_OFFICIAL_NAME,
TYPE_UNOFFICIAL_NAME,
TYPE_UNDEFINED,
)

def __init__(self, name_elements=None, subdivision_names=None, type_=None):
self.type_ = type_
self.name_elements = name_elements
self.subdivision_names = subdivision_names

@property
def type_(self):
return self._type

@type_.setter
def type_(self, value):
if value and value not in self.TYPES:
raise ValueError('value must be one of %s: ' % (self.TYPES,))

self._type = value

@property
def name_elements(self):
return self._name_elements
Expand Down Expand Up @@ -1226,6 +1305,7 @@ class _BaseNameElement(stix.Entity):
OrganisationNameElement
"""

def __init__(self, value=None):
self.value = value

Expand All @@ -1235,9 +1315,6 @@ def value(self):

@value.setter
def value(self, value):
# if not value:
# raise ValueError('value cannot be None')

self._value = value

@classmethod
Expand Down Expand Up @@ -1270,8 +1347,8 @@ class PersonNameElement(_BaseNameElement):
_namespace = XML_NS_XNL
XML_TAG = "{%s}NameElement" % _namespace

TYPE_TITLE = 'Title'
TYPE_PRECEDING_TITLE = 'PrecedingTitle'
TYPE_TITLE = 'Title'
TYPE_FIRST_NAME = 'FirstName'
TYPE_MIDDLE_NAME = 'MiddleName'
TYPE_LAST_NAME = 'LastName'
Expand Down
6 changes: 3 additions & 3 deletions stix/test/coa_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ class COATests(EntityTestCase, unittest.TestCase):
},
'objective': ObjectiveTests._full_dict,
'parameter_observables': {
'major_version': 2,
'minor_version': 1,
'update_version': 0,
'cybox_major_version': '2',
'cybox_minor_version': '1',
'cybox_update_version': '0',
'observables': [
{
'idref': "example:Observable-1"
Expand Down
6 changes: 3 additions & 3 deletions stix/test/core/stix_package_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ class STIXPackageTests(EntityTestCase, unittest.TestCase):
'incidents': IncidentsTests._full_dict,
'indicators': IndicatorsTests._full_dict,
'observables': {
'major_version': 2,
'minor_version': 1,
'update_version': 0,
'cybox_major_version': '2',
'cybox_minor_version': '1',
'cybox_update_version': '0',
'observables': [
{
'idref': "example:Observable-1"
Expand Down
10 changes: 9 additions & 1 deletion stix/test/extensions/identity/ciq_identity_3_0_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,16 @@ class CIQIdentity3_0InstanceTests(EntityTestCase, unittest.TestCase):
],
'person_names': [
{
'type': 'LegalName',
'name_elements': [
{'value': 'John Smith'}
{
'element_type': 'FirstName',
'value': 'John',
},
{
'element_type': 'LastName',
'value': 'Smith',
}
]
},
{
Expand Down
12 changes: 6 additions & 6 deletions stix/test/ttp_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ class InfrastructureTests(EntityTestCase, unittest.TestCase):
'short_description': 'Short Description',
'types': ['foo', 'bar'],
'observable_characterization': {
'major_version': 2,
'minor_version': 1,
'update_version': 0,
'cybox_major_version': '2',
'cybox_minor_version': '1',
'cybox_update_version': '0',
'observables': [
{
'idref': "example:Observable-1"
Expand Down Expand Up @@ -167,9 +167,9 @@ class VictimTargetingTests(EntityTestCase, unittest.TestCase):
}
],
'targeted_technical_details': {
'major_version': 2,
'minor_version': 1,
'update_version': 0,
'cybox_major_version': '2',
'cybox_minor_version': '1',
'cybox_update_version': '0',
'observables': [
{
'idref': "example:Observable-2"
Expand Down

0 comments on commit 26f588b

Please sign in to comment.