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.

118 lines
3.3KB

  1. {% from "iptables/map.jinja" import service with context %}
  2. {%- if grains.get('virtual_subtype', None) not in ['Docker', 'LXC'] %}
  3. {%- set chains = service.get('chain', {}).keys() %}
  4. {%- for chain_name, chain in service.get('chain', {}).items() %}
  5. {%- set tables = [] %}
  6. {%- for rule in chain.get('rules', []) %}
  7. {%- set table = rule.get('table', 'filter') %}
  8. {%- if table not in tables %}
  9. {%- do tables.append(table) %}
  10. {%- endif %}
  11. {%- endfor %}
  12. {%- if chain.policy is defined %}
  13. {%- if chain.policy is string %}
  14. {%- if 'filter' not in tables %}
  15. {%- do tables.append('filter') %}
  16. {%- endif %}
  17. {%- else %}
  18. {%- for policy in chain.policy %}
  19. {%- if policy.table not in tables %}
  20. {%- do tables.append(policy.table) %}
  21. {%- endif %}
  22. {%- endfor %}
  23. {%- endif %}
  24. {%- endif %}
  25. {%- for table in tables %}
  26. iptables_{{ table }}_{{ chain_name }}:
  27. iptables.chain_present:
  28. - family: ipv4
  29. - name: {{ chain_name }}
  30. - table: {{ table }}
  31. - require:
  32. - pkg: iptables_packages
  33. {%- if grains.ipv6|default(False) and service.ipv6|default(True) %}
  34. iptables_{{ table }}_{{ chain_name }}_ipv6:
  35. iptables.chain_present:
  36. - family: ipv6
  37. - name: {{ chain_name }}
  38. - table: {{ table }}
  39. - require:
  40. - pkg: iptables_packages
  41. {%- if chain.policy is defined %}
  42. {%- if chain.policy is string %}
  43. - require_in:
  44. - iptables: iptables_filter_{{ chain_name }}_ipv6_policy
  45. {%- else %}
  46. {%- if table in chain.policy %}
  47. - require_in:
  48. - iptables: iptables_filter_{{ chain_name }}_ipv6_policy
  49. {%- endif %}
  50. {%- endif %}
  51. {%- endif %}
  52. {%- endif %}
  53. {%- endfor %}
  54. {%- if chain.policy is defined %}
  55. {%- if chain.policy is string %}
  56. {%- set map = [{'table':'filter', 'policy':chain.policy}] %}
  57. {%- else %}
  58. {%- set map = chain.policy %}
  59. {%- endif %}
  60. {%- for policy in map %}
  61. iptables_{{ policy.table }}_{{ chain_name }}_policy:
  62. iptables.set_policy:
  63. - family: ipv4
  64. - chain: {{ chain_name }}
  65. - policy: {{ policy.policy }}
  66. - table: {{ policy.table }}
  67. - require:
  68. - iptables: iptables_{{ policy.table }}_{{ chain_name }}
  69. {%- if grains.ipv6|default(False) and service.ipv6|default(True) %}
  70. iptables_{{ policy.table }}_{{ chain_name }}_ipv6_policy:
  71. iptables.set_policy:
  72. - family: ipv6
  73. - chain: {{ chain_name }}
  74. - policy: {{ policy.policy }}
  75. - table: {{ policy.table }}
  76. - require:
  77. - iptables: iptables_{{ policy.table }}_{{ chain_name }}_ipv6
  78. {%- endif %}
  79. {%- endfor %}
  80. {%- endif %}
  81. {%- for service_name, service in pillar.items() %}
  82. {%- if service is mapping %}
  83. {%- if service.get('_support', {}).get('iptables', {}).get('enabled', False) %}
  84. {%- set grains_fragment_file = service_name+'/meta/iptables.yml' %}
  85. {%- macro load_grains_file() %}{% include grains_fragment_file %}{% endmacro %}
  86. {%- set grains_yaml = load_grains_file()|load_yaml %}
  87. {%- if grains_yaml is iterable %}
  88. {%- if grains_yaml.get('iptables',{}).rules is defined %}
  89. {%- for rule in grains_yaml.iptables.rules %}
  90. {%- set rule_name = service_name+'_'+loop.index|string %}
  91. {% include "iptables/_rule.sls" %}
  92. {%- endfor %}
  93. {%- endif %}
  94. {%- endif %}
  95. {%- endif %}
  96. {%- endif %}
  97. {%- endfor %}
  98. {%- for rule in chain.get('rules', []) %}
  99. {%- set rule_name = loop.index %}
  100. {% include "iptables/_rule.sls" %}
  101. {%- endfor %}
  102. {%- endfor %}
  103. {%- endif %}