Skip to content

Commit 9653bd7

Browse files
committed
minor edits
1 parent ddf36d4 commit 9653bd7

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import datetime as dt
2+
3+
from database_backends.postgres_backend import PostgresBackend
4+
import sys
5+
import json
6+
7+
import stix2
8+
from stix2.datastore.relational_db.relational_db import RelationalDBStore
9+
import stix2.properties
10+
11+
# needed so the relational db code knows to create tables for this
12+
from incident import incident, event, task, impact
13+
from identity_contact_information import identity_contact_information
14+
from observed_string import observed_string
15+
16+
17+
def main():
18+
with open(sys.argv[1], "r") as f:
19+
bundle = stix2.parse(json.load(f), allow_custom=True)
20+
store = RelationalDBStore(
21+
PostgresBackend("postgresql://localhost/stix-data-sink", force_recreate=True),
22+
True,
23+
None,
24+
True,
25+
print_sql=True,
26+
)
27+
28+
if store.sink.db_backend.database_exists:
29+
for obj in bundle.objects:
30+
store.add(obj)
31+
else:
32+
print("database does not exist")
33+
34+
35+
if __name__ == '__main__':
36+
main()

stix2/datastore/relational_db/relational_db.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from stix2.parsing import parse
1313

1414

15-
def _add(store, stix_data, allow_custom=True, version="2.1"):
15+
def _add(sink, stix_data, allow_custom=True, version="2.1"):
1616
"""Add STIX objects to MemoryStore/Sink.
1717
1818
Adds STIX objects to an in-memory dictionary for fast lookup.
@@ -32,12 +32,12 @@ def _add(store, stix_data, allow_custom=True, version="2.1"):
3232
if isinstance(stix_data, list):
3333
# STIX objects are in a list- recurse on each object
3434
for stix_obj in stix_data:
35-
_add(store, stix_obj, allow_custom, version)
35+
_add(sink, stix_obj, allow_custom, version)
3636

3737
elif stix_data["type"] == "bundle":
3838
# adding a json bundle - so just grab STIX objects
3939
for stix_obj in stix_data.get("objects", []):
40-
_add(store, stix_obj, allow_custom, version)
40+
_add(sink, stix_obj, allow_custom, version)
4141

4242
else:
4343
# Adding a single non-bundle object
@@ -46,7 +46,7 @@ def _add(store, stix_data, allow_custom=True, version="2.1"):
4646
else:
4747
stix_obj = parse(stix_data, allow_custom, version)
4848

49-
store.insert_object(stix_obj)
49+
sink.insert_object(stix_obj)
5050

5151

5252
class RelationalDBStore(DataStoreMixin):

0 commit comments

Comments
 (0)