firewall.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 self.OS_TYPE == OSConst.OS_UBUNTU:
  39. return UbuntuFirewallChecks()
  40. elif self.OS_TYPE == OSConst.OS_FEDORA and int(self.OS_VERSION) >= 18:
  41. return Fedora18FirewallChecks()
  42. elif self.OS_FAMILY == OSConst.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 iptables...'
  57. def get_command(self):
  58. return "%s %s %s" % (self.SERVICE_CMD, self.FIREWALL_SERVICE_NAME, self.SERVICE_SUBCMD)
  59. def check_result(self):
  60. result = False
  61. if self.returncode == 3:
  62. result = False
  63. elif self.returncode == 0:
  64. if "Table: filter" in self.stdoutdata:
  65. result = True
  66. return result
  67. def run_command(self):
  68. retcode, out, err = run_os_command(self.get_command())
  69. self.returncode = retcode
  70. self.stdoutdata = out
  71. self.stderrdata = err
  72. def check_iptables(self):
  73. try:
  74. self.run_command()
  75. return self.check_result()
  76. except OSError:
  77. return False
  78. class UbuntuFirewallChecks(FirewallChecks):
  79. def __init__(self):
  80. super(UbuntuFirewallChecks, self).__init__()
  81. self.FIREWALL_SERVICE_NAME = "ufw"
  82. def get_command(self):
  83. return "%s %s" % (self.FIREWALL_SERVICE_NAME, self.SERVICE_SUBCMD)
  84. def check_result(self):
  85. # On ubuntu, the status command returns 0 whether running or not
  86. result = False
  87. if self.returncode == 0:
  88. if "Status: inactive" in self.stdoutdata:
  89. result = False
  90. elif "Status: active" in self.stdoutdata:
  91. result = True
  92. return result
  93. class Fedora18FirewallChecks(FirewallChecks):
  94. def __init__(self):
  95. super(Fedora18FirewallChecks, self).__init__()
  96. def get_command(self):
  97. return "systemctl is-active %s" % (self.FIREWALL_SERVICE_NAME)
  98. def check_result(self):
  99. result = False
  100. if self.returncode == 0:
  101. if "active" in self.stdoutdata:
  102. result = True
  103. return result
  104. class SuseFirewallChecks(FirewallChecks):
  105. def __init__(self):
  106. super(SuseFirewallChecks, self).__init__()
  107. self.FIREWALL_SERVICE_NAME = "SuSEfirewall2"
  108. def get_command(self):
  109. return "%s %s" % (self.FIREWALL_SERVICE_NAME, self.SERVICE_SUBCMD)
  110. def check_result(self):
  111. result = False
  112. if self.returncode == 0:
  113. if "SuSEfirewall2 not active" in self.stdoutdata:
  114. result = False
  115. elif "### iptables" in self.stdoutdata:
  116. result = True
  117. return result
  118. class WindowsFirewallChecks(FirewallChecks):
  119. def __init__(self):
  120. super(WindowsFirewallChecks, self).__init__()
  121. self.MESSAGE_CHECK_FIREWALL = 'Checking firewall status...'
  122. def run_command(self):
  123. from ambari_commons.os_windows import run_powershell_script, CHECK_FIREWALL_SCRIPT
  124. retcode, out, err = run_powershell_script(CHECK_FIREWALL_SCRIPT)
  125. self.returncode = retcode
  126. self.stdoutdata = out
  127. self.stderrdata = err
  128. def check_result(self):
  129. if self.returncode != 0:
  130. print_warning_msg("Unable to check firewall status:{0}".format(self.stderrdata))
  131. return False
  132. profiles_status = [i for i in self.stdoutdata.split("\n") if not i == ""]
  133. if "1" in profiles_status:
  134. enabled_profiles = []
  135. if profiles_status[0] == "1":
  136. enabled_profiles.append("DomainProfile")
  137. if profiles_status[1] == "1":
  138. enabled_profiles.append("StandardProfile")
  139. if profiles_status[2] == "1":
  140. enabled_profiles.append("PublicProfile")
  141. print_warning_msg(
  142. "Following firewall profiles are enabled:{0}. Make sure that the firewall is properly configured.".format(
  143. ",".join(enabled_profiles)))
  144. return False
  145. return True