Saltstack Official IPTables 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.

129 lines
3.7KB

  1. {% from "iptables/map.jinja" import service with context %}
  2. {%- if grains.get('virtual_subtype', None) not in ['Docker', 'LXC'] %}
  3. {%- if grains.os_family == 'Debian' and service.get('provider') == "iptables-restore" %}
  4. /etc/iptables/rules.v4.tmp:
  5. file.managed:
  6. - source: salt://iptables/files/rules.v4
  7. - template: jinja
  8. - makedirs: True
  9. - defaults:
  10. chains: {{ service.get('chain', {}) }}
  11. - require:
  12. - pkg: iptables_packages
  13. - file: /usr/share/netfilter-persistent/plugins.d/15-ip4tables
  14. iptables-restore --test /etc/iptables/rules.v4.tmp:
  15. cmd.run:
  16. - onchanges:
  17. - file: /etc/iptables/rules.v4.tmp
  18. cp -a /etc/iptables/rules.v4.tmp /etc/iptables/rules.v4:
  19. cmd.run:
  20. - onchanges:
  21. - cmd: "iptables-restore --test /etc/iptables/rules.v4.tmp"
  22. - watch_in:
  23. - service: iptables_services
  24. cp -a /etc/iptables/rules.v4 /etc/iptables/rules.v4.tmp:
  25. cmd.run:
  26. - onfail:
  27. - cmd: "iptables-restore --test /etc/iptables/rules.v4.tmp"
  28. {%- if grains.ipv6|default(False) and service.ipv6|default(True) %}
  29. /etc/iptables/rules.v6.tmp:
  30. file.managed:
  31. - source: salt://iptables/files/rules.v6
  32. - template: jinja
  33. - makedirs: True
  34. - defaults:
  35. chains: {{ service.get('chain', {}) }}
  36. - require:
  37. - pkg: iptables_packages
  38. - file: /usr/share/netfilter-persistent/plugins.d/25-ip6tables
  39. - watch_in:
  40. - service: iptables_services
  41. ip6tables-restore --test /etc/iptables/rules.v6.tmp:
  42. cmd.run:
  43. - onchanges:
  44. - file: /etc/iptables/rules.v6.tmp
  45. cp -a /etc/iptables/rules.v6.tmp /etc/iptables/rules.v6:
  46. cmd.run:
  47. - onchanges:
  48. - cmd: "ip6tables-restore --test /etc/iptables/rules.v6.tmp"
  49. - watch_in:
  50. - service: iptables_services
  51. cp -a /etc/iptables/rules.v6 /etc/iptables/rules.v6.tmp:
  52. cmd.run:
  53. - onfail:
  54. - cmd: "ip6tables-restore --test /etc/iptables/rules.v6.tmp"
  55. {%- endif %}
  56. {%- else %}
  57. {%- for chain_name, chain in service.get('chain', {}).iteritems() %}
  58. iptables_{{ chain_name }}:
  59. iptables.chain_present:
  60. - family: ipv4
  61. - name: {{ chain_name }}
  62. - table: filter
  63. - require:
  64. - pkg: iptables_packages
  65. {%- if grains.ipv6|default(False) and service.ipv6|default(True) %}
  66. iptables_{{ chain_name }}_ipv6:
  67. iptables.chain_present:
  68. - family: ipv6
  69. - name: {{ chain_name }}
  70. - table: filter
  71. - require:
  72. - pkg: iptables_packages
  73. {%- if chain.policy is defined %}
  74. - require_in:
  75. - iptables: iptables_{{ chain_name }}_ipv6_policy
  76. {%- endif %}
  77. {%- endif %}
  78. {%- if chain.policy is defined %}
  79. iptables_{{ chain_name }}_policy:
  80. iptables.set_policy:
  81. - family: ipv4
  82. - chain: {{ chain_name }}
  83. - policy: {{ chain.policy }}
  84. - table: filter
  85. - require:
  86. - iptables: iptables_{{ chain_name }}
  87. {%- if grains.ipv6|default(False) and service.ipv6|default(True) %}
  88. iptables_{{ chain_name }}_ipv6_policy:
  89. iptables.set_policy:
  90. - family: ipv6
  91. - chain: {{ chain_name }}
  92. - policy: {{ chain.policy }}
  93. - table: filter
  94. - require:
  95. - iptables: iptables_{{ chain_name }}_ipv6
  96. {%- endif %}
  97. {%- endif %}
  98. {%- for service_name, service in pillar.items() %}
  99. {%- if service.get('_support', {}).get('iptables', {}).get('enabled', False) %}
  100. {%- set grains_fragment_file = service_name+'/meta/iptables.yml' %}
  101. {%- macro load_grains_file() %}{% include grains_fragment_file %}{% endmacro %}
  102. {%- set grains_yaml = load_grains_file()|load_yaml %}
  103. {%- for rule in grains_yaml.iptables.rules %}
  104. {%- set rule_name = service_name+'_'+loop.index|string %}
  105. {% include "iptables/_rule.sls" %}
  106. {%- endfor %}
  107. {%- endif %}
  108. {%- endfor %}
  109. {%- for rule in chain.get('rules', []) %}
  110. {%- set rule_name = loop.index %}
  111. {% include "iptables/_rule.sls" %}
  112. {%- endfor %}
  113. {%- endfor %}
  114. {%- endif %}
  115. {%- endif %}