使用 SaltStack 管理 IPtables
IPtables management with SaltStack
我正在尝试使用 SaltStack 配置一个灵活的 iptables 管理解决方案,但我发现它比我想象的要难。
我的主要要求:能够有一个支柱,我在其中保留一个 IP 列表,应该将其列入白名单,以便在所有 minions 上进行 SSH 访问。这个 IP 列表当然会不时更改:添加一些 IP,删除一些 IP。我面临的问题是已删除的 IP - 当我从 pillar 文件中删除它们时,SaltStack 不会从 minions 中删除实际的白名单。
我能找到的唯一解决方法是创建一个名为 "removed-ips" 的新密钥,每当我想删除 IP 时,我都会将其添加到那里。然后第二个 for 循环将删除它。当然,这是一个非常糟糕的解决方法,有更好的方法吗?
/srv/pillar/iptables-default.sls:
iptables-default:
whitelisted-ips:
- '55.55.55.55'
- '66.66.66.66'
- '77.77.77.77'
removed-ips:
- '88.88.88.88'
/srv/salt/iptables-default.sls:
{% for ip in salt['pillar.get']('iptables-default:whitelisted-ips') %}
Whitelist OSF IP {{ip}} for SSH access:
iptables.append:
- table: filter
- family: ipv4
- chain: INPUT
- jump: ACCEPT
- match: state
- connstate: NEW
- source: '{{ ip }}'
- dport: 22
- proto: tcp
- save: True
{% endfor %}
{% for ip in salt['pillar.get']('iptables-default:removed-ips') %}
Remove old IPs that are not needed anymore:
iptables.delete:
- table: filter
- family: ipv4
- chain: INPUT
- jump: ACCEPT
- match: state
- connstate: NEW
- source: {{ ip }}
- dport: 22
- proto: tcp
- save: True
{% endfor %}
在附加基于支柱数据的规则之前刷新所有规则。因此,添加一个刷新状态,并在添加规则的所有其他状态中要求该状态 - 这确保在添加规则之前刷新运行一次。
未经测试的例子:
flush_all_rules:
iptables.flush:
- table: filter
- family: ipv4
{% for ip in salt['pillar.get']('iptables-default:whitelisted-ips') %}
Whitelist OSF IP {{ip}} for SSH access:
iptables.append:
- table: filter
- family: ipv4
# [...]
- require:
- iptables: flush_all_rules
{% endfor %}
我喜欢管理 /etc/iptables/rules.v4 和 v6,而不是使用 salt 的 iptables 状态,就像这样:
firewall-ipv4:
pkg.installed:
- pkgs:
- iptables
- iptables-persistent
file.managed:
- name: /etc/iptables/rules.v4
- source: salt://firewall/files/rules.jinja
- template: jinja
- context:
slspath: {{ slspath }}
family: ipv4
cmd.wait:
- name: iptables-restore rules.v4
- cwd: /etc/iptables
- order: last
- watch:
- file: firewall-ipv4
{{ similar for v6... }}
其中 rules.jinja 从支柱生成规则集。这种方法的好处是,当支柱规则被移除时,它会做正确的事情,而不需要在每个高状态上刷新(即改变)。缺点是它不会注意到并恢复从本地计算机对防火墙的手动更改。
我有一个使用技巧 here 的公式。忽略关于兼容性问题的自述文件说明,它在当前 salt 上运行良好。或者我上次检查过。
我正在尝试使用 SaltStack 配置一个灵活的 iptables 管理解决方案,但我发现它比我想象的要难。
我的主要要求:能够有一个支柱,我在其中保留一个 IP 列表,应该将其列入白名单,以便在所有 minions 上进行 SSH 访问。这个 IP 列表当然会不时更改:添加一些 IP,删除一些 IP。我面临的问题是已删除的 IP - 当我从 pillar 文件中删除它们时,SaltStack 不会从 minions 中删除实际的白名单。
我能找到的唯一解决方法是创建一个名为 "removed-ips" 的新密钥,每当我想删除 IP 时,我都会将其添加到那里。然后第二个 for 循环将删除它。当然,这是一个非常糟糕的解决方法,有更好的方法吗?
/srv/pillar/iptables-default.sls:
iptables-default:
whitelisted-ips:
- '55.55.55.55'
- '66.66.66.66'
- '77.77.77.77'
removed-ips:
- '88.88.88.88'
/srv/salt/iptables-default.sls:
{% for ip in salt['pillar.get']('iptables-default:whitelisted-ips') %}
Whitelist OSF IP {{ip}} for SSH access:
iptables.append:
- table: filter
- family: ipv4
- chain: INPUT
- jump: ACCEPT
- match: state
- connstate: NEW
- source: '{{ ip }}'
- dport: 22
- proto: tcp
- save: True
{% endfor %}
{% for ip in salt['pillar.get']('iptables-default:removed-ips') %}
Remove old IPs that are not needed anymore:
iptables.delete:
- table: filter
- family: ipv4
- chain: INPUT
- jump: ACCEPT
- match: state
- connstate: NEW
- source: {{ ip }}
- dport: 22
- proto: tcp
- save: True
{% endfor %}
在附加基于支柱数据的规则之前刷新所有规则。因此,添加一个刷新状态,并在添加规则的所有其他状态中要求该状态 - 这确保在添加规则之前刷新运行一次。
未经测试的例子:
flush_all_rules:
iptables.flush:
- table: filter
- family: ipv4
{% for ip in salt['pillar.get']('iptables-default:whitelisted-ips') %}
Whitelist OSF IP {{ip}} for SSH access:
iptables.append:
- table: filter
- family: ipv4
# [...]
- require:
- iptables: flush_all_rules
{% endfor %}
我喜欢管理 /etc/iptables/rules.v4 和 v6,而不是使用 salt 的 iptables 状态,就像这样:
firewall-ipv4:
pkg.installed:
- pkgs:
- iptables
- iptables-persistent
file.managed:
- name: /etc/iptables/rules.v4
- source: salt://firewall/files/rules.jinja
- template: jinja
- context:
slspath: {{ slspath }}
family: ipv4
cmd.wait:
- name: iptables-restore rules.v4
- cwd: /etc/iptables
- order: last
- watch:
- file: firewall-ipv4
{{ similar for v6... }}
其中 rules.jinja 从支柱生成规则集。这种方法的好处是,当支柱规则被移除时,它会做正确的事情,而不需要在每个高状态上刷新(即改变)。缺点是它不会注意到并恢复从本地计算机对防火墙的手动更改。
我有一个使用技巧 here 的公式。忽略关于兼容性问题的自述文件说明,它在当前 salt 上运行良好。或者我上次检查过。