Browse Source

HUE-5465 [fb] Autoconfigure to enable S3 browser if IAM role being detected

Jenny Kim 9 years ago
parent
commit
2600bdc

+ 3 - 2
desktop/libs/aws/src/aws/__init__.py

@@ -15,8 +15,6 @@
 # limitations under the License.
 from __future__ import absolute_import
 
-import aws.s3
-
 from aws import conf
 from aws.client import Client
 from aws.s3.s3fs import S3FileSystem
@@ -39,6 +37,9 @@ def _init_clients():
   CLIENT_CACHE = {}
   for identifier in conf.AWS_ACCOUNTS.keys():
     CLIENT_CACHE[identifier] = _make_client(identifier)
+  # If default configuration not initialized, initialize client connection with IAM metadata
+  if 'default' not in CLIENT_CACHE and conf.has_iam_metadata():
+    CLIENT_CACHE['default'] = Client()
 
 
 def _make_client(identifier):

+ 17 - 12
desktop/libs/aws/src/aws/client.py

@@ -20,7 +20,7 @@ import boto.s3
 import boto.s3.connection
 import boto.utils
 
-from aws.conf import get_default_region, has_iam_metadata
+from aws.conf import get_default_region, has_iam_metadata, DEFAULT_CALLING_FORMAT
 
 
 HTTP_SOCKET_TIMEOUT_S = 60
@@ -37,7 +37,7 @@ class Client(object):
     self._timeout = timeout
     self._proxy_address = proxy_address
     self._proxy_port = proxy_port
-    self._calling_format = conf.DEFAULT_CALLING_FORMAT if calling_format is None else calling_format
+    self._calling_format = DEFAULT_CALLING_FORMAT if calling_format is None else calling_format
     self._is_secure = is_secure
 
     boto.config.add_section('Boto')
@@ -68,20 +68,25 @@ class Client(object):
     # First attempt to connect via specified credentials
     if self._proxy_address is not None and self._proxy_port is not None:
       connection = boto.s3.connection.S3Connection(aws_access_key_id=self._access_key_id,
-                                        aws_secret_access_key=self._secret_access_key,
-                                        security_token=self._security_token,
-                                        is_secure=self._is_secure,
-                                        calling_format=self._calling_format,
-                                        proxy=self._proxy_address, proxy_port=self._proxy_port)
-    else:
+        aws_secret_access_key=self._secret_access_key,
+        security_token=self._security_token,
+        is_secure=self._is_secure,
+        calling_format=self._calling_format,
+        proxy=self._proxy_address,
+        proxy_port=self._proxy_port)
+    elif self._region:
       connection = boto.s3.connect_to_region(self._region,
-                                             aws_access_key_id=self._access_key_id,
-                                             aws_secret_access_key=self._secret_access_key,
-                                             security_token=self._security_token)
+        aws_access_key_id=self._access_key_id,
+        aws_secret_access_key=self._secret_access_key,
+        security_token=self._security_token)
+    else:
+      connection = boto.s3.connection.S3Connection(aws_access_key_id=self._access_key_id,
+        aws_secret_access_key=self._secret_access_key,
+        security_token=self._security_token)
 
     if connection is None:
       # If no connection, attemt to fallback to IAM instance metadata
-      connection = boto.s3.connect_to_region(self._region)
+      connection = boto.connect_s3()
 
       if connection is None:
         raise ValueError('Can not construct S3 Connection for region %s' % self._region)

+ 4 - 2
desktop/libs/aws/src/aws/conf.py

@@ -25,6 +25,9 @@ from desktop.lib.conf import Config, UnspecifiedConfigSection, ConfigSection, co
 from hadoop.core_site import get_s3a_access_key, get_s3a_secret_key
 
 
+DEFAULT_CALLING_FORMAT='boto.s3.connection.S3Connection.DefaultCallingFormat'
+
+
 def get_default_access_key_id():
   """
   Attempt to set AWS access key ID from script, else core-site, else None
@@ -42,9 +45,8 @@ def get_default_secret_key():
 
 
 def get_default_region():
-  return AWS_ACCOUNTS['default'].REGION.get()
+  return AWS_ACCOUNTS['default'].REGION.get() if 'default' in AWS_ACCOUNTS else 'us-east-1'
 
-DEFAULT_CALLING_FORMAT='boto.s3.connection.S3Connection.DefaultCallingFormat'
 
 AWS_ACCOUNTS = UnspecifiedConfigSection(
   'aws_accounts',