Преглед изворни кода

[beeswax] Support for _HOST in kerberos principal

Should be replaced with fqdn lowercased.
Abraham Elmahrek пре 12 година
родитељ
комит
3b1c3ee

+ 2 - 1
apps/beeswax/src/beeswax/hive_site.py

@@ -22,6 +22,7 @@ import errno
 import logging
 import os.path
 import re
+import socket
 
 from desktop.lib import security_util
 
@@ -73,7 +74,7 @@ def get_metastore():
   """
   global _METASTORE_LOC_CACHE
   if not _METASTORE_LOC_CACHE:
-    kerberos_principal = security_util.get_kerberos_principal(get_conf().get(_CNF_METASTORE_KERBEROS_PRINCIPAL, None))
+    kerberos_principal = security_util.get_kerberos_principal(get_conf().get(_CNF_METASTORE_KERBEROS_PRINCIPAL, None), socket.getfqdn())
     kerberos_principal_components = security_util.get_components(kerberos_principal)
     thrift_uris = get_conf().get(_CNF_METASTORE_URIS)
     is_local = thrift_uris is None or thrift_uris == ''

+ 53 - 0
apps/beeswax/src/beeswax/tests.py

@@ -26,6 +26,7 @@ import logging
 import os
 import re
 import shutil
+import socket
 import tempfile
 import threading
 
@@ -1313,6 +1314,58 @@ def test_hive_site():
       beeswax.conf.BEESWAX_HIVE_CONF_DIR = saved
     shutil.rmtree(tmpdir)
 
+def test_hive_site_host_pattern():
+  """Test hive-site parsing"""
+  HIVE_SITE = """
+    <configuration>
+      <property>
+        <name>hive.metastore.local</name>
+        <value>false</value>
+      </property>
+
+      <property>
+        <name>hive.metastore.uris</name>
+        <value>thrift://%s:9999</value>
+      </property>
+
+      <property>
+        <name>hive.metastore.warehouse.dir</name>
+        <value>/abc</value>
+      </property>
+
+      <property>
+        <name>hive.metastore.kerberos.principal</name>
+        <value>test/_HOST@TEST.COM</value>
+      </property>
+    </configuration>
+  """ % socket.getfqdn()
+
+  beeswax.hive_site.reset()
+  tmpdir = tempfile.mkdtemp()
+  saved = None
+  try:
+    file(os.path.join(tmpdir, 'hive-site.xml'), 'w').write(HIVE_SITE)
+
+    # We just replace the Beeswax conf variable
+    class Getter(object):
+      def get(self):
+        return tmpdir
+
+    saved = beeswax.conf.BEESWAX_HIVE_CONF_DIR
+    beeswax.conf.BEESWAX_HIVE_CONF_DIR = Getter()
+
+    is_local, host, port, kerberos_principal = beeswax.hive_site.get_metastore()
+    assert_false(is_local)
+    assert_equal(host, socket.getfqdn())
+    assert_equal(port, 9999)
+    assert_equal(beeswax.hive_site.get_conf()['hive.metastore.warehouse.dir'], u'/abc')
+    assert_equal(kerberos_principal, 'test/' + socket.getfqdn().lower() + '@TEST.COM')
+  finally:
+    beeswax.hive_site.reset()
+    if saved is not None:
+      beeswax.conf.BEESWAX_HIVE_CONF_DIR = saved
+    shutil.rmtree(tmpdir)
+
 def test_hive_site_sasl():
   """Test hive-site parsing with sasl enabled"""
   HIVE_SITE = """

+ 8 - 2
desktop/core/src/desktop/lib/security_util.py

@@ -18,14 +18,20 @@
 import re
 import socket
 
+from django.utils.translation import ugettext as _
+
 # Pattern to replace with hostname
 HOSTNAME_PATTERN = '_HOST'
 
 
-def get_kerberos_principal(principal, host=None):
+def get_kerberos_principal(principal, host):
   components = get_components(principal)
   if not components or len(components) != 3 or components[1] != HOSTNAME_PATTERN:
     return principal
+  else:
+    if not host:
+      raise IOError(_("Can't replace %s pattern since host is null.") % HOSTNAME_PATTERN)
+    return replace_hostname_pattern(components, host)
 
 
 def get_components(principal):
@@ -42,7 +48,7 @@ def replace_hostname_pattern(components, host):
   fqdn = host
   if not fqdn or fqdn == '0.0.0.0':
     fqdn = get_localhost_name()
-  return '%s/%s@%s' % (components[0], fqdn.lowercase(), components[2])
+  return '%s/%s@%s' % (components[0], fqdn.lower(), components[2])
 
 def get_localhost_name():
   return socket.get_localhost()