Skip to content

Commit

Permalink
More TypedLists, EntityLists, and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Bryan Worrell committed Mar 3, 2015
1 parent 5835eb7 commit ecf0558
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 54 deletions.
4 changes: 2 additions & 2 deletions stix/incident/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ def from_dict(cls, dict_repr, return_obj=None):
return_obj = cls()

get = dict_repr.get

return_obj.id_ = get('id')
return_obj.idref = get('idref')
return_obj.timestamp = get('timestamp')
Expand Down Expand Up @@ -599,7 +599,7 @@ def _fix_value(self, value):

class AffectedAssets(stix.EntityList):
_namespace = "http://stix.mitre.org/Incident-1"
_contained_type = VocabString
_contained_type = AffectedAsset
_binding_class = incident_binding.AffectedAssetsType
_binding_var = "Affected_Asset"
_inner_name = "affected_assets"
Expand Down
72 changes: 24 additions & 48 deletions stix/incident/affected_asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
from .property_affected import PropertyAffected




class AffectedAsset(stix.Entity):
_namespace = "http://stix.mitre.org/Incident-1"
_binding = incident_binding
Expand Down Expand Up @@ -116,22 +114,10 @@ def nature_of_security_effect(self):

@nature_of_security_effect.setter
def nature_of_security_effect(self, value):
self._nature_of_security_effect = []
if not value:
return
elif utils.is_sequence(value):
for v in value:
self.add_property_affected(v)
else:
self.add_property_affected(value)

self._nature_of_security_effect = NatureOfSecurityEffect(value)

def add_property_affected(self, v):
if not v:
return
elif isinstance(v, PropertyAffected):
self.nature_of_security_effect.append(v)
else:
raise ValueError('Cannot add type %s to nature_of_security_effect list' % type(v))
self.nature_of_security_effect.append(v)

@property
def structured_description(self):
Expand Down Expand Up @@ -201,39 +187,22 @@ def from_dict(cls, d, return_obj=None):
return None
if not return_obj:
return_obj = cls()

return_obj.type_ = AssetType.from_dict(d.get('type'))
return_obj.description = StructuredText.from_dict(d.get('description'))
return_obj.business_function_or_role = StructuredText.from_dict(d.get('business_function_or_role'))
return_obj.ownership_class = VocabString.from_dict(d.get('ownership_class'))
return_obj.management_class = VocabString.from_dict(d.get('management_class'))
return_obj.location_class = VocabString.from_dict(d.get('location_class'))
#return_obj.location = Location.from_dict(d.get('location'))
return_obj.nature_of_security_effect = [PropertyAffected.from_dict(x) for x in d.get('nature_of_security_effect')]
return_obj.structured_description = Observables.from_dict(d.get('structured_description'))

get = d.get
return_obj.type_ = AssetType.from_dict(get('type'))
return_obj.description = StructuredText.from_dict(get('description'))
return_obj.business_function_or_role = StructuredText.from_dict(get('business_function_or_role'))
return_obj.ownership_class = VocabString.from_dict(get('ownership_class'))
return_obj.management_class = VocabString.from_dict(get('management_class'))
return_obj.location_class = VocabString.from_dict(get('location_class'))
#return_obj.location = Location.from_dict(get('location'))
return_obj.nature_of_security_effect = NatureOfSecurityEffect.from_dict(get('nature_of_security_effect'))
return_obj.structured_description = Observables.from_dict(get('structured_description'))
return return_obj

def to_dict(self):
d = {}
if self.type_:
d['type'] = self.type_.to_dict()
if self.description:
d['description'] = self.description.to_dict()
if self.business_function_or_role:
d['business_function_or_role'] = self.business_function_or_role.to_dict()
if self.ownership_class:
d['ownership_class'] = self.ownership_class.to_dict()
if self.management_class:
d['management_class'] = self.management_class.to_dict()
if self.location_class:
d['location_class'] = self.location_class.to_dict()
# if self.location:
# d['location'] = self.location.to_dict()
if self.nature_of_security_effect:
d['nature_of_security_effect'] = [x.to_dict() for x in self.nature_of_security_effect]
if self.structured_description:
d['structured_description'] = self.structured_description.to_dict()
return d
return super(AffectedAsset, self).to_dict()


#from stix.common.vocabs import AssetType as DefaultAssetType

Expand Down Expand Up @@ -298,4 +267,11 @@ def to_dict(self):
d['count_affected'] = self.count_affected
return d



class NatureOfSecurityEffect(stix.EntityList):
_namespace = "http://stix.mitre.org/Incident-1"
_contained_type = PropertyAffected
_binding_class = incident_binding.NatureOfSecurityEffectType
_binding_var = "Property_Affected"
_inner_name = "nature_of_security_effect"
_dict_as_list = True
5 changes: 4 additions & 1 deletion stix/incident/property_affected.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ def from_dict(cls, d, return_obj=None):
super(NonPublicDataCompromised, cls).from_dict(d, return_obj=return_obj)
return_obj.data_encrypted = d.get('data_encrypted')
return return_obj


def is_plain(self):
return False

def to_dict(self):
d = super(NonPublicDataCompromised, self).to_dict()
if self.data_encrypted:
Expand Down
55 changes: 52 additions & 3 deletions stix/test/incident_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,58 @@ class IncidentTest(EntityTestCase, unittest.TestCase):
'name': "Ms. Good",
},
},
'security_compromise': {"value": "Suspected", "xsi:type":"stixVocabs:SecurityCompromiseVocab-1.0"},
'history': {'history_items':[{'journal_entry': {'value': 'hi',
'author': 'Paul'}}]}
'security_compromise': {
"value": "Suspected",
"xsi:type":"stixVocabs:SecurityCompromiseVocab-1.0"
},
'history': {
'history_items':
[
{
'journal_entry': {
'value': 'hi',
'author': 'Paul'
}
}
]
},
'affected_assets': [
{
'business_function_or_role': 'Foobar',
'description': 'Foobar',
'location_class': {
'value': 'Unknown',
'xsi:type': 'stixVocabs:LocationClassVocab-1.0'
},
'management_class': {
'value': 'Unknown',
'xsi:type': 'stixVocabs:ManagementClassVocab-1.0'
},
'nature_of_security_effect': [
{
'description_of_effect': 'Foobar',
'duration_of_availability_loss': {
'value': 'Days',
'xsi:type': 'stixVocabs:LossDurationVocab-1.0'
},
'non_public_data_compromised': {
'value': 'Yes',
'xsi:type': 'stixVocabs:SecurityCompromiseVocab-1.0',
'data_encrypted': True
},
'type_of_availability_loss': {
'value': 'Loss',
'xsi:type': 'stixVocabs:AvailabilityLossTypeVocab-1.1.1'
}
}
],
'ownership_class': {
'value': 'Unknown',
'xsi:type': 'stixVocabs:OwnershipClassVocab-1.0'
},
'type': {'value': 'Foobar'}
}
]
}


Expand Down

0 comments on commit ecf0558

Please sign in to comment.