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.

109 lines
2.9KB

  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. # pure IP address / netmask IPv4 or IPv6 ?
  14. if re.match(r'^([0-9\.](::))+(/[0-9]+)?$', host):
  15. return
  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(app)
  21. elif interface is not None:
  22. cmd.append("in")
  23. cmd.append("on")
  24. cmd.append(interface)
  25. else:
  26. if protocol is not None:
  27. cmd.append("proto")
  28. cmd.append(protocol)
  29. cmd.append("from")
  30. if from_addr is not None:
  31. cmd.append(_resolve(from_addr))
  32. else:
  33. cmd.append("any")
  34. if from_port is not None:
  35. cmd.append("port")
  36. cmd.append(_resolve(from_port))
  37. cmd.append("to")
  38. if to_addr is not None:
  39. cmd.append(to_addr)
  40. else:
  41. cmd.append("any")
  42. if to_port is not None:
  43. cmd.append("port")
  44. cmd.append(to_port)
  45. real_cmd = ' '.join(cmd)
  46. return real_cmd
  47. def enabled(name, **kwargs):
  48. if __salt__['ufw.is_enabled']():
  49. return _unchanged(name, "UFW is already enabled")
  50. if __opts__['test']:
  51. return _test(name, "UFW will be enabled")
  52. try:
  53. __salt__['ufw.set_enabled'](True)
  54. except (CommandExecutionError, CommandNotFoundError) as e:
  55. return _error(name, e.message)
  56. return _changed(name, "UFW is enabled", enabled=True)
  57. def allowed(name, app=None, interface=None, protocol=None,
  58. from_addr=None, from_port=None, to_addr=None, to_port=None):
  59. rule = _as_rule("allow", app=app, interface=interface, protocol=protocol,
  60. from_addr=from_addr, from_port=from_port, to_addr=to_addr, to_port=to_port)
  61. if __opts__['test']:
  62. return _test(name, "{0}: {1}".format(name, rule))
  63. try:
  64. out = __salt__['ufw.add_rule'](rule)
  65. except (CommandExecutionError, CommandNotFoundError) as e:
  66. return _error(name, e.message)
  67. changes = False
  68. for line in out.split('\n'):
  69. if line.startswith("Skipping"):
  70. continue
  71. if line.startswith("Rule added") or line.startswith("Rules updated"):
  72. changes = True
  73. break
  74. return _error(name, line)
  75. if changes:
  76. return _changed(name, "{0} allowed".format(name), rule=rule)
  77. else:
  78. return _unchanged(name, "{0} was already allowed".format(name))