aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md4
-rw-r--r--manifests/chain.pp2
-rw-r--r--manifests/rule.pp32
-rw-r--r--spec/defines/rule_spec.rb42
4 files changed, 64 insertions, 16 deletions
diff --git a/README.md b/README.md
index e05cba5..324134f 100644
--- a/README.md
+++ b/README.md
@@ -178,6 +178,10 @@ Same as above, just for the destination IP address
Add or remove it from the ruleset
+#### `interface`
+
+If set, this rule only applies to this specific interface
+
### chain defined resource
The module defines the three default chains for you, INPUT, FORWARD and OUTPUT.
diff --git a/manifests/chain.pp b/manifests/chain.pp
index 6a01607..0a0071a 100644
--- a/manifests/chain.pp
+++ b/manifests/chain.pp
@@ -31,7 +31,7 @@ define ferm::chain (
concat::fragment{"${chain}-footer":
target => "/etc/ferm.d/chains/${chain}.conf",
content => epp("${module_name}/ferm_chain_footer.conf.epp", { 'chain' => $chain }),
- order => '99',
+ order => 'zzzzzzzzzzzzzzzzzzzzz',
}
}
}
diff --git a/manifests/rule.pp b/manifests/rule.pp
index c87ef7f..b8ae29a 100644
--- a/manifests/rule.pp
+++ b/manifests/rule.pp
@@ -8,6 +8,7 @@
# @param saddr The source address we want to match
# @param daddr The destination address we want to match
# @param proto_options Optional parameters that will be passed to the protocol (for example to match specific ICMP types)
+# @param interface an Optional interface where this rule should be applied
# @param ensure Set the rule to present or absent
define ferm::rule (
Ferm::Chains $chain,
@@ -19,6 +20,7 @@ define ferm::rule (
Optional[String[1]] $saddr = undef,
Optional[String[1]] $daddr = undef,
Optional[String[1]] $proto_options = undef,
+ Optional[String[1]] $interface = undef,
Enum['absent','present'] $ensure = 'present',
){
$proto_real = "proto ${proto}"
@@ -47,9 +49,33 @@ define ferm::rule (
$rule = squeeze("${comment_real} ${proto_real} ${proto_options_real} ${dport_real} ${sport_real} ${daddr_real} ${saddr_real} ${policy};", ' ')
if $ensure == 'present' {
- concat::fragment{"${chain}-${name}":
- target => "/etc/ferm.d/chains/${chain}.conf",
- content => "${rule}\n",
+ if $interface {
+ unless defined(Concat::Fragment["${chain}-${interface}-aaa"]) {
+ concat::fragment{"${chain}-${interface}-aaa":
+ target => "/etc/ferm.d/chains/${chain}.conf",
+ content => "interface ${interface} {\n",
+ order => $interface,
+ }
+ }
+
+ concat::fragment{"${chain}-${interface}-${name}":
+ target => "/etc/ferm.d/chains/${chain}.conf",
+ content => " ${rule}\n",
+ order => $interface,
+ }
+
+ unless defined(Concat::Fragment["${chain}-${interface}-zzz"]) {
+ concat::fragment{"${chain}-${interface}-zzz":
+ target => "/etc/ferm.d/chains/${chain}.conf",
+ content => "}\n",
+ order => $interface,
+ }
+ }
+ } else {
+ concat::fragment{"${chain}-${name}":
+ target => "/etc/ferm.d/chains/${chain}.conf",
+ content => "${rule}\n",
+ }
}
}
}
diff --git a/spec/defines/rule_spec.rb b/spec/defines/rule_spec.rb
index 0473970..bd4ed73 100644
--- a/spec/defines/rule_spec.rb
+++ b/spec/defines/rule_spec.rb
@@ -6,21 +6,39 @@ describe 'ferm::rule', type: :define do
let :facts do
facts
end
- let(:title) { 'filter-ssh' }
- let :params do
- {
- chain: 'INPUT',
- policy: 'ACCEPT',
- proto: 'tcp',
- dport: '22',
- saddr: '127.0.0.1'
- }
- end
- context 'default params create simple rule' do
+ context 'without a specific interface' do
+ let(:title) { 'filter-ssh' }
+ let :params do
+ {
+ chain: 'INPUT',
+ policy: 'ACCEPT',
+ proto: 'tcp',
+ dport: '22',
+ saddr: '127.0.0.1'
+ }
+ end
+
it { is_expected.to compile.with_all_deps }
it { is_expected.to contain_concat__fragment('INPUT-filter-ssh').with_content("mod comment comment 'filter-ssh' proto tcp dport 22 saddr @ipfilter(127.0.0.1) ACCEPT;\n") }
- it { is_expected.to contain_concat__fragment('INPUT-filter-ssh') }
+ end
+ context 'with a specific interface' do
+ let(:title) { 'filter-ssh' }
+ let :params do
+ {
+ chain: 'INPUT',
+ policy: 'ACCEPT',
+ proto: 'tcp',
+ dport: '22',
+ saddr: '127.0.0.1',
+ interface: 'eth0'
+ }
+ end
+
+ it { is_expected.to compile.with_all_deps }
+ it { is_expected.to contain_concat__fragment('INPUT-eth0-filter-ssh').with_content(" mod comment comment 'filter-ssh' proto tcp dport 22 saddr @ipfilter(127.0.0.1) ACCEPT;\n") }
+ it { is_expected.to contain_concat__fragment('INPUT-eth0-aaa').with_content("interface eth0 {\n") }
+ it { is_expected.to contain_concat__fragment('INPUT-eth0-zzz').with_content("}\n") }
end
end
end