You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by ds...@apache.org on 2014/05/19 19:08:14 UTC

git commit: AMBARI-5809 Usability: Ensure that hostname (fqdn) and reverse lookup resolve to the same name - this host checkup should be done for server and agent hosts (dsen)

Repository: ambari
Updated Branches:
  refs/heads/trunk a9ca4747f -> f11f0b3f8


AMBARI-5809 Usability: Ensure that hostname (fqdn) and reverse lookup resolve to the same name - this host checkup should be done for server and agent hosts (dsen)


Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/f11f0b3f
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/f11f0b3f
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/f11f0b3f

Branch: refs/heads/trunk
Commit: f11f0b3f809d60928b52a824523e5c9cd6121117
Parents: a9ca474
Author: Dmitry Sen <ds...@hortonworks.com>
Authored: Mon May 19 20:07:57 2014 +0300
Committer: Dmitry Sen <ds...@hortonworks.com>
Committed: Mon May 19 20:07:57 2014 +0300

----------------------------------------------------------------------
 .../src/main/python/ambari_agent/HostInfo.py    | 19 +++++++++++++---
 .../test/python/ambari_agent/TestHostInfo.py    | 24 ++++++++++++++++++++
 .../apache/ambari/server/agent/AgentEnv.java    | 10 ++++++++
 ambari-server/src/main/python/ambari-server.py  | 17 ++++++++++++++
 4 files changed, 67 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/f11f0b3f/ambari-agent/src/main/python/ambari_agent/HostInfo.py
----------------------------------------------------------------------
diff --git a/ambari-agent/src/main/python/ambari_agent/HostInfo.py b/ambari-agent/src/main/python/ambari_agent/HostInfo.py
index af6afa9..64e9cb9 100644
--- a/ambari-agent/src/main/python/ambari_agent/HostInfo.py
+++ b/ambari-agent/src/main/python/ambari_agent/HostInfo.py
@@ -32,6 +32,7 @@ from PackagesAnalyzer import PackagesAnalyzer
 from HostCheckReportFileHandler import HostCheckReportFileHandler
 from Hardware import Hardware
 from common_functions import OSCheck
+import socket
 
 logger = logging.getLogger()
 
@@ -327,7 +328,7 @@ class HostInfo:
     isSuse =  'suse' == OSCheck.get_os_family()
 
     dict['iptablesIsRunning'] = self.checkIptables()
-
+    dict['reverseLookup'] = self.checkReverseLookup()
     # If commands are in progress or components are already mapped to this host
     # Then do not perform certain expensive host checks
     if componentsMapped or commandsInProgress or isSuse:
@@ -374,7 +375,19 @@ class HostInfo:
     dict['hostHealth']['agentTimeStampAtReporting'] = int(time.time() * 1000)
 
     pass
-
+  def checkReverseLookup(self):
+    """
+    Check if host fqdn resolves to current host ip
+    """
+    try:
+      host_name = socket.gethostname()
+      host_ip = socket.gethostbyname(host_name)
+      host_fqdn = socket.getfqdn()
+      fqdn_ip = socket.gethostbyname(host_fqdn)
+      return host_ip == fqdn_ip
+    except socket.herror:
+      pass
+    return False
 
 class FirewallChecks(object):
   def __init__(self):
@@ -455,4 +468,4 @@ def main(argv=None):
 
 
 if __name__ == '__main__':
-  main()
\ No newline at end of file
+  main()

http://git-wip-us.apache.org/repos/asf/ambari/blob/f11f0b3f/ambari-agent/src/test/python/ambari_agent/TestHostInfo.py
----------------------------------------------------------------------
diff --git a/ambari-agent/src/test/python/ambari_agent/TestHostInfo.py b/ambari-agent/src/test/python/ambari_agent/TestHostInfo.py
index 5272633..baf95f0 100644
--- a/ambari-agent/src/test/python/ambari_agent/TestHostInfo.py
+++ b/ambari-agent/src/test/python/ambari_agent/TestHostInfo.py
@@ -23,6 +23,7 @@ from unittest import TestCase
 import logging
 import unittest
 import subprocess
+import socket
 from mock.mock import patch
 from mock.mock import MagicMock
 from mock.mock import create_autospec
@@ -522,6 +523,29 @@ class TestHostInfo(TestCase):
       run_os_command_mock.return_value = firewall.get_running_result()
       self.assertTrue(firewall.check_iptables())
 
+  @patch.object(socket, "getfqdn")
+  @patch.object(socket, "gethostbyname")
+  @patch.object(socket, "gethostname")
+  def test_checkReverseLookup(self, gethostname_mock, gethostbyname_mock, getfqdn_mock):
+    gethostname_mock.return_value = "test"
+    gethostbyname_mock.side_effect = ["123.123.123.123", "123.123.123.123"]
+    getfqdn_mock.return_value = "test.example.com"
+
+    hostInfo = HostInfo()
+
+    self.assertTrue(hostInfo.checkReverseLookup())
+    gethostbyname_mock.assert_any_call("test.example.com")
+    gethostbyname_mock.assert_any_call("test")
+    self.assertEqual(2, gethostbyname_mock.call_count)
+
+    gethostbyname_mock.side_effect = ["123.123.123.123", "231.231.231.231"]
+
+    self.assertFalse(hostInfo.checkReverseLookup())
+
+    gethostbyname_mock.side_effect = ["123.123.123.123", "123.123.123.123"]
+    getfqdn_mock.side_effect = socket.herror()
+
+    self.assertFalse(hostInfo.checkReverseLookup())
 
   @patch.object(FirewallChecks, "run_os_command")
   def test_IpTablesStopped(self, run_os_command_mock):

http://git-wip-us.apache.org/repos/asf/ambari/blob/f11f0b3f/ambari-server/src/main/java/org/apache/ambari/server/agent/AgentEnv.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/agent/AgentEnv.java b/ambari-server/src/main/java/org/apache/ambari/server/agent/AgentEnv.java
index 912de7e..eed901c 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/agent/AgentEnv.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/agent/AgentEnv.java
@@ -60,6 +60,16 @@ public class AgentEnv {
 
   private Boolean iptablesIsRunning;
 
+  private Boolean reverseLookup;
+
+  public Boolean getReverseLookup() {
+    return reverseLookup;
+  }
+
+  public void setReverseLookup(Boolean reverseLookup) {
+    this.reverseLookup = reverseLookup;
+  }
+
   public Integer getUmask() {
     return umask;
   }

http://git-wip-us.apache.org/repos/asf/ambari/blob/f11f0b3f/ambari-server/src/main/python/ambari-server.py
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/python/ambari-server.py b/ambari-server/src/main/python/ambari-server.py
index 689a96d..81dadbd 100755
--- a/ambari-server/src/main/python/ambari-server.py
+++ b/ambari-server/src/main/python/ambari-server.py
@@ -806,6 +806,19 @@ def create_custom_user():
   print_info_msg("User configuration is done.")
   return 0, user
 
+def check_reverse_lookup():
+  """
+  Check if host fqdn resolves to current host ip
+  """
+  try:
+    host_name = socket.gethostname()
+    host_ip = socket.gethostbyname(host_name)
+    host_fqdn = socket.getfqdn()
+    fqdn_ip = socket.gethostbyname(host_fqdn)
+    return host_ip == fqdn_ip
+  except socket.herror:
+    pass
+  return False
 
 def check_ambari_user():
   try:
@@ -2355,6 +2368,10 @@ def reset(args):
 # Starts the Ambari Server.
 #
 def start(args):
+  if not check_reverse_lookup():
+    print_warning_msg("The hostname was not found in the reverse DNS lookup. "
+                      "This may result in incorrect behavior. "
+                      "Please check the DNS setup and fix the issue.")
   current_user = getpass.getuser()
   ambari_user = read_ambari_user()
   if ambari_user is None: