Explorar o código

AMBARI-2681. setup ldap does not validate secondary url. (smohanty)

Sumit Mohanty %!s(int64=12) %!d(string=hai) anos
pai
achega
d5810bd615

+ 2 - 2
ambari-server/src/main/python/ambari-server.py

@@ -2434,7 +2434,7 @@ def setup_ldap():
   ldap_properties_map_reqd =\
   {
     ldap_property_list_reqd[0]:(LDAP_PRIMARY_URL_DEFAULT, "Primary URL* {{host:port}} {0}: ".format(get_prompt_default(LDAP_PRIMARY_URL_DEFAULT)), False),\
-    ldap_property_list_reqd[1]:(LDAP_SECONDARY_URL_DEFAULT, "Secondary URL {0}: ".format(get_prompt_default(LDAP_SECONDARY_URL_DEFAULT)), True),\
+    ldap_property_list_reqd[1]:(LDAP_SECONDARY_URL_DEFAULT, "Secondary URL {{host:port}} {0}: ".format(get_prompt_default(LDAP_SECONDARY_URL_DEFAULT)), True),\
     ldap_property_list_reqd[2]:(LDAP_USE_SSL_DEFAULT, "Use SSL* [true/false] {0}: ".format(get_prompt_default(LDAP_USE_SSL_DEFAULT)), False),\
     ldap_property_list_reqd[3]:(LDAP_USER_ATT_DEFAULT, "User name attribute* {0}: ".format(get_prompt_default(LDAP_USER_ATT_DEFAULT)), False),\
     ldap_property_list_reqd[4]:(LDAP_BASE_DN_DEFAULT, "Base DN* {0}: ".format(get_prompt_default(LDAP_BASE_DN_DEFAULT)), False),\
@@ -2443,7 +2443,7 @@ def setup_ldap():
 
   ldap_property_value_map = {}
   for idx, key in enumerate(ldap_property_list_reqd):
-    if idx == 0:
+    if idx in [0, 1]:
       pattern = REGEX_HOSTNAME_PORT
     elif idx in [2, 5]:
       pattern = REGEX_TRUE_FALSE

+ 70 - 0
ambari-server/src/test/python/TestAmbaryServer.py

@@ -3402,6 +3402,76 @@ MIIFHjCCAwYCCQDpHKOBI+Lt0zANBgkqhkiG9w0BAQUFADBRMQswCQYDVQQGEwJV
                       key=operator.itemgetter(0))
     self.assertEquals(sorted_x, sorted_y)
 
+  @patch('__builtin__.raw_input')
+  @patch.object(ambari_server, 'get_is_secure')
+  @patch.object(ambari_server, 'get_YN_input')
+  @patch.object(ambari_server, 'update_properties')
+  @patch.object(ambari_server, 'search_file')
+  @patch.object(ambari_server, 'get_ambari_properties')
+  @patch.object(ambari_server, 'is_root')
+  def test_setup_ldap_invalid_input(self, is_root_method, get_ambari_properties_method,
+                      search_file_message,
+                      update_properties_method,
+                      get_YN_input_method,
+                      get_is_secure_method,
+                      raw_input_mock):
+    out = StringIO.StringIO()
+    sys.stdout = out
+    is_root_method.return_value = True
+    search_file_message.return_value = "filepath"
+
+    configs = { ambari_server.SECURITY_MASTER_KEY_LOCATION : "filepath",
+                ambari_server.SECURITY_KEYS_DIR : tempfile.gettempdir(),
+                ambari_server.SECURITY_IS_ENCRYPTION_ENABLED : "true"
+    }
+
+    get_ambari_properties_method.return_value = configs
+    raw_input_mock.side_effect = ['a:3', 'b:b', 'host', 'b:2', 'false', 'uid', 'base', 'true']
+    ambari_server.SILENT = False
+    get_YN_input_method.return_value = True
+
+    ambari_server.setup_ldap()
+
+    ldap_properties_map = \
+      {
+        "authentication.ldap.primaryUrl" : "a:3",
+        "authentication.ldap.secondaryUrl" : "b:2",
+        "authentication.ldap.useSSL" : "false",
+        "authentication.ldap.usernameAttribute" : "uid",
+        "authentication.ldap.baseDn" : "base",
+        "authentication.ldap.bindAnonymously" : "true",
+        "client.security" : "ldap"
+      }
+
+    sorted_x = sorted(ldap_properties_map.iteritems(), key=operator.itemgetter(0))
+    sorted_y = sorted(update_properties_method.call_args[0][1].iteritems(),
+                      key=operator.itemgetter(0))
+    self.assertEquals(sorted_x, sorted_y)
+    self.assertTrue(get_YN_input_method.called)
+    self.assertTrue(8, raw_input_mock.call_count)
+
+    raw_input_mock.reset_mock()
+    raw_input_mock.side_effect = ['a:3', '', 'b:2', 'false', 'uid', 'base', 'true']
+
+    ambari_server.setup_ldap()
+
+    ldap_properties_map = \
+      {
+        "authentication.ldap.primaryUrl" : "a:3",
+        "authentication.ldap.useSSL" : "false",
+        "authentication.ldap.usernameAttribute" : "uid",
+        "authentication.ldap.baseDn" : "base",
+        "authentication.ldap.bindAnonymously" : "true",
+        "client.security" : "ldap"
+      }
+
+    sorted_x = sorted(ldap_properties_map.iteritems(), key=operator.itemgetter(0))
+    sorted_y = sorted(update_properties_method.call_args[0][1].iteritems(),
+                      key=operator.itemgetter(0))
+    self.assertEquals(sorted_x, sorted_y)
+    self.assertTrue(5, raw_input_mock.call_count)
+
+    sys.stdout = sys.__stdout__
 
   @patch.object(ambari_server, 'get_is_secure')
   @patch.object(ambari_server, 'encrypt_password')