Pārlūkot izejas kodu

HUE-8942 [editor] Opening query when editor is not loaded yet can throw 403

Romain 6 gadi atpakaļ
vecāks
revīzija
3298e3ea0c

+ 11 - 4
desktop/libs/notebook/src/notebook/decorators.py

@@ -25,31 +25,38 @@ from django.http import Http404
 from django.utils.functional import wraps
 from django.utils.translation import ugettext as _
 
+from dashboard.models import extract_solr_exception_message
 from desktop.lib.django_util import JsonResponse
 from desktop.lib.exceptions_renderable import PopupException
 from desktop.lib.i18n import smart_unicode
 from desktop.lib.rest.http_client import RestException
 from desktop.models import Document2, Document, FilesystemException
-from dashboard.models import extract_solr_exception_message
 
 from notebook.conf import check_permissions
 from notebook.connectors.base import QueryExpired, QueryError, SessionExpired, AuthenticationRequired, OperationTimeout,\
   OperationNotSupported
+from notebook.models import _get_editor_type
 
 
 LOG = logging.getLogger(__name__)
 
+
 def check_editor_access_permission():
   def inner(view_func):
     def decorate(request, *args, **kwargs):
-      editor_id = request.GET.get('type', 'hive')
+      editor_id = request.GET.get('editor')
+      editor_type = request.GET.get('type', 'hive')
 
-      if check_permissions(request.user, editor_id):
-        raise PopupException(_('Missing permission to access the %s Editor' % editor_id), error_code=401)
+      if editor_id:  # Open existing saved editor document
+        editor_type = _get_editor_type(editor_id)
+
+      if check_permissions(request.user, editor_type):
+        raise PopupException(_('Missing permission to access the %s Editor' % editor_type), error_code=401)
       return view_func(request, *args, **kwargs)
     return wraps(view_func)(decorate)
   return inner
 
+
 def check_document_access_permission():
   def inner(view_func):
     def decorate(request, *args, **kwargs):

+ 5 - 0
desktop/libs/notebook/src/notebook/models.py

@@ -476,6 +476,11 @@ def _update_property_value(properties, key, value):
       prop.update({'value': value})
 
 
+def _get_editor_type(editor_id):
+  document = Document2.objects.get(id=editor_id)
+  return document.type.rsplit('-', 1)[-1]
+
+
 class Analytics():
 
   @classmethod

+ 4 - 3
desktop/libs/notebook/src/notebook/templates/editor_components.mako

@@ -17,15 +17,16 @@
 <%!
 from django.utils.translation import ugettext as _
 
+from webpack_loader.templatetags.webpack_loader import render_bundle
+
 from desktop import conf
+from desktop.auth.backend import is_admin
 from desktop.lib.i18n import smart_unicode
 from desktop.views import _ko, antixss
-
 from desktop.conf import IS_EMBEDDED
 from metadata.conf import has_optimizer, OPTIMIZER
+
 from notebook.conf import ENABLE_QUERY_BUILDER, ENABLE_QUERY_SCHEDULING, ENABLE_BATCH_EXECUTE, ENABLE_EXTERNAL_STATEMENT, ENABLE_PRESENTATION
-from desktop.auth.backend import is_admin
-from webpack_loader.templatetags.webpack_loader import render_bundle
 %>
 
 <%def name="includes(is_embeddable=False, suffix='')">

+ 21 - 1
desktop/libs/notebook/src/notebook/tests.py

@@ -19,7 +19,7 @@
 import json
 
 from collections import OrderedDict
-
+from mock import patch, Mock, MagicMock
 from nose.plugins.attrib import attr
 from nose.tools import assert_equal, assert_true, assert_false
 
@@ -548,3 +548,23 @@ class TestAnalytics():
       Analytics.query_stats(query=doc)
     finally:
       doc.delete()
+
+
+class TestEditor(object):
+
+  def setUp(self):
+    self.client = make_logged_in_client(username="test", groupname="empty", recreate=True, is_superuser=False)
+
+    self.user = User.objects.get(username="test")
+
+    grant_access("test", "empty", "impala")
+
+  def test_open_saved_impala_query_when_no_hive_interepreter(self):
+    try:
+      doc, created = Document2.objects.get_or_create(name='open_saved_query_with_hive_not_present', type='query-impala', owner=self.user, data={})
+
+      with patch('desktop.middleware.fsmanager') as fsmanager:
+        response = self.client.get(reverse('notebook:editor'), {'editor': doc.id, 'is_embeddable': True})
+        assert_equal(200, response.status_code)
+    finally:
+      doc.delete()

+ 2 - 3
desktop/libs/notebook/src/notebook/views.py

@@ -40,7 +40,7 @@ from notebook.connectors.base import Notebook, get_api as _get_api, _get_snippet
 from notebook.connectors.spark_shell import SparkApi
 from notebook.decorators import check_editor_access_permission, check_document_access_permission, check_document_modify_permission
 from notebook.management.commands.notebook_setup import Command
-from notebook.models import make_notebook
+from notebook.models import make_notebook, _get_editor_type
 
 
 LOG = logging.getLogger(__name__)
@@ -129,8 +129,7 @@ def editor(request, is_mobile=False, is_embeddable=False):
     return notebook(request)
 
   if editor_id:  # Open existing saved editor document
-    document = Document2.objects.get(id=editor_id)
-    editor_type = document.type.rsplit('-', 1)[-1]
+    editor_type = _get_editor_type(editor_id)
 
   template = 'editor.mako'
   if is_mobile: