Projet

Général

Profil

Révision 6be2adf7

ID6be2adf7e1b3776436c99b8c18cc81bd21665750
Parent 7a77d75a
Enfant c3515492

Ajouté par Luis Fernández Álvarez il y a environ 4 ans

Add Docker-CE default rules

Voir les différences:

manifests/rules/docker_ce.pp
1
# @summary Default firewall configuration for Docker-CE
2
#
3
# The configuration distributed in this class represents the default firewall
4
# configuration done by docker-ce when the iptables integration is enabled.
5
#
6
# This class is needed as the default docker-ce rules added to ip-filter conflict
7
# with the inet-filter forward rules set by default in this module.
8
#
9
# When using this class 'docker::iptables: false' should be set.
10
#
11
# @param docker_interface
12
#   Interface name used by docker. It defaults to docker0.
13
# @param docker_prefix
14
#   The address space used by docker. It defaults to 172.17.0.0/16.
15
#
16
class nftables::rules::docker_ce (
17
  String[1]                     $docker_interface = 'docker0',
18
  Stdlib::IP::Address::V4::CIDR $docker_prefix    = '172.17.0.0/16',
19
) {
20
  #
21
  # inet-filter
22
  #
23

  
24
  nftables::chain {
25
    'DOCKER': ;
26
    'DOCKER_ISOLATION_STAGE_1': ;
27
    'DOCKER_ISOLATION_STAGE_2': ;
28
    'DOCKER_USER': ;
29
  }
30

  
31
  nftables::rule {
32
    'DOCKER_ISOLATION_STAGE_1-iifname':
33
      order   => '01',
34
      content => "iifname \"${docker_interface}\" oifname != \"${docker_interface}\" counter jump DOCKER_ISOLATION_STAGE_2";
35
    'DOCKER_ISOLATION_STAGE_1-counter':
36
      order   => '02',
37
      content => 'counter return';
38
    'DOCKER_ISOLATION_STAGE_2-drop':
39
      order   => '01',
40
      content => "oifname \"${docker_interface}\" counter drop";
41
    'DOCKER_ISOLATION_STAGE_2-counter':
42
      order   => '02',
43
      content => 'counter return';
44
    'DOCKER_USER-counter':
45
      order   => '01',
46
      content => 'counter return',
47
  }
48

  
49
  nftables::rule {
50
    'default_fwd-jump_docker_user':
51
      order   => '40',
52
      content => 'counter jump DOCKER_USER';
53
    'default_fwd-jump_docker_isolation_stage_1':
54
      order   => '41',
55
      content => 'counter jump DOCKER_ISOLATION_STAGE_1';
56
    'default_fwd-out_docker_accept':
57
      order   => '42',
58
      content => "oifname \"${docker_interface}\" ct state established,related counter accept";
59
    'default_fwd-jump_docker':
60
      order   => '43',
61
      content => "oifname \"${docker_interface}\" counter jump DOCKER";
62
    'default_fwd-idocker_onot_accept':
63
      order   => '44',
64
      content => "iifname \"${docker_interface}\" oifname != \"${docker_interface}\" counter accept";
65
    'default_fwd-idocker_odocker_accept':
66
      order   => '45',
67
      content => "iifname \"${docker_interface}\" oifname \"${docker_interface}\" counter accept";
68
  }
69

  
70
  #
71
  # ip-nat
72
  #
73

  
74
  nftables::chain {
75
    'DOCKER-nat':
76
      table => 'ip-nat',
77
      chain => 'DOCKER';
78
    'OUTPUT-nat':
79
      table => 'ip-nat',
80
      chain => 'OUTPUT';
81
    'INPUT-nat':
82
      table => 'ip-nat',
83
      chain => 'INPUT';
84
  }
85

  
86
  nftables::rule {
87
    'POSTROUTING-docker':
88
      table   => 'ip-nat',
89
      content => "oifname != \"${docker_interface}\" ip saddr ${docker_prefix} counter masquerade";
90
    'PREROUTING-docker':
91
      table   => 'ip-nat',
92
      content => 'fib daddr type local counter jump DOCKER';
93
    'OUTPUT-jump_docker@ip-nat':
94
      rule_name => 'OUTPUT-jump_docker',
95
      table     => 'ip-nat',
96
      content   => 'ip daddr != 127.0.0.0/8 fib daddr type local counter jump DOCKER';
97
    'DOCKER-counter':
98
      table   => 'ip-nat',
99
      content => "iifname \"${docker_interface}\" counter return";
100
    'INPUT-type@ip-nat':
101
      rulename => 'INPUT-type',
102
      table    => 'ip-nat',
103
      order    => '01',
104
      content  => 'type nat hook input priority 100';
105
    'INPUT-policy@ip-nat':
106
      rulename => 'INPUT-policy',
107
      table    => 'ip-nat',
108
      order    => '02',
109
      content  => 'policy accept';
110
  }
111
}
spec/acceptance/all_rules_spec.rb
38 38
      include nftables::rules::ceph
39 39
      include nftables::rules::samba
40 40
      include nftables::rules::activemq
41
      include nftables::rules::docker_ce
41 42
      include nftables::rules::out::postgres
42 43
      include nftables::rules::out::icmp
43 44
      include nftables::rules::out::dns
spec/classes/rules/docker_ce_spec.rb
1
require 'spec_helper'
2

  
3
describe 'nftables::rules::docker_ce' do
4
  let(:pre_condition) { 'include nftables' }
5

  
6
  on_supported_os.each do |os, os_facts|
7
    context "on #{os}" do
8
      let(:facts) { os_facts }
9

  
10
      context 'default options' do
11
        it { is_expected.to compile }
12
        it { is_expected.to contain_nftables__chain('DOCKER') }
13
        it { is_expected.to contain_nftables__chain('DOCKER_ISOLATION_STAGE_1') }
14
        it { is_expected.to contain_nftables__chain('DOCKER_ISOLATION_STAGE_2') }
15
        it { is_expected.to contain_nftables__chain('DOCKER_USER') }
16
        it {
17
          is_expected.to contain_nftables__chain('DOCKER-nat').with(
18
            chain: 'DOCKER',
19
            table: 'ip-nat',
20
          )
21
        }
22
        it {
23
          is_expected.to contain_nftables__chain('OUTPUT-nat').with(
24
            chain: 'OUTPUT',
25
            table: 'ip-nat',
26
          )
27
        }
28
        it {
29
          is_expected.to contain_nftables__chain('INPUT-nat').with(
30
            chain: 'INPUT',
31
            table: 'ip-nat',
32
          )
33
        }
34
        it { is_expected.to contain_nftables__rule('DOCKER_ISOLATION_STAGE_2-drop').with_content('oifname "docker0" counter drop') }
35
        it {
36
          is_expected.to contain_nftables__rule('POSTROUTING-docker').with(
37
            content: 'oifname != "docker0" ip saddr 172.17.0.0/16 counter masquerade',
38
            table: 'ip-nat',
39
          )
40
        }
41
      end
42

  
43
      context 'with custom interface and subnet' do
44
        let(:params) do
45
          {
46
            docker_interface: 'ifdo0',
47
            docker_prefix: '192.168.4.0/24',
48
          }
49
        end
50

  
51
        it { is_expected.to compile }
52
        it { is_expected.to contain_nftables__rule('DOCKER_ISOLATION_STAGE_2-drop').with_content('oifname "ifdo0" counter drop') }
53
        it {
54
          is_expected.to contain_nftables__rule('POSTROUTING-docker').with(
55
            content: 'oifname != "ifdo0" ip saddr 192.168.4.0/24 counter masquerade',
56
            table: 'ip-nat',
57
          )
58
        }
59
      end
60
    end
61
  end
62
end

Formats disponibles : Unified diff