Browse Source

HUE-1453 [beeswax] hive server 2 principal hostname replacement incorrect

Abraham Elmahrek 12 years ago
parent
commit
0048739d78

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

@@ -107,8 +107,19 @@ def get_metastore():
   return _METASTORE_LOC_CACHE
   return _METASTORE_LOC_CACHE
 
 
 
 
-def get_hiveserver2_kerberos_principal():
-  return security_util.get_kerberos_principal(get_conf().get(_CNF_HIVESERVER2_KERBEROS_PRINCIPAL, None), socket.getfqdn())
+def get_hiveserver2_kerberos_principal(hostname_or_ip):
+  """
+  Retrieves principal for HiveServer 2.
+
+  Raises socket.herror
+  """
+  fqdn = security_util.get_fqdn(hostname_or_ip)
+  # Get kerberos principal and replace host pattern
+  principal = get_conf().get(_CNF_HIVESERVER2_KERBEROS_PRINCIPAL, None)
+  if principal:
+    return security_util.get_kerberos_principal(principal, fqdn)
+  else:
+    return None
 
 
 def get_hiveserver2_authentication():
 def get_hiveserver2_authentication():
   return get_conf().get(_CNF_HIVESERVER2_AUTHENTICATION, 'NONE').upper() # NONE == PLAIN SASL
   return get_conf().get(_CNF_HIVESERVER2_AUTHENTICATION, 'NONE').upper() # NONE == PLAIN SASL

+ 1 - 1
apps/beeswax/src/beeswax/server/dbms.py

@@ -73,7 +73,7 @@ def get_query_server_config(name='beeswax'):
     }
     }
   else:
   else:
     if SERVER_INTERFACE.get() == 'hiveserver2':
     if SERVER_INTERFACE.get() == 'hiveserver2':
-      kerberos_principal = hive_site.get_hiveserver2_kerberos_principal()
+      kerberos_principal = hive_site.get_hiveserver2_kerberos_principal(BEESWAX_SERVER_HOST.get())
     else:
     else:
       # Beeswaxd runs as 'hue'
       # Beeswaxd runs as 'hue'
       kerberos_principal = KERBEROS.HUE_PRINCIPAL.get()
       kerberos_principal = KERBEROS.HUE_PRINCIPAL.get()

+ 94 - 10
apps/beeswax/src/beeswax/tests.py

@@ -1324,7 +1324,7 @@ def test_hive_site():
     assert_not_equal(port, 9999)
     assert_not_equal(port, 9999)
     assert_not_equal(kerberos_principal, 'test/test.com@TEST.COM')
     assert_not_equal(kerberos_principal, 'test/test.com@TEST.COM')
     assert_equal(beeswax.hive_site.get_conf()['hive.metastore.warehouse.dir'], u'/abc')
     assert_equal(beeswax.hive_site.get_conf()['hive.metastore.warehouse.dir'], u'/abc')
-    assert_equal(beeswax.hive_site.get_hiveserver2_kerberos_principal(), 'hs2test/test.com@TEST.COM')
+    assert_equal(beeswax.hive_site.get_hiveserver2_kerberos_principal('localhost'), 'hs2test/test.com@TEST.COM')
     assert_equal(beeswax.hive_site.get_hiveserver2_authentication(), 'NONE')
     assert_equal(beeswax.hive_site.get_hiveserver2_authentication(), 'NONE')
   finally:
   finally:
     beeswax.hive_site.reset()
     beeswax.hive_site.reset()
@@ -1332,8 +1332,9 @@ def test_hive_site():
       beeswax.conf.BEESWAX_HIVE_CONF_DIR = saved
       beeswax.conf.BEESWAX_HIVE_CONF_DIR = saved
     shutil.rmtree(tmpdir)
     shutil.rmtree(tmpdir)
 
 
-def test_hive_site_host_pattern():
+def test_hive_site_host_pattern_local_host():
   """Test hive-site parsing"""
   """Test hive-site parsing"""
+  hostname = socket.getfqdn()
   tmpdir = tempfile.mkdtemp()
   tmpdir = tempfile.mkdtemp()
   saved = None
   saved = None
   try:
   try:
@@ -1342,21 +1343,64 @@ def test_hive_site_host_pattern():
       def get(self):
       def get(self):
         return tmpdir
         return tmpdir
 
 
-    thrift_uris = 'thrift://%s:9999' % socket.getfqdn()
-    xml = hive_site_xml(is_local=False, use_sasl=False, thrift_uris=thrift_uris, kerberos_principal='test/_HOST@TEST.COM')
+    thrift_uris = 'thrift://%s:9999' % hostname
+    xml = hive_site_xml(is_local=False, use_sasl=False, thrift_uris=thrift_uris, kerberos_principal='test/_HOST@TEST.COM', hs2_kerberos_principal='test/_HOST@TEST.COM')
     file(os.path.join(tmpdir, 'hive-site.xml'), 'w').write(xml)
     file(os.path.join(tmpdir, 'hive-site.xml'), 'w').write(xml)
 
 
     beeswax.hive_site.reset()
     beeswax.hive_site.reset()
     saved = beeswax.conf.BEESWAX_HIVE_CONF_DIR
     saved = beeswax.conf.BEESWAX_HIVE_CONF_DIR
     beeswax.conf.BEESWAX_HIVE_CONF_DIR = Getter()
     beeswax.conf.BEESWAX_HIVE_CONF_DIR = Getter()
 
 
+    reset = []
+    reset.append(beeswax.conf.BEESWAX_SERVER_HOST.set_for_testing(hostname))
+
     is_local, host, port, kerberos_principal = beeswax.hive_site.get_metastore()
     is_local, host, port, kerberos_principal = beeswax.hive_site.get_metastore()
     assert_false(is_local)
     assert_false(is_local)
-    assert_equal(host, socket.getfqdn())
+    assert_equal(host, hostname)
     assert_equal(port, 9999)
     assert_equal(port, 9999)
     assert_equal(beeswax.hive_site.get_conf()['hive.metastore.warehouse.dir'], u'/abc')
     assert_equal(beeswax.hive_site.get_conf()['hive.metastore.warehouse.dir'], u'/abc')
     assert_equal(kerberos_principal, 'test/' + socket.getfqdn().lower() + '@TEST.COM')
     assert_equal(kerberos_principal, 'test/' + socket.getfqdn().lower() + '@TEST.COM')
+    assert_equal(beeswax.hive_site.get_hiveserver2_kerberos_principal(hostname), 'test/' + socket.getfqdn().lower() + '@TEST.COM')
+  finally:
+    for finish in reset:
+      finish()
+    beeswax.hive_site.reset()
+    if saved is not None:
+      beeswax.conf.BEESWAX_HIVE_CONF_DIR = saved
+    shutil.rmtree(tmpdir)
+
+def test_hive_site_host_pattern_remote_host():
+  """Test hive-site parsing"""
+  hostname = 'darkside-12345'
+  tmpdir = tempfile.mkdtemp()
+  saved = None
+  try:
+    # We just replace the Beeswax conf variable
+    class Getter(object):
+      def get(self):
+        return tmpdir
+
+    thrift_uris = 'thrift://%s:9999' % hostname
+    xml = hive_site_xml(is_local=False, use_sasl=False, thrift_uris=thrift_uris, kerberos_principal='test/_HOST@TEST.COM', hs2_kerberos_principal='test/_HOST@TEST.COM')
+    file(os.path.join(tmpdir, 'hive-site.xml'), 'w').write(xml)
+
+    beeswax.hive_site.reset()
+    saved = beeswax.conf.BEESWAX_HIVE_CONF_DIR
+    beeswax.conf.BEESWAX_HIVE_CONF_DIR = Getter()
+
+    reset = []
+    reset.append(beeswax.conf.BEESWAX_SERVER_HOST.set_for_testing(hostname))
+
+    is_local, host, port, kerberos_principal = beeswax.hive_site.get_metastore()
+    assert_false(is_local)
+    assert_equal(host, hostname)
+    assert_equal(port, 9999)
+    assert_equal(beeswax.hive_site.get_conf()['hive.metastore.warehouse.dir'], u'/abc')
+    assert_equal(kerberos_principal, 'test/%s@TEST.COM' % hostname)
+    assert_equal(beeswax.hive_site.get_hiveserver2_kerberos_principal(hostname), 'test/%s@TEST.COM' % hostname)
   finally:
   finally:
+    for finish in reset:
+      finish()
     beeswax.hive_site.reset()
     beeswax.hive_site.reset()
     if saved is not None:
     if saved is not None:
       beeswax.conf.BEESWAX_HIVE_CONF_DIR = saved
       beeswax.conf.BEESWAX_HIVE_CONF_DIR = saved
@@ -1453,6 +1497,39 @@ def test_hive_site_local_metastore():
     shutil.rmtree(tmpdir)
     shutil.rmtree(tmpdir)
 
 
 
 
+def test_hive_site_null_hs2krb():
+  """Test hive-site parsing with null hs2 kerberos principal"""
+  tmpdir = tempfile.mkdtemp()
+  saved = None
+  try:
+    # We just replace the Beeswax conf variable
+    class Getter(object):
+      def get(self):
+        return tmpdir
+
+    xml = hive_site_xml(is_local=True, use_sasl=False, hs2_kerberos_principal=None)
+    file(os.path.join(tmpdir, 'hive-site.xml'), 'w').write(xml)
+
+    beeswax.hive_site.reset()
+    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_true(is_local)
+    # Local so don't use hive-site.xml
+    assert_not_equal(host, 'darkside-1234')
+    assert_not_equal(port, 9999)
+    assert_not_equal(kerberos_principal, 'test/test.com@TEST.COM')
+    assert_equal(beeswax.hive_site.get_conf()['hive.metastore.warehouse.dir'], u'/abc')
+    assert_equal(beeswax.hive_site.get_hiveserver2_kerberos_principal('localhost'), None)
+    assert_equal(beeswax.hive_site.get_hiveserver2_authentication(), 'NONE')
+  finally:
+    beeswax.hive_site.reset()
+    if saved is not None:
+      beeswax.conf.BEESWAX_HIVE_CONF_DIR = saved
+    shutil.rmtree(tmpdir)
+
+
 def test_hive_site_multi_metastore_uris():
 def test_hive_site_multi_metastore_uris():
   """Test hive-site parsing"""
   """Test hive-site parsing"""
   tmpdir = tempfile.mkdtemp()
   tmpdir = tempfile.mkdtemp()
@@ -1700,6 +1777,16 @@ def hive_site_xml(is_local=False, use_sasl=False, thrift_uris='thrift://darkside
   else:
   else:
     uris = ''
     uris = ''
 
 
+  if hs2_kerberos_principal:
+    hs2_krb_princ = """
+      <property>
+        <name>hive.server2.authentication.kerberos.principal</name>
+        <value>%(hs2_kerberos_principal)s</value>
+      </property>
+    """ % {'hs2_kerberos_principal': hs2_kerberos_principal}
+  else:
+    hs2_krb_princ = ""
+
   return """
   return """
     <configuration>
     <configuration>
       %(uris)s
       %(uris)s
@@ -1713,10 +1800,7 @@ def hive_site_xml(is_local=False, use_sasl=False, thrift_uris='thrift://darkside
         <value>%(kerberos_principal)s</value>
         <value>%(kerberos_principal)s</value>
       </property>
       </property>
 
 
-      <property>
-        <name>hive.server2.authentication.kerberos.principal</name>
-        <value>%(hs2_kerberos_principal)s</value>
-      </property>
+      %(hs2_krb_princ)s
 
 
       <property>
       <property>
         <name>hive.server2.enable.impersonation</name>
         <name>hive.server2.enable.impersonation</name>
@@ -1737,7 +1821,7 @@ def hive_site_xml(is_local=False, use_sasl=False, thrift_uris='thrift://darkside
     'uris': uris,
     'uris': uris,
     'warehouse_dir': warehouse_dir,
     'warehouse_dir': warehouse_dir,
     'kerberos_principal': kerberos_principal,
     'kerberos_principal': kerberos_principal,
-    'hs2_kerberos_principal': hs2_kerberos_principal,
+    'hs2_krb_princ': hs2_krb_princ,
     'hs2_authentication': hs2_authentication,
     'hs2_authentication': hs2_authentication,
     'use_sasl': str(use_sasl).lower(),
     'use_sasl': str(use_sasl).lower(),
     'hs2_impersonation': hs2_impersonation,
     'hs2_impersonation': hs2_impersonation,

+ 13 - 1
desktop/core/src/desktop/lib/security_util.py

@@ -51,4 +51,16 @@ def replace_hostname_pattern(components, host):
   return '%s/%s@%s' % (components[0], fqdn.lower(), components[2])
   return '%s/%s@%s' % (components[0], fqdn.lower(), components[2])
 
 
 def get_localhost_name():
 def get_localhost_name():
-  return socket.get_localhost()
+  return socket.getfqdn()
+
+def get_fqdn(hostname_or_ip):
+  # Get hostname
+  try:
+    fqdn = socket.gethostbyaddr(hostname_or_ip)[0]
+  except:
+    fqdn = hostname_or_ip
+
+  if fqdn == 'localhost':
+    fqdn = get_localhost_name()
+
+  return fqdn