Saltstack Official UFW Formula
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

172 行
5.0KB

  1. from salt.exceptions import CommandExecutionError, CommandNotFoundError
  2. import re
  3. import socket
  4. def _unchanged(name, msg):
  5. return {'name': name, 'result': True, 'comment': msg, 'changes': {}}
  6. def _test(name, msg):
  7. return {'name': name, 'result': None, 'comment': msg, 'changes': {}}
  8. def _error(name, msg):
  9. return {'name': name, 'result': False, 'comment': msg, 'changes': {}}
  10. def _changed(name, msg, **changes):
  11. return {'name': name, 'result': True, 'comment': msg, 'changes': changes}
  12. def _resolve(host):
  13. # let's just see if it starts with a number or a colon, for simplicity
  14. if re.match(r'^[0-9:]', host):
  15. return host
  16. return socket.gethostbyname(host)
  17. def _as_rule(method, app, interface, protocol, from_addr, from_port, to_addr, to_port, comment):
  18. cmd = [method]
  19. if app is not None:
  20. cmd.append("from")
  21. if from_addr is not None:
  22. cmd.append(from_addr)
  23. else:
  24. cmd.append("any")
  25. cmd.append("to")
  26. if to_addr is not None:
  27. cmd.append(to_addr)
  28. else:
  29. cmd.append("any")
  30. cmd.append("app")
  31. cmd.append(app)
  32. elif interface is not None:
  33. cmd.append("in")
  34. cmd.append("on")
  35. cmd.append(interface)
  36. else:
  37. if protocol is not None:
  38. cmd.append("proto")
  39. cmd.append(protocol)
  40. cmd.append("from")
  41. if from_addr is not None:
  42. cmd.append(_resolve(from_addr))
  43. else:
  44. cmd.append("any")
  45. if from_port is not None:
  46. cmd.append("port")
  47. cmd.append(_resolve(from_port))
  48. cmd.append("to")
  49. if to_addr is not None:
  50. cmd.append(to_addr)
  51. else:
  52. cmd.append("any")
  53. if to_port is not None:
  54. cmd.append("port")
  55. cmd.append(to_port)
  56. if comment is not None:
  57. cmd.append("comment")
  58. cmd.append(comment)
  59. real_cmd = ' '.join(cmd)
  60. return real_cmd
  61. def enabled(name, **kwargs):
  62. if __salt__['ufw.is_enabled']():
  63. return _unchanged(name, "UFW is already enabled")
  64. try:
  65. __salt__['ufw.set_enabled'](True)
  66. except (CommandExecutionError, CommandNotFoundError) as e:
  67. return _error(name, e.message)
  68. if __opts__['test']:
  69. return _test(name, "UFW would have been enabled")
  70. else:
  71. return _changed(name, "UFW is enabled", enabled=True)
  72. def default_incoming(name, default):
  73. rule = "default {0} incoming".format(default)
  74. if __opts__['test']:
  75. return _test(name, "{0}: {1}".format(name, rule))
  76. current = __salt__['ufw.get_default_incoming']()
  77. if default != current:
  78. try:
  79. out = __salt__['ufw.add_rule'](rule)
  80. except (CommandExecutionError, CommandNotFoundError) as e:
  81. return _error(name, e.message)
  82. for line in out.split('\n'):
  83. if line.startswith("Default incoming policy changed to"):
  84. return _changed(name, "{0} set to {1}".format(name, default), rule=rule)
  85. return _error(name, line)
  86. return _unchanged(name, "{0} was already set to {1}".format(name, default))
  87. def default_outgoing(name, default):
  88. rule = "default {0} outgoing".format(default)
  89. if __opts__['test']:
  90. return _test(name, "{0}: {1}".format(name, rule))
  91. current = __salt__['ufw.get_default_outgoing']()
  92. if default != current:
  93. try:
  94. out = __salt__['ufw.add_rule'](rule)
  95. except (CommandExecutionError, CommandNotFoundError) as e:
  96. return _error(name, e.message)
  97. for line in out.split('\n'):
  98. if line.startswith("Default outgoing policy changed to"):
  99. return _changed(name, "{0} set to {1}".format(name, default), rule=rule)
  100. return _error(name, line)
  101. return _unchanged(name, "{0} was already set to {1}".format(name, default))
  102. def allowed(name, app=None, interface=None, protocol=None,
  103. from_addr=None, from_port=None, to_addr=None, to_port=None, comment=None):
  104. rule = _as_rule("allow", app=app, interface=interface, protocol=protocol,
  105. from_addr=from_addr, from_port=from_port, to_addr=to_addr, to_port=to_port, comment=comment)
  106. try:
  107. out = __salt__['ufw.add_rule'](rule)
  108. except (CommandExecutionError, CommandNotFoundError) as e:
  109. return _error(name, e.message)
  110. changes = False
  111. for line in out.split('\n'):
  112. if line.startswith("Skipping"):
  113. if __opts__['test']:
  114. return _unchanged(name, "{0} was already allowed".format(name))
  115. break
  116. else:
  117. continue
  118. if line.startswith("Rule added") or line.startswith("Rules updated"):
  119. changes = True
  120. break
  121. if __opts__['test']:
  122. return _test(name, "{0} would have been allowed".format(name))
  123. break
  124. return _error(name, line)
  125. if changes:
  126. return _changed(name, "{0} allowed".format(name), rule=rule)
  127. else:
  128. return _unchanged(name, "{0} was already allowed".format(name))