|
| 1 | +# Copyright 2018 dhtech |
| 2 | +# |
| 3 | +# Use of this source code is governed by a BSD-style |
| 4 | +# license that can be found in the LICENSE file |
| 5 | +# |
| 6 | +# == Class: iptables |
| 7 | +# |
| 8 | +# Firewall hooks for the firewall lib. |
| 9 | +# |
| 10 | +# === Parameters |
| 11 | +# |
| 12 | +# [*rules*] |
| 13 | +# The host specific rules for this machine as calculated from ipplan. |
| 14 | +# |
| 15 | +# [*log_fallthrough*] |
| 16 | +# Log the packets that will be policy dropped in the INPUT chain. |
| 17 | +# |
| 18 | +# [*chains*] |
| 19 | +# A hash containing chains with their default policy. Defaults to |
| 20 | +# ``` |
| 21 | +# { |
| 22 | +# 'INPUT' => 'DROP', |
| 23 | +# 'FORWARD' => 'DROP', |
| 24 | +# 'OUTPUT' => 'ACCEPT', |
| 25 | +# } |
| 26 | +# ``` |
| 27 | +# [*ipv4file*] |
| 28 | +# The file to store the IPv4 rules in. Defaults to |
| 29 | +# `/etc/iptables/rules.v4.puppet` |
| 30 | +# |
| 31 | +# [*ipv6file*] |
| 32 | +# The file to store the IPv6 rules in. Defaults to |
| 33 | +# `/etc/iptables/rules.v6.puppet` |
| 34 | + |
| 35 | +class iptables::ng ( |
| 36 | + |
| 37 | + Hash $rules, |
| 38 | + Boolean $log_fallthrough, |
| 39 | + Hash[String, Enum['ACCEPT', 'DROP', 'REJECT'], 1] $chains = { |
| 40 | + 'INPUT' => 'DROP', |
| 41 | + 'FORWARD' => 'DROP', |
| 42 | + 'OUTPUT' => 'ACCEPT', |
| 43 | + }, |
| 44 | + String $ipv4file = '/etc/iptables/rules.v4.puppet', |
| 45 | + String $ipv6file = '/etc/iptables/rules.v6.puppet', |
| 46 | + |
| 47 | +) { |
| 48 | + |
| 49 | + $chains_header = $chains.map |$chain,$policy| { sprintf(":%s %s [0:0]", $chain, $policy) } |
| 50 | + |
| 51 | + $enforce_command = '/usr/local/sbin/enforce-iptables' |
| 52 | + file { 'enforce-command': |
| 53 | + path => $enforce_command, |
| 54 | + source => 'puppet:///scripts/iptables/enforce-iptables.sh', |
| 55 | + owner => 'root', |
| 56 | + group => 'root', |
| 57 | + mode => '0750', |
| 58 | + } |
| 59 | + |
| 60 | + |
| 61 | + # Header and trailer rules |
| 62 | + class { 'iptables::ng::header': } |
| 63 | + class { 'iptables::ng::trailer': |
| 64 | + log_input => $log_fallthrough, |
| 65 | + } |
| 66 | + |
| 67 | + |
| 68 | + # IPv4 |
| 69 | + concat { $ipv4file: |
| 70 | + ensure => present, |
| 71 | + backup => true, |
| 72 | + warn => '# This file is managed by Puppet. Do not edit.', |
| 73 | + order => 'numeric', |
| 74 | + validate_cmd => '/usr/sbin/iptables-restore -t < %', |
| 75 | + ensure_newline => true, |
| 76 | + notify => Exec['enforce-puppet-iptables'], |
| 77 | + } |
| 78 | + |
| 79 | + concat::fragment { '00-ipv4-header': |
| 80 | + target => $ipv4file, |
| 81 | + order => 0, |
| 82 | + content => ([ |
| 83 | + '*filter' |
| 84 | + ] + $chains_header).join("\n"), |
| 85 | + } |
| 86 | + |
| 87 | + concat::fragment { '99-ipv4-trailer': |
| 88 | + target => $ipv4file, |
| 89 | + order => 9999, |
| 90 | + content => [ |
| 91 | + 'COMMIT' |
| 92 | + ].join("\n"), |
| 93 | + } |
| 94 | + |
| 95 | + exec { 'enforce-puppet-iptables': |
| 96 | + command => "/usr/bin/echo ${enforce_command} ipv4 '${ipv4file}'", |
| 97 | + refreshonly => true, |
| 98 | + require => File['enforce-command'], |
| 99 | + } |
| 100 | + |
| 101 | + each($rules['v4']) |$rule| { |
| 102 | + $name = $rule['name'] |
| 103 | + $proto = $rule['proto'] |
| 104 | + |
| 105 | + iptables::ng::rule { "v4 ${name} ${proto}": |
| 106 | + type => 'ipv4', |
| 107 | + chain => 'INPUT', |
| 108 | + action => 'ACCEPT', |
| 109 | + order => 500, |
| 110 | + source => $rule['src'], |
| 111 | + proto => $rule['proto'], |
| 112 | + dport => $rule['dports'], |
| 113 | + sport => $rule['sports'], |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + |
| 118 | + # IPv6 |
| 119 | + concat { $ipv6file: |
| 120 | + ensure => present, |
| 121 | + backup => true, |
| 122 | + warn => '# This file is managed by Puppet. Do not edit.', |
| 123 | + order => numeric, |
| 124 | + validate_cmd => '/usr/sbin/ip6tables-restore -t < %', |
| 125 | + ensure_newline => true, |
| 126 | + notify => Exec['enforce-puppet-ip6tables'], |
| 127 | + } |
| 128 | + |
| 129 | + concat::fragment { '00-ipv6-header': |
| 130 | + target => $ipv6file, |
| 131 | + order => 0, |
| 132 | + content => ([ |
| 133 | + '*filter' |
| 134 | + ] + $chains_header).join("\n"), |
| 135 | + } |
| 136 | + |
| 137 | + concat::fragment { '99-ipv6-trailer': |
| 138 | + target => $ipv6file, |
| 139 | + order => 9999, |
| 140 | + content => [ |
| 141 | + 'COMMIT', |
| 142 | + ].join("\n"), |
| 143 | + } |
| 144 | + |
| 145 | + exec { 'enforce-puppet-ip6tables': |
| 146 | + command => "/usr/bin/echo ${enforce_command} ipv6 '${ipv6file}'", |
| 147 | + refreshonly => true, |
| 148 | + require => File['enforce-command'], |
| 149 | + } |
| 150 | + |
| 151 | + each($rules['v6']) |$rule| { |
| 152 | + $name = $rule['name'] |
| 153 | + $proto = $rule['proto'] |
| 154 | + |
| 155 | + iptables::ng::rule { "v6 ${name} ${proto}": |
| 156 | + type => 'ipv6', |
| 157 | + chain => 'INPUT', |
| 158 | + action => 'ACCEPT', |
| 159 | + order => 500, |
| 160 | + source => $rule['src'], |
| 161 | + proto => $rule['proto'], |
| 162 | + dport => $rule['dports'], |
| 163 | + sport => $rule['sports'], |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | +} |
0 commit comments