Skip to content

Commit

Permalink
handle ARRAY
Browse files Browse the repository at this point in the history
  • Loading branch information
rpiazza committed Nov 7, 2024
1 parent f1af0dd commit 8fd7226
Show file tree
Hide file tree
Showing 6 changed files with 374 additions and 256 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

from sqlalchemy import create_engine
from sqlalchemy_utils import create_database, database_exists, drop_database

from sqlalchemy import ( # create_engine,; insert,
ARRAY, TIMESTAMP, Boolean, CheckConstraint, Column, Float, ForeignKey,
Integer, LargeBinary, Table, Text, UniqueConstraint,
)

class DatabaseBackend:
def __init__(self, database_connection_url, force_recreate=False, **kwargs: Any):
Expand All @@ -20,7 +23,7 @@ def __init__(self, database_connection_url, force_recreate=False, **kwargs: Any)
self.database_connection = create_engine(database_connection_url)

def _create_schemas(self):
pass
return

@staticmethod
def _determine_schema_name(stix_object):
Expand All @@ -32,4 +35,61 @@ def _create_database(self):
create_database(self.database_connection.url)
self.database_exists = database_exists(self.database_connection.url)

def schema_for(self, stix_class):
return ""

@staticmethod
def determine_sql_type_for_property(): # noqa: F811
pass

@staticmethod
def determine_sql_type_for_kill_chain_phase(): # noqa: F811
return None

@staticmethod
def determine_sql_type_for_binary_property(): # noqa: F811
return Text

@staticmethod
def determine_sql_type_for_boolean_property(): # noqa: F811
return Boolean

@staticmethod
def determine_sql_type_for_float_property(): # noqa: F811
return Float

@staticmethod
def determine_sql_type_for_hex_property(): # noqa: F811
return LargeBinary

@staticmethod
def determine_sql_type_for_integer_property(): # noqa: F811
return Integer

@staticmethod
def determine_sql_type_for_reference_property(): # noqa: F811
return Text

@staticmethod
def determine_sql_type_for_string_property(): # noqa: F811
return Text

@staticmethod
def determine_sql_type_for_timestamp_property(): # noqa: F811
return TIMESTAMP(timezone=True)

@staticmethod
def determine_sql_type_for_key_as_int(): # noqa: F811
return Integer


@staticmethod
def determine_sql_type_for_key_as_id(): # noqa: F811
return Text

@staticmethod
def array_allowed():
return False



Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
import os
from typing import Any
from sqlalchemy.schema import CreateSchema
from sqlalchemy import ( # create_engine,; insert,
ARRAY, TIMESTAMP, Boolean, CheckConstraint, Column, Float, ForeignKey,
Integer, LargeBinary, Table, Text, UniqueConstraint,
)

from .database_backend_base import DatabaseBackend
from stix2.datastore.relational_db.utils import schema_for

from stix2.base import (
_DomainObject, _MetaObject, _Observable, _RelationshipObject, _STIXBase,
_DomainObject, _Extension, _MetaObject, _Observable, _RelationshipObject, _STIXBase,
)

from stix2.properties import (
BinaryProperty, BooleanProperty, DictionaryProperty,
EmbeddedObjectProperty, EnumProperty, ExtensionsProperty, FloatProperty,
HashesProperty, HexProperty, IDProperty, IntegerProperty, ListProperty,
ObjectReferenceProperty, Property, ReferenceProperty, StringProperty,
TimestampProperty, TypeProperty,
)

from .database_backend_base import DatabaseBackend

class PostgresBackend(DatabaseBackend):
default_database_connection_url = \
Expand Down Expand Up @@ -35,4 +48,15 @@ def _determine_schema_name(stix_object):
elif isinstance(stix_object, _RelationshipObject):
return "sro"
elif isinstance(stix_object, _MetaObject):
return "common"
return "common"

def schema_for(self, stix_class):
return schema_for(stix_class)


@staticmethod
def array_allowed():
return True



6 changes: 3 additions & 3 deletions stix2/datastore/relational_db/relational_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def __init__(

self.metadata = MetaData()
create_table_objects(
self.metadata, stix_object_classes,
self.metadata, db_backend, stix_object_classes,
)

super().__init__(
Expand Down Expand Up @@ -251,11 +251,11 @@ def __init__(
else:
self.metadata = MetaData()
create_table_objects(
self.metadata, stix_object_classes,
self.metadata, db_backend, stix_object_classes,
)

def get(self, stix_id, version=None, _composite_filters=None):
with self.db_backend.connect() as conn:
with self.db_backend.database_connection.connect() as conn:
stix_obj = read_object(
stix_id,
self.metadata,
Expand Down
120 changes: 60 additions & 60 deletions stix2/datastore/relational_db/relational_db_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,61 +194,61 @@ 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"}
)
# @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():
Expand All @@ -263,17 +263,17 @@ def main():
store.sink.generate_stix_schema()
store.sink.clear_tables()

td = test_dictionary()
# td = test_dictionary()

store.add(td)
# store.add(td)

th = test_hex_list()
# th = test_hex_list()

# store.add(th)

tb = test_binary_list()
# tb = test_binary_list()

store.add(tb)
# store.add(tb)



Expand Down
Loading

0 comments on commit 8fd7226

Please sign in to comment.