# @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,
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}"
$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",
+ }
}
}
}
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