From f4f1828a42088e25b2d8a3f322aa6e4afed71f86 Mon Sep 17 00:00:00 2001 From: Rich Piazza Date: Thu, 12 Sep 2024 22:38:26 -0400 Subject: [PATCH] fixed binary list, dictionary insert --- .../datastore/relational_db/input_creation.py | 3 + .../relational_db/relational_db_testing.py | 71 +++++++++++++++++++ .../datastore/relational_db/table_creation.py | 2 +- 3 files changed, 75 insertions(+), 1 deletion(-) diff --git a/stix2/datastore/relational_db/input_creation.py b/stix2/datastore/relational_db/input_creation.py index 1b859751..d66fd4f3 100644 --- a/stix2/datastore/relational_db/input_creation.py +++ b/stix2/datastore/relational_db/input_creation.py @@ -53,6 +53,7 @@ def generate_insert_information(self, dictionary_name, stix_object, **kwargs): # integer, string, timestamp valid_types = stix_object._properties[dictionary_name].valid_types for name, value in stix_object[dictionary_name].items(): + bindings = dict() if not valid_types or len(self.valid_types) == 1: value_binding = "value" elif isinstance(value, int) and IntegerProperty in valid_types: @@ -61,6 +62,8 @@ def generate_insert_information(self, dictionary_name, stix_object, **kwargs): value_binding = "string_value" elif isinstance(value, bool) and BooleanProperty in valid_types: value_binding = "boolean_value" + elif isinstance(value, float) and FloatProperty in valid_types: + value_binding = "float_value" elif isinstance(value, STIXdatetime) and TimestampProperty in valid_types: value_binding = "timestamp_value" else: diff --git a/stix2/datastore/relational_db/relational_db_testing.py b/stix2/datastore/relational_db/relational_db_testing.py index 699e2262..3e720ace 100644 --- a/stix2/datastore/relational_db/relational_db_testing.py +++ b/stix2/datastore/relational_db/relational_db_testing.py @@ -192,6 +192,63 @@ def custom_obj(): return obj +@stix2.CustomObject( + "test-object", [ + ("prop_name", stix2.properties.ListProperty(stix2.properties.BinaryProperty())) + ], + "extension-definition--15de9cdb-3515-4271-8479-8141154c5647", + is_sdo=True +) +class TestClass: + pass + + +def test_binary_list(): + return TestClass(prop_name=["AREi", "7t3M"]) + +@stix2.CustomObject( + "test2-object", [ + ("prop_name", stix2.properties.ListProperty( + stix2.properties.HexProperty() + )) + ], + "extension-definition--15de9cdb-4567-4271-8479-8141154c5647", + is_sdo=True + ) + +class Test2Class: + pass + +def test_hex_list(): + return Test2Class( + prop_name=["1122", "fedc"] + ) + +@stix2.CustomObject( + "test3-object", [ + ("prop_name", + stix2.properties.DictionaryProperty( + valid_types=[ + stix2.properties.IntegerProperty, + stix2.properties.FloatProperty, + stix2.properties.StringProperty + ] + ) + ) + ], + "extension-definition--15de9cdb-1234-4271-8479-8141154c5647", + is_sdo=True + ) +class Test3Class: + pass + + +def test_dictionary(): + return Test3Class( + prop_name={"a": 1, "b": 2.3, "c": "foo"} + ) + + def main(): store = RelationalDBStore( "postgresql://localhost/stix-data-sink", @@ -205,6 +262,20 @@ def main(): store.sink.generate_stix_schema() store.sink.clear_tables() + td = test_dictionary() + + store.add(td) + + th = test_hex_list() + + # store.add(th) + + tb = test_binary_list() + + store.add(tb) + + + co = custom_obj() store.add(co) diff --git a/stix2/datastore/relational_db/table_creation.py b/stix2/datastore/relational_db/table_creation.py index b3cb4ac1..79e61397 100644 --- a/stix2/datastore/relational_db/table_creation.py +++ b/stix2/datastore/relational_db/table_creation.py @@ -245,7 +245,7 @@ def determine_sql_type(self): # noqa: F811 @add_method(BinaryProperty) def determine_sql_type(self): # noqa: F811 - return Boolean + return Text @add_method(BooleanProperty)