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.

176 lines
4.6KB

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