Saltstack Official UFW Formula
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

vor 6 Jahren
vor 6 Jahren
vor 6 Jahren
vor 6 Jahren
vor 6 Jahren
vor 6 Jahren
vor 6 Jahren
vor 6 Jahren
vor 6 Jahren
vor 6 Jahren
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. adds = False
  111. updates = False
  112. for line in out.split('\n'):
  113. if re.match('^Skipping', line):
  114. return _unchanged(name, "{0} is already configured".format(name))
  115. break
  116. if re.match('^Rule(s)? added', line):
  117. adds = True
  118. break
  119. if re.match('^Rule(s)? updated', line):
  120. updates = True
  121. break
  122. if __opts__['test']:
  123. return _test(name, "{0} would have been configured".format(name))
  124. break
  125. return _error(name, line)
  126. if adds:
  127. return _changed(name, "{0} added".format(name), rule=rule)
  128. elif updates:
  129. return _changed(name, "{0} updated".format(name), rule=rule)
  130. else:
  131. return _unchanged(name, "{0} was already configured".format(name))