firewall.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #!/usr/bin/env python
  2. '''
  3. Licensed to the Apache Software Foundation (ASF) under one
  4. or more contributor license agreements. See the NOTICE file
  5. distributed with this work for additional information
  6. regarding copyright ownership. The ASF licenses this file
  7. to you under the Apache License, Version 2.0 (the
  8. "License"); you may not use this file except in compliance
  9. with the License. You may obtain a copy of the License at
  10. http://www.apache.org/licenses/LICENSE-2.0
  11. Unless required by applicable law or agreed to in writing, software
  12. distributed under the License is distributed on an "AS IS" BASIS,
  13. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. See the License for the specific language governing permissions and
  15. limitations under the License.
  16. '''
  17. import subprocess
  18. import shlex
  19. from ambari_commons import OSCheck, OSConst
  20. from ambari_commons.logging_utils import print_warning_msg
  21. from ambari_commons.os_family_impl import OsFamilyImpl
  22. from ambari_commons.os_utils import run_os_command
  23. class Firewall(object):
  24. def __init__(self):
  25. # OS info
  26. self.OS_VERSION = OSCheck().get_os_major_version()
  27. self.OS_TYPE = OSCheck.get_os_type()
  28. self.OS_FAMILY = OSCheck.get_os_family()
  29. def getFirewallObject(self):
  30. pass
  31. @OsFamilyImpl(os_family=OSConst.WINSRV_FAMILY)
  32. class FirewallWindows(Firewall):
  33. def getFirewallObject(self):
  34. return WindowsFirewallChecks()
  35. @OsFamilyImpl(os_family=OsFamilyImpl.DEFAULT)
  36. class FirewallLinux(Firewall):
  37. def getFirewallObject(self):
  38. if OSCheck.is_ubuntu_family():
  39. return UbuntuFirewallChecks()
  40. elif self.OS_TYPE == OSConst.OS_FEDORA and int(self.OS_VERSION) >= 18:
  41. return Fedora18FirewallChecks()
  42. elif OSCheck.is_suse_family():
  43. return SuseFirewallChecks()
  44. else:
  45. return FirewallChecks()
  46. class FirewallChecks(object):
  47. def __init__(self):
  48. self.FIREWALL_SERVICE_NAME = "iptables"
  49. self.SERVICE_SUBCMD = "status"
  50. # service cmd
  51. self.SERVICE_CMD = "/sbin/service"
  52. self.returncode = None
  53. self.stdoutdata = None
  54. self.stderrdata = None
  55. # stdout message
  56. self.MESSAGE_CHECK_FIREWALL = 'Checking firewall status...'
  57. def get_firewall_name(self):
  58. return self.FIREWALL_SERVICE_NAME
  59. def get_command(self):
  60. return "%s %s %s" % (self.SERVICE_CMD, self.FIREWALL_SERVICE_NAME, self.SERVICE_SUBCMD)
  61. def check_result(self):
  62. result = False
  63. if self.returncode == 3:
  64. result = False
  65. elif self.returncode == 0:
  66. if "Table: filter" in self.stdoutdata:
  67. result = True
  68. return result
  69. def run_command(self):
  70. retcode, out, err = run_os_command(self.get_command())
  71. self.returncode = retcode
  72. self.stdoutdata = out
  73. self.stderrdata = err
  74. def check_firewall(self):
  75. try:
  76. self.run_command()
  77. return self.check_result()
  78. except OSError:
  79. return False
  80. class UbuntuFirewallChecks(FirewallChecks):
  81. def __init__(self):
  82. super(UbuntuFirewallChecks, self).__init__()
  83. self.FIREWALL_SERVICE_NAME = "ufw"
  84. def get_command(self):
  85. return "%s %s" % (self.FIREWALL_SERVICE_NAME, self.SERVICE_SUBCMD)
  86. def check_result(self):
  87. # On ubuntu, the status command returns 0 whether running or not
  88. result = False
  89. if self.returncode == 0:
  90. if "Status: inactive" in self.stdoutdata:
  91. result = False
  92. elif "Status: active" in self.stdoutdata:
  93. result = True
  94. return result
  95. class Fedora18FirewallChecks(FirewallChecks):
  96. def __init__(self):
  97. super(Fedora18FirewallChecks, self).__init__()
  98. def get_command(self):
  99. return "systemctl is-active %s" % (self.FIREWALL_SERVICE_NAME)
  100. def check_result(self):
  101. result = False
  102. if self.returncode == 0:
  103. if "active" in self.stdoutdata:
  104. result = True
  105. return result
  106. class SuseFirewallChecks(FirewallChecks):
  107. def __init__(self):
  108. super(SuseFirewallChecks, self).__init__()
  109. self.FIREWALL_SERVICE_NAME = "SuSEfirewall2"
  110. def get_command(self):
  111. return "%s %s" % (self.FIREWALL_SERVICE_NAME, self.SERVICE_SUBCMD)
  112. def check_result(self):
  113. result = False
  114. if self.returncode == 0:
  115. if "SuSEfirewall2 not active" in self.stdoutdata:
  116. result = False
  117. elif "### iptables" in self.stdoutdata:
  118. result = True
  119. return result
  120. class WindowsFirewallChecks(FirewallChecks):
  121. def __init__(self):
  122. super(WindowsFirewallChecks, self).__init__()
  123. self.FIREWALL_SERVICE_NAME = "MpsSvc"
  124. def run_command(self):
  125. from ambari_commons.os_windows import run_powershell_script, CHECK_FIREWALL_SCRIPT, WinServiceController, SERVICE_STATUS_RUNNING
  126. if WinServiceController.QueryStatus(self.FIREWALL_SERVICE_NAME) != SERVICE_STATUS_RUNNING:
  127. self.returncode = 0
  128. self.stdoutdata = ""
  129. self.stderrdata = ""
  130. else:
  131. retcode, out, err = run_powershell_script(CHECK_FIREWALL_SCRIPT)
  132. self.returncode = retcode
  133. self.stdoutdata = out
  134. self.stderrdata = err
  135. def check_result(self):
  136. if self.returncode != 0:
  137. print_warning_msg("Unable to check firewall status:{0}".format(self.stderrdata))
  138. return False
  139. profiles_status = [i for i in self.stdoutdata.split("\n") if not i == ""]
  140. if "1" in profiles_status:
  141. enabled_profiles = []
  142. if profiles_status[0] == "1":
  143. enabled_profiles.append("DomainProfile")
  144. if profiles_status[1] == "1":
  145. enabled_profiles.append("StandardProfile")
  146. if profiles_status[2] == "1":
  147. enabled_profiles.append("PublicProfile")
  148. print_warning_msg(
  149. "Following firewall profiles are enabled:{0}. Make sure that the firewall is properly configured.".format(
  150. ",".join(enabled_profiles)))
  151. return True
  152. return False