Bläddra i källkod

HUE-6137 [core] Add audit events for export and download

Jenny Kim 8 år sedan
förälder
incheckning
3529c41

+ 7 - 0
apps/filebrowser/src/filebrowser/views.py

@@ -161,6 +161,13 @@ def download(request, path):
     response["Last-Modified"] = http_date(stats['mtime'])
     response["Content-Length"] = stats['size']
     response['Content-Disposition'] = request.GET.get('disposition', 'attachment') if _can_inline_display(path) else 'attachment'
+
+    request.audit = {
+        'operation': 'DOWNLOAD',
+        'operationText': 'User %s downloaded file %s with size: %d bytes' % (request.user.username, path, stats['size']),
+        'allowed': True
+    }
+
     return response
 
 

+ 20 - 0
desktop/libs/notebook/src/notebook/api.py

@@ -649,6 +649,11 @@ def export_result(request):
       request.fs.do_as_user(request.user.username, request.fs.rmtree, destination)
     response['watch_url'] = api.export_data_as_hdfs_file(snippet, destination, overwrite)
     response['status'] = 0
+    request.audit = {
+      'operation': 'EXPORT',
+      'operationText': 'User %s exported to HDFS destination: %s' % (request.user.username, destination),
+      'allowed': True
+    }
   elif data_format == 'hive-table':
     if is_embedded:
       sql, success_url = api.export_data_as_table(notebook, snippet, destination)
@@ -668,6 +673,11 @@ def export_result(request):
       notebook_id = notebook['id'] or request.GET.get('editor', request.GET.get('notebook'))
       response['watch_url'] = reverse('notebook:execute_and_watch') + '?action=save_as_table&notebook=' + str(notebook_id) + '&snippet=0&destination=' + destination
       response['status'] = 0
+    request.audit = {
+      'operation': 'EXPORT',
+      'operationText': 'User %s exported to Hive table: %s' % (request.user.username, destination),
+      'allowed': True
+    }
   elif data_format == 'hdfs-directory':
     if is_embedded:
       sql, success_url = api.export_large_data_to_hdfs(notebook, snippet, destination)
@@ -687,6 +697,11 @@ def export_result(request):
       notebook_id = notebook['id'] or request.GET.get('editor', request.GET.get('notebook'))
       response['watch_url'] = reverse('notebook:execute_and_watch') + '?action=insert_as_query&notebook=' + str(notebook_id) + '&snippet=0&destination=' + destination
       response['status'] = 0
+    request.audit = {
+      'operation': 'EXPORT',
+      'operationText': 'User %s exported to HDFS directory: %s' % (request.user.username, destination),
+      'allowed': True
+    }
   elif data_format == 'search-index':
     if is_embedded:
       if destination == '__hue__':
@@ -724,6 +739,11 @@ def export_result(request):
       notebook_id = notebook['id'] or request.GET.get('editor', request.GET.get('notebook'))
       response['watch_url'] = reverse('notebook:execute_and_watch') + '?action=index_query&notebook=' + str(notebook_id) + '&snippet=0&destination=' + destination
       response['status'] = 0
+    request.audit = {
+      'operation': 'EXPORT',
+      'operationText': 'User %s exported to Search index: %s' % (request.user.username, destination),
+      'allowed': True
+    }
 
   return JsonResponse(response)
 

+ 10 - 1
desktop/libs/notebook/src/notebook/views.py

@@ -273,7 +273,16 @@ def download(request):
   snippet = json.loads(request.POST.get('snippet', '{}'))
   file_format = request.POST.get('format', 'csv')
 
-  return get_api(request, snippet).download(notebook, snippet, file_format)
+  response = get_api(request, snippet).download(notebook, snippet, file_format)
+
+  if response:
+    request.audit = {
+      'operation': 'DOWNLOAD',
+      'operationText': 'User %s downloaded results from %s as %s' % (request.user.username, _get_snippet_name(notebook), file_format),
+      'allowed': True
+    }
+
+  return response
 
 
 def install_examples(request):