Projet

Général

Profil

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

root / manifests / init.pp @ bbc93ede

Historique | Voir | Annoter | Télécharger (4,71 ko)

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