-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
126 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
stix2/datastore/relational_db/database_backends/database_backend_base.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
|
||
from typing import Any | ||
import os | ||
|
||
from sqlalchemy import create_engine | ||
from sqlalchemy_utils import create_database, database_exists, drop_database | ||
|
||
|
||
class DatabaseBackend: | ||
def __init__(self, database_connection_url, force_recreate=False, **kwargs: Any): | ||
self.database_connection_url = database_connection_url | ||
self.database_exists = database_exists(database_connection_url) | ||
|
||
if force_recreate: | ||
if self.database_exists: | ||
drop_database(database_connection_url) | ||
create_database(database_connection_url) | ||
self.database_exists = database_exists(database_connection_url) | ||
|
||
self.database_connection = create_engine(database_connection_url) | ||
|
||
def _create_schemas(self): | ||
pass | ||
|
||
@staticmethod | ||
def _determine_schema_name(stix_object): | ||
return "" | ||
|
||
def _create_database(self): | ||
if self.database_exists: | ||
drop_database(self.database_connection.url) | ||
create_database(self.database_connection.url) | ||
self.database_exists = database_exists(self.database_connection.url) | ||
|
||
|
38 changes: 38 additions & 0 deletions
38
stix2/datastore/relational_db/database_backends/postgres_backend.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import os | ||
from typing import Any | ||
from sqlalchemy.schema import CreateSchema | ||
|
||
from .database_backend_base import DatabaseBackend | ||
|
||
from stix2.base import ( | ||
_DomainObject, _MetaObject, _Observable, _RelationshipObject, _STIXBase, | ||
) | ||
|
||
|
||
class PostgresBackend(DatabaseBackend): | ||
default_database_connection_url = \ | ||
f"postgresql://{os.getenv('POSTGRES_USER', 'postgres')}:" + \ | ||
f"{os.getenv('POSTGRES_PASSWORD', 'postgres')}@" + \ | ||
f"{os.getenv('POSTGRES_IP_ADDRESS', '0.0.0.0')}:" + \ | ||
f"{os.getenv('POSTGRES_PORT', '5432')}/postgres" | ||
|
||
def __init__(self, database_connection_url=default_database_connection_url, force_recreate=False, **kwargs: Any): | ||
super().__init__(database_connection_url, force_recreate=False, **kwargs) | ||
|
||
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)) | ||
|
||
@staticmethod | ||
def _determine_schema_name(stix_object): | ||
if isinstance(stix_object, _DomainObject): | ||
return "sdo" | ||
elif isinstance(stix_object, _Observable): | ||
return "sco" | ||
elif isinstance(stix_object, _RelationshipObject): | ||
return "sro" | ||
elif isinstance(stix_object, _MetaObject): | ||
return "common" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters