Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if public:
searchindex.grant_permission(permission='read', user=None)
# Create the index in Elasticsearch
self.datastore.create_index(
index_name=es_index_name, doc_type='generic_event')
db_session.add(searchindex)
db_session.commit()
status_code = HTTP_STATUS_CODE_CREATED
return self.to_json(
searchindex, meta=metadata, status_code=status_code)
class SearchIndexResource(ResourceMixin, Resource):
"""Resource to get search index."""
@login_required
def get(self, searchindex_id):
"""Handles GET request to the resource.
Returns:
Search index in JSON (instance of flask.wrappers.Response)
"""
searchindex = SearchIndex.query.get_with_acl(searchindex_id)
return self.to_json(searchindex)
class UserListResource(ResourceMixin, Resource):
"""Resource to get list of users."""
sketch_id: Integer primary key for a sketch database model
Returns:
A view in JSON (instance of flask.wrappers.Response)
"""
form = SaveViewForm.build(request)
if not form.validate_on_submit():
abort(
HTTP_STATUS_CODE_BAD_REQUEST,
'Unable to save view, not able to validate form data.')
sketch = Sketch.query.get_with_acl(sketch_id)
view = self.create_view_from_form(sketch, form)
return self.to_json(view, status_code=HTTP_STATUS_CODE_CREATED)
class ViewResource(ResourceMixin, Resource):
"""Resource to get a view."""
@login_required
def get(self, sketch_id, view_id):
"""Handles GET request to the resource.
Args:
sketch_id: Integer primary key for a sketch database model
view_id: Integer primary key for a view database model
Returns:
A view in JSON (instance of flask.wrappers.Response)
"""
sketch = Sketch.query.get_with_acl(sketch_id)
view = View.query.get(view_id)
if not form.validate_on_submit():
abort(
HTTP_STATUS_CODE_BAD_REQUEST, 'Unable to validate form data.')
title = ''
if form.title.data:
title = form.title.data
sketch = Sketch.query.get_with_acl(sketch_id)
story = Story(
title=title, content='', sketch=sketch, user=current_user)
db_session.add(story)
db_session.commit()
return self.to_json(story, status_code=HTTP_STATUS_CODE_CREATED)
class StoryResource(ResourceMixin, Resource):
"""Resource to get a story."""
@login_required
def get(self, sketch_id, story_id):
"""Handles GET request to the resource.
Args:
sketch_id: Integer primary key for a sketch database model
story_id: Integer primary key for a story database model
Returns:
A story in JSON (instance of flask.wrappers.Response)
"""
sketch = Sketch.query.get_with_acl(sketch_id)
story = Story.query.get(story_id)
else:
abort(
HTTP_STATUS_CODE_BAD_REQUEST,
'Annotation type needs to be either label or comment, '
'not {0!s}'.format(annotation_type))
annotations.append(annotation)
# Save the event to the database
db_session.add(event)
db_session.commit()
return self.to_json(
annotations, status_code=HTTP_STATUS_CODE_CREATED)
class UploadFileResource(ResourceMixin, Resource):
"""Resource that processes uploaded files."""
@login_required
def post(self):
"""Handles POST request to the resource.
Returns:
A view in JSON (instance of flask.wrappers.Response)
"""
upload_enabled = current_app.config['UPLOAD_ENABLED']
if not upload_enabled:
abort(HTTP_STATUS_CODE_BAD_REQUEST, 'Upload not enabled')
upload_folder = current_app.config['UPLOAD_FOLDER']
form = UploadFileForm()
sketch_id: Integer primary key for a sketch database model
Returns:
An aggregation in JSON (instance of flask.wrappers.Response)
"""
form = SaveAggregationForm.build(request)
if not form.validate_on_submit():
abort(
HTTP_STATUS_CODE_BAD_REQUEST, 'Unable to verify form data.')
sketch = Sketch.query.get_with_acl(sketch_id)
aggregation = self.create_aggregation_from_form(sketch, form)
return self.to_json(aggregation, status_code=HTTP_STATUS_CODE_CREATED)
class AggregationLegacyResource(ResourceMixin, Resource):
"""Resource to query for the legacy aggregated results."""
@login_required
def post(self, sketch_id):
"""Handles POST request to the resource.
Handler for /api/v1/sketches/:sketch_id/aggregation/legacy
Args:
sketch_id: Integer primary key for a sketch database model
Returns:
JSON with aggregation results
"""
sketch = Sketch.query.get_with_acl(sketch_id)
form = AggregationLegacyForm.build(request)
@login_required
def get(self, sketch_id):
"""Only for debugging."""
return jsonify(win_logins(sketch_id))
class WinServicesResource(Resource):
@login_required
def get(self, sketch_id):
"""Only for debugging."""
return jsonify(win_services(sketch_id))
class CreateGraphResource(ResourceMixin, Resource):
@login_required
def post(self, sketch_id):
"""For given sketch, create a lateral movement graph from Elasticsearch
events and save it to Neo4j. Nodes and edges have sketch_id property.
"""
# Maximun number of timestamps to add to edges.
max_timestamps = 1000
result = []
params = {
u'sketch_id': sketch_id,
u'max_timestamps': max_timestamps,
u'win_logins': win_logins(sketch_id),
u'win_services': win_services(sketch_id),
}
class UserListResource(ResourceMixin, Resource):
"""Resource to get list of users."""
@login_required
def get(self):
"""Handles GET request to the resource.
Returns:
List of usernames
"""
return self.to_json(User.query.all())
class GroupListResource(ResourceMixin, Resource):
"""Resource to get list of groups."""
@login_required
def get(self):
"""Handles GET request to the resource.
Returns:
List of group names
"""
return self.to_json(Group.query.all())
class CollaboratorResource(ResourceMixin, Resource):
"""Resource to update sketch collaborators."""
@login_required
pipeline = tasks.build_index_pipeline(
file_path, timeline_name, index_name, file_extension, sketch_id,
only_index=enable_stream)
pipeline.apply_async()
# Return Timeline if it was created.
# pylint: disable=no-else-return
if timeline:
return self.to_json(
timeline, status_code=HTTP_STATUS_CODE_CREATED)
return self.to_json(
searchindex, status_code=HTTP_STATUS_CODE_CREATED)
class TaskResource(ResourceMixin, Resource):
"""Resource to get information on celery task."""
def __init__(self):
super(TaskResource, self).__init__()
from timesketch import create_celery_app
self.celery = create_celery_app()
@login_required
def get(self):
"""Handles GET request to the resource.
Returns:
A view in JSON (instance of flask.wrappers.Response)
"""
TIMEOUT_THRESHOLD_SECONDS = current_app.config.get(
'CELERY_TASK_TIMEOUT', 7200)
for timeline in sketch.timelines:
tl_colors[timeline.searchindex.index_name] = timeline.color
tl_names[timeline.searchindex.index_name] = timeline.name
meta = {
'es_time': result['took'],
'es_total_count': result['hits']['total'],
'timeline_colors': tl_colors,
'timeline_names': tl_names,
'scroll_id': result.get('_scroll_id', ''),
}
schema = {'meta': meta, 'objects': result['hits']['hits']}
return jsonify(schema)
class AggregationResource(ResourceMixin, Resource):
"""Resource to query for aggregated results."""
@login_required
def get(self, sketch_id, aggregation_id): # pylint: disable=unused-argument
"""Handles GET request to the resource.
Handler for /api/v1/sketches/:sketch_id/aggregation/:aggregation_id
Args:
sketch_id: Integer primary key for a sketch database model
aggregation_id: Integer primary key for an agregation database model
Returns:
JSON with aggregation results
"""
sketch = Sketch.query.get_with_acl(sketch_id)
state=celery_task.state,
successful=celery_task.successful(),
name=search_index.name,
result=False)
if celery_task.state == 'SUCCESS':
task['result'] = celery_task.result
elif celery_task.state == 'PENDING':
time_pending = (
search_index.updated_at - datetime.datetime.now())
if time_pending.seconds > TIMEOUT_THRESHOLD_SECONDS:
search_index.set_status('timeout')
schema['objects'].append(task)
return jsonify(schema)
class StoryListResource(ResourceMixin, Resource):
"""Resource to get all stories for a sketch or to create a new story."""
@login_required
def get(self, sketch_id):
"""Handles GET request to the resource.
Args:
sketch_id: Integer primary key for a sketch database model
Returns:
Stories in JSON (instance of flask.wrappers.Response)
"""
sketch = Sketch.query.get_with_acl(sketch_id)
stories = []
for story in Story.query.filter_by(
sketch=sketch).order_by(desc(Story.created_at)):