Saltstack Official UFW Formula
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

164 lines
4.7KB

  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):
  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. real_cmd = ' '.join(cmd)
  57. return real_cmd
  58. def enabled(name, **kwargs):
  59. if __salt__['ufw.is_enabled']():
  60. return _unchanged(name, "UFW is already enabled")
  61. if __opts__['test']:
  62. return _test(name, "UFW will be enabled")
  63. try:
  64. __salt__['ufw.set_enabled'](True)
  65. except (CommandExecutionError, CommandNotFoundError) as e:
  66. return _error(name, e.message)
  67. return _changed(name, "UFW is enabled", enabled=True)
  68. def default_incoming(name, default):
  69. rule = "default {0} incoming".format(default)
  70. if __opts__['test']:
  71. return _test(name, "{0}: {1}".format(name, rule))
  72. current = __salt__['ufw.get_default_incoming']()
  73. if default != current:
  74. try:
  75. out = __salt__['ufw.add_rule'](rule)
  76. except (CommandExecutionError, CommandNotFoundError) as e:
  77. return _error(name, e.message)
  78. for line in out.split('\n'):
  79. if line.startswith("Default incoming policy changed to"):
  80. return _changed(name, "{0} set to {1}".format(name, default), rule=rule)
  81. return _error(name, line)
  82. return _unchanged(name, "{0} was already set to {1}".format(name, default))
  83. def default_outgoing(name, default):
  84. rule = "default {0} outgoing".format(default)
  85. if __opts__['test']:
  86. return _test(name, "{0}: {1}".format(name, rule))
  87. current = __salt__['ufw.get_default_outgoing']()
  88. if default != current:
  89. try:
  90. out = __salt__['ufw.add_rule'](rule)
  91. except (CommandExecutionError, CommandNotFoundError) as e:
  92. return _error(name, e.message)
  93. for line in out.split('\n'):
  94. if line.startswith("Default outgoing policy changed to"):
  95. return _changed(name, "{0} set to {1}".format(name, default), rule=rule)
  96. return _error(name, line)
  97. return _unchanged(name, "{0} was already set to {1}".format(name, default))
  98. def allowed(name, app=None, interface=None, protocol=None,
  99. from_addr=None, from_port=None, to_addr=None, to_port=None):
  100. rule = _as_rule("allow", app=app, interface=interface, protocol=protocol,
  101. from_addr=from_addr, from_port=from_port, to_addr=to_addr, to_port=to_port)
  102. if __opts__['test']:
  103. return _test(name, "{0}: {1}".format(name, rule))
  104. try:
  105. out = __salt__['ufw.add_rule'](rule)
  106. except (CommandExecutionError, CommandNotFoundError) as e:
  107. return _error(name, e.message)
  108. changes = False
  109. for line in out.split('\n'):
  110. if line.startswith("Skipping"):
  111. continue
  112. if line.startswith("Rule added") or line.startswith("Rules updated"):
  113. changes = True
  114. break
  115. return _error(name, line)
  116. if changes:
  117. return _changed(name, "{0} allowed".format(name), rule=rule)
  118. else:
  119. return _unchanged(name, "{0} was already allowed".format(name))