Selaa lähdekoodia

[raz] Remove checks for disabling S3 browser (#2548)

Harsh Gupta 4 vuotta sitten
vanhempi
commit
af2a50a212

+ 1 - 1
desktop/libs/aws/src/aws/client.py

@@ -45,7 +45,7 @@ def get_credential_provider(identifier, user):
 def _make_client(identifier, user):
   client_conf = aws_conf.AWS_ACCOUNTS[identifier] if identifier in aws_conf.AWS_ACCOUNTS else None
 
-  if RAZ.IS_ENABLED.get() and bool(get_raz_s3_default_bucket()) and not aws_conf.IS_SELF_SIGNING_ENABLED.get():
+  if RAZ.IS_ENABLED.get() and not aws_conf.IS_SELF_SIGNING_ENABLED.get():
     host = aws_conf.get_default_host() or client_conf.HOST.get()
     s3_client = RazS3Connection(username=user, host=host)  # Note: Remaining AWS configuration is fully skipped
     s3_client_expiration = None

+ 8 - 7
desktop/libs/aws/src/aws/conf.py

@@ -288,10 +288,7 @@ def is_enabled():
       has_iam_metadata() or \
       conf_idbroker.is_idbroker_enabled('s3a') or \
       IS_SELF_SIGNING_ENABLED.get() or \
-      (
-        RAZ.IS_ENABLED.get() and bool(get_raz_s3_default_bucket()) and \
-        'default' in list(AWS_ACCOUNTS.keys()) and AWS_ACCOUNTS['default'].get_raw()
-      )
+      (RAZ.IS_ENABLED.get() and 'default' in list(AWS_ACCOUNTS.keys()) and AWS_ACCOUNTS['default'].get_raw())
 
 
 def is_ec2_instance():
@@ -352,9 +349,13 @@ def has_s3_access(user):
   from desktop.conf import RAZ  # Must be imported dynamically in order to have proper value
 
   return user.is_authenticated and user.is_active and (
-    is_admin(user) or user.has_hue_permission(action="s3_access", app="filebrowser") or \
-    (RAZ.IS_ENABLED.get() and bool(get_raz_s3_default_bucket()))
-  )
+    is_admin(user) or user.has_hue_permission(action="s3_access", app="filebrowser") or RAZ.IS_ENABLED.get())
+
+
+def has_raz_s3():
+  from desktop.conf import RAZ  # Must be imported dynamically in order to have proper value
+
+  return (RAZ.IS_ENABLED.get() and bool(get_raz_s3_default_bucket()))
 
 
 def config_validator(user):

+ 74 - 0
desktop/libs/aws/src/aws/conf_tests.py

@@ -0,0 +1,74 @@
+# Licensed to Cloudera, Inc. under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  Cloudera, Inc. licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+from __future__ import absolute_import
+
+import logging
+import sys
+import unittest
+
+from nose.tools import assert_equal, assert_true, assert_not_equal, assert_false
+
+from aws import conf
+
+from desktop.conf import RAZ
+from desktop.lib.django_test_util import make_logged_in_client
+
+from useradmin.models import User
+
+if sys.version_info[0] > 2:
+  from unittest.mock import patch
+else:
+  from mock import patch
+
+LOG = logging.getLogger(__name__)
+
+class TestAWSConf(unittest.TestCase):
+  def setUp(self):
+    self.client = make_logged_in_client(username="test_user", groupname="default", recreate=True, is_superuser=False)
+    self.user = User.objects.get(username="test_user")
+
+  def test_is_enabled_when_raz_enabled(self):
+
+    # When RAZ is not enabled
+    assert_false(conf.is_enabled())
+
+    resets = [
+      RAZ.IS_ENABLED.set_for_testing(True),
+      conf.AWS_ACCOUNTS.set_for_testing({'default': {
+        'region': 'us-west-2',
+        'host': 's3-us-west-2.amazonaws.com',
+        'allow_environment_credentials': 'false'
+      }})
+    ]
+
+    try:
+      assert_true(conf.is_enabled())
+    finally:
+      for reset in resets:
+        reset()
+      conf.clear_cache()
+  
+  def test_has_s3_access_when_raz_enabled(self):
+
+    # When RAZ is not enabled
+    assert_false(conf.has_s3_access(self.user))
+
+    reset = RAZ.IS_ENABLED.set_for_testing(True)
+    try:
+      assert_true(conf.has_s3_access(self.user))
+    finally:
+      reset()
+      conf.clear_cache()

+ 23 - 2
desktop/libs/aws/src/aws/tests.py

@@ -26,6 +26,7 @@ from aws.client import Client, get_credential_provider
 
 from desktop.lib.fsmanager import get_client, clear_cache
 from desktop.lib.python_util import current_ms_from_utc
+from desktop.conf import RAZ
 
 if sys.version_info[0] > 2:
   from unittest.mock import patch
@@ -38,7 +39,7 @@ LOG = logging.getLogger(__name__)
 class TestAWS(unittest.TestCase):
   def test_with_credentials(self):
     try:
-      finish = conf.AWS_ACCOUNTS.set_for_testing({'default': {'access_key_id':'access_key_id', 'secret_access_key': 'secret_access_key'}})
+      finish = conf.AWS_ACCOUNTS.set_for_testing({'default': {'access_key_id': 'access_key_id', 'secret_access_key': 'secret_access_key'}})
       with patch('aws.client.conf_idbroker.get_conf') as get_conf:
         with patch('aws.client.Client.get_s3_connection'):
           get_conf.return_value = {}
@@ -131,4 +132,24 @@ class TestAWS(unittest.TestCase):
     finally:
       finish()
       clear_cache()
-      conf.clear_cache()
+      conf.clear_cache()
+
+  def test_with_raz_enabled(self):
+    with patch('aws.client.RazS3Connection') as raz_s3_connection:
+      resets = [
+        RAZ.IS_ENABLED.set_for_testing(True),
+        conf.AWS_ACCOUNTS.set_for_testing({'default': {
+          'region': 'us-west-2',
+          'host': 's3-us-west-2.amazonaws.com',
+          'allow_environment_credentials': 'false'
+        }})
+      ]
+
+      try:
+        client = get_client(name='default', fs='s3a', user='hue')
+        assert_true(client)
+      finally:
+        for reset in resets:
+          reset()
+        clear_cache()
+        conf.clear_cache()