Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for Athena error handling, error log printing in tranlsation #1415

Merged
merged 2 commits into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ async def column_list(self, table):
for row in results:
columns.append(row['Data'][0]['VarCharValue'])
else:
raise Exception("Error in getting Athena table column list")
raise InvalidParameterException("Error in getting Athena table column list")

s3_output_bucket_with_file = s3_output_location.split('//')[1]
s3_output_bucket = s3_output_bucket_with_file.split('/')[0]
Expand All @@ -127,6 +127,9 @@ async def column_list(self, table):
if delete_object.get('Errors'):
message = delete_object.get('Errors')[0].get('Message')
raise Exception("Error in deleting s3 metadata after Athena query: " + message)

if not columns:
raise InvalidParameterException('No Athena table with name ' + table)

return columns

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,41 @@

# convert JSON data to STIX object using map_data and transformers
def convert_to_stix(data_source, map_data, data, transformers, options, callback=None):
try:
ds2stix = DataSourceObjToStixObj(data_source, map_data, transformers, options, callback)

# map data list to list of transformed objects
observation = ds2stix.transform
results = list(map(observation, data))

for stix_object in results:
if ds2stix.spec_version == "2.1":
del stix_object["objects"]
ds2stix.bundle["objects"].append(stix_object)

for _, value in ds2stix.unique_cybox_objects.items():
ds2stix.bundle["objects"].append(value)

if options.get('stix_validator'):
if ds2stix.spec_version == "2.1":
# Serialize and Deserialize bundle to covert StixObjectIds to strings
bundle_obj = json.dumps(ds2stix.bundle, sort_keys=False)
bundle_obj = json.loads(bundle_obj)
else:
bundle_obj = ds2stix.bundle
validated_result = validate_instance(bundle_obj, ValidationOptions(version=ds2stix.spec_version))
print_results(validated_result)

ds2stix = DataSourceObjToStixObj(data_source, map_data, transformers, options, callback)

# map data list to list of transformed objects
observation = ds2stix.transform
results = list(map(observation, data))

for stix_object in results:
if ds2stix.spec_version == "2.1":
del stix_object["objects"]
ds2stix.bundle["objects"].append(stix_object)

for _, value in ds2stix.unique_cybox_objects.items():
ds2stix.bundle["objects"].append(value)

if options.get('stix_validator'):
if ds2stix.spec_version == "2.1":
# Serialize and Deserialize bundle to covert StixObjectIds to strings
bundle_obj = json.dumps(ds2stix.bundle, sort_keys=False)
bundle_obj = json.loads(bundle_obj)
else:
bundle_obj = ds2stix.bundle
validated_result = validate_instance(bundle_obj, ValidationOptions(version=ds2stix.spec_version))
print_results(validated_result)

return ds2stix.bundle
return ds2stix.bundle

except Exception as e:
try:
# try to print the error line
logger_log = logger.set_logger(__name__)
logger_log.error(logger.last_tb_to_string(e))
except:
pass
raise e

class DataSourceObjToStixObj:
logger = logger.set_logger(__name__)
Expand Down
5 changes: 5 additions & 0 deletions stix_shifter_utils/utils/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,8 @@ def exception_to_string(excp):
stack = traceback.extract_stack()[:-3] + traceback.extract_tb(excp.__traceback__)
pretty = traceback.format_list(stack)
return ''.join(pretty) + '\n {} {}'.format(excp.__class__, excp)

def last_tb_to_string(excp):
stack_summary = traceback.extract_tb(excp.__traceback__)
last_frame = stack_summary[-1]
return '{}:{} {} {}'.format(last_frame.filename, last_frame.lineno, excp.__class__, excp)