Projet

Général

Profil

Révision 331b8d85

ID331b8d85f7c38b6dbfe602f75d6c9831b1534694
Parent 753540f1
Enfant 77503f49

Ajouté par Steve Traylen il y a plus de 2 ans

New nftables::file type to include raw file

For example:

```puppet
nftables::file{'geoip':
content => "include \"/files/geoipsets/dbip/*.ipv4\"\n",
}
```

will right a file or content into the nftables configuration.

The file written will be included in configuration.

Fixes #146

Voir les différences:

REFERENCE.md
78 78

  
79 79
* [`nftables::chain`](#nftableschain): manage a chain
80 80
* [`nftables::config`](#nftablesconfig): manage a config snippet
81
* [`nftables::file`](#nftablesfile): Insert a file into the nftables configuration
81 82
* [`nftables::rule`](#nftablesrule): Provides an interface to create a firewall rule
82 83
* [`nftables::rules::dnat4`](#nftablesrulesdnat4): manage a ipv4 dnat rule
83 84
* [`nftables::rules::masquerade`](#nftablesrulesmasquerade): masquerade all outgoing traffic
......
1233 1234

  
1234 1235
Default value: `'custom-'`
1235 1236

  
1237
### <a name="nftablesfile"></a>`nftables::file`
1238

  
1239
Insert a file into the nftables configuration
1240

  
1241
#### Examples
1242

  
1243
##### Include a file that includes other files
1244

  
1245
```puppet
1246
nftables::file{'geoip':
1247
  content => @(EOT)
1248
    include "/var/local/geoipsets/dbip/nftset/ipv4/*.ipv4"
1249
    include "/var/local/geoipsets/dbip/nftset/ipv6/*.ipv6"
1250
    |EOT,
1251
}
1252
```
1253

  
1254
#### Parameters
1255

  
1256
The following parameters are available in the `nftables::file` defined type:
1257

  
1258
* [`label`](#label)
1259
* [`content`](#content)
1260
* [`source`](#source)
1261
* [`prefix`](#prefix)
1262

  
1263
##### <a name="label"></a>`label`
1264

  
1265
Data type: `String[1]`
1266

  
1267
Unique name to include in filename.
1268

  
1269
Default value: `$title`
1270

  
1271
##### <a name="content"></a>`content`
1272

  
1273
Data type: `Optional[String]`
1274

  
1275
The content to place in the file.
1276

  
1277
Default value: ``undef``
1278

  
1279
##### <a name="source"></a>`source`
1280

  
1281
Data type: `Optional[Variant[String,Array[String,1]]]`
1282

  
1283
A source to obtain the file content from.
1284

  
1285
Default value: ``undef``
1286

  
1287
##### <a name="prefix"></a>`prefix`
1288

  
1289
Data type: `String`
1290

  
1291
Prefix of file name to be created, if left as `file-` it will be
1292
auto included in the main nft configuration
1293

  
1294
Default value: `'file-'`
1295

  
1236 1296
### <a name="nftablesrule"></a>`nftables::rule`
1237 1297

  
1238 1298
Provides an interface to create a firewall rule
manifests/file.pp
1
# @summary Insert a file into the nftables configuration
2
# @example Include a file that includes other files
3
#   nftables::file{'geoip':
4
#     content => @(EOT)
5
#       include "/var/local/geoipsets/dbip/nftset/ipv4/*.ipv4"
6
#       include "/var/local/geoipsets/dbip/nftset/ipv6/*.ipv6"
7
#       |EOT,
8
#   }
9
#
10
# @param label Unique name to include in filename.
11
# @param content The content to place in the file.
12
# @param source A source to obtain the file content from.
13
# @param prefix
14
#   Prefix of file name to be created, if left as `file-` it will be
15
#   auto included in the main nft configuration
16
#
17
define nftables::file (
18
  String[1] $label = $title,
19
  Optional[String] $content = undef,
20
  Optional[Variant[String,Array[String,1]]] $source = undef,
21
  String $prefix = 'file-',
22
) {
23
  if $content and $source {
24
    fail('Please pass only $content or $source, not both.')
25
  }
26

  
27
  $concat_name = "nftables-${name}"
28

  
29
  Package['nftables'] -> file { "/etc/nftables/puppet-preflight/${prefix}${label}.nft":
30
    ensure  => file,
31
    owner   => root,
32
    group   => root,
33
    mode    => '0640',
34
    content => $content,
35
    source  => $source,
36
  } ~> Exec['nft validate'] -> file { "/etc/nftables/puppet/${prefix}${label}.nft":
37
    ensure  => file,
38
    owner   => root,
39
    group   => root,
40
    mode    => '0640',
41
    content => $content,
42
    source  => $source,
43
  } ~> Service['nftables']
44
}
spec/acceptance/file_spec.rb
1
# frozen_string_literal: true
2

  
3
require 'spec_helper_acceptance'
4

  
5
describe 'nftables class' do
6
  context 'configure a nftables::file raw file' do
7
    it 'works idempotently with no errors' do
8
      pp = <<-EOS
9
      # default mask of firewalld service fails if service is not installed.
10
      # https://tickets.puppetlabs.com/browse/PUP-10814
11
      class { 'nftables':
12
        firewalld_enable => false,
13
      }
14
      nftables::file{'geoip':
15
        content => "# A comment should not fail\n",
16
      }
17
      $config_path = $facts['os']['family'] ? {
18
        'Archlinux' => '/etc/nftables.conf',
19
        'Debian' => '/etc/nftables.conf',
20
        default => '/etc/sysconfig/nftables.conf',
21
      }
22
      $nft_path = $facts['os']['family'] ? {
23
        'Archlinux' => '/usr/bin/nft',
24
        default => '/usr/sbin/nft',
25
      }
26
      # nftables cannot be started in docker so replace service with a validation only.
27
      systemd::dropin_file{"zzz_docker_nft.conf":
28
        ensure  => present,
29
        unit    => "nftables.service",
30
        content => [
31
          "[Service]",
32
          "ExecStart=",
33
          "ExecStart=${nft_path} -c -I /etc/nftables/puppet -f ${config_path}",
34
          "ExecReload=",
35
          "ExecReload=${nft_path} -c -I /etc/nftables/puppet -f ${config_path}",
36
          "",
37
          ].join("\n"),
38
        notify  => Service["nftables"],
39
      }
40
      EOS
41
      # Run it twice and test for idempotency
42
      apply_manifest(pp, catch_failures: true)
43
      apply_manifest(pp, catch_changes: true)
44
    end
45

  
46
    describe package('nftables') do
47
      it { is_expected.to be_installed }
48
    end
49

  
50
    describe service('nftables') do
51
      it { is_expected.to be_running }
52
      it { is_expected.to be_enabled }
53
    end
54

  
55
    describe file('/etc/nftables/puppet/file-geoip.nft', '/etc/nftables/puppet/file-geoip.nft') do
56
      it { is_expected.to be_file }
57
    end
58
  end
59
end
spec/classes/nftables_spec.rb
46 46
      }
47 47

  
48 48
      it {
49
        expect(subject).to contain_file('/etc/nftables/puppet.nft').with(
50
          content: %r{^include "file-\*\.nft"$}
51
        )
52
      }
53

  
54
      it {
49 55
        expect(subject).to contain_file('/etc/nftables/puppet').with(
50 56
          ensure: 'directory',
51 57
          owner: 'root',
......
68 74
      }
69 75

  
70 76
      it {
77
        expect(subject).to contain_file('/etc/nftables/puppet-preflight.nft').with(
78
          content: %r{^include "file-\*\.nft"$}
79
        )
80
      }
81

  
82
      it {
71 83
        expect(subject).to contain_file('/etc/nftables/puppet-preflight').with(
72 84
          ensure: 'directory',
73 85
          owner: 'root',
spec/defines/file_spec.rb
1
# frozen_string_literal: true
2

  
3
require 'spec_helper'
4

  
5
describe 'nftables::file' do
6
  let(:pre_condition) { 'include nftables' }
7
  let(:title) { 'FOO' }
8

  
9
  on_supported_os.each do |os, facts|
10
    context "on #{os}" do
11
      let(:facts) do
12
        facts
13
      end
14

  
15
      context 'with source and content both unset' do
16
        it { is_expected.to compile }
17
        it { is_expected.to contain_file('/etc/nftables/puppet-preflight/file-FOO.nft').without_source }
18
        it { is_expected.to contain_file('/etc/nftables/puppet-preflight/file-FOO.nft').without_content }
19
        it { is_expected.to contain_file('/etc/nftables/puppet/file-FOO.nft').without_source }
20
        it { is_expected.to contain_file('/etc/nftables/puppet/file-FOO.nft').without_content }
21
        it { is_expected.to contain_exec('nft validate') }
22
        it { is_expected.to contain_service('nftables') }
23
        it { is_expected.to contain_package('nftables').that_comes_before('File[/etc/nftables/puppet-preflight/file-FOO.nft]') }
24
        it { is_expected.to contain_file('/etc/nftables/puppet-preflight/file-FOO.nft').that_notifies('Exec[nft validate]') }
25
        it { is_expected.to contain_exec('nft validate').that_comes_before('File[/etc/nftables/puppet/file-FOO.nft]') }
26
        it { is_expected.to contain_file('/etc/nftables/puppet/file-FOO.nft').that_notifies('Service[nftables]') }
27
      end
28

  
29
      context 'with source set only' do
30
        let(:params) do
31
          { source: 'puppet:///module/foobar.nft' }
32
        end
33

  
34
        it { is_expected.to contain_file('/etc/nftables/puppet-preflight/file-FOO.nft').without_content }
35
        it { is_expected.to contain_file('/etc/nftables/puppet-preflight/file-FOO.nft').with_source('puppet:///module/foobar.nft') }
36
        it { is_expected.to contain_file('/etc/nftables/puppet/file-FOO.nft').without_content }
37
        it { is_expected.to contain_file('/etc/nftables/puppet/file-FOO.nft').with_source('puppet:///module/foobar.nft') }
38
      end
39

  
40
      context 'with content set only' do
41
        let(:params) do
42
          { content: '# my rules' }
43
        end
44

  
45
        it { is_expected.to contain_file('/etc/nftables/puppet-preflight/file-FOO.nft').without_source }
46
        it { is_expected.to contain_file('/etc/nftables/puppet-preflight/file-FOO.nft').with_content('# my rules') }
47
        it { is_expected.to contain_file('/etc/nftables/puppet/file-FOO.nft').without_source }
48
        it { is_expected.to contain_file('/etc/nftables/puppet/file-FOO.nft').with_content('# my rules') }
49
      end
50

  
51
      context 'with content and source set' do
52
        let(:params) do
53
          { content: '# my rules', source: 'puppet:///modules/foobar.nft' }
54
        end
55

  
56
        it { is_expected.not_to compile }
57
      end
58
    end
59
  end
60
end
templates/config/puppet.nft.epp
21 21
# drop any existing nftables ruleset, ensure tables are initialized
22 22
<%= $_flush_command.join("\n") %>
23 23

  
24
include "file-*.nft"
24 25
include "custom-*.nft"
25 26
<% if $inet_filter { -%>
26 27
include "inet-filter.nft"

Formats disponibles : Unified diff