Browse Source

[raz] Automatically set host and check env credential values

Romain Rigaux 4 years ago
parent
commit
ed0e627bd4
2 changed files with 17 additions and 4 deletions
  1. 16 3
      desktop/libs/aws/src/aws/conf.py
  2. 1 1
      desktop/libs/hadoop/src/hadoop/core_site.py

+ 16 - 3
desktop/libs/aws/src/aws/conf.py

@@ -24,7 +24,7 @@ import requests
 
 
 from desktop.lib.conf import Config, UnspecifiedConfigSection, ConfigSection, coerce_bool, coerce_password_from_script
 from desktop.lib.conf import Config, UnspecifiedConfigSection, ConfigSection, coerce_bool, coerce_password_from_script
 from desktop.lib.idbroker import conf as conf_idbroker
 from desktop.lib.idbroker import conf as conf_idbroker
-from hadoop.core_site import get_s3a_access_key, get_s3a_secret_key, get_s3a_session_token
+from hadoop.core_site import get_s3a_access_key, get_s3a_secret_key, get_s3a_session_token, get_raz_api_url, get_raz_default_endpoint
 
 
 if sys.version_info[0] > 2:
 if sys.version_info[0] > 2:
   from django.utils.translation import gettext_lazy as _, gettext as _t
   from django.utils.translation import gettext_lazy as _, gettext as _t
@@ -169,6 +169,19 @@ IS_SELF_SIGNING_ENABLED = Config(
   default=False,
   default=False,
 )
 )
 
 
+def get_default_host():
+  '''Returns the S3 host when Raz is configued'''
+
+  if get_raz_api_url():
+    endpoint = get_raz_default_endpoint()
+    if endpoint:
+      return endpoint.get('host')
+
+def get_default_get_environment_credentials():
+  '''Allow to check if environment credentials are present or not'''
+  return not get_raz_api_url()
+
+
 AWS_ACCOUNTS = UnspecifiedConfigSection(
 AWS_ACCOUNTS = UnspecifiedConfigSection(
   'aws_accounts',
   'aws_accounts',
   help=_('One entry for each AWS account'),
   help=_('One entry for each AWS account'),
@@ -208,12 +221,12 @@ AWS_ACCOUNTS = UnspecifiedConfigSection(
       ALLOW_ENVIRONMENT_CREDENTIALS=Config(
       ALLOW_ENVIRONMENT_CREDENTIALS=Config(
         help=_('Allow to use environment sources of credentials (environment variables, EC2 profile).'),
         help=_('Allow to use environment sources of credentials (environment variables, EC2 profile).'),
         key='allow_environment_credentials',
         key='allow_environment_credentials',
-        default=True,
+        dynamic_default=get_default_get_environment_credentials(),
         type=coerce_bool
         type=coerce_bool
       ),
       ),
       REGION=Config(
       REGION=Config(
         key='region',
         key='region',
-        default=None,
+        dynamic_default=get_default_host,
         type=str
         type=str
       ),
       ),
       HOST=Config(
       HOST=Config(

+ 1 - 1
desktop/libs/hadoop/src/hadoop/core_site.py

@@ -144,7 +144,7 @@ def get_raz_default_endpoint():
     match = re.search(_CNF_S3A_RAZ_BUCKET_ENDPOINT, key)
     match = re.search(_CNF_S3A_RAZ_BUCKET_ENDPOINT, key)
     if match:
     if match:
       return {
       return {
-        'region': val,
+        'host': val,
         'bucket': match.group('bucket')
         'bucket': match.group('bucket')
       }
       }