tests.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #!/usr/bin/env python
  2. # Licensed to Cloudera, Inc. under one
  3. # or more contributor license agreements. See the NOTICE file
  4. # distributed with this work for additional information
  5. # regarding copyright ownership. Cloudera, Inc. licenses this file
  6. # to you under the Apache License, Version 2.0 (the
  7. # "License"); you may not use this file except in compliance
  8. # with the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. from builtins import object
  18. import json
  19. import os
  20. import shutil
  21. import tempfile
  22. from nose.tools import assert_true, assert_equal
  23. from nose.plugins.skip import SkipTest
  24. from django.urls import reverse
  25. from desktop.lib.django_test_util import make_logged_in_client
  26. from desktop.lib.test_utils import grant_access, add_to_group
  27. from hadoop.pseudo_hdfs4 import is_live_cluster
  28. from useradmin.models import User
  29. from hbase.api import HbaseApi
  30. from hbase.conf import HBASE_CONF_DIR
  31. from hbase.hbase_site import get_server_authentication, get_server_principal, get_conf, reset, _CNF_HBASE_IMPERSONATION_ENABLED, is_impersonation_enabled
  32. def test_security_plain():
  33. tmpdir = tempfile.mkdtemp()
  34. finish = HBASE_CONF_DIR.set_for_testing(tmpdir)
  35. try:
  36. xml = hbase_site_xml()
  37. file(os.path.join(tmpdir, 'hbase-site.xml'), 'w').write(xml)
  38. reset()
  39. assert_equal('NOSASL', get_server_authentication())
  40. assert_equal('test', get_server_principal())
  41. security = HbaseApi._get_security()
  42. assert_equal('test', security['kerberos_principal_short_name'])
  43. assert_equal(False, security['use_sasl'])
  44. finally:
  45. reset()
  46. finish()
  47. shutil.rmtree(tmpdir)
  48. def test_security_kerberos():
  49. tmpdir = tempfile.mkdtemp()
  50. finish = HBASE_CONF_DIR.set_for_testing(tmpdir)
  51. try:
  52. xml = hbase_site_xml(authentication='kerberos')
  53. file(os.path.join(tmpdir, 'hbase-site.xml'), 'w').write(xml)
  54. reset()
  55. assert_equal('KERBEROS', get_server_authentication())
  56. assert_equal('test', get_server_principal())
  57. security = HbaseApi._get_security()
  58. assert_equal('test', security['kerberos_principal_short_name'])
  59. assert_equal(True, security['use_sasl'])
  60. finally:
  61. reset()
  62. finish()
  63. shutil.rmtree(tmpdir)
  64. def hbase_site_xml(
  65. kerberos_principal='test/test.com@TEST.COM',
  66. authentication='NOSASL'):
  67. return """
  68. <configuration>
  69. <property>
  70. <name>hbase.thrift.kerberos.principal</name>
  71. <value>%(kerberos_principal)s</value>
  72. </property>
  73. <property>
  74. <name>hbase.security.authentication</name>
  75. <value>%(authentication)s</value>
  76. </property>
  77. </configuration>
  78. """ % {
  79. 'kerberos_principal': kerberos_principal,
  80. 'authentication': authentication,
  81. }
  82. def test_impersonation_is_decorator_is_there():
  83. # Decorator is still there
  84. from hbased.Hbase import do_as
  85. def test_impersonation():
  86. from hbased import Hbase as thrift_hbase
  87. c = make_logged_in_client(username='test_hbase', is_superuser=False)
  88. grant_access('test_hbase', 'test_hbase', 'hbase')
  89. user = User.objects.get(username='test_hbase')
  90. proto = MockProtocol()
  91. client = thrift_hbase.Client(proto)
  92. impersonation_enabled = is_impersonation_enabled()
  93. get_conf()[_CNF_HBASE_IMPERSONATION_ENABLED] = 'FALSE'
  94. try:
  95. client.getTableNames(doas=user.username)
  96. except AttributeError:
  97. pass # We don't mock everything
  98. finally:
  99. get_conf()[_CNF_HBASE_IMPERSONATION_ENABLED] = impersonation_enabled
  100. assert_equal({}, proto.get_headers())
  101. get_conf()[_CNF_HBASE_IMPERSONATION_ENABLED] = 'TRUE'
  102. try:
  103. client.getTableNames(doas=user.username)
  104. except AttributeError:
  105. pass # We don't mock everything
  106. finally:
  107. get_conf()[_CNF_HBASE_IMPERSONATION_ENABLED] = impersonation_enabled
  108. assert_equal({'doAs': u'test_hbase'}, proto.get_headers())
  109. class MockHttpClient(object):
  110. def __init__(self):
  111. self.headers = {}
  112. def setCustomHeaders(self, headers):
  113. self.headers = headers
  114. class MockTransport(object):
  115. def __init__(self):
  116. self._TBufferedTransport__trans = MockHttpClient()
  117. class MockProtocol(object):
  118. def __init__(self):
  119. self.trans = MockTransport()
  120. def getTableNames(self):
  121. pass
  122. def get_headers(self):
  123. return self.trans._TBufferedTransport__trans.headers
  124. class TestIntegrationWithHBase(object):
  125. integration = True
  126. @classmethod
  127. def setup_class(cls):
  128. if not is_live_cluster():
  129. raise SkipTest('These tests can only run on a live cluster')
  130. cls.client = make_logged_in_client(username='test', is_superuser=False)
  131. cls.user = User.objects.get(username='test')
  132. add_to_group('test')
  133. grant_access("test", "test", "indexer")
  134. def test_list_tables(self):
  135. if not is_live_cluster():
  136. raise SkipTest('HUE-2910: Skipping because test is not reentrant')
  137. for cluster in HbaseApi(self.user).getClusters():
  138. resp = self.client.post('/hbase/api/getTableList/' + cluster['name'])
  139. content = json.loads(resp.content)
  140. assert_true('data' in content, content)