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.

162 lines
3.7KB

  1. # UFW management module
  2. {%- set ufw = pillar.get('ufw', {}) %}
  3. {%- if ufw.get('enabled', False) %}
  4. {% from "ufw/map.jinja" import ufwmap with context %}
  5. {% set default_template = ufw.get('default_template', 'salt://ufw/templates/default.jinja') -%}
  6. {% set sysctl_template = ufw.get('sysctl_template', 'salt://ufw/templates/sysctl.jinja') -%}
  7. ufw:
  8. pkg.installed:
  9. - name: {{ ufwmap.pkg }}
  10. service.running:
  11. - enable: True
  12. - watch:
  13. - file: /etc/default/ufw
  14. - file: /etc/ufw/sysctl.conf
  15. /etc/default/ufw:
  16. file.managed:
  17. - template: jinja
  18. - user: root
  19. - group: root
  20. - mode: 644
  21. - source: {{ default_template }}
  22. /etc/ufw/sysctl.conf:
  23. file.managed:
  24. - template: jinja
  25. - user: root
  26. - group: root
  27. - mode: 644
  28. - source: {{ sysctl_template }}
  29. /etc/ufw/applications.d:
  30. file.recurse:
  31. - user: root
  32. - group: root
  33. - file_mode: 644
  34. - clean: False
  35. - source: salt://ufw/files/applications.d
  36. # services
  37. {%- for service_name, service_details in ufw.get('services', {}).items() %}
  38. {%- for from_addr in service_details.get('from_addr', [None]) %}
  39. {%- set protocol = service_details.get('protocol', None) %}
  40. {%- set from_port = service_details.get('from_port', None) %}
  41. {%- set to_addr = service_details.get('to_addr', None) %}
  42. {%- set comment = service_details.get('comment', None) %}
  43. ufw-svc-{{service_name}}-{{from_addr}}:
  44. ufw.allowed:
  45. {%- if protocol != None %}
  46. - protocol: {{protocol}}
  47. {%- endif %}
  48. {%- if from_addr != None %}
  49. - from_addr: {{from_addr}}
  50. {%- endif %}
  51. {%- if from_port != None %}
  52. - from_port: "{{from_port}}"
  53. {%- endif %}
  54. {%- if to_addr != None %}
  55. - to_addr: {{to_addr}}
  56. {%- endif %}
  57. {%- if comment != None %}
  58. - comment: '"{{comment}}"'
  59. {%- endif %}
  60. - to_port: "{{service_name}}"
  61. - require:
  62. - pkg: ufw
  63. - listen_in:
  64. - cmd: reload-ufw
  65. {%- endfor %}
  66. {%- endfor %}
  67. # Applications
  68. {%- for app_name, app_details in ufw.get('applications', {}).items() %}
  69. {%- for from_addr in app_details.get('from_addr', [None]) %}
  70. {%- set to_addr = app_details.get('to_addr', None) %}
  71. {%- set comment = app_details.get('comment', None) %}
  72. {%- if from_addr != None%}
  73. ufw-app-{{app_name}}-{{from_addr}}:
  74. {%- else %}
  75. ufw-app-{{app_name}}:
  76. {%- endif %}
  77. ufw.allowed:
  78. - app: '"{{app_name}}"'
  79. {%- if from_addr != None %}
  80. - from_addr: {{from_addr}}
  81. {%- endif %}
  82. {%- if to_addr != None %}
  83. - to_addr: {{to_addr}}
  84. {%- endif %}
  85. {%- if comment != None %}
  86. - comment: '"{{comment}}"'
  87. {%- endif %}
  88. - require:
  89. - pkg: ufw
  90. - listen_in:
  91. - cmd: reload-ufw
  92. {%- endfor %}
  93. {%- endfor %}
  94. # Interfaces
  95. {%- for interface_name, interface_details in ufw.get('interfaces', {}).items() %}
  96. {%- set comment = interface_details.get('comment', None) %}
  97. ufw-interface-{{interface_name}}:
  98. ufw.allowed:
  99. - interface: {{interface_name}}
  100. {%- if comment != None %}
  101. - comment: '"{{comment}}"'
  102. {%- endif %}
  103. - require:
  104. - pkg: ufw
  105. - listen_in:
  106. - cmd: reload-ufw
  107. {%- endfor %}
  108. # Open
  109. {%- for open_addr, open_details in ufw.get('open', {}).items() %}
  110. {%- set comment = open_details.get('comment', None) %}
  111. ufw-open-{{open_addr}}:
  112. ufw.allowed:
  113. - from_addr: {{open_addr}}
  114. {%- if comment != None %}
  115. - comment: '"{{comment}}"'
  116. {%- endif %}
  117. - require:
  118. - pkg: ufw
  119. - listen_in:
  120. - cmd: reload-ufw
  121. {%- endfor %}
  122. enable-ufw:
  123. ufw.enabled:
  124. - require:
  125. - pkg: ufw
  126. reload-ufw:
  127. cmd.wait:
  128. - name: ufw reload
  129. disable-logging:
  130. cmd.run:
  131. - name: ufw logging off
  132. - unless: "grep 'LOGLEVEL=off' /etc/ufw/ufw.conf"
  133. {% else %}
  134. #ufw:
  135. #ufw:
  136. #- disabled
  137. {% endif %}