This is an automated email from the ASF dual-hosted git repository.

rlevas pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/ambari.git


The following commit(s) were added to refs/heads/trunk by this push:
     new e1d45a0  AMBARI-23440. Receiving HTTP error code of 404 when fetching 
sso-configuration from the DB is not a real error and should be handled
e1d45a0 is described below

commit e1d45a0884d9fd7ff5a82fc5ac48f7df1a027410
Author: Sandor Molnar <[email protected]>
AuthorDate: Tue Apr 3 22:01:25 2018 +0200

    AMBARI-23440. Receiving HTTP error code of 404 when fetching 
sso-configuration from the DB is not a real error and should be handled
---
 .../src/main/python/ambari_server/setupSso.py      | 33 ++++++++++----------
 ambari-server/src/test/python/TestSetupSso.py      | 35 ++++++++++++++++++++++
 2 files changed, 53 insertions(+), 15 deletions(-)

diff --git a/ambari-server/src/main/python/ambari_server/setupSso.py 
b/ambari-server/src/main/python/ambari_server/setupSso.py
index dc97f22..48e5d77 100644
--- a/ambari-server/src/main/python/ambari_server/setupSso.py
+++ b/ambari-server/src/main/python/ambari_server/setupSso.py
@@ -27,7 +27,7 @@ import urllib2
 
 from ambari_commons.os_utils import is_root, run_os_command, copy_file, 
set_file_permissions, remove_file
 from ambari_commons.exceptions import FatalException, NonFatalException
-from ambari_commons.logging_utils import get_silent, print_info_msg
+from ambari_commons.logging_utils import get_silent
 from ambari_server.userInput import get_validated_string_input, get_YN_input, 
get_multi_line_input
 from ambari_server.serverUtils import is_server_runing, 
get_ambari_server_api_base, get_ambari_admin_username_password_pair, 
get_cluster_name, perform_changes_via_rest_api
 from ambari_server.setupSecurity import REGEX_TRUE_FALSE
@@ -225,20 +225,23 @@ def get_sso_property_from_db(properties, admin_login, 
admin_password, property_n
         response_status_code = response.getcode()
         if response_status_code != 200:
           request_in_progress = False
-          if response_status_code == 404:
-            ## This means that there is no SSO configuration in the database 
yet -> we can not fetch the property (but this is NOT an error)
-            sso_property = None
-          else:
-            err = 'Error while fetching SSO configuration. Http status code - 
' + str(response_status_code)
-            raise FatalException(1, err)
+          err = 'Error while fetching SSO configuration. Http status code - ' 
+ str(response_status_code)
+          raise FatalException(1, err)
         else:
-            response_body = json.loads(response.read())
-            sso_properties = response_body['Configuration']['properties']
-            sso_property = sso_properties[property_name]
-            if not sso_property:
-              time.sleep(1)
-            else:
-              request_in_progress = False
+          response_body = json.loads(response.read())
+          sso_properties = response_body['Configuration']['properties']
+          sso_property = sso_properties[property_name]
+          if not sso_property:
+            time.sleep(1)
+          else:
+            request_in_progress = False
+    except urllib2.HTTPError as http_error:
+      if http_error.code == 404:
+        # This means that there is no SSO configuration in the database yet -> 
we can not fetch the property (but this is NOT an error)
+        request_in_progress = False
+        sso_property = None
+      else:
+        raise http_error
     except Exception as e:
       request_in_progress = False
       err = 'Error while fetching SSO configuration. Error details: %s' % e
@@ -281,7 +284,7 @@ def setup_sso(options):
     if not options.sso_enabled:
       sso_enabled_from_db = get_sso_property_from_db(properties, admin_login, 
admin_password, SSO_MANAGE_SERVICES)
       sso_enabled = sso_enabled_from_db == None or sso_enabled_from_db in 
['true']
-      print_info_msg("SSO is currently {0}".format("not configured" if 
sso_enabled_from_db == None else ("enabled" if sso_enabled else "disabled")), 
True)
+      sys.stdout.write("\nSSO is currently {0}\n".format("not configured" if 
sso_enabled_from_db == None else ("enabled" if sso_enabled else "disabled")))
       if sso_enabled:
         enable_sso = not get_YN_input("Do you want to disable SSO 
authentication [y/n] (n)? ", False)
       else:
diff --git a/ambari-server/src/test/python/TestSetupSso.py 
b/ambari-server/src/test/python/TestSetupSso.py
index 53455a6..9b822c6 100644
--- a/ambari-server/src/test/python/TestSetupSso.py
+++ b/ambari-server/src/test/python/TestSetupSso.py
@@ -26,6 +26,7 @@ from mock.mock import patch, MagicMock
 
 from only_for_platform import os_distro_value
 from ambari_commons import os_utils
+from urllib2 import HTTPError
 
 import shutil
 project_dir = 
os.path.join(os.path.abspath(os.path.dirname(__file__)),os.path.normpath("../../../../"))
@@ -376,6 +377,40 @@ class TestSetupSso(unittest.TestCase):
     sys.stdout = sys.__stdout__
     pass
 
+  @patch("urllib2.urlopen")
+  @patch("ambari_server.setupSso.perform_changes_via_rest_api")
+  @patch("ambari_server.setupSso.get_cluster_name")
+  @patch("ambari_server.setupSso.get_YN_input")
+  @patch("ambari_server.setupSso.update_properties")
+  @patch("ambari_server.setupSso.get_ambari_properties")
+  @patch("ambari_server.setupSso.get_silent")
+  @patch("ambari_server.setupSso.is_server_runing")
+  @patch("ambari_server.setupSso.is_root")
+  def 
test_setup_sso_should_not_fail_when_sso_config_cannot_be_loaded_due_to_404_error(self,
 is_root_mock, is_server_runing_mock, get_silent_mock, 
get_ambari_properties_mock, update_properties_mock, get_YN_input_mock,
+                                                             
get_cluster_name_mock, perform_changes_via_rest_api_mock, urlopen_mock):
+    out = StringIO.StringIO()
+    sys.stdout = out
+
+    is_root_mock.return_value = True
+    is_server_runing_mock.return_value = (True, 0)
+    get_silent_mock.return_value = False
+    get_ambari_properties_mock.return_value = Properties()
+    get_cluster_name_mock.return_value = 'cluster1'
+    get_YN_input_mock.__return_value = True
+
+    urlopen_mock.side_effect = HTTPError(MagicMock(status=404), 404, 'not 
found', None, None)
+
+    options = self._create_empty_options_mock()
+    options.sso_provider_url = 'http://testHost:8080'
+    options.sso_public_cert_file = '/test/file/path'
+    options.sso_jwt_cookie_name = 'test_cookie'
+    options.sso_jwt_audience_list = 'test, audience, list'
+
+    setup_sso(options)
+
+    self.assertTrue(update_properties_mock.called)
+    pass
+
 
   @patch("urllib2.urlopen")
   @patch("ambari_server.setupSso.perform_changes_via_rest_api")

-- 
To stop receiving notification emails like this one, please contact
[email protected].

Reply via email to