Skip to content

Commit 90602d1

Browse files
Merge pull request #948 from DavidS/iac-1190-ignore_foreign
(IAC-1190) add `ignore_foreign` when purging firewallchains
2 parents bf238be + 4dd9caa commit 90602d1

File tree

2 files changed

+36
-22
lines changed

2 files changed

+36
-22
lines changed

README.md

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,28 @@ The rules you create here are helpful if you don’t have any existing rules; th
127127

128128
Rules are persisted automatically between reboots, although there are known issues with ip6tables on older Debian/Ubuntu distributions. There are also known issues with ebtables.
129129

130-
1. In site.pp or another top-scope file, add the following code to set up a metatype to purge unmanaged firewall resources. This will clear any existing rules and make sure that only rules defined in Puppet exist on the machine.
130+
1. Use the following code to set up the default parameters for all of the firewall rules that you will establish later. These defaults will ensure that the `pre` and `post` classes are run in the correct order and avoid locking you out of your box during the first Puppet run.
131+
132+
```puppet
133+
Firewall {
134+
before => Class['my_fw::post'],
135+
require => Class['my_fw::pre'],
136+
}
137+
```
138+
139+
2. Declare the `my_fw::pre` and `my_fw::post` classes to satisfy dependencies. You can declare these classes using an external node classifier or the following code:
140+
141+
```puppet
142+
class { ['my_fw::pre', 'my_fw::post']: }
143+
```
144+
145+
3. Include the `firewall` class to ensure the correct packages are installed:
146+
147+
```puppet
148+
class { 'firewall': }
149+
```
150+
151+
4. If you want to remove unmanaged firewall rules, add the following code to set up a metatype to purge unmanaged firewall resources in your site.pp or another top-scope file. This will clear any existing rules and make sure that only rules defined in Puppet exist on the machine.
131152

132153
```puppet
133154
resources { 'firewall':
@@ -168,28 +189,9 @@ resources { 'firewallchain':
168189
}
169190
```
170191

171-
**Note** - If there are unmanaged rules in unmanaged chains, it will take two Puppet runs for the firewall chain to be purged. This is different than the `purge` parameter available in `firewallchain`.
192+
> **Note:** If there are unmanaged rules in unmanaged chains, it will take a second Puppet run for the firewall chain to be purged.
172193
173-
2. Use the following code to set up the default parameters for all of the firewall rules that you will establish later. These defaults will ensure that the `pre` and `post` classes are run in the correct order and avoid locking you out of your box during the first Puppet run.
174-
175-
```puppet
176-
Firewall {
177-
before => Class['my_fw::post'],
178-
require => Class['my_fw::pre'],
179-
}
180-
```
181-
182-
3. Declare the `my_fw::pre` and `my_fw::post` classes to satisfy dependencies. You can declare these classes using an external node classifier or the following code:
183-
184-
```puppet
185-
class { ['my_fw::pre', 'my_fw::post']: }
186-
```
187-
188-
4. Include the `firewall` class to ensure the correct packages are installed:
189-
190-
```puppet
191-
class { 'firewall': }
192-
```
194+
> **Note:** If you need more fine-grained control about which unmananged rules get removed, investigate the `purge` and `ignore_foreign` parameters available in `firewallchain`.
193195
194196
### Upgrading
195197

lib/puppet/type/firewallchain.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,15 @@
163163
end
164164
end
165165

166+
newparam(:ignore_foreign, boolean: true) do
167+
desc <<-PUPPETCODE
168+
Ignore rules that do not match the puppet title pattern "^\d+[[:graph:][:space:]]" when purging unmanaged firewall rules in this chain.
169+
This can be used to ignore rules that were not put in by puppet. Beware that nothing keeps other systems from configuring firewall rules with a comment that starts with digits, and is indistinguishable from puppet-configured rules.
170+
PUPPETCODE
171+
newvalues(false, true)
172+
defaultto false
173+
end
174+
166175
# Classes would be a better abstraction, pending:
167176
# http://projects.puppetlabs.com/issues/19001
168177
autorequire(:package) do
@@ -240,6 +249,9 @@ def generate
240249
# Remove rules which match our ignore filter
241250
rules_resources.delete_if { |res| value(:ignore).find_index { |f| res.provider.properties[:line].match(f) } } if value(:ignore)
242251

252+
# Remove rules that were (presumably) not put in by puppet
253+
rules_resources.delete_if { |res| res.provider.properties[:name].match(%r{^(\d+)[[:graph:][:space:]]})[1].to_i >= 9000 } if value(:ignore_foreign) == :true
254+
243255
# We mark all remaining rules for deletion, and then let the catalog override us on rules which should be present
244256
rules_resources.each { |res| res[:ensure] = :absent }
245257

0 commit comments

Comments
 (0)