Projet

Général

Profil

Révision cd2a3cbf

IDcd2a3cbf5e6d9c17b8e9a92097d0e33cf769791f
Parent 18b211e7
Enfant 3f2f50c9

Ajouté par Nacho Barrientos il y a environ 4 ans

Add rules for QEMU/libvirt guests

Voir les différences:

REFERENCE.md
59 59
* [`nftables::rules::out::tor`](#nftablesrulesouttor): manage out tor
60 60
* [`nftables::rules::out::wireguard`](#nftablesrulesoutwireguard): manage out wireguard
61 61
* [`nftables::rules::puppet`](#nftablesrulespuppet): manage in puppet
62
* [`nftables::rules::qemu`](#nftablesrulesqemu): Bridged network configuration for qemu/libvirt
62 63
* [`nftables::rules::samba`](#nftablesrulessamba): manage Samba, the suite to allow Windows file sharing on Linux resources.
63 64
* [`nftables::rules::smtp`](#nftablesrulessmtp): manage in smtp
64 65
* [`nftables::rules::smtp_submission`](#nftablesrulessmtp_submission): manage in smtp submission
......
791 792

  
792 793
Default value: `[8140]`
793 794

  
795
### <a name="nftablesrulesqemu"></a>`nftables::rules::qemu`
796

  
797
This class configures the typical firewall setup that libvirt
798
creates. Depending on your requirements you can switch on and off
799
several aspects, for instance if you don't do DHCP to your guests
800
you can disable the rules that accept DHCP traffic on the host or if
801
you don't want your guests to talk to hosts outside you can disable
802
forwarding and/or masquerading for IPv4 traffic.
803

  
804
#### Parameters
805

  
806
The following parameters are available in the `nftables::rules::qemu` class:
807

  
808
* [`interface`](#interface)
809
* [`network_v4`](#network_v4)
810
* [`network_v6`](#network_v6)
811
* [`dns`](#dns)
812
* [`dhcpv4`](#dhcpv4)
813
* [`forward_traffic`](#forward_traffic)
814
* [`internal_traffic`](#internal_traffic)
815
* [`masquerade`](#masquerade)
816

  
817
##### <a name="interface"></a>`interface`
818

  
819
Data type: `String[1]`
820

  
821
Interface name used by the bridge.
822

  
823
Default value: `'virbr0'`
824

  
825
##### <a name="network_v4"></a>`network_v4`
826

  
827
Data type: `Stdlib::IP::Address::V4::CIDR`
828

  
829
The IPv4 network prefix used in the virtual network.
830

  
831
Default value: `'192.168.122.0/24'`
832

  
833
##### <a name="network_v6"></a>`network_v6`
834

  
835
Data type: `Optional[Stdlib::IP::Address::V6::CIDR]`
836

  
837
The IPv6 network prefix used in the virtual network.
838

  
839
Default value: ``undef``
840

  
841
##### <a name="dns"></a>`dns`
842

  
843
Data type: `Boolean`
844

  
845
Allow DNS traffic from the guests to the host.
846

  
847
Default value: ``true``
848

  
849
##### <a name="dhcpv4"></a>`dhcpv4`
850

  
851
Data type: `Boolean`
852

  
853
Allow DHCPv4 traffic from the guests to the host.
854

  
855
Default value: ``true``
856

  
857
##### <a name="forward_traffic"></a>`forward_traffic`
858

  
859
Data type: `Boolean`
860

  
861
Allow forwarded traffic (out all, in related/established)
862
generated by the virtual network.
863

  
864
Default value: ``true``
865

  
866
##### <a name="internal_traffic"></a>`internal_traffic`
867

  
868
Data type: `Boolean`
869

  
870
Allow guests in the virtual network to talk to each other.
871

  
872
Default value: ``true``
873

  
874
##### <a name="masquerade"></a>`masquerade`
875

  
876
Data type: `Boolean`
877

  
878
Do NAT masquerade on all IPv4 traffic generated by guests
879
to external networks.
880

  
881
Default value: ``true``
882

  
794 883
### <a name="nftablesrulessamba"></a>`nftables::rules::samba`
795 884

  
796 885
manage Samba, the suite to allow Windows file sharing on Linux resources.
manifests/rules/qemu.pp
1
# @summary Bridged network configuration for qemu/libvirt
2
#
3
# This class configures the typical firewall setup that libvirt
4
# creates. Depending on your requirements you can switch on and off
5
# several aspects, for instance if you don't do DHCP to your guests
6
# you can disable the rules that accept DHCP traffic on the host or if
7
# you don't want your guests to talk to hosts outside you can disable
8
# forwarding and/or masquerading for IPv4 traffic.
9
#
10
# @param interface
11
#   Interface name used by the bridge.
12
#
13
# @param network_v4
14
#   The IPv4 network prefix used in the virtual network.
15
#
16
# @param network_v6
17
#   The IPv6 network prefix used in the virtual network.
18
#
19
# @param dns
20
#   Allow DNS traffic from the guests to the host.
21
#
22
# @param dhcpv4
23
#   Allow DHCPv4 traffic from the guests to the host.
24
#
25
# @param forward_traffic
26
#   Allow forwarded traffic (out all, in related/established)
27
#   generated by the virtual network.
28
#
29
# @param internal_traffic
30
#   Allow guests in the virtual network to talk to each other.
31
#
32
# @param masquerade
33
#   Do NAT masquerade on all IPv4 traffic generated by guests
34
#   to external networks.
35
class nftables::rules::qemu (
36
  String[1]                               $interface         = 'virbr0',
37
  Stdlib::IP::Address::V4::CIDR           $network_v4        = '192.168.122.0/24',
38
  Optional[Stdlib::IP::Address::V6::CIDR] $network_v6        = undef,
39
  Boolean                                 $dns               = true,
40
  Boolean                                 $dhcpv4            = true,
41
  Boolean                                 $forward_traffic   = true,
42
  Boolean                                 $internal_traffic  = true,
43
  Boolean                                 $masquerade        = true,
44
) {
45
  if $dns {
46
    nftables::rule {
47
      'default_in-qemu_udp_dns':
48
        content => "iifname \"${interface}\" udp dport 53 accept";
49
      'default_in-qemu_tcp_dns':
50
        content => "iifname \"${interface}\" tcp dport 53 accept";
51
    }
52
  }
53

  
54
  if $dhcpv4 {
55
    nftables::rule {
56
      'default_in-qemu_dhcpv4':
57
        content => "iifname \"${interface}\" meta l4proto udp udp dport 67 accept";
58
      # The rule below is created by libvirt. It should not be necessary here
59
      # as it should be accepted by the conntrack rules in OUTPUT.
60
      #'default_out-qemu_dhcpv4':
61
      #  content => "oifname \"${interface}\" meta l4proto udp udp dport 68 accept";
62
    }
63
  }
64

  
65
  if $forward_traffic {
66
    nftables::rule {
67
      'default_fwd-qemu_oip_v4':
68
        content => "oifname \"${interface}\" ip daddr ${network_v4} ct state related,established accept";
69
      'default_fwd-qemu_iip_v4':
70
        content => "iifname \"${interface}\" ip saddr ${network_v4} accept";
71
    }
72
    if $network_v6 {
73
      nftables::rule {
74
        'default_fwd-qemu_oip_v6':
75
          content => "oifname \"${interface}\" ip6 daddr ${network_v6} ct state related,established accept";
76
        'default_fwd-qemu_iip_v6':
77
          content => "iifname \"${interface}\" ip6 saddr ${network_v6} accept";
78
      }
79
    }
80
  }
81

  
82
  if $internal_traffic {
83
    nftables::rule {
84
      'default_fwd-qemu_io_internal':
85
        content => "iifname \"${interface}\" oifname \"${interface}\" accept",
86
    }
87
  }
88

  
89
  # Libvirt rejects all the remaining forwarded traffic passing
90
  # through the virtual interface. This is not necessary here because
91
  # of the default policy in default_fwd.
92

  
93
  if $masquerade {
94
    nftables::rule {
95
      'POSTROUTING-qemu_ignore_multicast':
96
        table   => 'ip-nat',
97
        content => "ip saddr ${network_v4} ip daddr 224.0.0.0/24 return";
98
      'POSTROUTING-qemu_ignore_broadcast':
99
        table   => 'ip-nat',
100
        content => "ip saddr ${network_v4} ip daddr 255.255.255.255 return";
101
      'POSTROUTING-qemu_masq_tcp':
102
        table   => 'ip-nat',
103
        content => "meta l4proto tcp ip saddr ${network_v4} ip daddr != ${network_v4} masquerade to :1024-65535";
104
      'POSTROUTING-qemu_masq_udp':
105
        table   => 'ip-nat',
106
        content => "meta l4proto udp ip saddr ${network_v4} ip daddr != ${network_v4} masquerade to :1024-65535";
107
      'POSTROUTING-qemu_masq_ip':
108
        table   => 'ip-nat',
109
        content => "ip saddr ${network_v4} ip daddr != ${network_v4} masquerade";
110
    }
111
  }
112
}
spec/acceptance/all_rules_spec.rb
39 39
      include nftables::rules::samba
40 40
      include nftables::rules::activemq
41 41
      include nftables::rules::docker_ce
42
      include nftables::rules::qemu
42 43
      include nftables::rules::out::postgres
43 44
      include nftables::rules::out::icmp
44 45
      include nftables::rules::out::dns
spec/classes/rules/qemu_spec.rb
1
require 'spec_helper'
2

  
3
describe 'nftables::rules::qemu' do
4
  on_supported_os.each do |os, os_facts|
5
    context "on #{os}" do
6
      let(:facts) { os_facts }
7

  
8
      context 'default options' do
9
        it { is_expected.to compile }
10
        it {
11
          is_expected.to contain_nftables__rule('default_in-qemu_udp_dns').
12
            with_content('iifname "virbr0" udp dport 53 accept')
13
        }
14
        it {
15
          is_expected.to contain_nftables__rule('default_in-qemu_tcp_dns').
16
            with_content('iifname "virbr0" tcp dport 53 accept')
17
        }
18
        it {
19
          is_expected.to contain_nftables__rule('default_in-qemu_dhcpv4').
20
            with_content('iifname "virbr0" meta l4proto udp udp dport 67 accept')
21
        }
22
        it {
23
          is_expected.to contain_nftables__rule('default_fwd-qemu_oip_v4').
24
            with_content('oifname "virbr0" ip daddr 192.168.122.0/24 ct state related,established accept')
25
        }
26
        it {
27
          is_expected.to contain_nftables__rule('default_fwd-qemu_iip_v4').
28
            with_content('iifname "virbr0" ip saddr 192.168.122.0/24 accept')
29
        }
30
        it { is_expected.not_to contain_nftables__rule('default_fwd-qemu_oip_v6') }
31
        it { is_expected.not_to contain_nftables__rule('default_fwd-qemu_iip_v6') }
32
        it {
33
          is_expected.to contain_nftables__rule('default_fwd-qemu_io_internal').
34
            with_content('iifname "virbr0" oifname "virbr0" accept')
35
        }
36
        it {
37
          is_expected.to contain_nftables__rule('POSTROUTING-qemu_ignore_multicast').with(
38
            content: 'ip saddr 192.168.122.0/24 ip daddr 224.0.0.0/24 return',
39
            table: 'ip-nat'
40
          )
41
        }
42
        it {
43
          is_expected.to contain_nftables__rule('POSTROUTING-qemu_ignore_broadcast').with(
44
            content: 'ip saddr 192.168.122.0/24 ip daddr 255.255.255.255 return',
45
            table: 'ip-nat'
46
          )
47
        }
48
        it {
49
          is_expected.to contain_nftables__rule('POSTROUTING-qemu_masq_tcp').with(
50
            content: 'meta l4proto tcp ip saddr 192.168.122.0/24 ip daddr != 192.168.122.0/24 masquerade to :1024-65535',
51
            table: 'ip-nat'
52
          )
53
        }
54
        it {
55
          is_expected.to contain_nftables__rule('POSTROUTING-qemu_masq_udp').with(
56
            content: 'meta l4proto udp ip saddr 192.168.122.0/24 ip daddr != 192.168.122.0/24 masquerade to :1024-65535',
57
            table: 'ip-nat'
58
          )
59
        }
60
        it {
61
          is_expected.to contain_nftables__rule('POSTROUTING-qemu_masq_ip').with(
62
            content: 'ip saddr 192.168.122.0/24 ip daddr != 192.168.122.0/24 masquerade',
63
            table: 'ip-nat'
64
          )
65
        }
66
      end
67

  
68
      context 'with all off' do
69
        let(:params) do
70
          {
71
            dns: false,
72
            dhcpv4: false,
73
            forward_traffic: false,
74
            internal_traffic: false,
75
            masquerade: false,
76
          }
77
        end
78

  
79
        it { is_expected.to compile }
80
        it { is_expected.to have_resource_count(0) }
81
      end
82

  
83
      context 'ipv6 prefix' do
84
        let(:params) do
85
          {
86
            network_v6: '20ac:cafe:1:1::/64',
87
          }
88
        end
89

  
90
        it { is_expected.to compile }
91
        it {
92
          is_expected.to contain_nftables__rule('default_fwd-qemu_oip_v4').
93
            with_content('oifname "virbr0" ip daddr 192.168.122.0/24 ct state related,established accept')
94
        }
95
        it {
96
          is_expected.to contain_nftables__rule('default_fwd-qemu_iip_v4').
97
            with_content('iifname "virbr0" ip saddr 192.168.122.0/24 accept')
98
        }
99
        it {
100
          is_expected.to contain_nftables__rule('default_fwd-qemu_oip_v6').
101
            with_content('oifname "virbr0" ip6 daddr 20ac:cafe:1:1::/64 ct state related,established accept')
102
        }
103
        it {
104
          is_expected.to contain_nftables__rule('default_fwd-qemu_iip_v6').
105
            with_content('iifname "virbr0" ip6 saddr 20ac:cafe:1:1::/64 accept')
106
        }
107
      end
108

  
109
      context 'change interface' do
110
        let(:params) do
111
          {
112
            interface: 'vfoo0'
113
          }
114
        end
115

  
116
        it { is_expected.to compile }
117
        it {
118
          is_expected.to contain_nftables__rule('default_fwd-qemu_iip_v4').
119
            with_content('iifname "vfoo0" ip saddr 192.168.122.0/24 accept')
120
        }
121
      end
122

  
123
      context 'change ipv4 prefix' do
124
        let(:params) do
125
          {
126
            network_v4: '172.16.0.0/12'
127
          }
128
        end
129

  
130
        it { is_expected.to compile }
131
        it {
132
          is_expected.to contain_nftables__rule('default_fwd-qemu_iip_v4').
133
            with_content('iifname "virbr0" ip saddr 172.16.0.0/12 accept')
134
        }
135
      end
136
    end
137
  end
138
end

Formats disponibles : Unified diff