cti-python-stix2/stix2/markings/object_markings.py

130 lines
3.4 KiB
Python
Raw Normal View History

2017-06-09 20:21:42 +02:00
from stix2 import exceptions
2017-06-09 20:21:42 +02:00
from stix2.markings import utils
def get_markings(obj):
"""
Get all object level markings from the given SDO or SRO object.
2017-06-09 20:21:42 +02:00
Args:
obj: A SDO or SRO object.
2017-06-09 20:21:42 +02:00
Returns:
list: Marking identifiers contained in the SDO or SRO. Empty list if no
markings are present in `object_marking_refs`.
2017-06-09 20:21:42 +02:00
"""
return obj.get("object_marking_refs", [])
2017-06-09 20:21:42 +02:00
def add_markings(obj, marking):
"""
Appends an object level marking to the object_marking_refs collection.
Args:
obj: A SDO or SRO object.
marking: identifier or list of identifiers to apply SDO or SRO object.
2017-06-09 20:21:42 +02:00
Returns:
A new version of the given SDO or SRO with specified markings added.
2017-06-09 20:21:42 +02:00
"""
marking = utils.convert_to_list(marking)
object_markings = set(obj.get("object_marking_refs", []) + marking)
2017-06-09 20:21:42 +02:00
return obj.new_version(object_marking_refs=list(object_markings))
2017-06-09 20:21:42 +02:00
def remove_markings(obj, marking):
"""
Removes object level marking from the object_marking_refs collection.
Args:
obj: A SDO or SRO object.
marking: identifier or list of identifiers that apply to the
SDO or SRO object.
2017-06-09 20:21:42 +02:00
Raises:
MarkingNotFoundError: If markings to remove are not found on
the provided SDO or SRO.
Returns:
A new version of the given SDO or SRO with specified markings removed.
2017-06-09 20:21:42 +02:00
"""
marking = utils.convert_to_list(marking)
object_markings = obj.get("object_marking_refs", [])
if not object_markings:
return obj
2017-06-09 20:21:42 +02:00
if any(x not in obj["object_marking_refs"] for x in marking):
raise exceptions.MarkingNotFoundError(obj, marking)
2017-06-09 20:21:42 +02:00
new_markings = [x for x in object_markings if x not in marking]
if new_markings:
return obj.new_version(object_marking_refs=new_markings)
else:
return obj.new_version(object_marking_refs=None)
2017-06-09 20:21:42 +02:00
def set_markings(obj, marking):
"""
Removes all object level markings and appends new object level markings to
the collection. Refer to `clear_markings` and `add_markings` for details.
Args:
obj: A SDO or SRO object.
marking: identifier or list of identifiers to apply in the
SDO or SRO object.
2017-06-09 20:21:42 +02:00
Returns:
A new version of the given SDO or SRO with specified markings removed
and new ones added.
2017-06-09 20:21:42 +02:00
"""
return add_markings(clear_markings(obj), marking)
2017-06-09 20:21:42 +02:00
def clear_markings(obj):
"""
Removes all object level markings from the object_marking_refs collection.
Args:
obj: A SDO or SRO object.
2017-06-09 20:21:42 +02:00
Returns:
A new version of the given SDO or SRO with object_marking_refs cleared.
2017-06-09 20:21:42 +02:00
"""
return obj.new_version(object_marking_refs=None)
2017-06-09 20:21:42 +02:00
def is_marked(obj, marking=None):
"""
Checks if SDO or SRO is marked by any marking or by specific marking(s).
2017-06-09 20:21:42 +02:00
Args:
obj: A SDO or SRO object.
2017-06-09 20:21:42 +02:00
marking: identifier or list of marking identifiers that apply to the
SDO or SRO object.
2017-06-09 20:21:42 +02:00
Returns:
bool: True if SDO or SRO has object level markings. False otherwise.
2017-06-09 20:21:42 +02:00
Note:
When an identifier or list of identifiers is provided, if ANY of the
provided marking refs match, True is returned.
2017-06-09 20:21:42 +02:00
"""
marking = utils.convert_to_list(marking)
object_markings = obj.get("object_marking_refs", [])
if marking:
return any(x in object_markings for x in marking)
else:
return bool(object_markings)