Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / manifests / init.pp @ 11bf7237

Historique | Voir | Annoter | Télécharger (5,43 ko)

1 e17693e3 Steve Traylen
# @summary Configure nftables
2
#
3 03d9e7da Steve Traylen
# @example allow dns out and do not allow ntp out
4 e17693e3 Steve Traylen
#   class{'nftables:
5
#     out_ntp = false,
6
#     out_dns = true,
7 b3a7a6dd tr
#   }
8 e17693e3 Steve Traylen
#
9 03d9e7da Steve Traylen
# @example do not flush particular tables
10
# In this case ignoring the fail2ban maintained
11
# table
12
#   class{'nftables':
13
#     noflush_tables = ['inet-f2b-table'],
14
#   }
15
#
16 b3a7a6dd tr
# @param out_all
17 e17693e3 Steve Traylen
#   Allow all outbound connections. If `true` then all other
18
#   out parameters `out_ntp`, `out_dns`, ... will be assuemed
19
#   false.
20
#
21
# @param out_ntp
22
#   Allow outbound to ntp servers.
23
#
24
# @param out_http
25
#   Allow outbound to http servers.
26
#
27
# @param out_https
28
#   Allow outbound to https servers.
29
#
30
# @param out_https
31
#   Allow outbound to https servers.
32
#
33 79e9a23f Nacho Barrientos
# @param out_icmp
34
#   Allow outbound ICMPv4/v6 traffic.
35
#
36 e17693e3 Steve Traylen
# @param in_ssh
37
#   Allow inbound to ssh servers.
38
#
39 79e9a23f Nacho Barrientos
# @param in_icmp
40
#   Allow inbound ICMPv4/v6 traffic.
41
#
42 82d10659 Nacho Barrientos
# @param nat
43
#   Add default tables and chains to process NAT traffic.
44
#
45 802d80d1 Nacho Barrientos
# @param sets
46
#   Allows sourcing set definitions directly from Hiera.
47
#
48 ac0af4aa Nacho Barrientos
# @param log_prefix
49
#   String that will be used as prefix when logging packets. It can contain
50
#   two variables using standard sprintf() string-formatting:
51
#    * chain: Will be replaced by the name of the chain.
52
#    * comment: Allows chains to add extra comments.
53
#
54 b10c6216 Nacho Barrientos
# @param log_limit
55
#  String with the content of a limit statement to be applied
56
#  to the rules that log discarded traffic. Set to false to
57
#  disable rate limiting.
58
#
59 70727742 Nacho Barrientos
# @param reject_with
60
#   How to discard packets not matching any rule. If `false`, the
61
#   fate of the packet will be defined by the chain policy (normally
62
#   drop), otherwise the packet will be rejected with the REJECT_WITH
63
#   policy indicated by the value of this parameter.
64
#
65 ea96d5db Nacho Barrientos
# @param in_out_conntrack
66
#   Adds INPUT and OUTPUT rules to allow traffic that's part of an
67
#   established connection and also to drop invalid packets.
68
#
69 24a5a2a7 tr
# @param fwd_conntrack
70
#   Adds FORWARD rules to allow traffic that's part of an
71
#   established connection and also to drop invalid packets.
72
#
73 ae9872e2 Nacho Barrientos
# @param firewalld_enable
74
#   Configures how the firewalld systemd service unit is enabled. It might be
75
#   useful to set this to false if you're externaly removing firewalld from
76
#   the system completely.
77
#
78 03d9e7da Steve Traylen
# @param noflush_tables
79
#   If specified only other existings tables will be flushed.
80
#   If left unset all tables will be flushed via a `flush ruleset`
81
#
82 be0b08e1 tr
class nftables (
83 70727742 Nacho Barrientos
  Boolean $in_ssh                = true,
84 79e9a23f Nacho Barrientos
  Boolean $in_icmp               = true,
85 70727742 Nacho Barrientos
  Boolean $out_ntp               = true,
86
  Boolean $out_dns               = true,
87
  Boolean $out_http              = true,
88
  Boolean $out_https             = true,
89 79e9a23f Nacho Barrientos
  Boolean $out_icmp              = true,
90 70727742 Nacho Barrientos
  Boolean $out_all               = false,
91 ea96d5db Nacho Barrientos
  Boolean $in_out_conntrack      = true,
92 24a5a2a7 tr
  Boolean $fwd_conntrack         = false,
93 82d10659 Nacho Barrientos
  Boolean $nat                   = true,
94 70727742 Nacho Barrientos
  Hash $rules                    = {},
95 802d80d1 Nacho Barrientos
  Hash $sets                     = {},
96 ac0af4aa Nacho Barrientos
  String $log_prefix             = '[nftables] %<chain>s %<comment>s',
97 b10c6216 Nacho Barrientos
  Variant[Boolean[false], String]
98 11bf7237 Steve Traylen
  $log_limit                   = '3/minute burst 5 packets',
99 70727742 Nacho Barrientos
  Variant[Boolean[false], Pattern[
100 11bf7237 Steve Traylen
  /icmp(v6|x)? type .+|tcp reset/]]
101
  $reject_with                 = 'icmpx type port-unreachable',
102 ae9872e2 Nacho Barrientos
  Variant[Boolean[false], Enum['mask']]
103 11bf7237 Steve Traylen
  $firewalld_enable            = 'mask',
104 03d9e7da Steve Traylen
  Optional[Array[Pattern[/^(ip|ip6|inet)-[-a-zA-Z0-9_]+$/],1]]
105 11bf7237 Steve Traylen
  $noflush_tables = undef,
106 be0b08e1 tr
) {
107 11bf7237 Steve Traylen
  package { 'nftables':
108 0ba57c66 mh
    ensure => installed,
109 11bf7237 Steve Traylen
  } -> file_line {
110 0ba57c66 mh
    'enable_nftables':
111
      line   => 'include "/etc/nftables/puppet.nft"',
112
      path   => '/etc/sysconfig/nftables.conf',
113
      notify => Service['nftables'],
114 11bf7237 Steve Traylen
  } -> file {
115 0ba57c66 mh
    default:
116 e140adff tr
      owner => 'root',
117
      group => 'root',
118
      mode  => '0640';
119 30462da1 Steve Traylen
    '/etc/nftables/puppet-preflight':
120
      ensure  => directory,
121
      mode    => '0750',
122
      purge   => true,
123
      force   => true,
124
      recurse => true;
125
    '/etc/nftables/puppet-preflight.nft':
126 82d10659 Nacho Barrientos
      ensure  => file,
127 03d9e7da Steve Traylen
      content => epp('nftables/config/puppet.nft.epp', { 'nat' => $nat, 'noflush' => $noflush_tables });
128 11bf7237 Steve Traylen
  } ~> exec {
129 30462da1 Steve Traylen
    'nft validate':
130
      refreshonly => true,
131
      command     => '/usr/sbin/nft -I /etc/nftables/puppet-preflight -c -f /etc/nftables/puppet-preflight.nft || ( /usr/bin/echo "#CONFIG BROKEN" >> /etc/nftables/puppet-preflight.nft && /bin/false)';
132 11bf7237 Steve Traylen
  } -> file {
133 30462da1 Steve Traylen
    default:
134
      owner => 'root',
135
      group => 'root',
136
      mode  => '0640';
137 0ba57c66 mh
    '/etc/nftables/puppet.nft':
138 82d10659 Nacho Barrientos
      ensure  => file,
139 03d9e7da Steve Traylen
      content => epp('nftables/config/puppet.nft.epp', { 'nat' => $nat, 'noflush' => $noflush_tables });
140 0ba57c66 mh
    '/etc/nftables/puppet':
141
      ensure  => directory,
142 5acb554a tr
      mode    => '0750',
143 0ba57c66 mh
      purge   => true,
144
      force   => true,
145
      recurse => true;
146 11bf7237 Steve Traylen
  } ~> service { 'nftables':
147 30462da1 Steve Traylen
    ensure     => running,
148
    enable     => true,
149
    hasrestart => true,
150
    restart    => '/usr/bin/systemctl reload nftables',
151
  }
152
153 11bf7237 Steve Traylen
  systemd::dropin_file { 'puppet_nft.conf':
154 03d9e7da Steve Traylen
    ensure  => present,
155
    unit    => 'nftables.service',
156
    content => epp('nftables/systemd/puppet_nft.conf.epp', { 'noflush' => $noflush_tables }),
157
    notify  => Service['nftables'],
158 0ba57c66 mh
  }
159
160 11bf7237 Steve Traylen
  service { 'firewalld':
161 f02562f2 tr
    ensure => stopped,
162 ae9872e2 Nacho Barrientos
    enable => $firewalld_enable,
163 f02562f2 tr
  }
164
165 c8092701 tr
  include nftables::inet_filter
166 82d10659 Nacho Barrientos
  if $nat {
167
    include nftables::ip_nat
168
  }
169 b3a7a6dd tr
170
  # inject custom rules e.g. from hiera
171 66ed7f61 mh
  $rules.each |$n,$v| {
172 11bf7237 Steve Traylen
    nftables::rule {
173 66ed7f61 mh
      $n:
174 11bf7237 Steve Traylen
        * => $v,
175 66ed7f61 mh
    }
176
  }
177 802d80d1 Nacho Barrientos
178
  # inject custom sets e.g. from hiera
179
  $sets.each |$n,$v| {
180 11bf7237 Steve Traylen
    nftables::set {
181 802d80d1 Nacho Barrientos
      $n:
182 11bf7237 Steve Traylen
        * => $v,
183 802d80d1 Nacho Barrientos
    }
184
  }
185 0ba57c66 mh
}