class firewall { exec { 'minimal-firewall': path => ["/bin", "/sbin", "/usr/bin", "/usr/sbin"], command => "iptables -P INPUT DROP \ && iptables -P FORWARD DROP \ && iptables --flush \ && iptables -t nat --flush \ && iptables --delete-chain \ && iptables -P FORWARD DROP \ && iptables -P INPUT DROP \ && iptables -A INPUT -i lo --source 127.0.0.1 --destination 127.0.0.1 -j ACCEPT \ && iptables -A INPUT -m state --state \"ESTABLISHED,RELATED\" -j ACCEPT \ && iptables -A INPUT -p icmp --icmp-type destination-unreachable -j ACCEPT \ && iptables -A INPUT -p icmp --icmp-type time-exceeded -j ACCEPT \ && iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT \ && iptables -A INPUT -p icmp --icmp-type echo-reply -j ACCEPT \ && iptables -A INPUT -p tcp --dport ssh -j ACCEPT \ && iptables -A INPUT -j LOG -m limit --limit 40/minute \ && iptables -A INPUT -j DROP", } }
&&
operator. node 'www' { include firewall }
iptables -L
.iptables -L --line-numbers
command and look for the string mentioning ssh 7 ACCEPT tcp -- anywhere anywhere tcp dpt:ssh
. So we will insert the rule in position 7. define allowport ($protocol, $port) { exec { "/sbin/iptables -I INPUT 7 -p $protocol --dport $port -j ACCEPT": require => Exec['minimal-firewall'], } }
node 'www' { allowport { http: protocol => tcp, port => 80, } allowport { https: protocol => tcp, port => 443, } include firewall }
require => Exec['minimal-firewall']
node 'admin' { allowport { dns1: protocol => tcp, port => 53, } allowport { dns2: protocol => udp, port => 53, } allowport { http: protocol => tcp, port => 80, } allowport { webmin: protocol => tcp, port => 10000, } include firewall }
define dnat ($protocol, $from, $to) { exec { 'dnat-$protocol-$from-$to': command => "/sbin/iptables -t nat -A PREROUTING -d $hostname -p $protocol --dport $from -j DNAT --to-destination $to \ && /sbin/iptables -t nat -A OUTPUT -d $hostname -p $protocol --dport $from -j DNAT --to-destination $to ", require => Exec['minimal-firewall'], } }
node 'app' { allowport { https: protocol => tcp, port => 443, } allowport { tomcat: protocol => tcp, port => 8443, } dnat { java: protocol => tcp, from => 443, to => ':8443', } include firewall }
define dmzport ($protocol, $port) { exec { 'dmz-$protocol-$port': command => "/sbin/iptables -I INPUT 7 -s admin.example.com -p $protocol --dport $port -j ACCEPT \ && /sbin/iptables -I INPUT 7 -s www.example.com -p $protocol --dport $port -j ACCEPT \ && /sbin/iptables -I INPUT 7 -s mysql.example.com -p $protocol --dport $port -j ACCEPT \ && /sbin/iptables -I INPUT 7 -s app.example.com -p $protocol --dport $port -j ACCEPT", require => Exec['minimal-firewall'], } }
node 'mysql' { dmzport { mysql: protocol => tcp, port => 3306, } include firewall }
Source: https://habr.com/ru/post/111313/
All Articles