浏览代码

HUE-6335 [optimizer] Unify hue.ini settings with auth_key

Romain Rigaux 8 年之前
父节点
当前提交
360c0bc

+ 12 - 15
desktop/conf.dist/hue.ini

@@ -1626,38 +1626,35 @@
 [metadata]
 
   [[optimizer]]
-    # Cache timeout in milliseconds for the Optimizer metadata used in assist, autocomplete, etc.
-    # defaults to 432000000 (5 days), set to 0 to disable caching
-    # cacheable_ttl=432000000
-
     # Base URL to Optimizer API.
     ## api_url=https://optimizer.cloudera.com
 
-    # Aka workload or tenant id, keep commented and it will be guessed from the email.
-    ## product_name=e0819f3a-1e6f-4904-be69-5b704bacd1244
+    # The name of the key of the service.
+    ## auth_key=e0819f3a-1e6f-4904-be69-5b704bacd1245
 
-    # A secret passphrase associated with the account.
-    ## product_secret=e0819f3a-1e6f-4904-be69-5b704bacd1245
+    # The private part of the key associated with the auth_key.
+    ## auth_key_secret='-----BEGIN PRIVATE KEY....'
 
-    # A secret key passphrase associated with the account.
-    ## product_auth_secret='-----BEGIN PRIVATE KEY....'
+    # Execute this script to produce the auth_key secret. This will be used when `auth_key_secret` is not set.
+    ## auth_key_secret_script=/path/to/script.sh
 
-    # The email of the Optimizer account you want to associate with the Product.
-    ## email=hue@gethue.com
+    # The name of the workload where queries are uploaded and optimizations are calculated from. Automatically guessed from auth_key and cluster_id if not specified.
+    ## tenant_id=
 
     # Perform Sentry privilege filtering.
     # Default to true automatically if the cluster is secure.
     ## apply_sentry_permissions=False
 
+    # Cache timeout in milliseconds for the Optimizer metadata used in assist, autocomplete, etc.
+    # defaults to 432000000 (5 days), set to 0 to disable caching.
+    # cacheable_ttl=432000000
+
     # Automatically upload queries after their execution in order to improve recommendations.
     ## auto_upload_queries=true
 
     # Allow admins to upload the last N executed queries in the quick start wizard. Use 0 to disable.
     ## query_history_upload_limit=10000
 
-    # In secure mode (HTTPS), if Optimizer SSL certificates have to be verified against certificate authority.
-    ## ssl_cert_ca_verify=True
-
   [[navigator]]
     # Navigator API URL (without version suffix).
     ## api_url=http://localhost:7187/api

+ 12 - 15
desktop/conf/pseudo-distributed.ini.tmpl

@@ -1630,38 +1630,35 @@
 [metadata]
 
   [[optimizer]]
-    # Cache timeout in milliseconds for the Optimizer metadata used in assist, autocomplete, etc.
-    # defaults to 432000000 (5 days), set to 0 to disable caching
-    # cacheable_ttl=432000000
-
     # Base URL to Optimizer API.
     ## api_url=https://optimizer.cloudera.com
 
-    # Aka workload or tenant id, keep commented and it will be guessed from the email.
-    ## product_name=e0819f3a-1e6f-4904-be69-5b704bacd1244
+    # The name of the key of the service.
+    ## auth_key=e0819f3a-1e6f-4904-be69-5b704bacd1245
 
-    # A secret passphrase associated with the account.
-    ## product_secret=e0819f3a-1e6f-4904-be69-5b704bacd1245
+    # The private part of the key associated with the auth_key.
+    ## auth_key_secret='-----BEGIN PRIVATE KEY....'
 
-    # A secret key passphrase associated with the account.
-    ## product_auth_secret='-----BEGIN PRIVATE KEY....'
+    # Execute this script to produce the auth_key secret. This will be used when `auth_key_secret` is not set.
+    ## auth_key_secret_script=/path/to/script.sh
 
-    # The email of the Optimizer account you want to associate with the Product.
-    ## email=hue@gethue.com
+    # The name of the workload where queries are uploaded and optimizations are calculated from. Automatically guessed from auth_key and cluster_id if not specified.
+    ## tenant_id=
 
     # Perform Sentry privilege filtering.
     # Default to true automatically if the cluster is secure.
     ## apply_sentry_permissions=False
 
+    # Cache timeout in milliseconds for the Optimizer metadata used in assist, autocomplete, etc.
+    # defaults to 432000000 (5 days), set to 0 to disable caching.
+    # cacheable_ttl=432000000
+
     # Automatically upload queries after their execution in order to improve recommendations.
     ## auto_upload_queries=true
 
     # Allow admins to upload the last N executed queries in the quick start wizard. Use 0 to disable.
     ## query_history_upload_limit=10000
 
-    # In secure mode (HTTPS), if Optimizer SSL certificates have to be verified against certificate authority.
-    ## ssl_cert_ca_verify=True
-
   [[navigator]]
     # Navigator API URL (without version suffix).
     ## api_url=http://localhost:7187/api

+ 1 - 1
desktop/core/src/desktop/conf.py

@@ -421,7 +421,7 @@ APP_BLACKLIST = Config(
 
 CLUSTER_ID = Config(
   key="cluster_id",
-  help=_("Id of the cluster here Hue is located."),
+  help=_("Id of the cluster where Hue is located."),
   private=False,
   default='default')
 

+ 27 - 48
desktop/libs/metadata/src/metadata/conf.py

@@ -19,7 +19,7 @@ import logging
 
 from django.utils.translation import ugettext_lazy as _t
 
-from desktop.conf import AUTH_USERNAME as DEFAULT_AUTH_USERNAME, default_ssl_validate
+from desktop.conf import AUTH_USERNAME as DEFAULT_AUTH_USERNAME, CLUSTER_ID as DEFAULT_CLUSTER_ID
 from desktop.lib.conf import Config, ConfigSection, coerce_bool, coerce_password_from_script
 from desktop.lib.paths import get_config_root
 
@@ -72,43 +72,41 @@ OPTIMIZER = ConfigSection(
   key='optimizer',
   help=_t("""Configuration options for Optimizer API"""),
   members=dict(
-    CACHEABLE_TTL=Config(
-      key='cacheable_ttl',
-      type=int,
-      help=_t('The cache TTL in milliseconds for the assist/autocomplete/etc calls. Set to 0 to disable the cache.'),
-      default=432000000),
-
     API_URL=Config(
       key='api_url',
       help=_t('Base URL to Optimizer API (e.g. - https://alpha.optimizer.cloudera.com/)'),
       default=None),
 
-    PRODUCT_NAME=Config(
-      key="product_name",
-      help=_t("The name of the product or group which will have API access to the emails associated with it."),
-      private=True,
+    AUTH_KEY=Config(
+      key="auth_key",
+      help=_t("The name of the key of the service."),
+      private=False,
       default=None),
-    PRODUCT_SECRET=Config(
-      key="product_secret",
-      help=_t("A secret passphrase associated with the productName."),
+    AUTH_KEY_SECRET=Config(
+      key="auth_key_secret",
+      help=_t("The private part of the key associated with the auth_key."),
       private=True,
       default=None),
-    PRODUCT_SECRET_SCRIPT=Config(
-      key="product_secret_script",
-      help=_t("Execute this script to produce the product secret. This will be used when `product_secret` is not set."),
+    AUTH_KEY_SECRET_SCRIPT=Config(
+      key="auth_key_secret_script",
+      help=_t("Execute this script to produce the auth_key secret. This will be used when `auth_key_secret` is not set."),
       private=True,
       type=coerce_password_from_script,
       default=None),
-    PRODUCT_AUTH_SECRET=Config(
-      key="product_auth_secret",
-      help=_t("A secret passphrase associated with the productName."),
+    TENANT_ID=Config(
+      key="tenant_id",
+      help=_t("The name of the workload where queries are uploaded and optimizations are calculated from. Automatically guessed from auth_key and cluster_id if not specified."),
       private=True,
       default=None),
-    PRODUCT_AUTH_SECRET_SCRIPT=Config(
-      key="product_auth_secret_script",
-      help=_t("Execute this script to produce the product secret. This will be used when `product_secret` is not set."),
+    CLUSTER_ID=Config(
+      key="cluster_id",
+      help=_t("The name of the cluster used to determine the tenant id when this one is not specified. Defaults to the cluster Id or 'default'."),
+      private=True,
+      default=DEFAULT_CLUSTER_ID.get()),
+    EMAIL=Config(
+      key="email",
+      help=_t("The email of the Optimizer account to use (deprecated)."),
       private=True,
-      type=coerce_password_from_script,
       default=None),
 
     APPLY_SENTRY_PERMISSIONS = Config(
@@ -117,6 +115,11 @@ OPTIMIZER = ConfigSection(
       dynamic_default=get_security_default,
       type=coerce_bool
     ),
+    CACHEABLE_TTL=Config(
+      key='cacheable_ttl',
+      type=int,
+      help=_t('The cache TTL in milliseconds for the assist/autocomplete/etc calls. Set to 0 to disable the cache.'),
+      default=432000000),
     AUTO_UPLOAD_QUERIES = Config(
       key="auto_upload_queries",
       help=_t("Automatically upload queries after their execution in order to improve recommendations."),
@@ -129,30 +132,6 @@ OPTIMIZER = ConfigSection(
       default=10000,
       type=int
     ),
-
-    EMAIL=Config(
-      key="email",
-      help=_t("The email of the Optimizer account you want to associate with the Product."),
-      private=True,
-      dynamic_default=get_auth_username),
-    EMAIL_PASSWORD=Config(
-      key="email_password",
-      help=_t("The password associated with the Optimizer account you to associate with the Product."),
-      private=True,
-      default=None),
-    EMAIL_PASSWORD_SCRIPT=Config(
-      key="password_script",
-      help=_t("Execute this script to produce the email password. This will be used when `email_password` is not set."),
-      private=True,
-      type=coerce_password_from_script,
-      default=None),
-
-    SSL_CERT_CA_VERIFY = Config(
-      key="ssl_cert_ca_verify",
-      help=_t("In secure mode (HTTPS), if Optimizer SSL certificates have to be verified against certificate authority"),
-      dynamic_default=default_ssl_validate,
-      type=coerce_bool
-    ),
   )
 )
 

+ 23 - 27
desktop/libs/metadata/src/metadata/optimizer_client.py

@@ -109,23 +109,17 @@ def _secure_results(results, user, action='SELECT'):
 
 class OptimizerApi(object):
 
-  def __init__(self, user, api_url=None, product_name=None, product_secret=None, ssl_cert_ca_verify=OPTIMIZER.SSL_CERT_CA_VERIFY.get(), product_auth_secret=None):
+  def __init__(self, user, api_url=None, auth_key=None, auth_key_secret=None, tenant_id=None):
     self.user = user
     self._api_url = (api_url or get_optimizer_url()).strip('/')
+    self._auth_key = auth_key if auth_key else OPTIMIZER.AUTH_KEY.get()
+    self._auth_key_secret = auth_key_secret if auth_key_secret else (OPTIMIZER.AUTH_KEY_SECRET.get() and OPTIMIZER.AUTH_KEY_SECRET.get().replace('\\n', '\n'))
     self._email = OPTIMIZER.EMAIL.get()
-    self._email_password = OPTIMIZER.EMAIL_PASSWORD.get()
-    self._product_secret = product_secret if product_secret else OPTIMIZER.PRODUCT_SECRET.get()
-    self._product_auth_secret = product_auth_secret if product_auth_secret else (OPTIMIZER.PRODUCT_AUTH_SECRET.get() and OPTIMIZER.PRODUCT_AUTH_SECRET.get().replace('\\n', '\n'))
 
-    self._api = ApiLib("navopt", urlparse(self._api_url).hostname, self._product_secret, self._product_auth_secret)
+    self._api = ApiLib("navopt", urlparse(self._api_url).hostname, self._auth_key, self._auth_key_secret)
 
-    self._product_name = product_name if product_name else (OPTIMIZER.PRODUCT_NAME.get() or self.get_tenant()['tenant']) # Aka "workload"
+    self._tenant_id = tenant_id if tenant_id else (OPTIMIZER.TENANT_ID.get() or self.get_tenant(cluster_id=OPTIMIZER.CLUSTER_ID.get())['tenant']) # Aka "workload"
 
-  def _authenticate(self, force=False):
-    if self._token is None or force:
-      self._token = self.authenticate()['token']
-
-    return self._token
 
   def _call(self, *kwargs):
     resp = self._api.call_api(*kwargs)
@@ -141,12 +135,14 @@ class OptimizerApi(object):
     else:
       return data
 
-  def get_tenant(self, email=None):
-    return self._call("getTenant", {"email" : email or self._email})
 
+  def get_tenant(self, email=None, cluster_id=None):
+    if cluster_id is not None:
+      args = {'clusterId' : cluster_id}
+    else:
+      args = {'email': email or self._email}
 
-  def create_tenant(self, group):
-    return self._call('createTenant', {'userGroup' : group})
+    return self._call('getTenant', args)
 
 
   def upload(self, data, data_type='queries', source_platform='generic', workload_id=None):
@@ -193,7 +189,7 @@ class OptimizerApi(object):
         f_queries.close()
 
       parameters = {
-          'tenant' : self._product_name,
+          'tenant' : self._tenant_id,
           'fileLocation': f_queries.name,
           'sourcePlatform': source_platform,
       }
@@ -211,11 +207,11 @@ class OptimizerApi(object):
 
 
   def upload_status(self, workload_id):
-    return self._call('uploadStatus', {'tenant' : self._product_name, 'workloadId': workload_id})
+    return self._call('uploadStatus', {'tenant' : self._tenant_id, 'workloadId': workload_id})
 
   @check_privileges
   def top_tables(self, workfloadId=None, database_name='default', page_size=1000, startingToken=None):
-    data = self._call('getTopTables', {'tenant' : self._product_name, 'dbName': database_name.lower(), 'pageSize': page_size, startingToken: startingToken})
+    data = self._call('getTopTables', {'tenant' : self._tenant_id, 'dbName': database_name.lower(), 'pageSize': page_size, 'startingToken': startingToken})
 
     if OPTIMIZER.APPLY_SENTRY_PERMISSIONS.get():
       checker = get_checker(user=self.user)
@@ -231,16 +227,16 @@ class OptimizerApi(object):
 
   @check_privileges
   def table_details(self, database_name, table_name, page_size=100, startingToken=None):
-    return self._call('getTablesDetail', {'tenant' : self._product_name, 'dbName': database_name.lower(), 'tableName': table_name.lower(), 'pageSize': page_size, startingToken: startingToken})
+    return self._call('getTablesDetail', {'tenant' : self._tenant_id, 'dbName': database_name.lower(), 'tableName': table_name.lower(), 'pageSize': page_size, 'startingToken': startingToken})
 
 
   def query_compatibility(self, source_platform, target_platform, query, page_size=100, startingToken=None):
-    return self._call('getQueryCompatible', {'tenant' : self._product_name, 'query': query, 'sourcePlatform': source_platform, 'targetPlatform': target_platform, startingToken: startingToken})
+    return self._call('getQueryCompatible', {'tenant' : self._tenant_id, 'query': query, 'sourcePlatform': source_platform, 'targetPlatform': target_platform, 'startingToken': startingToken})
 
 
   def query_risk(self, query, source_platform, db_name, page_size=100, startingToken=None):
     response = self._call('getQueryRisk', {
-      'tenant' : self._product_name,
+      'tenant' : self._tenant_id,
       'query': query,
       'dbName': db_name,
       'sourcePlatform': source_platform,
@@ -261,7 +257,7 @@ class OptimizerApi(object):
 
   def similar_queries(self, source_platform, query, page_size=100, startingToken=None):
     if self.user.is_superuser:
-      return self._call('getSimilarQueries', {'tenant' : self._product_name, 'sourcePlatform': source_platform, 'query': query, 'pageSize': page_size, startingToken: startingToken})
+      return self._call('getSimilarQueries', {'tenant' : self._tenant_id, 'sourcePlatform': source_platform, 'query': query, 'pageSize': page_size, 'startingToken': startingToken})
     else:
       raise PopupException(_('Call not supported'))
 
@@ -269,7 +265,7 @@ class OptimizerApi(object):
   @check_privileges
   def top_filters(self, db_tables=None, page_size=100, startingToken=None):
     args = {
-      'tenant' : self._product_name,
+      'tenant' : self._tenant_id,
       'pageSize': page_size,
       'startingToken': startingToken
     }
@@ -291,7 +287,7 @@ class OptimizerApi(object):
   @check_privileges
   def top_aggs(self, db_tables=None, page_size=100, startingToken=None):
     args = {
-      'tenant' : self._product_name,
+      'tenant' : self._tenant_id,
       'pageSize': page_size,
       'startingToken': startingToken
     }
@@ -317,7 +313,7 @@ class OptimizerApi(object):
   @check_privileges
   def top_columns(self, db_tables=None, page_size=100, startingToken=None):
     args = {
-      'tenant' : self._product_name,
+      'tenant' : self._tenant_id,
       'pageSize': page_size,
       'startingToken': startingToken
     }
@@ -335,7 +331,7 @@ class OptimizerApi(object):
   @check_privileges
   def top_joins(self, db_tables=None, page_size=100, startingToken=None):
     args = {
-      'tenant' : self._product_name,
+      'tenant' : self._tenant_id,
       'pageSize': page_size,
       'startingToken': startingToken
     }
@@ -356,7 +352,7 @@ class OptimizerApi(object):
 
   def top_databases(self, page_size=100, startingToken=None):
     args = {
-      'tenant' : self._product_name,
+      'tenant' : self._tenant_id,
       'pageSize': page_size,
       'startingToken': startingToken
     }