From 7883614d2f5a7adbd4f589ddeecc37ba703cd9cb Mon Sep 17 00:00:00 2001 From: "Desai, Kartikey H" Date: Wed, 9 Jan 2019 08:36:10 -0500 Subject: [PATCH 1/6] Fixes #232 --- stix2/datastore/filesystem.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stix2/datastore/filesystem.py b/stix2/datastore/filesystem.py index b4f3d15..27dc45d 100644 --- a/stix2/datastore/filesystem.py +++ b/stix2/datastore/filesystem.py @@ -15,7 +15,7 @@ import six from stix2 import v20, v21 from stix2.base import _STIXBase from stix2.core import parse -from stix2.datastore import DataSink, DataSource, DataStoreMixin +from stix2.datastore import DataSink, DataSource, DataStoreMixin, DataSourceError from stix2.datastore.filters import Filter, FilterSet, apply_common_filters from stix2.utils import format_datetime, get_type_from_id, is_marking @@ -546,7 +546,7 @@ class FileSystemSink(DataSink): # TODO: Better handling of the overwriting case. if os.path.isfile(file_path): - print("Attempted to overwrite file!", file_path, file=sys.stderr) + raise DataSourceError("Attempted to overwrite file (!) at: {}".format(file_path)) else: with io.open(file_path, 'w', encoding=encoding) as f: stix_obj = stix_obj.serialize(pretty=True, encoding=encoding, ensure_ascii=False) From 77b2e0e3e3db594b21dfa61ac52b63883bc096b5 Mon Sep 17 00:00:00 2001 From: "Desai, Kartikey H" Date: Wed, 9 Jan 2019 10:22:33 -0500 Subject: [PATCH 2/6] Remove a few comments and Fixes #232 --- stix2/datastore/filesystem.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/stix2/datastore/filesystem.py b/stix2/datastore/filesystem.py index 27dc45d..e0f5d68 100644 --- a/stix2/datastore/filesystem.py +++ b/stix2/datastore/filesystem.py @@ -1,7 +1,4 @@ """Python STIX2 FileSystem Source/Sink""" -# Temporary while we address TODO statement -from __future__ import print_function - import errno import io import json @@ -544,7 +541,6 @@ class FileSystemSink(DataSink): else: stix_obj = v20.Bundle(stix_obj, allow_custom=self.allow_custom) - # TODO: Better handling of the overwriting case. if os.path.isfile(file_path): raise DataSourceError("Attempted to overwrite file (!) at: {}".format(file_path)) else: From 767a758b28b5a3569ade11e37b76605a31971da3 Mon Sep 17 00:00:00 2001 From: "Desai, Kartikey H" Date: Wed, 9 Jan 2019 11:32:51 -0500 Subject: [PATCH 3/6] Fix styling issue around imports for issue 232 --- stix2/datastore/filesystem.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/stix2/datastore/filesystem.py b/stix2/datastore/filesystem.py index e0f5d68..0e8bed2 100644 --- a/stix2/datastore/filesystem.py +++ b/stix2/datastore/filesystem.py @@ -5,14 +5,15 @@ import json import os import re import stat -import sys import six from stix2 import v20, v21 from stix2.base import _STIXBase from stix2.core import parse -from stix2.datastore import DataSink, DataSource, DataStoreMixin, DataSourceError +from stix2.datastore import ( + DataSink, DataSource, DataSourceError, DataStoreMixin, +) from stix2.datastore.filters import Filter, FilterSet, apply_common_filters from stix2.utils import format_datetime, get_type_from_id, is_marking From 6e28cc8fe68a84c685f36bb674d76ed1da3a00a3 Mon Sep 17 00:00:00 2001 From: "Desai, Kartikey H" Date: Fri, 11 Jan 2019 09:26:55 -0500 Subject: [PATCH 4/6] Add test to fix for issue 232 --- stix2/test/v20/test_datastore_filesystem.py | 36 +++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/stix2/test/v20/test_datastore_filesystem.py b/stix2/test/v20/test_datastore_filesystem.py index 84a3034..c8607e2 100644 --- a/stix2/test/v20/test_datastore_filesystem.py +++ b/stix2/test/v20/test_datastore_filesystem.py @@ -9,6 +9,7 @@ import pytest import pytz import stix2 +from stix2.datastore import DataSourceError from stix2.datastore.filesystem import ( AuthSet, _find_search_optimizations, _get_matching_dir_entries, _timestamp2filename, @@ -420,6 +421,41 @@ def test_filesystem_sink_add_objects_list(fs_sink, fs_source): os.remove(camp7filepath) +def test_filesystem_sink_attempt_stix_file_overwrite(fs_sink, fs_source): + # add python stix object + camp8 = stix2.v20.Campaign( + name="George Washington", + objective="Create an awesome country", + aliases=["Georgey"], + ) + + fs_sink.add(camp8) + + filepath = os.path.join( + FS_PATH, "campaign", camp8.id, + _timestamp2filename(camp8.modified) + ".json", + ) + assert os.path.exists(filepath) + + camp8_r = fs_source.get(camp8.id) + assert camp8_r.id == camp8.id + assert camp8_r.name == "George Washington" + assert "Georgey" in camp8_r.aliases + + # now attempt to overwrite the same file + camp9 = stix2.v20.Campaign( + name="George Washington", + objective="Create an awesome country", + aliases=["Georgey"], + ) + + with pytest.raises(DataSourceError) as excinfo: + fs_sink.add(camp9) + assert "Attempted to overwrite file" in str(excinfo) + + os.remove(filepath) + + def test_filesystem_sink_marking(fs_sink): marking = stix2.v20.MarkingDefinition( definition_type="tlp", From 5dea09547e1d689777d391e3ed6ed0ddcea46968 Mon Sep 17 00:00:00 2001 From: "Desai, Kartikey H" Date: Fri, 11 Jan 2019 09:40:57 -0500 Subject: [PATCH 5/6] Fix test for fix to issue 232 --- stix2/test/v20/test_datastore_filesystem.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/stix2/test/v20/test_datastore_filesystem.py b/stix2/test/v20/test_datastore_filesystem.py index c8607e2..e447c73 100644 --- a/stix2/test/v20/test_datastore_filesystem.py +++ b/stix2/test/v20/test_datastore_filesystem.py @@ -443,14 +443,14 @@ def test_filesystem_sink_attempt_stix_file_overwrite(fs_sink, fs_source): assert "Georgey" in camp8_r.aliases # now attempt to overwrite the same file - camp9 = stix2.v20.Campaign( - name="George Washington", - objective="Create an awesome country", - aliases=["Georgey"], - ) + # camp9 = stix2.v20.Campaign( + # name="George Washington", + # objective="Create an awesome country", + # aliases=["Georgey"], + # ) with pytest.raises(DataSourceError) as excinfo: - fs_sink.add(camp9) + fs_sink.add(camp8) assert "Attempted to overwrite file" in str(excinfo) os.remove(filepath) From 72d7757c7b292450c604dae44a7b1c723d78b991 Mon Sep 17 00:00:00 2001 From: "Desai, Kartikey H" Date: Fri, 11 Jan 2019 10:46:16 -0500 Subject: [PATCH 6/6] Change test to use store instead of source & sink --- stix2/test/v20/test_datastore_filesystem.py | 28 ++++++++------------- 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/stix2/test/v20/test_datastore_filesystem.py b/stix2/test/v20/test_datastore_filesystem.py index e447c73..86846c4 100644 --- a/stix2/test/v20/test_datastore_filesystem.py +++ b/stix2/test/v20/test_datastore_filesystem.py @@ -421,7 +421,7 @@ def test_filesystem_sink_add_objects_list(fs_sink, fs_source): os.remove(camp7filepath) -def test_filesystem_sink_attempt_stix_file_overwrite(fs_sink, fs_source): +def test_filesystem_attempt_stix_file_overwrite(fs_store): # add python stix object camp8 = stix2.v20.Campaign( name="George Washington", @@ -429,28 +429,20 @@ def test_filesystem_sink_attempt_stix_file_overwrite(fs_sink, fs_source): aliases=["Georgey"], ) - fs_sink.add(camp8) + fs_store.add(camp8) + + camp8_r = fs_store.get(camp8.id) + assert camp8_r.id == camp8_r.id + assert camp8_r.name == camp8.name filepath = os.path.join( - FS_PATH, "campaign", camp8.id, - _timestamp2filename(camp8.modified) + ".json", + FS_PATH, "campaign", camp8_r.id, + _timestamp2filename(camp8_r.modified) + ".json", ) - assert os.path.exists(filepath) - - camp8_r = fs_source.get(camp8.id) - assert camp8_r.id == camp8.id - assert camp8_r.name == "George Washington" - assert "Georgey" in camp8_r.aliases - - # now attempt to overwrite the same file - # camp9 = stix2.v20.Campaign( - # name="George Washington", - # objective="Create an awesome country", - # aliases=["Georgey"], - # ) + # Now attempt to overwrite the existing file with pytest.raises(DataSourceError) as excinfo: - fs_sink.add(camp8) + fs_store.add(camp8) assert "Attempted to overwrite file" in str(excinfo) os.remove(filepath)