def _generate_id(self): """ Generate a UUIDv5 for this observable, using its "ID contributing properties". :return: The ID, or None if no ID contributing properties are set """ id_ = None json_serializable_object = {} for key in self._id_contributing_properties: if key in self: obj_value = self[key] if key == "hashes": serializable_value = _choose_one_hash(obj_value) if serializable_value is None: raise InvalidValueError( self, key, "No hashes given", ) else: serializable_value = _make_json_serializable(obj_value) json_serializable_object[key] = serializable_value if json_serializable_object: data = canonicalize(json_serializable_object, utf8=False) # The situation is complicated w.r.t. python 2/3 behavior, so # I'd rather not rely on particular exceptions being raised to # determine what to do. Better to just check the python version # directly. if six.PY3: uuid_ = uuid.uuid5(SCO_DET_ID_NAMESPACE, data) else: uuid_ = uuid.uuid5( SCO_DET_ID_NAMESPACE, data.encode("utf-8"), ) id_ = "{}--{}".format(self._type, six.text_type(uuid_)) return id_
def generate_sco_id(type, instance): required_prefix = type + "--" if not _SCO_CLASSES: # compute it once module = importlib.import_module("stix2.v21") for k, c in inspect.getmembers(module, inspect.isclass): if hasattr(c, "_properties") and "type" in c._properties: _SCO_CLASSES[c._properties["type"]._fixed_value] = c if type in _SCO_CLASSES: klass = _SCO_CLASSES[type] if klass and hasattr(klass, "_id_contributing_properties" ) and klass._id_contributing_properties: contributing_properties = klass._id_contributing_properties # streamlined_obj_vals = [] streamlined_object = {} possible_hash = None if "hashes" in instance and "hashes" in contributing_properties: possible_hash = _choose_one_hash(instance["hashes"]) if possible_hash: # streamlined_obj_vals.append(possible_hash) streamlined_object["hashes"] = possible_hash for key in contributing_properties: if key != "hashes" and key in instance: # We don't need to handle the isinstance(...) cases here # because the elevator uses Python default containers # to represent its content. # streamlined_obj_vals.append(instance[key]) streamlined_object[key] = instance[key] # if streamlined_obj_vals: if streamlined_object: # data = canonicalize(streamlined_obj_vals, utf8=False) data = canonicalize(streamlined_object, utf8=False) # try/except here to enable python 2 compatibility try: return required_prefix + text_type( uuid.uuid5(SCO_DET_ID_NAMESPACE, data)) except UnicodeDecodeError: return required_prefix + text_type( uuid.uuid5(SCO_DET_ID_NAMESPACE, data.encode("utf-8"))) return required_prefix + text_type(uuid.uuid4())
def _generate_id(self, kwargs): required_prefix = self._type + "--" properties_to_use = self._id_contributing_properties if properties_to_use: streamlined_obj_vals = [] if "hashes" in kwargs and "hashes" in properties_to_use: possible_hash = _choose_one_hash(kwargs["hashes"]) if possible_hash: streamlined_obj_vals.append(possible_hash) for key in properties_to_use: if key != "hashes" and key in kwargs: if isinstance(kwargs[key], dict) or isinstance( kwargs[key], _STIXBase): temp_deep_copy = copy.deepcopy(dict(kwargs[key])) _recursive_stix_to_dict(temp_deep_copy) streamlined_obj_vals.append(temp_deep_copy) elif isinstance(kwargs[key], list) and isinstance( kwargs[key][0], _STIXBase): for obj in kwargs[key]: temp_deep_copy = copy.deepcopy(dict(obj)) _recursive_stix_to_dict(temp_deep_copy) streamlined_obj_vals.append(temp_deep_copy) else: streamlined_obj_vals.append(kwargs[key]) if streamlined_obj_vals: data = canonicalize(streamlined_obj_vals, utf8=False) # The situation is complicated w.r.t. python 2/3 behavior, so # I'd rather not rely on particular exceptions being raised to # determine what to do. Better to just check the python version # directly. if six.PY3: return required_prefix + six.text_type( uuid.uuid5(SCO_DET_ID_NAMESPACE, data)) else: return required_prefix + six.text_type( uuid.uuid5(SCO_DET_ID_NAMESPACE, data.encode("utf-8"))) # We return None if there are no values specified for any of the id-contributing-properties return None
def _generate_id(self, kwargs): required_prefix = self._type + "--" properties_to_use = self._id_contributing_properties if properties_to_use: streamlined_obj_vals = [] if "hashes" in kwargs and "hashes" in properties_to_use: possible_hash = _choose_one_hash(kwargs["hashes"]) if possible_hash: streamlined_obj_vals.append(possible_hash) for key in properties_to_use: if key != "hashes" and key in kwargs: if isinstance(kwargs[key], dict) or isinstance( kwargs[key], _STIXBase): temp_deep_copy = copy.deepcopy(dict(kwargs[key])) _recursive_stix_to_dict(temp_deep_copy) streamlined_obj_vals.append(temp_deep_copy) elif isinstance(kwargs[key], list) and isinstance( kwargs[key][0], _STIXBase): for obj in kwargs[key]: temp_deep_copy = copy.deepcopy(dict(obj)) _recursive_stix_to_dict(temp_deep_copy) streamlined_obj_vals.append(temp_deep_copy) else: streamlined_obj_vals.append(kwargs[key]) if streamlined_obj_vals: data = canonicalize(streamlined_obj_vals, utf8=False) # try/except here to enable python 2 compatibility try: return required_prefix + six.text_type( uuid.uuid5(SCO_DET_ID_NAMESPACE, data)) except UnicodeDecodeError: return required_prefix + six.text_type( uuid.uuid5(SCO_DET_ID_NAMESPACE, six.binary_type(data))) # We return None if there are no values specified for any of the id-contributing-properties return None
def _generate_id(self): """ Generate a UUIDv5 for this observable, using its "ID contributing properties". :return: The ID, or None if no ID contributing properties are set """ id_ = None json_serializable_object = {} for key in self._id_contributing_properties: if key in self: obj_value = self[key] if key == "hashes": serializable_value = _choose_one_hash(obj_value) if serializable_value is None: raise InvalidValueError( self, key, "No hashes given", ) else: serializable_value = _make_json_serializable(obj_value) json_serializable_object[key] = serializable_value if json_serializable_object: data = canonicalize(json_serializable_object, utf8=False) uuid_ = uuid.uuid5(SCO_DET_ID_NAMESPACE, data) id_ = "{}--{}".format(self._type, str(uuid_)) return id_