From 80c87340923fdfc111d7dd9ba38b0895368e6838 Mon Sep 17 00:00:00 2001 From: Rich Piazza Date: Tue, 26 Mar 2024 09:06:11 -0400 Subject: [PATCH] flaky --- .pre-commit-config.yaml | 8 +- .../datastore/relational_db/input_creation.py | 129 ++++++++++-------- .../datastore/relational_db/relational_db.py | 24 +++- .../datastore/relational_db/table_creation.py | 75 +++++----- stix2/datastore/relational_db/utils.py | 8 +- stix2/environment.py | 16 +-- stix2/equivalence/graph/__init__.py | 8 +- stix2/equivalence/object/__init__.py | 6 +- stix2/properties.py | 4 +- stix2/test/test_workbench.py | 14 +- stix2/test/v20/test_environment.py | 2 +- stix2/test/v20/test_granular_markings.py | 82 +++++------ stix2/test/v20/test_object_markings.py | 50 +++---- stix2/test/v20/test_utils.py | 2 +- stix2/test/v20/test_versioning.py | 2 +- stix2/test/v21/test_campaign.py | 2 +- stix2/test/v21/test_environment.py | 6 +- stix2/test/v21/test_granular_markings.py | 98 ++++++------- stix2/test/v21/test_object_markings.py | 50 +++---- stix2/test/v21/test_utils.py | 2 +- stix2/test/v21/test_versioning.py | 2 +- stix2/v20/sro.py | 2 +- stix2/v21/sro.py | 2 +- 23 files changed, 317 insertions(+), 277 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index c39aaf6d..35dff666 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,22 +1,22 @@ repos: - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v3.4.0 + rev: v4.5.0 hooks: - id: trailing-whitespace - id: check-merge-conflict - repo: https://github.com/asottile/add-trailing-comma - rev: v2.0.2 + rev: v3.1.0 hooks: - id: add-trailing-comma - repo: https://github.com/PyCQA/flake8 - rev: 3.8.4 + rev: 7.0.0 hooks: - id: flake8 name: Check project styling args: - --max-line-length=160 - repo: https://github.com/PyCQA/isort - rev: 5.12.0 + rev: 5.13.2 hooks: - id: isort name: Sort python imports (shows diff) diff --git a/stix2/datastore/relational_db/input_creation.py b/stix2/datastore/relational_db/input_creation.py index ded5cbf5..22a83fcf 100644 --- a/stix2/datastore/relational_db/input_creation.py +++ b/stix2/datastore/relational_db/input_creation.py @@ -1,28 +1,25 @@ from collections import OrderedDict -from sqlalchemy import ( - TIMESTAMP, - CheckConstraint, - Column, - ForeignKey, - Table, - Text, - create_engine, - insert, -) - +from sqlalchemy import insert +from stix2.datastore.relational_db.utils import ( + SCO_COMMON_PROPERTIES, SDO_COMMON_PROPERTIES, canonicalize_table_name, +) from stix2.properties import ( - DictionaryProperty, EmbeddedObjectProperty, EnumProperty, ExtensionsProperty, FloatProperty, - IntegerProperty, ListProperty, ReferenceProperty, StringProperty) - -from stix2.datastore.relational_db.utils import SCO_COMMON_PROPERTIES, SDO_COMMON_PROPERTIES, canonicalize_table_name + DictionaryProperty, EmbeddedObjectProperty, EnumProperty, + ExtensionsProperty, FloatProperty, IntegerProperty, ListProperty, + ReferenceProperty, StringProperty, +) def single_value(p): - return not(isinstance(p, (EmbeddedObjectProperty, - ListProperty, - DictionaryProperty))) + return not isinstance( + p, ( + EmbeddedObjectProperty, + ListProperty, + DictionaryProperty, + ), + ) def table_property(prop, name, core_properties): @@ -59,10 +56,10 @@ def derive_column_name(prop): return "value" -def generate_insert_for_array_in_table(table, property_name, values, prop, foreign_key_value): +def generate_insert_for_array_in_table(table, values, foreign_key_value): bindings = { - "id": foreign_key_value + "id": foreign_key_value, } for idx, item in enumerate(values): @@ -76,24 +73,20 @@ def generate_insert_for_array_in_table(table, property_name, values, prop, forei def generate_single_values(stix_object, properties, core_properties=[]): bindings = OrderedDict() for name, prop in properties.items(): - if (single_value(prop) and (name == 'id' or name not in core_properties) or - array_property(prop, name, core_properties)): + if ( + single_value(prop) and (name == 'id' or name not in core_properties) or + array_property(prop, name, core_properties) + ): if name in stix_object and name != "type": bindings[name] = stix_object[name] if not array_property(prop, name, core_properties) else "{" + ",".join( - ['"' + x + '"' for x in stix_object[name]]) + "}" + ['"' + x + '"' for x in stix_object[name]], + ) + "}" return bindings def generate_insert_for_embedded_object(type_name, item, foreign_key_value): bindings = generate_single_values(item, item._properties) bindings["id"] = foreign_key_value - sql = f"INSERT INTO {canonicalize_table_name(type_name, item._type)}" \ - f" ({','.join(bindings.keys())})" \ - f" VALUES ({','.join(values)}, %(id)s )" - - print("sql:", sql) - print("embedded:", bindings) - return [(sql, bindings)] def generate_insert_for_dictionary(item, dictionary_table, foreign_key_value, value_types): @@ -147,17 +140,21 @@ def generate_insert_for_external_references(data_sink, stix_object): if "hashes" in er: hashes_table = data_sink.tables_dictionary[canonicalize_table_name("external_references_hashes", "sdo")] - insert_statements.extend(generate_insert_for_hashes(er["hashes"], - hashes_table, - stix_object["id"])) + insert_statements.extend( + generate_insert_for_hashes( + er["hashes"], + hashes_table, + stix_object["id"], + ), + ) return insert_statements -def generate_insert_for_granular_markings(data_sink, stix_object, granular_markings_table): +def generate_insert_for_granular_markings(granular_markings_table, stix_object): granular_markings = stix_object["granular_markings"] bindings = { - "id": stix_object["id"] + "id": stix_object["id"], } for idx, granular_marking in enumerate(granular_markings): lang_binding_name = f"lang{idx}" @@ -171,15 +168,19 @@ def generate_insert_for_granular_markings(data_sink, stix_object, granular_marki return [insert(granular_markings_table).values(bindings)] -def generate_insert_for_extensions(extensions, foreign_key_value, type_name, core_properties): - sql_bindings_tuples = list() - for name, ex in extensions.items(): - sql_bindings_tuples.extend(generate_insert_for_subtype_extension(name, - ex, - foreign_key_value, - type_name, - core_properties)) - return sql_bindings_tuples +# def generate_insert_for_extensions(extensions, foreign_key_value, type_name, core_properties): +# sql_bindings_tuples = list() +# for name, ex in extensions.items(): +# sql_bindings_tuples.extend( +# generate_insert_for_subtype_extension( +# name, +# ex, +# foreign_key_value, +# type_name, +# core_properties, +# ), +# ) +# return sql_bindings_tuples def generate_insert_for_core(data_sink, stix_object, core_properties, schema_name): @@ -205,7 +206,13 @@ def generate_insert_for_core(data_sink, stix_object, core_properties, schema_nam object_markings_ref_table = data_sink.tables_dictionary["common.object_marking_refs_sdo"] else: object_markings_ref_table = data_sink.tables_dictionary["common.object_marking_refs_sco"] - insert_statements.extend(generate_insert_for_array_in_table(data_sink, stix_object, object_markings_ref_table)) + insert_statements.extend( + generate_insert_for_array_in_table( + object_markings_ref_table, + stix_object["object_marking_refs"], + stix_object["id"], + ), + ) # Granular markings if "granular_markings" in stix_object: @@ -213,12 +220,12 @@ def generate_insert_for_core(data_sink, stix_object, core_properties, schema_nam granular_marking_table = data_sink.tables_dictionary["common.granular_marking_sdo"] else: granular_marking_table = data_sink.tables_dictionary["common.granular_marking_sco"] - granular_input_statements = generate_insert_for_granular_markings(data_sink, - stix_object.granular_markings, - granular_marking_table) + granular_input_statements = generate_insert_for_granular_markings( + granular_marking_table, + stix_object.granular_markings, + ) insert_statements.extend(granular_input_statements) - return insert_statements @@ -255,16 +262,22 @@ def generate_insert_for_object(data_sink, stix_object, schema_name, foreign_key_ if table_property(prop, name, core_properties): if name in stix_object: if embedded_object_list_property(prop, name, core_properties): - insert_statements.extend(generate_insert_for_embedded_objects(name, - stix_object[name], - stix_object["id"])) + insert_statements.extend( + generate_insert_for_embedded_objects( + name, + stix_object[name], + stix_object["id"], + ), + ) elif isinstance(prop, ExtensionsProperty): pass else: - insert_statements.extend(generate_insert_for_array_in_table(stix_object["type"], - name, - stix_object[name], - properties[name], - stix_object["id"] )) + insert_statements.extend( + generate_insert_for_array_in_table( + stix_object["type"], + name, + stix_object[name], + properties[name], + stix_object["id"], ), + ) return insert_statements - diff --git a/stix2/datastore/relational_db/relational_db.py b/stix2/datastore/relational_db/relational_db.py index 30bc6a04..d05fb08e 100644 --- a/stix2/datastore/relational_db/relational_db.py +++ b/stix2/datastore/relational_db/relational_db.py @@ -1,13 +1,18 @@ from sqlalchemy import MetaData, create_engine -from sqlalchemy.schema import CreateTable +from sqlalchemy.schema import CreateSchema, CreateTable from stix2.base import _STIXBase from stix2.datastore import DataSink -from stix2.datastore.relational_db.table_creation import create_core_tables, generate_object_table -from stix2.datastore.relational_db.input_creation import generate_insert_for_object - +from stix2.datastore.relational_db.input_creation import ( + generate_insert_for_object, +) +from stix2.datastore.relational_db.table_creation import ( + create_core_tables, generate_object_table, +) from stix2.parsing import parse -from stix2.v21.base import (_DomainObject, _Extension, _Observable, _RelationshipObject,) +from stix2.v21.base import ( + _DomainObject, _Extension, _Observable, _RelationshipObject, +) def _get_all_subclasses(cls): @@ -89,6 +94,8 @@ def __init__( self.metadata = MetaData() self.database_connection = create_engine(database_connection_url) + self._create_schemas() + self.tables = self._create_table_objects() self.tables_dictionary = dict() for t in self.tables: @@ -97,6 +104,13 @@ def __init__( if instantiate_database: self._instantiate_database() + def _create_schemas(self): + with self.database_connection.begin() as trans: + trans.execute(CreateSchema("common", if_not_exists=True)) + trans.execute(CreateSchema("sdo", if_not_exists=True)) + trans.execute(CreateSchema("sco", if_not_exists=True)) + trans.execute(CreateSchema("sro", if_not_exists=True)) + def _create_table_objects(self): tables = create_core_tables(self.metadata) for stix_class in _get_all_subclasses(_DomainObject): diff --git a/stix2/datastore/relational_db/table_creation.py b/stix2/datastore/relational_db/table_creation.py index 7b935fd1..0f343c3f 100644 --- a/stix2/datastore/relational_db/table_creation.py +++ b/stix2/datastore/relational_db/table_creation.py @@ -6,6 +6,9 @@ ) from stix2.datastore.relational_db.add_method import add_method +from stix2.datastore.relational_db.utils import ( + SCO_COMMON_PROPERTIES, SDO_COMMON_PROPERTIES, canonicalize_table_name, +) from stix2.properties import ( BinaryProperty, BooleanProperty, DictionaryProperty, EmbeddedObjectProperty, EnumProperty, ExtensionsProperty, FloatProperty, @@ -15,7 +18,6 @@ ) from stix2.v21.common import KillChainPhase -from stix2.datastore.relational_db.utils import SCO_COMMON_PROPERTIES, SDO_COMMON_PROPERTIES, canonicalize_table_name def aux_table_property(prop, name, core_properties): if isinstance(prop, ListProperty) and name not in core_properties: @@ -36,14 +38,17 @@ def derive_column_name(prop): def create_object_markings_refs_table(metadata, sco_or_sdo): - return create_ref_table(metadata, - {"marking_definition"}, - "common.object_marking_refs_" + sco_or_sdo, - "common.core_" + sco_or_sdo + ".id", - 0) + return create_ref_table( + metadata, + {"marking_definition"}, + "object_marking_refs_" + sco_or_sdo, + "common.core_" + sco_or_sdo + ".id", + "common", + 0, + ) -def create_ref_table(metadata, specifics, table_name, foreign_key_name, auth_type=0): +def create_ref_table(metadata, specifics, table_name, foreign_key_name, schema_name, auth_type=0): columns = list() columns.append( Column( @@ -57,7 +62,7 @@ def create_ref_table(metadata, specifics, table_name, foreign_key_name, auth_typ ), ) columns.append(ref_column("ref_id", specifics, auth_type)) - return Table(table_name, metadata, *columns) + return Table(table_name, metadata, *columns, schema=schema_name) def create_hashes_table(name, metadata, schema_name, table_name): @@ -87,12 +92,12 @@ def create_hashes_table(name, metadata, schema_name, table_name): nullable=False, ), ) - return Table(canonicalize_table_name(table_name + "_" + name, schema_name), metadata, *columns) + return Table(canonicalize_table_name(table_name + "_" + name), metadata, *columns, schema=schema_name) def create_granular_markings_table(metadata, sco_or_sdo): return Table( - "common.granular_marking_" + sco_or_sdo, + "granular_marking_" + sco_or_sdo, metadata, Column( "id", @@ -119,6 +124,7 @@ def create_granular_markings_table(metadata, sco_or_sdo): OR (lang IS NOT NULL AND marking_ref IS NULL)""", ), + schema="common", ) @@ -132,7 +138,7 @@ def create_external_references_tables(metadata): "id ~ '^[a-z][a-z0-9-]+[a-z0-9]--[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$'", # noqa: E131 ), - primary_key=True + primary_key=True, ), Column("source_name", Text), Column("description", Text), @@ -140,7 +146,7 @@ def create_external_references_tables(metadata): Column("external_id", Text), ] return [ - Table("common.external_references", metadata, *columns), + Table("external_references", metadata, *columns, schema="common"), # create_hashes_table("hashes", metadata, "common", "external_references") ] @@ -179,9 +185,10 @@ def create_core_table(metadata, schema_name): else: columns.append(Column("defanged", Boolean, default=False)), return Table( - "common.core_" + schema_name, + "core_" + schema_name, metadata, - *columns + *columns, + schema="common", ) @@ -205,7 +212,7 @@ def generate_table_information(self, name, **kwargs): # noqa: F811 return Column( name, Text, - nullable=not(self.required), + nullable=not self.required, default=self._fixed_value if hasattr(self, "_fixed_value") else None, ) @@ -220,7 +227,7 @@ def generate_table_information(self, name, **kwargs): # noqa: F811 return Column( name, Integer, - nullable=not(self.required), + nullable=not self.required, default=self._fixed_value if hasattr(self, "_fixed_value") else None, ) @@ -235,7 +242,7 @@ def generate_table_information(self, name, **kwargs): # noqa: F811 return Column( name, Float, - nullable=not(self.required), + nullable=not self.required, default=self._fixed_value if hasattr(self, "_fixed_value") else None, ) @@ -250,7 +257,7 @@ def generate_table_information(self, name, **kwargs): # noqa: F811 return Column( name, Boolean, - nullable=not(self.required), + nullable=not self.required, default=self._fixed_value if hasattr(self, "_fixed_value") else None, ) @@ -265,7 +272,7 @@ def generate_table_information(self, name, **kwargs): # noqa: F811 return Column( name, Text, - nullable=not(self.required), + nullable=not self.required, default=self._fixed_value if hasattr(self, "_fixed_value") else None, ) @@ -359,7 +366,7 @@ def generate_table_information(self, name, metadata, schema_name, table_name, is Integer, ), ) - return [Table(canonicalize_table_name(table_name + "_" + name, schema_name), metadata, *columns)] + return [Table(canonicalize_table_name(table_name + "_" + name), metadata, *columns, schema=schema_name)] @add_method(HashesProperty) @@ -400,7 +407,7 @@ def generate_table_information(self, name, metadata, schema_name, table_name, ** nullable=False, ), ) - return [Table(canonicalize_table_name(table_name + "_" + name, schema_name), metadata, *columns)] + return [Table(canonicalize_table_name(table_name + "_" + name), metadata, *columns, schema=schema_name)] def ref_column(name, specifics, auth_type=0): @@ -409,13 +416,14 @@ def ref_column(name, specifics, auth_type=0): if auth_type == 0: constraint = \ CheckConstraint( - f"{name} ~ '^({types})" + "--[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$'", # noqa: E131 + f"{name} ~ '^({types})" + + "--[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$'", ) else: constraint = \ CheckConstraint( - f"(NOT({name} ~ '^({types})') AND ({name} ~" + "'--[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$')", - # noqa: E131 + f"(NOT({name} ~ '^({types})') AND ({name} ~ " + + "'--[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$')", ) return Column(name, Text, constraint) else: @@ -454,10 +462,15 @@ def generate_table_information(self, name, metadata, schema_name, table_name, ** is_extension = kwargs.get('is_extension') tables = list() if isinstance(self.contained, ReferenceProperty): - return [create_ref_table(metadata, - self.contained.specifics, - canonicalize_table_name(table_name + "_" + name, schema_name), - canonicalize_table_name(table_name, schema_name) + ".id", )] + return [ + create_ref_table( + metadata, + self.contained.specifics, + canonicalize_table_name(table_name + "_" + name), + canonicalize_table_name(table_name, schema_name) + ".id", + "common", + ), + ] elif isinstance(self.contained, EmbeddedObjectProperty): columns = list() columns.append( @@ -478,7 +491,7 @@ def generate_table_information(self, name, metadata, schema_name, table_name, ** nullable=False, ), ) - tables.append(Table(canonicalize_table_name(table_name + "_" + name, schema_name), metadata, *columns)) + tables.append(Table(canonicalize_table_name(table_name + "_" + name), metadata, *columns, schema=schema_name)) tables.extend( self.contained.generate_table_information( name, @@ -560,7 +573,7 @@ def generate_object_table( ) columns.append(column) - all_tables = [Table(canonicalize_table_name(table_name, schema_name), metadata, *columns)] + all_tables = [Table(canonicalize_table_name(table_name), metadata, *columns, schema=schema_name)] all_tables.extend(tables) return all_tables @@ -572,7 +585,7 @@ def create_core_tables(metadata): create_core_table(metadata, "sco"), create_granular_markings_table(metadata, "sco"), create_object_markings_refs_table(metadata, "sdo"), - create_object_markings_refs_table(metadata, "sco") + create_object_markings_refs_table(metadata, "sco"), ] tables.extend(create_external_references_tables(metadata)) return tables diff --git a/stix2/datastore/relational_db/utils.py b/stix2/datastore/relational_db/utils.py index ffa90eaa..e908ce89 100644 --- a/stix2/datastore/relational_db/utils.py +++ b/stix2/datastore/relational_db/utils.py @@ -5,7 +5,7 @@ "spec_version", "object_marking_refs", "granular_markings", - "defanged" + "defanged", } # Helps us know which data goes in core, and which in a type-specific table. @@ -23,13 +23,13 @@ "labels", "confidence", "lang", - "external_references" + "external_references", } -def canonicalize_table_name(table_name, schema_name): +def canonicalize_table_name(table_name, schema_name=None): if schema_name: full_name = schema_name + "." + table_name else: full_name = table_name - return full_name.replace("-", "_") \ No newline at end of file + return full_name.replace("-", "_") diff --git a/stix2/environment.py b/stix2/environment.py index eab2ba9e..76d7be0d 100644 --- a/stix2/environment.py +++ b/stix2/environment.py @@ -191,7 +191,7 @@ def creator_of(self, obj): def object_similarity( obj1, obj2, prop_scores={}, ds1=None, ds2=None, ignore_spec_version=False, versioning_checks=False, - max_depth=1, **weight_dict + max_depth=1, **weight_dict, ): """This method returns a measure of how similar the two objects are. @@ -236,14 +236,14 @@ def object_similarity( """ return object_similarity( obj1, obj2, prop_scores, ds1, ds2, ignore_spec_version, - versioning_checks, max_depth, **weight_dict + versioning_checks, max_depth, **weight_dict, ) @staticmethod def object_equivalence( obj1, obj2, prop_scores={}, threshold=70, ds1=None, ds2=None, ignore_spec_version=False, versioning_checks=False, - max_depth=1, **weight_dict + max_depth=1, **weight_dict, ): """This method returns a true/false value if two objects are semantically equivalent. Internally, it calls the object_similarity function and compares it against the given @@ -294,13 +294,13 @@ def object_equivalence( """ return object_equivalence( obj1, obj2, prop_scores, threshold, ds1, ds2, - ignore_spec_version, versioning_checks, max_depth, **weight_dict + ignore_spec_version, versioning_checks, max_depth, **weight_dict, ) @staticmethod def graph_similarity( ds1, ds2, prop_scores={}, ignore_spec_version=False, - versioning_checks=False, max_depth=1, **weight_dict + versioning_checks=False, max_depth=1, **weight_dict, ): """This method returns a similarity score for two given graphs. Each DataStore can contain a connected or disconnected graph and the @@ -347,14 +347,14 @@ def graph_similarity( """ return graph_similarity( ds1, ds2, prop_scores, ignore_spec_version, - versioning_checks, max_depth, **weight_dict + versioning_checks, max_depth, **weight_dict, ) @staticmethod def graph_equivalence( ds1, ds2, prop_scores={}, threshold=70, ignore_spec_version=False, versioning_checks=False, - max_depth=1, **weight_dict + max_depth=1, **weight_dict, ): """This method returns a true/false value if two graphs are semantically equivalent. Internally, it calls the graph_similarity function and compares it against the given @@ -403,5 +403,5 @@ def graph_equivalence( """ return graph_equivalence( ds1, ds2, prop_scores, threshold, ignore_spec_version, - versioning_checks, max_depth, **weight_dict + versioning_checks, max_depth, **weight_dict, ) diff --git a/stix2/equivalence/graph/__init__.py b/stix2/equivalence/graph/__init__.py index 1f46fd3e..1beee4aa 100644 --- a/stix2/equivalence/graph/__init__.py +++ b/stix2/equivalence/graph/__init__.py @@ -11,7 +11,7 @@ def graph_equivalence( ds1, ds2, prop_scores={}, threshold=70, ignore_spec_version=False, versioning_checks=False, - max_depth=1, **weight_dict + max_depth=1, **weight_dict, ): """This method returns a true/false value if two graphs are semantically equivalent. Internally, it calls the graph_similarity function and compares it against the given @@ -60,7 +60,7 @@ def graph_equivalence( """ similarity_result = graph_similarity( ds1, ds2, prop_scores, ignore_spec_version, - versioning_checks, max_depth, **weight_dict + versioning_checks, max_depth, **weight_dict, ) if similarity_result >= threshold: return True @@ -69,7 +69,7 @@ def graph_equivalence( def graph_similarity( ds1, ds2, prop_scores={}, ignore_spec_version=False, - versioning_checks=False, max_depth=1, **weight_dict + versioning_checks=False, max_depth=1, **weight_dict, ): """This method returns a similarity score for two given graphs. Each DataStore can contain a connected or disconnected graph and the @@ -147,7 +147,7 @@ def graph_similarity( result = object_similarity( object1, object2, iprop_score, ds1, ds2, ignore_spec_version, versioning_checks, - max_depth, **weights + max_depth, **weights, ) if object1_id not in results: diff --git a/stix2/equivalence/object/__init__.py b/stix2/equivalence/object/__init__.py index 2b16a346..00534881 100644 --- a/stix2/equivalence/object/__init__.py +++ b/stix2/equivalence/object/__init__.py @@ -14,7 +14,7 @@ def object_equivalence( obj1, obj2, prop_scores={}, threshold=70, ds1=None, ds2=None, ignore_spec_version=False, - versioning_checks=False, max_depth=1, **weight_dict + versioning_checks=False, max_depth=1, **weight_dict, ): """This method returns a true/false value if two objects are semantically equivalent. Internally, it calls the object_similarity function and compares it against the given @@ -65,7 +65,7 @@ def object_equivalence( """ similarity_result = object_similarity( obj1, obj2, prop_scores, ds1, ds2, ignore_spec_version, - versioning_checks, max_depth, **weight_dict + versioning_checks, max_depth, **weight_dict, ) if similarity_result >= threshold: return True @@ -75,7 +75,7 @@ def object_equivalence( def object_similarity( obj1, obj2, prop_scores={}, ds1=None, ds2=None, ignore_spec_version=False, versioning_checks=False, - max_depth=1, **weight_dict + max_depth=1, **weight_dict, ): """This method returns a measure of similarity depending on how similar the two objects are. diff --git a/stix2/properties.py b/stix2/properties.py index 00f24624..24a83eb6 100644 --- a/stix2/properties.py +++ b/stix2/properties.py @@ -588,13 +588,13 @@ def clean(self, value, allow_custom): if auth_type == self._WHITELIST: type_ok = is_stix_type( - obj_type, self.spec_version, *generics + obj_type, self.spec_version, *generics, ) or obj_type in specifics else: type_ok = ( not is_stix_type( - obj_type, self.spec_version, *generics + obj_type, self.spec_version, *generics, ) and obj_type not in specifics ) or obj_type in blacklist_exceptions diff --git a/stix2/test/test_workbench.py b/stix2/test/test_workbench.py index 84f97a59..dc3f66f5 100644 --- a/stix2/test/test_workbench.py +++ b/stix2/test/test_workbench.py @@ -32,7 +32,7 @@ def test_workbench_environment(): # Create a STIX object ind = create( - Indicator, id=constants.INDICATOR_ID, **constants.INDICATOR_KWARGS + Indicator, id=constants.INDICATOR_ID, **constants.INDICATOR_KWARGS, ) save(ind) @@ -50,7 +50,7 @@ def test_workbench_environment(): def test_workbench_get_all_attack_patterns(): mal = AttackPattern( - id=constants.ATTACK_PATTERN_ID, **constants.ATTACK_PATTERN_KWARGS + id=constants.ATTACK_PATTERN_ID, **constants.ATTACK_PATTERN_KWARGS, ) save(mal) @@ -70,7 +70,7 @@ def test_workbench_get_all_campaigns(): def test_workbench_get_all_courses_of_action(): coa = CourseOfAction( - id=constants.COURSE_OF_ACTION_ID, **constants.COURSE_OF_ACTION_KWARGS + id=constants.COURSE_OF_ACTION_ID, **constants.COURSE_OF_ACTION_KWARGS, ) save(coa) @@ -114,7 +114,7 @@ def test_workbench_get_all_infrastructures(): def test_workbench_get_all_intrusion_sets(): ins = IntrusionSet( - id=constants.INTRUSION_SET_ID, **constants.INTRUSION_SET_KWARGS + id=constants.INTRUSION_SET_ID, **constants.INTRUSION_SET_KWARGS, ) save(ins) @@ -161,7 +161,7 @@ def test_workbench_get_all_notes(): def test_workbench_get_all_observed_data(): od = ObservedData( - id=constants.OBSERVED_DATA_ID, **constants.OBSERVED_DATA_KWARGS + id=constants.OBSERVED_DATA_ID, **constants.OBSERVED_DATA_KWARGS, ) save(od) @@ -190,7 +190,7 @@ def test_workbench_get_all_reports(): def test_workbench_get_all_threat_actors(): thr = ThreatActor( - id=constants.THREAT_ACTOR_ID, **constants.THREAT_ACTOR_KWARGS + id=constants.THREAT_ACTOR_ID, **constants.THREAT_ACTOR_KWARGS, ) save(thr) @@ -210,7 +210,7 @@ def test_workbench_get_all_tools(): def test_workbench_get_all_vulnerabilities(): vuln = Vulnerability( - id=constants.VULNERABILITY_ID, **constants.VULNERABILITY_KWARGS + id=constants.VULNERABILITY_ID, **constants.VULNERABILITY_KWARGS, ) save(vuln) diff --git a/stix2/test/v20/test_environment.py b/stix2/test/v20/test_environment.py index b6f3a0b3..8dd4eca7 100644 --- a/stix2/test/v20/test_environment.py +++ b/stix2/test/v20/test_environment.py @@ -459,7 +459,7 @@ def test_semantic_check_with_versioning(ds, ds2): }, ], object_marking_refs=[stix2.v20.TLP_WHITE], - ) + ), ) ds.add(ind) score = stix2.equivalence.object.reference_check(ind.id, INDICATOR_ID, ds, ds2, **weights) diff --git a/stix2/test/v20/test_granular_markings.py b/stix2/test/v20/test_granular_markings.py index ae2da3b0..e20d5685 100644 --- a/stix2/test/v20/test_granular_markings.py +++ b/stix2/test/v20/test_granular_markings.py @@ -15,7 +15,7 @@ def test_add_marking_mark_one_selector_multiple_refs(): before = Malware( - **MALWARE_KWARGS + **MALWARE_KWARGS, ) after = Malware( granular_markings=[ @@ -28,7 +28,7 @@ def test_add_marking_mark_one_selector_multiple_refs(): "marking_ref": MARKING_IDS[1], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) before = markings.add_markings(before, [MARKING_IDS[0], MARKING_IDS[1]], ["description"]) @@ -47,7 +47,7 @@ def test_add_marking_mark_one_selector_multiple_refs(): "marking_ref": MARKING_IDS[0], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ), MARKING_IDS[0], ), @@ -60,7 +60,7 @@ def test_add_marking_mark_one_selector_multiple_refs(): "marking_ref": MARKING_IDS[0], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ), MARKING_IDS[0], ), @@ -73,7 +73,7 @@ def test_add_marking_mark_one_selector_multiple_refs(): "marking_ref": TLP_RED.id, }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ), TLP_RED, ), @@ -91,7 +91,7 @@ def test_add_marking_mark_multiple_selector_one_refs(data): def test_add_marking_mark_multiple_selector_multiple_refs(): before = Malware( - **MALWARE_KWARGS + **MALWARE_KWARGS, ) after = Malware( granular_markings=[ @@ -104,7 +104,7 @@ def test_add_marking_mark_multiple_selector_multiple_refs(): "marking_ref": MARKING_IDS[1], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) before = markings.add_markings(before, [MARKING_IDS[0], MARKING_IDS[1]], ["description", "name"]) @@ -120,7 +120,7 @@ def test_add_marking_mark_another_property_same_marking(): "marking_ref": MARKING_IDS[0], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) after = Malware( granular_markings=[ @@ -129,7 +129,7 @@ def test_add_marking_mark_another_property_same_marking(): "marking_ref": MARKING_IDS[0], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) before = markings.add_markings(before, [MARKING_IDS[0]], ["name"]) @@ -145,7 +145,7 @@ def test_add_marking_mark_same_property_same_marking(): "marking_ref": MARKING_IDS[0], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) after = Malware( granular_markings=[ @@ -154,7 +154,7 @@ def test_add_marking_mark_same_property_same_marking(): "marking_ref": MARKING_IDS[0], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) before = markings.add_markings(before, [MARKING_IDS[0]], ["description"]) @@ -391,7 +391,7 @@ def test_get_markings_positional_arguments_combinations(data): "marking_ref": MARKING_IDS[1], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ), [MARKING_IDS[0], MARKING_IDS[1]], ), @@ -407,7 +407,7 @@ def test_get_markings_positional_arguments_combinations(data): "marking_ref": MARKING_IDS[1], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ), [MARKING_IDS[0], MARKING_IDS[1]], ), @@ -426,7 +426,7 @@ def test_remove_marking_remove_multiple_selector_one_ref(): "marking_ref": MARKING_IDS[0], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) before = markings.remove_markings(before, MARKING_IDS[0], ["description", "modified"]) assert "granular_markings" not in before @@ -440,7 +440,7 @@ def test_remove_marking_mark_one_selector_from_multiple_ones(): "marking_ref": MARKING_IDS[0], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) before = Malware( granular_markings=[ @@ -449,7 +449,7 @@ def test_remove_marking_mark_one_selector_from_multiple_ones(): "marking_ref": MARKING_IDS[0], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) before = markings.remove_markings(before, [MARKING_IDS[0]], ["modified"]) for m in before["granular_markings"]: @@ -468,7 +468,7 @@ def test_remove_marking_mark_one_selector_markings_from_multiple_ones(): "marking_ref": MARKING_IDS[1], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) before = Malware( granular_markings=[ @@ -481,7 +481,7 @@ def test_remove_marking_mark_one_selector_markings_from_multiple_ones(): "marking_ref": MARKING_IDS[1], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) before = markings.remove_markings(before, [MARKING_IDS[0]], ["modified"]) for m in before["granular_markings"]: @@ -500,7 +500,7 @@ def test_remove_marking_mark_mutilple_selector_multiple_refs(): "marking_ref": MARKING_IDS[1], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) before = markings.remove_markings(before, [MARKING_IDS[0], MARKING_IDS[1]], ["description", "modified"]) assert "granular_markings" not in before @@ -514,7 +514,7 @@ def test_remove_marking_mark_another_property_same_marking(): "marking_ref": MARKING_IDS[0], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) before = Malware( granular_markings=[ @@ -527,7 +527,7 @@ def test_remove_marking_mark_another_property_same_marking(): "marking_ref": MARKING_IDS[0], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) before = markings.remove_markings(before, [MARKING_IDS[0]], ["modified"]) for m in before["granular_markings"]: @@ -542,7 +542,7 @@ def test_remove_marking_mark_same_property_same_marking(): "marking_ref": MARKING_IDS[0], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) before = markings.remove_markings(before, [MARKING_IDS[0]], ["description"]) assert "granular_markings" not in before @@ -572,7 +572,7 @@ def test_remove_marking_not_present(): "marking_ref": MARKING_IDS[0], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) with pytest.raises(MarkingNotFoundError): markings.remove_markings(before, [MARKING_IDS[1]], ["description"]) @@ -594,7 +594,7 @@ def test_remove_marking_not_present(): "marking_ref": MARKING_IDS[3], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ), dict( granular_markings=[ @@ -611,7 +611,7 @@ def test_remove_marking_not_present(): "marking_ref": MARKING_IDS[3], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ), ] @@ -844,14 +844,14 @@ def test_create_sdo_with_invalid_marking(): "marking_ref": MARKING_IDS[0], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) assert str(excinfo.value) == "Selector foo in Malware is not valid!" def test_set_marking_mark_one_selector_multiple_refs(): before = Malware( - **MALWARE_KWARGS + **MALWARE_KWARGS, ) after = Malware( granular_markings=[ @@ -864,7 +864,7 @@ def test_set_marking_mark_one_selector_multiple_refs(): "marking_ref": MARKING_IDS[1], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) before = markings.set_markings(before, [MARKING_IDS[0], MARKING_IDS[1]], ["description"]) for m in before["granular_markings"]: @@ -879,7 +879,7 @@ def test_set_marking_mark_multiple_selector_one_refs(): "marking_ref": MARKING_IDS[1], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) after = Malware( granular_markings=[ @@ -888,7 +888,7 @@ def test_set_marking_mark_multiple_selector_one_refs(): "marking_ref": MARKING_IDS[0], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) before = markings.set_markings(before, [MARKING_IDS[0]], ["description", "modified"]) for m in before["granular_markings"]: @@ -897,7 +897,7 @@ def test_set_marking_mark_multiple_selector_one_refs(): def test_set_marking_mark_multiple_selector_multiple_refs_from_none(): before = Malware( - **MALWARE_KWARGS + **MALWARE_KWARGS, ) after = Malware( granular_markings=[ @@ -910,7 +910,7 @@ def test_set_marking_mark_multiple_selector_multiple_refs_from_none(): "marking_ref": MARKING_IDS[1], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) before = markings.set_markings(before, [MARKING_IDS[0], MARKING_IDS[1]], ["description", "modified"]) for m in before["granular_markings"]: @@ -925,7 +925,7 @@ def test_set_marking_mark_another_property_same_marking(): "marking_ref": MARKING_IDS[0], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) after = Malware( granular_markings=[ @@ -938,7 +938,7 @@ def test_set_marking_mark_another_property_same_marking(): "marking_ref": MARKING_IDS[2], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) before = markings.set_markings(before, [MARKING_IDS[1], MARKING_IDS[2]], ["description"]) @@ -962,7 +962,7 @@ def test_set_marking_bad_selector(marking): "marking_ref": MARKING_IDS[0], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) after = Malware( granular_markings=[ @@ -971,7 +971,7 @@ def test_set_marking_bad_selector(marking): "marking_ref": MARKING_IDS[0], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) with pytest.raises(InvalidSelectorError): @@ -988,7 +988,7 @@ def test_set_marking_mark_same_property_same_marking(): "marking_ref": MARKING_IDS[0], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) after = Malware( granular_markings=[ @@ -997,7 +997,7 @@ def test_set_marking_mark_same_property_same_marking(): "marking_ref": MARKING_IDS[0], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) before = markings.set_markings(before, [MARKING_IDS[0]], ["description"]) for m in before["granular_markings"]: @@ -1020,7 +1020,7 @@ def test_set_marking_mark_same_property_same_marking(): "marking_ref": MARKING_IDS[2], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ), dict( granular_markings=[ @@ -1037,7 +1037,7 @@ def test_set_marking_mark_same_property_same_marking(): "marking_ref": MARKING_IDS[2], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ), ] @@ -1099,7 +1099,7 @@ def test_set_marking_on_id_property(): "marking_ref": MARKING_IDS[0], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) assert "id" in malware["granular_markings"][0]["selectors"] diff --git a/stix2/test/v20/test_object_markings.py b/stix2/test/v20/test_object_markings.py index 6bd2269d..88b3f14b 100644 --- a/stix2/test/v20/test_object_markings.py +++ b/stix2/test/v20/test_object_markings.py @@ -26,7 +26,7 @@ Malware(**MALWARE_KWARGS), Malware( object_marking_refs=[MARKING_IDS[0]], - **MALWARE_KWARGS + **MALWARE_KWARGS, ), MARKING_IDS[0], ), @@ -34,7 +34,7 @@ MALWARE_KWARGS, dict( object_marking_refs=[MARKING_IDS[0]], - **MALWARE_KWARGS + **MALWARE_KWARGS, ), MARKING_IDS[0], ), @@ -42,7 +42,7 @@ Malware(**MALWARE_KWARGS), Malware( object_marking_refs=[TLP_AMBER.id], - **MALWARE_KWARGS + **MALWARE_KWARGS, ), TLP_AMBER, ), @@ -60,12 +60,12 @@ def test_add_markings_one_marking(data): def test_add_markings_multiple_marking(): before = Malware( - **MALWARE_KWARGS + **MALWARE_KWARGS, ) after = Malware( object_marking_refs=[MARKING_IDS[0], MARKING_IDS[1]], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) before = markings.add_markings(before, [MARKING_IDS[0], MARKING_IDS[1]], None) @@ -76,7 +76,7 @@ def test_add_markings_multiple_marking(): def test_add_markings_combination(): before = Malware( - **MALWARE_KWARGS + **MALWARE_KWARGS, ) after = Malware( object_marking_refs=[MARKING_IDS[0], MARKING_IDS[1]], @@ -90,7 +90,7 @@ def test_add_markings_combination(): "marking_ref": MARKING_IDS[3], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) before = markings.add_markings(before, MARKING_IDS[0], None) @@ -114,7 +114,7 @@ def test_add_markings_combination(): ) def test_add_markings_bad_markings(data): before = Malware( - **MALWARE_KWARGS + **MALWARE_KWARGS, ) with pytest.raises(exceptions.InvalidValueError): before = markings.add_markings(before, data, None) @@ -274,14 +274,14 @@ def test_get_markings_object_and_granular_combinations(data): ( Malware( object_marking_refs=[MARKING_IDS[0]], - **MALWARE_KWARGS + **MALWARE_KWARGS, ), Malware(**MALWARE_KWARGS), ), ( dict( object_marking_refs=[MARKING_IDS[0]], - **MALWARE_KWARGS + **MALWARE_KWARGS, ), MALWARE_KWARGS, ), @@ -306,33 +306,33 @@ def test_remove_markings_object_level(data): ( Malware( object_marking_refs=[MARKING_IDS[0], MARKING_IDS[1], MARKING_IDS[2]], - **MALWARE_KWARGS + **MALWARE_KWARGS, ), Malware( object_marking_refs=[MARKING_IDS[1]], - **MALWARE_KWARGS + **MALWARE_KWARGS, ), [MARKING_IDS[0], MARKING_IDS[2]], ), ( dict( object_marking_refs=[MARKING_IDS[0], MARKING_IDS[1], MARKING_IDS[2]], - **MALWARE_KWARGS + **MALWARE_KWARGS, ), dict( object_marking_refs=[MARKING_IDS[1]], - **MALWARE_KWARGS + **MALWARE_KWARGS, ), [MARKING_IDS[0], MARKING_IDS[2]], ), ( Malware( object_marking_refs=[MARKING_IDS[0], MARKING_IDS[1], TLP_AMBER.id], - **MALWARE_KWARGS + **MALWARE_KWARGS, ), Malware( object_marking_refs=[MARKING_IDS[1]], - **MALWARE_KWARGS + **MALWARE_KWARGS, ), [MARKING_IDS[0], TLP_AMBER], ), @@ -350,7 +350,7 @@ def test_remove_markings_multiple(data): def test_remove_markings_bad_markings(): before = Malware( object_marking_refs=[MARKING_IDS[0], MARKING_IDS[1], MARKING_IDS[2]], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) with pytest.raises(MarkingNotFoundError) as excinfo: markings.remove_markings(before, [MARKING_IDS[4]], None) @@ -362,14 +362,14 @@ def test_remove_markings_bad_markings(): ( Malware( object_marking_refs=[MARKING_IDS[0], MARKING_IDS[1], MARKING_IDS[2]], - **MALWARE_KWARGS + **MALWARE_KWARGS, ), Malware(**MALWARE_KWARGS), ), ( dict( object_marking_refs=[MARKING_IDS[0], MARKING_IDS[1], MARKING_IDS[2]], - **MALWARE_KWARGS + **MALWARE_KWARGS, ), MALWARE_KWARGS, ), @@ -533,14 +533,14 @@ def test_is_marked_object_and_granular_combinations(): ( Malware( object_marking_refs=[MARKING_IDS[0], MARKING_IDS[1], MARKING_IDS[2]], - **MALWARE_KWARGS + **MALWARE_KWARGS, ), Malware(**MALWARE_KWARGS), ), ( dict( object_marking_refs=[MARKING_IDS[0], MARKING_IDS[1], MARKING_IDS[2]], - **MALWARE_KWARGS + **MALWARE_KWARGS, ), MALWARE_KWARGS, ), @@ -557,11 +557,11 @@ def test_is_marked_no_markings(data): def test_set_marking(): before = Malware( object_marking_refs=[MARKING_IDS[0], MARKING_IDS[1], MARKING_IDS[2]], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) after = Malware( object_marking_refs=[MARKING_IDS[4], MARKING_IDS[5]], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) before = markings.set_markings(before, [MARKING_IDS[4], MARKING_IDS[5]], None) @@ -585,11 +585,11 @@ def test_set_marking(): def test_set_marking_bad_input(data): before = Malware( object_marking_refs=[MARKING_IDS[0]], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) after = Malware( object_marking_refs=[MARKING_IDS[0]], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) with pytest.raises(exceptions.InvalidValueError): before = markings.set_markings(before, data, None) diff --git a/stix2/test/v20/test_utils.py b/stix2/test/v20/test_utils.py index f61369b9..e3b15279 100644 --- a/stix2/test/v20/test_utils.py +++ b/stix2/test/v20/test_utils.py @@ -201,7 +201,7 @@ def test_deduplicate(stix_objs1): def test_find_property_index(object, tuple_to_find, expected_index): assert stix2.serialization.find_property_index( object, - *tuple_to_find + *tuple_to_find, ) == expected_index diff --git a/stix2/test/v20/test_versioning.py b/stix2/test/v20/test_versioning.py index d3973f03..ace1cc82 100644 --- a/stix2/test/v20/test_versioning.py +++ b/stix2/test/v20/test_versioning.py @@ -43,7 +43,7 @@ def test_making_new_version_with_embedded_object(): "source_name": "capec", "external_id": "CAPEC-163", }], - **CAMPAIGN_MORE_KWARGS + **CAMPAIGN_MORE_KWARGS, ) campaign_v2 = campaign_v1.new_version( diff --git a/stix2/test/v21/test_campaign.py b/stix2/test/v21/test_campaign.py index edc7d777..5fc8e4bb 100644 --- a/stix2/test/v21/test_campaign.py +++ b/stix2/test/v21/test_campaign.py @@ -21,7 +21,7 @@ def test_campaign_example(): campaign = stix2.v21.Campaign( - **CAMPAIGN_MORE_KWARGS + **CAMPAIGN_MORE_KWARGS, ) assert campaign.serialize(pretty=True) == EXPECTED diff --git a/stix2/test/v21/test_environment.py b/stix2/test/v21/test_environment.py index 51ca15a5..502f2b77 100644 --- a/stix2/test/v21/test_environment.py +++ b/stix2/test/v21/test_environment.py @@ -973,7 +973,7 @@ def test_semantic_check_with_versioning(ds, ds2): }, ], object_marking_refs=[stix2.v21.TLP_WHITE], - ) + ), ) ds.add(ind) score = stix2.equivalence.object.reference_check(ind.id, INDICATOR_ID, ds, ds2, **weights) @@ -1146,7 +1146,7 @@ def test_depth_limiting(): } prop_scores1 = {} env1 = stix2.equivalence.graph.graph_similarity( - mem_store1, mem_store2, prop_scores1, **custom_weights + mem_store1, mem_store2, prop_scores1, **custom_weights, ) assert round(env1) == 38 @@ -1159,7 +1159,7 @@ def test_depth_limiting(): # Switching parameters prop_scores2 = {} env2 = stix2.equivalence.graph.graph_similarity( - mem_store2, mem_store1, prop_scores2, **custom_weights + mem_store2, mem_store1, prop_scores2, **custom_weights, ) assert round(env2) == 38 diff --git a/stix2/test/v21/test_granular_markings.py b/stix2/test/v21/test_granular_markings.py index ff8fe26d..2f5bae8c 100644 --- a/stix2/test/v21/test_granular_markings.py +++ b/stix2/test/v21/test_granular_markings.py @@ -14,7 +14,7 @@ def test_add_marking_mark_one_selector_multiple_refs(): before = Malware( - **MALWARE_KWARGS + **MALWARE_KWARGS, ) after = Malware( granular_markings=[ @@ -27,7 +27,7 @@ def test_add_marking_mark_one_selector_multiple_refs(): "marking_ref": MARKING_IDS[1], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) before = markings.add_markings(before, [MARKING_IDS[0], MARKING_IDS[1]], ["description"]) @@ -46,7 +46,7 @@ def test_add_marking_mark_one_selector_multiple_refs(): "marking_ref": MARKING_IDS[0], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ), MARKING_IDS[0], ), @@ -59,7 +59,7 @@ def test_add_marking_mark_one_selector_multiple_refs(): "marking_ref": MARKING_IDS[0], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ), MARKING_IDS[0], ), @@ -72,7 +72,7 @@ def test_add_marking_mark_one_selector_multiple_refs(): "marking_ref": TLP_RED.id, }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ), TLP_RED, ), @@ -90,7 +90,7 @@ def test_add_marking_mark_multiple_selector_one_refs(data): def test_add_marking_mark_multiple_selector_multiple_refs(): before = Malware( - **MALWARE_KWARGS + **MALWARE_KWARGS, ) after = Malware( granular_markings=[ @@ -103,7 +103,7 @@ def test_add_marking_mark_multiple_selector_multiple_refs(): "marking_ref": MARKING_IDS[1], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) before = markings.add_markings(before, [MARKING_IDS[0], MARKING_IDS[1]], ["description", "name"]) @@ -113,7 +113,7 @@ def test_add_marking_mark_multiple_selector_multiple_refs(): def test_add_marking_mark_multiple_selector_multiple_refs_mixed(): before = Malware( - **MALWARE_KWARGS + **MALWARE_KWARGS, ) after = Malware( granular_markings=[ @@ -134,7 +134,7 @@ def test_add_marking_mark_multiple_selector_multiple_refs_mixed(): "lang": MARKING_LANGS[1], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) before = markings.add_markings(before, [MARKING_IDS[0], MARKING_IDS[1], MARKING_LANGS[0], MARKING_LANGS[1]], ["description", "name"]) @@ -150,7 +150,7 @@ def test_add_marking_mark_another_property_same_marking(): "marking_ref": MARKING_IDS[0], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) after = Malware( granular_markings=[ @@ -159,7 +159,7 @@ def test_add_marking_mark_another_property_same_marking(): "marking_ref": MARKING_IDS[0], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) before = markings.add_markings(before, [MARKING_IDS[0]], ["name"]) @@ -175,7 +175,7 @@ def test_add_marking_mark_same_property_same_marking(): "marking_ref": MARKING_IDS[0], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) after = Malware( granular_markings=[ @@ -184,7 +184,7 @@ def test_add_marking_mark_same_property_same_marking(): "marking_ref": MARKING_IDS[0], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) before = markings.add_markings(before, [MARKING_IDS[0]], ["description"]) @@ -513,7 +513,7 @@ def test_get_markings_multiple_selectors_with_options(data): "marking_ref": MARKING_IDS[1], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ), [MARKING_IDS[0], MARKING_IDS[1]], ), @@ -529,7 +529,7 @@ def test_get_markings_multiple_selectors_with_options(data): "marking_ref": MARKING_IDS[1], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ), [MARKING_IDS[0], MARKING_IDS[1]], ), @@ -548,7 +548,7 @@ def test_remove_marking_remove_multiple_selector_one_ref(): "marking_ref": MARKING_IDS[0], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) before = markings.remove_markings(before, MARKING_IDS[0], ["description", "modified"]) assert "granular_markings" not in before @@ -562,7 +562,7 @@ def test_remove_marking_mark_one_selector_from_multiple_ones(): "marking_ref": MARKING_IDS[0], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) before = Malware( granular_markings=[ @@ -571,7 +571,7 @@ def test_remove_marking_mark_one_selector_from_multiple_ones(): "marking_ref": MARKING_IDS[0], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) before = markings.remove_markings(before, [MARKING_IDS[0]], ["modified"]) for m in before["granular_markings"]: @@ -590,7 +590,7 @@ def test_remove_marking_mark_one_selector_from_multiple_ones_mixed(): "lang": MARKING_LANGS[0], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) before = Malware( granular_markings=[ @@ -603,7 +603,7 @@ def test_remove_marking_mark_one_selector_from_multiple_ones_mixed(): "lang": MARKING_LANGS[0], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) before = markings.remove_markings(before, [MARKING_IDS[0], MARKING_LANGS[0]], ["modified"]) for m in before["granular_markings"]: @@ -622,7 +622,7 @@ def test_remove_marking_mark_one_selector_markings_from_multiple_ones(): "marking_ref": MARKING_IDS[1], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) before = Malware( granular_markings=[ @@ -635,7 +635,7 @@ def test_remove_marking_mark_one_selector_markings_from_multiple_ones(): "marking_ref": MARKING_IDS[1], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) before = markings.remove_markings(before, [MARKING_IDS[0]], ["modified"]) for m in before["granular_markings"]: @@ -654,7 +654,7 @@ def test_remove_marking_mark_mutilple_selector_multiple_refs(): "marking_ref": MARKING_IDS[1], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) before = markings.remove_markings(before, [MARKING_IDS[0], MARKING_IDS[1]], ["description", "modified"]) assert "granular_markings" not in before @@ -668,7 +668,7 @@ def test_remove_marking_mark_another_property_same_marking(): "marking_ref": MARKING_IDS[0], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) before = Malware( granular_markings=[ @@ -681,7 +681,7 @@ def test_remove_marking_mark_another_property_same_marking(): "marking_ref": MARKING_IDS[0], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) before = markings.remove_markings(before, [MARKING_IDS[0]], ["modified"]) for m in before["granular_markings"]: @@ -696,7 +696,7 @@ def test_remove_marking_mark_same_property_same_marking(): "marking_ref": MARKING_IDS[0], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) before = markings.remove_markings(before, [MARKING_IDS[0]], ["description"]) assert "granular_markings" not in before @@ -726,7 +726,7 @@ def test_remove_marking_not_present(): "marking_ref": MARKING_IDS[0], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) with pytest.raises(MarkingNotFoundError): markings.remove_markings(before, [MARKING_IDS[1]], ["description"]) @@ -752,7 +752,7 @@ def test_remove_marking_not_present(): "lang": MARKING_LANGS[1], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ), dict( granular_markings=[ @@ -773,7 +773,7 @@ def test_remove_marking_not_present(): "lang": MARKING_LANGS[1], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ), ] @@ -1008,14 +1008,14 @@ def test_create_sdo_with_invalid_marking(): "marking_ref": MARKING_IDS[0], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) assert str(excinfo.value) == "Selector foo in Malware is not valid!" def test_set_marking_mark_one_selector_multiple_refs(): before = Malware( - **MALWARE_KWARGS + **MALWARE_KWARGS, ) after = Malware( granular_markings=[ @@ -1028,7 +1028,7 @@ def test_set_marking_mark_one_selector_multiple_refs(): "marking_ref": MARKING_IDS[1], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) before = markings.set_markings(before, [MARKING_IDS[0], MARKING_IDS[1]], ["description"]) for m in before["granular_markings"]: @@ -1037,7 +1037,7 @@ def test_set_marking_mark_one_selector_multiple_refs(): def test_set_marking_mark_one_selector_multiple_lang_refs(): before = Malware( - **MALWARE_KWARGS + **MALWARE_KWARGS, ) after = Malware( granular_markings=[ @@ -1050,7 +1050,7 @@ def test_set_marking_mark_one_selector_multiple_lang_refs(): "lang": MARKING_LANGS[1], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) before = markings.set_markings(before, [MARKING_LANGS[0], MARKING_LANGS[1]], ["description"]) for m in before["granular_markings"]: @@ -1065,7 +1065,7 @@ def test_set_marking_mark_multiple_selector_one_refs(): "marking_ref": MARKING_IDS[1], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) after = Malware( granular_markings=[ @@ -1074,7 +1074,7 @@ def test_set_marking_mark_multiple_selector_one_refs(): "marking_ref": MARKING_IDS[0], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) before = markings.set_markings(before, [MARKING_IDS[0]], ["description", "modified"]) for m in before["granular_markings"]: @@ -1093,7 +1093,7 @@ def test_set_marking_mark_multiple_mixed_markings(): "lang": MARKING_LANGS[2], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) after = Malware( granular_markings=[ @@ -1106,7 +1106,7 @@ def test_set_marking_mark_multiple_mixed_markings(): "lang": MARKING_LANGS[3], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) before = markings.set_markings(before, [MARKING_IDS[2], MARKING_LANGS[3]], ["description", "modified"]) for m in before["granular_markings"]: @@ -1115,7 +1115,7 @@ def test_set_marking_mark_multiple_mixed_markings(): def test_set_marking_mark_multiple_selector_multiple_refs_from_none(): before = Malware( - **MALWARE_KWARGS + **MALWARE_KWARGS, ) after = Malware( granular_markings=[ @@ -1128,7 +1128,7 @@ def test_set_marking_mark_multiple_selector_multiple_refs_from_none(): "marking_ref": MARKING_IDS[1], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) before = markings.set_markings(before, [MARKING_IDS[0], MARKING_IDS[1]], ["description", "modified"]) for m in before["granular_markings"]: @@ -1143,7 +1143,7 @@ def test_set_marking_mark_another_property_same_marking(): "marking_ref": MARKING_IDS[0], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) after = Malware( granular_markings=[ @@ -1156,7 +1156,7 @@ def test_set_marking_mark_another_property_same_marking(): "marking_ref": MARKING_IDS[2], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) before = markings.set_markings(before, [MARKING_IDS[1], MARKING_IDS[2]], ["description"]) @@ -1180,7 +1180,7 @@ def test_set_marking_bad_selector(marking): "marking_ref": MARKING_IDS[0], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) after = Malware( granular_markings=[ @@ -1189,7 +1189,7 @@ def test_set_marking_bad_selector(marking): "marking_ref": MARKING_IDS[0], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) with pytest.raises(InvalidSelectorError): @@ -1206,7 +1206,7 @@ def test_set_marking_mark_same_property_same_marking(): "marking_ref": MARKING_IDS[0], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) after = Malware( granular_markings=[ @@ -1215,7 +1215,7 @@ def test_set_marking_mark_same_property_same_marking(): "marking_ref": MARKING_IDS[0], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) before = markings.set_markings(before, [MARKING_IDS[0]], ["description"]) for m in before["granular_markings"]: @@ -1238,7 +1238,7 @@ def test_set_marking_mark_same_property_same_marking(): "marking_ref": MARKING_IDS[2], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ), dict( granular_markings=[ @@ -1255,7 +1255,7 @@ def test_set_marking_mark_same_property_same_marking(): "marking_ref": MARKING_IDS[2], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ), ] @@ -1317,7 +1317,7 @@ def test_set_marking_on_id_property(): "marking_ref": MARKING_IDS[0], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) assert "id" in malware["granular_markings"][0]["selectors"] diff --git a/stix2/test/v21/test_object_markings.py b/stix2/test/v21/test_object_markings.py index bb1c4ab0..63273fb7 100644 --- a/stix2/test/v21/test_object_markings.py +++ b/stix2/test/v21/test_object_markings.py @@ -25,7 +25,7 @@ Malware(**MALWARE_KWARGS), Malware( object_marking_refs=[MARKING_IDS[0]], - **MALWARE_KWARGS + **MALWARE_KWARGS, ), MARKING_IDS[0], ), @@ -33,7 +33,7 @@ MALWARE_KWARGS, dict( object_marking_refs=[MARKING_IDS[0]], - **MALWARE_KWARGS + **MALWARE_KWARGS, ), MARKING_IDS[0], ), @@ -41,7 +41,7 @@ Malware(**MALWARE_KWARGS), Malware( object_marking_refs=[TLP_AMBER.id], - **MALWARE_KWARGS + **MALWARE_KWARGS, ), TLP_AMBER, ), @@ -59,12 +59,12 @@ def test_add_markings_one_marking(data): def test_add_markings_multiple_marking(): before = Malware( - **MALWARE_KWARGS + **MALWARE_KWARGS, ) after = Malware( object_marking_refs=[MARKING_IDS[0], MARKING_IDS[1]], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) before = markings.add_markings(before, [MARKING_IDS[0], MARKING_IDS[1]], None) @@ -75,7 +75,7 @@ def test_add_markings_multiple_marking(): def test_add_markings_combination(): before = Malware( - **MALWARE_KWARGS + **MALWARE_KWARGS, ) after = Malware( object_marking_refs=[MARKING_IDS[0], MARKING_IDS[1]], @@ -89,7 +89,7 @@ def test_add_markings_combination(): "marking_ref": MARKING_IDS[3], }, ], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) before = markings.add_markings(before, MARKING_IDS[0], None) @@ -113,7 +113,7 @@ def test_add_markings_combination(): ) def test_add_markings_bad_markings(data): before = Malware( - **MALWARE_KWARGS + **MALWARE_KWARGS, ) with pytest.raises(exceptions.InvalidValueError): before = markings.add_markings(before, data, None) @@ -273,14 +273,14 @@ def test_get_markings_object_and_granular_combinations(data): ( Malware( object_marking_refs=[MARKING_IDS[0]], - **MALWARE_KWARGS + **MALWARE_KWARGS, ), Malware(**MALWARE_KWARGS), ), ( dict( object_marking_refs=[MARKING_IDS[0]], - **MALWARE_KWARGS + **MALWARE_KWARGS, ), MALWARE_KWARGS, ), @@ -305,33 +305,33 @@ def test_remove_markings_object_level(data): ( Malware( object_marking_refs=[MARKING_IDS[0], MARKING_IDS[1], MARKING_IDS[2]], - **MALWARE_KWARGS + **MALWARE_KWARGS, ), Malware( object_marking_refs=[MARKING_IDS[1]], - **MALWARE_KWARGS + **MALWARE_KWARGS, ), [MARKING_IDS[0], MARKING_IDS[2]], ), ( dict( object_marking_refs=[MARKING_IDS[0], MARKING_IDS[1], MARKING_IDS[2]], - **MALWARE_KWARGS + **MALWARE_KWARGS, ), dict( object_marking_refs=[MARKING_IDS[1]], - **MALWARE_KWARGS + **MALWARE_KWARGS, ), [MARKING_IDS[0], MARKING_IDS[2]], ), ( Malware( object_marking_refs=[MARKING_IDS[0], MARKING_IDS[1], TLP_AMBER.id], - **MALWARE_KWARGS + **MALWARE_KWARGS, ), Malware( object_marking_refs=[MARKING_IDS[1]], - **MALWARE_KWARGS + **MALWARE_KWARGS, ), [MARKING_IDS[0], TLP_AMBER], ), @@ -349,7 +349,7 @@ def test_remove_markings_multiple(data): def test_remove_markings_bad_markings(): before = Malware( object_marking_refs=[MARKING_IDS[0], MARKING_IDS[1], MARKING_IDS[2]], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) with pytest.raises(MarkingNotFoundError) as excinfo: markings.remove_markings(before, [MARKING_IDS[4]], None) @@ -361,14 +361,14 @@ def test_remove_markings_bad_markings(): ( Malware( object_marking_refs=[MARKING_IDS[0], MARKING_IDS[1], MARKING_IDS[2]], - **MALWARE_KWARGS + **MALWARE_KWARGS, ), Malware(**MALWARE_KWARGS), ), ( dict( object_marking_refs=[MARKING_IDS[0], MARKING_IDS[1], MARKING_IDS[2]], - **MALWARE_KWARGS + **MALWARE_KWARGS, ), MALWARE_KWARGS, ), @@ -532,14 +532,14 @@ def test_is_marked_object_and_granular_combinations(): ( Malware( object_marking_refs=[MARKING_IDS[0], MARKING_IDS[1], MARKING_IDS[2]], - **MALWARE_KWARGS + **MALWARE_KWARGS, ), Malware(**MALWARE_KWARGS), ), ( dict( object_marking_refs=[MARKING_IDS[0], MARKING_IDS[1], MARKING_IDS[2]], - **MALWARE_KWARGS + **MALWARE_KWARGS, ), MALWARE_KWARGS, ), @@ -556,11 +556,11 @@ def test_is_marked_no_markings(data): def test_set_marking(): before = Malware( object_marking_refs=[MARKING_IDS[0], MARKING_IDS[1], MARKING_IDS[2]], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) after = Malware( object_marking_refs=[MARKING_IDS[4], MARKING_IDS[5]], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) before = markings.set_markings(before, [MARKING_IDS[4], MARKING_IDS[5]], None) @@ -584,11 +584,11 @@ def test_set_marking(): def test_set_marking_bad_input(data): before = Malware( object_marking_refs=[MARKING_IDS[0]], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) after = Malware( object_marking_refs=[MARKING_IDS[0]], - **MALWARE_KWARGS + **MALWARE_KWARGS, ) with pytest.raises(exceptions.InvalidValueError): before = markings.set_markings(before, data, None) diff --git a/stix2/test/v21/test_utils.py b/stix2/test/v21/test_utils.py index 33e7ea49..6dad23e8 100644 --- a/stix2/test/v21/test_utils.py +++ b/stix2/test/v21/test_utils.py @@ -205,7 +205,7 @@ def test_deduplicate(stix_objs1): def test_find_property_index(object, tuple_to_find, expected_index): assert stix2.serialization.find_property_index( object, - *tuple_to_find + *tuple_to_find, ) == expected_index diff --git a/stix2/test/v21/test_versioning.py b/stix2/test/v21/test_versioning.py index c7b6f119..98e01383 100644 --- a/stix2/test/v21/test_versioning.py +++ b/stix2/test/v21/test_versioning.py @@ -48,7 +48,7 @@ def test_making_new_version_with_embedded_object(): "source_name": "capec", "external_id": "CAPEC-163", }], - **CAMPAIGN_MORE_KWARGS + **CAMPAIGN_MORE_KWARGS, ) campaign_v2 = campaign_v1.new_version( diff --git a/stix2/v20/sro.py b/stix2/v20/sro.py index 1372a5e5..13d5e136 100644 --- a/stix2/v20/sro.py +++ b/stix2/v20/sro.py @@ -39,7 +39,7 @@ class Relationship(_RelationshipObject): # Explicitly define the first three kwargs to make readable Relationship declarations. def __init__( self, source_ref=None, relationship_type=None, - target_ref=None, **kwargs + target_ref=None, **kwargs, ): # Allow (source_ref, relationship_type, target_ref) as positional args. if source_ref and not kwargs.get('source_ref'): diff --git a/stix2/v21/sro.py b/stix2/v21/sro.py index bf636c3d..8ef1582c 100644 --- a/stix2/v21/sro.py +++ b/stix2/v21/sro.py @@ -46,7 +46,7 @@ class Relationship(_RelationshipObject): # Explicitly define the first three kwargs to make readable Relationship declarations. def __init__( self, source_ref=None, relationship_type=None, - target_ref=None, **kwargs + target_ref=None, **kwargs, ): # Allow (source_ref, relationship_type, target_ref) as positional args. if source_ref and not kwargs.get('source_ref'):