cloud-mu 2.0.1 → 2.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. checksums.yaml +4 -4
  2. data/Berksfile +2 -1
  3. data/bin/mu-upload-chef-artifacts +3 -0
  4. data/cloud-mu.gemspec +2 -2
  5. data/cookbooks/firewall/CHANGELOG.md +295 -0
  6. data/cookbooks/firewall/CONTRIBUTING.md +2 -0
  7. data/cookbooks/firewall/MAINTAINERS.md +19 -0
  8. data/cookbooks/firewall/README.md +339 -0
  9. data/cookbooks/firewall/attributes/default.rb +5 -0
  10. data/cookbooks/firewall/attributes/firewalld.rb +1 -0
  11. data/cookbooks/firewall/attributes/iptables.rb +17 -0
  12. data/cookbooks/firewall/attributes/ufw.rb +12 -0
  13. data/cookbooks/firewall/attributes/windows.rb +8 -0
  14. data/cookbooks/firewall/libraries/helpers.rb +100 -0
  15. data/cookbooks/firewall/libraries/helpers_firewalld.rb +116 -0
  16. data/cookbooks/firewall/libraries/helpers_iptables.rb +112 -0
  17. data/cookbooks/firewall/libraries/helpers_ufw.rb +135 -0
  18. data/cookbooks/firewall/libraries/helpers_windows.rb +130 -0
  19. data/cookbooks/firewall/libraries/matchers.rb +30 -0
  20. data/cookbooks/firewall/libraries/provider_firewall_firewalld.rb +179 -0
  21. data/cookbooks/firewall/libraries/provider_firewall_iptables.rb +171 -0
  22. data/cookbooks/firewall/libraries/provider_firewall_iptables_ubuntu.rb +196 -0
  23. data/cookbooks/firewall/libraries/provider_firewall_iptables_ubuntu1404.rb +196 -0
  24. data/cookbooks/firewall/libraries/provider_firewall_rule.rb +34 -0
  25. data/cookbooks/firewall/libraries/provider_firewall_ufw.rb +138 -0
  26. data/cookbooks/firewall/libraries/provider_firewall_windows.rb +126 -0
  27. data/cookbooks/firewall/libraries/resource_firewall.rb +26 -0
  28. data/cookbooks/firewall/libraries/resource_firewall_rule.rb +52 -0
  29. data/cookbooks/firewall/metadata.json +1 -0
  30. data/cookbooks/firewall/recipes/default.rb +80 -0
  31. data/cookbooks/firewall/recipes/disable_firewall.rb +23 -0
  32. data/cookbooks/firewall/templates/default/ufw/default.erb +13 -0
  33. data/cookbooks/mu-firewall/metadata.rb +1 -1
  34. data/cookbooks/mu-master/recipes/default.rb +3 -1
  35. data/cookbooks/mu-master/recipes/init.rb +3 -1
  36. data/cookbooks/mu-master/templates/default/mu.rc.erb +3 -0
  37. data/cookbooks/mu-tools/recipes/apply_security.rb +2 -2
  38. data/cookbooks/mu-tools/recipes/aws_api.rb +1 -1
  39. data/cookbooks/mu-tools/recipes/base_repositories.rb +1 -1
  40. data/cookbooks/mu-tools/recipes/clamav.rb +1 -1
  41. data/cookbooks/mu-tools/recipes/cloudinit.rb +1 -1
  42. data/cookbooks/mu-tools/recipes/disable-requiretty.rb +2 -2
  43. data/cookbooks/mu-tools/recipes/eks.rb +1 -1
  44. data/cookbooks/mu-tools/recipes/gcloud.rb +1 -1
  45. data/cookbooks/mu-tools/recipes/nrpe.rb +1 -1
  46. data/cookbooks/mu-tools/recipes/rsyslog.rb +2 -2
  47. data/cookbooks/mu-tools/recipes/set_local_fw.rb +37 -28
  48. data/environments/dev.json +1 -1
  49. data/environments/prod.json +1 -1
  50. data/modules/mu/cleanup.rb +4 -0
  51. data/modules/mu/clouds/aws/container_cluster.rb +3 -0
  52. data/modules/mu/clouds/aws/role.rb +14 -2
  53. data/modules/mu/clouds/aws/userdata/linux.erb +2 -3
  54. data/modules/mu/clouds/aws.rb +30 -14
  55. data/modules/mu.rb +4 -0
  56. metadata +30 -2
@@ -0,0 +1,339 @@
1
+ firewall Cookbook
2
+ =================
3
+
4
+ [![Build Status](https://travis-ci.org/chef-cookbooks/firewall.svg?branch=master)](http://travis-ci.org/chef-cookbooks/firewall)
5
+ [![Cookbook Version](https://img.shields.io/cookbook/v/firewall.svg)](https://supermarket.chef.io/cookbooks/firewall)
6
+
7
+ Provides a set of primitives for managing firewalls and associated rules.
8
+
9
+ PLEASE NOTE - The resource/providers in this cookbook are under heavy development. An attempt is being made to keep the resource simple/stupid by starting with less sophisticated firewall implementations first and refactor/vet the resource definition with each successive provider.
10
+
11
+ Requirements
12
+ ------------
13
+ **Chef 12.5.x+** is required. We are currently testing against Chef 13. If you need Chef 11 support, please try pinning back to a version less than 2.0, e.g.:
14
+ ```
15
+ depends 'firewall', '< 2.0'
16
+ ```
17
+
18
+ ### Supported firewalls and platforms
19
+ * UFW - Ubuntu, Debian (except 9)
20
+ * IPTables - Red Hat & CentOS, Ubuntu
21
+ * FirewallD - Red Hat & CentOS >= 7.0 (IPv4 only support, [needs contributions/testing](https://github.com/chef-cookbooks/firewall/issues/86))
22
+ * Windows Advanced Firewall - 2012 R2
23
+
24
+ Tested on:
25
+ * Ubuntu 14.04, 16.04 with iptables, ufw
26
+ * Debian 7, 8 with ufw
27
+ * Debian 9 with iptables
28
+ * CentOS 6 with iptables
29
+ * CentOS 7.1 with firewalld
30
+ * Windows Server 2012r2 with Windows Advanced Firewall
31
+
32
+ By default, Ubuntu chooses ufw. To switch to iptables, set this in an attribute file:
33
+ ```
34
+ default['firewall']['ubuntu_iptables'] = true
35
+ ```
36
+
37
+ By default, Red Hat & CentOS >= 7.0 chooses firewalld. To switch to iptables, set this in an attribute file:
38
+ ```
39
+ default['firewall']['redhat7_iptables'] = true
40
+ ```
41
+
42
+ # Considerations that apply to all firewall providers and resources
43
+
44
+ This cookbook comes with two resources, firewall and firewall rule. The typical usage scenario is as follows:
45
+
46
+ - run the `:install` action on the `firewall` resource named 'default', which installs appropriate packages and configures services to start on boot and starts them
47
+
48
+ - run the `:create` action on every `firewall_rule` resource, which adds to the list of rules that should be configured on the firewall. `firewall_rule` then automatically sends a delayed notification to the `firewall['default']` resource to run the `:restart` action.
49
+
50
+ - run the delayed notification with action `:restart` on the `firewall` resource. if any rules are different than the last run, the provider will update the current state of the firewall rules to match the expected rules.
51
+
52
+ There is a fundamental mismatch between the idea of a chef action and the action that should be taken on a firewall rule. For this reason, the chef action for a firewall_rule may be `:nothing` (the rule should not be present in the firewall) or `:create` (the rule should be present in the firewall), but the action taken on a packet in a firewall (`DROP`, `ACCEPT`, etc) is denoted as a `command` parameter on the `firewall_rule` resource.
53
+
54
+ # iptables considerations
55
+
56
+ If you need to use a table other than `*filter`, the best way to do so is like so:
57
+ ```
58
+ node.default['firewall']['iptables']['defaults'][:ruleset] = {
59
+ '*filter' => 1,
60
+ ':INPUT DROP' => 2,
61
+ ':FORWARD DROP' => 3,
62
+ ':OUTPUT ACCEPT_FILTER' => 4,
63
+ 'COMMIT_FILTER' => 100,
64
+ '*nat' => 101,
65
+ ':PREROUTING DROP' => 102,
66
+ ':POSTROUTING DROP' => 103,
67
+ ':OUTPUT ACCEPT_NAT' => 104,
68
+ 'COMMIT_NAT' => 200
69
+ }
70
+ ```
71
+
72
+ Note -- in order to support multiple hash keys containing the same rule, anything found after the underscore will be stripped for: `:OUTPUT :INPUT :POSTROUTING :PREROUTING COMMIT`. This allows an example like the above to be reduced to just repeated lines of `COMMIT` and `:OUTPUT ACCEPT` while still avoiding duplication of other things.
73
+
74
+ Then it's trivial to add additional rules to the `*nat` table using the raw parameter:
75
+ ```
76
+ firewall_rule "postroute" do
77
+ raw "-A POSTROUTING -o eth1 -p tcp -d 172.28.128.21 -j SNAT --to-source 172.28.128.6"
78
+ position 150
79
+ end
80
+ ```
81
+
82
+ Note that any line starting with `COMMIT` will become just `COMMIT`, as hash
83
+ keys must be unique but we need multiple commit lines.
84
+
85
+ # Recipes
86
+
87
+ ### default
88
+ The default recipe creates a firewall resource with action install.
89
+
90
+ ### disable_firewall
91
+ Used to disable platform specific firewall. Many clouds have their own firewall configured outside of the OS instance such as AWS Security Groups.
92
+
93
+ # Attributes
94
+
95
+ * `default['firewall']['allow_ssh'] = false`, set true to open port 22 for SSH when the default recipe runs
96
+ * `default['firewall']['allow_mosh'] = false`, set to true to open UDP ports 60000 - 61000 for [Mosh][0] when the default recipe runs
97
+ * `default['firewall']['allow_winrm'] = false`, set true to open port 5989 for WinRM when the default recipe runs
98
+ * `default['firewall']['allow_loopback'] = false`, set to true to allow all traffic on the loopback interface
99
+ * `default['firewall']['allow_icmp'] = false`, set true to allow icmp protocol on supported OSes (note: ufw and windows implementations don't support this)
100
+
101
+ * `default['firewall']['ubuntu_iptables'] = false`, set to true to use iptables on Ubuntu / Debian when using the default recipe
102
+ * `default['firewall']['redhat7_iptables'] = false`, set to true to use iptables on Red Hat / CentOS 7 when using the default recipe
103
+
104
+ * `default['firewall']['ufw']['defaults']` hash for template `/etc/default/ufw`
105
+ * `default['firewall']['iptables']['defaults']` hash for default policies for 'filter' table's chains`
106
+
107
+ * `default['firewall']['windows']['defaults']` hash to define inbound / outbound firewall policy on Windows platform
108
+
109
+ * `default['firewall']['allow_established'] = true`, set to false if you don't want a related/established default rule on iptables
110
+ * `default['firewall']['ipv6_enabled'] = true`, set to false if you don't want IPv6 related/established default rule on iptables (this enables ICMPv6, which is required for much of IPv6 communication)
111
+
112
+ * `default['firewall']['firewalld']['permanent'] = false`, set to true if you want firewalld rules to be added with `--permanent` so they survive a reboot. This will be changed to `true` by default in a future major version release.
113
+
114
+ # Resources
115
+
116
+ ### firewall
117
+
118
+ ***NB***: The name 'default' of this resource is important as it is used for firewall_rule providers to locate the firewall resource. If you change it, you must also supply the same value to any firewall_rule resources using the `firewall_name` parameter.
119
+
120
+ #### Actions
121
+ - `:install` (*default action*): Install and Enable the firewall. This will ensure the appropriate packages are installed and that any services have been started.
122
+ - `:disable`: Disable the firewall. Drop any rules and put the node in an unprotected state. Flush all current rules. Also erase any internal state used to detect when rules should be applied.
123
+ - `:flush`: Flush all current rules. Also erase any internal state used to detect when rules should be applied.
124
+ - `:save`: Ensure all rules are added permanently under firewalld using `--permanent`. Not supported on ufw, iptables. You must notify this action at the end of the chef run if you want permanent firewalld rules (they are not persistent by default).
125
+
126
+ #### Parameters
127
+
128
+ - `disabled` (default to `false`): If set to true, all actions will no-op on this resource. This is a way to prevent included cookbooks from configuring a firewall.
129
+ - `ipv6_enabled` (default to `true`): If set to false, firewall will not perform any ipv6 related work. Currently only supported in iptables.
130
+ - `log_level`: UFW only. Level of verbosity the firewall should log at. valid values are: :low, :medium, :high, :full, :off. default is :low.
131
+ - `rules`: This is used internally for firewall_rule resources to append their rules. You should NOT touch this value unless you plan to supply an entire firewall ruleset at once, and skip using firewall_rule resources.
132
+ - `disabled_zone` (firewalld only): The zone to set on firewalld when the firewall should be disabled. Can be any string in symbol form, e.g. :public, :drop, etc. Defaults to `:public.`
133
+ - `enabled_zone` (firewalld only): The zone to set on firewalld when the firewall should be enabled. Can be any string in symbol form, e.g. :public, :drop, etc. Defaults to `:drop.`
134
+ - `package_options`: Used to pass options to the package install of firewall
135
+
136
+ #### Examples
137
+
138
+ ```ruby
139
+ # all defaults
140
+ firewall 'default'
141
+
142
+ # enable platform default firewall
143
+ firewall 'default' do
144
+ action :install
145
+ end
146
+
147
+ # increase logging past default of 'low'
148
+ firewall 'default' do
149
+ log_level :high
150
+ action :install
151
+ end
152
+ ```
153
+
154
+ ### firewall_rule
155
+
156
+ #### Actions
157
+ - `:create` (_default action_): If a firewall_rule runs this action, the rule will be recorded in a chef resource's internal state, and applied when providers automatically notify the firewall resource with action `:reload`. The notification happens automatically.
158
+
159
+ #### Parameters
160
+
161
+ - `firewall_name`: the matching firewall resource that this rule applies to. Default value: `default`
162
+
163
+ - `raw`: Used to pass an entire rule as a string, omitting all other parameters. This line will be directly loaded by `iptables-restore`, fed directly into `ufw` on the command line, or run using `firewall-cmd`.
164
+
165
+ - `description` (_default: same as rule name_): Used to provide a comment that will be included when adding the firewall rule.
166
+
167
+ - `include_comment` (_default: true_): Used to optionally exclude the comment in the rule.
168
+
169
+ - `position` (_default: 50_): **relative** position to insert rule at. Position may be any integer between 0 < n < 100 (exclusive), and more than one rule may specify the same position.
170
+
171
+ - `command`: What action to take on a particular packet
172
+
173
+ - `:allow` (_default action_): the rule should allow matching packets
174
+ - `:deny`: the rule should deny matching packets
175
+ - `:reject`: the rule should reject matching packets
176
+ - `:masqerade`: Masquerade the matching packets
177
+ - `:redirect`: Redirect the matching packets
178
+ - `:log`: Configure logging
179
+
180
+ - `stateful`: a symbol or array of symbols, such as ``[:related, :established]` that will be passed to the state module in iptables or firewalld.
181
+
182
+ - `protocol`: `:tcp` (_default_), `:udp`, `:icmp`, `:none` or protocol number. Using protocol numbers is not supported using the ufw provider (default for debian/ubuntu systems).
183
+
184
+ - `direction`: For ufw, direction of the rule. valid values are: `:in` (_default_), `:out`, `:pre`, `:post`.
185
+
186
+ - `source` (_Default is `0.0.0.0/0` or `Anywhere`_): source ip address or subnet to filter.
187
+
188
+ - `source_port` (_Default is nil_): source port for filtering packets.
189
+
190
+ - `destination`: ip address or subnet to filter on packet destination, must be a valid IP
191
+
192
+ - `port` or `dest_port`: target port number (ie. 22 to allow inbound SSH), or an array of incoming port numbers (ie. [80,443] to allow inbound HTTP & HTTPS).
193
+
194
+ NOTE: `protocol` attribute is required with multiple ports, or a range of incoming port numbers (ie. 60000..61000 to allow inbound mobile-shell. NOTE: `protocol`, or an attribute is required with a range of ports.
195
+
196
+ - `interface`: (source) interface to apply rule (ie. `eth0`).
197
+
198
+ - `dest_interface`: interface where packets may be destined to go
199
+
200
+ - `redirect_port`: redirected port for rules with command `:redirect`
201
+
202
+ - `logging`: may be added to enable logging for a particular rule. valid values are: `:connections`, `:packets`. In the ufw provider, `:connections` logs new connections while `:packets` logs all packets.
203
+
204
+ #### Examples
205
+
206
+ ```ruby
207
+ # open standard ssh port
208
+ firewall_rule 'ssh' do
209
+ port 22
210
+ command :allow
211
+ end
212
+
213
+ # open standard http port to tcp traffic only; insert as first rule
214
+ firewall_rule 'http' do
215
+ port 80
216
+ protocol :tcp
217
+ position 1
218
+ command :allow
219
+ end
220
+
221
+ # restrict port 13579 to 10.0.111.0/24 on eth0
222
+ firewall_rule 'myapplication' do
223
+ port 13579
224
+ source '10.0.111.0/24'
225
+ direction :in
226
+ interface 'eth0'
227
+ command :allow
228
+ end
229
+
230
+ # specify a protocol number (supported on centos/redhat)
231
+ firewall_rule 'vrrp' do
232
+ protocol 112
233
+ command :allow
234
+ end
235
+
236
+ # use the iptables provider to specify protocol number on debian/ubuntu
237
+ firewall_rule 'vrrp' do
238
+ provider Chef::Provider::FirewallRuleIptables
239
+ protocol 112
240
+ command :allow
241
+ end
242
+
243
+ # can use :raw command with UFW provider for VRRP
244
+ firewall_rule "VRRP" do
245
+ command :allow
246
+ raw "allow to 224.0.0.18"
247
+ end
248
+
249
+ # open UDP ports 60000..61000 for mobile shell (mosh.mit.edu), note
250
+ # that the protocol attribute is required when using port_range
251
+ firewall_rule 'mosh' do
252
+ protocol :udp
253
+ port 60000..61000
254
+ command :allow
255
+ end
256
+
257
+ # open multiple ports for http/https, note that the protocol
258
+ # attribute is required when using ports
259
+ firewall_rule 'http/https' do
260
+ protocol :tcp
261
+ port [80, 443]
262
+ command :allow
263
+ end
264
+
265
+ firewall 'default' do
266
+ enabled false
267
+ action :nothing
268
+ end
269
+ ```
270
+
271
+ #### Providers
272
+
273
+ - See `libraries/z_provider_mapping.rb` for a full list of providers for each platform and version.
274
+
275
+ Different providers will determine the current state of the rules differently -- parsing the output of a command, maintaining the state in a file, or some other way. If the firewall is adjusted from outside of chef (non-idempotent), it's possible that chef may be caught unaware of the current state of the firewall. The best workaround is to add a `:flush` action to the firewall resource as early as possible in the chef run, if you plan to modify the firewall state outside of chef.
276
+
277
+ # Troubleshooting
278
+
279
+ To figure out what the position values are for current rules, print the hash that contains the weights:
280
+ ```
281
+ require pp
282
+ default_firewall = resources(:firewall, 'default')
283
+ pp default_firewall.rules
284
+ ```
285
+
286
+ # Development
287
+ This section details "quick development" steps. For a detailed explanation, see [[Contributing.md]].
288
+
289
+ 1. Clone this repository from GitHub:
290
+
291
+ $ git clone git@github.com:chef-cookbooks/firewall.git
292
+
293
+ 2. Create a git branch
294
+
295
+ $ git checkout -b my_bug_fix
296
+
297
+ 3. Install dependencies:
298
+
299
+ $ bundle install
300
+
301
+ 4. Make your changes/patches/fixes, committing appropiately
302
+ 5. **Write tests**
303
+ 6. Run the tests:
304
+ - `bundle exec foodcritic -f any .`
305
+ - `bundle exec rspec`
306
+ - `bundle exec rubocop`
307
+ - `bundle exec kitchen test`
308
+
309
+ In detail:
310
+ - Foodcritic will catch any Chef-specific style errors
311
+ - RSpec will run the unit tests
312
+ - Rubocop will check for Ruby-specific style errors
313
+ - Test Kitchen will run and converge the recipes
314
+
315
+
316
+ # License & Authors
317
+ <!-- $ find -type f -iname "*.rb" -exec grep -i author '{}' \; | sort -k4 | uniq | sed 's/#/-/g' -->
318
+ - Author:: Seth Chisamore (<schisamo@opscode.com>)
319
+ - Author:: Ronald Doorn (<rdoorn@schubergphilis.com>)
320
+ - Author:: Martin Smith (<martin@mbs3.org>)
321
+ - Author:: Sander van Harmelen (<svanharmelen@schubergphilis.com>)
322
+
323
+ ```text
324
+ Copyright:: 2011-2015, Chef Software, Inc
325
+
326
+ Licensed under the Apache License, Version 2.0 (the "License");
327
+ you may not use this file except in compliance with the License.
328
+ You may obtain a copy of the License at
329
+
330
+ http://www.apache.org/licenses/LICENSE-2.0
331
+
332
+ Unless required by applicable law or agreed to in writing, software
333
+ distributed under the License is distributed on an "AS IS" BASIS,
334
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
335
+ See the License for the specific language governing permissions and
336
+ limitations under the License.
337
+ ```
338
+
339
+ [0]: https://mosh.mit.edu/
@@ -0,0 +1,5 @@
1
+ default['firewall']['allow_ssh'] = false
2
+ default['firewall']['allow_winrm'] = false
3
+ default['firewall']['allow_mosh'] = false
4
+ default['firewall']['allow_loopback'] = false
5
+ default['firewall']['allow_icmp'] = false
@@ -0,0 +1 @@
1
+ default['firewall']['firewalld']['permanent'] = false
@@ -0,0 +1,17 @@
1
+ default['firewall']['iptables']['defaults'][:policy] = {
2
+ input: 'DROP',
3
+ forward: 'DROP',
4
+ output: 'ACCEPT',
5
+ }
6
+ default['firewall']['iptables']['defaults'][:ruleset] = {
7
+ '*filter' => 1,
8
+ ":INPUT #{node['firewall']['iptables']['defaults'][:policy][:input]}" => 2,
9
+ ":FORWARD #{node['firewall']['iptables']['defaults'][:policy][:forward]}" => 3,
10
+ ":OUTPUT #{node['firewall']['iptables']['defaults'][:policy][:output]}" => 4,
11
+ 'COMMIT_FILTER' => 100,
12
+ }
13
+
14
+ default['firewall']['ubuntu_iptables'] = false
15
+ default['firewall']['redhat7_iptables'] = false
16
+ default['firewall']['allow_established'] = true
17
+ default['firewall']['ipv6_enabled'] = true
@@ -0,0 +1,12 @@
1
+ default['firewall']['ufw']['defaults'] = {
2
+ ipv6: 'yes',
3
+ manage_builtins: 'no',
4
+ ipt_sysctl: '/etc/ufw/sysctl.conf',
5
+ ipt_modules: 'nf_conntrack_ftp nf_nat_ftp nf_conntrack_netbios_ns',
6
+ policy: {
7
+ input: 'DROP',
8
+ output: 'ACCEPT',
9
+ forward: 'DROP',
10
+ application: 'SKIP',
11
+ },
12
+ }
@@ -0,0 +1,8 @@
1
+ # Windows platform defult settings: block undefined inbould traffic, allow all outgoing traffic
2
+
3
+ default['firewall']['windows']['defaults'] = {
4
+ policy: {
5
+ input: 'blockinbound',
6
+ output: 'allowoutbound',
7
+ },
8
+ }
@@ -0,0 +1,100 @@
1
+ module FirewallCookbook
2
+ module Helpers
3
+ def dport_calc(new_resource)
4
+ new_resource.dest_port || new_resource.port
5
+ end
6
+
7
+ def port_to_s(p)
8
+ if p.is_a?(String)
9
+ p
10
+ elsif p && p.is_a?(Integer)
11
+ p.to_s
12
+ elsif p && p.is_a?(Array)
13
+ p_strings = p.map { |o| port_to_s(o) }
14
+ p_strings.sort.join(',')
15
+ elsif p && p.is_a?(Range)
16
+ if platform_family?('windows')
17
+ "#{p.first}-#{p.last}"
18
+ else
19
+ "#{p.first}:#{p.last}"
20
+ end
21
+ end
22
+ end
23
+
24
+ def ipv6_enabled?(new_resource)
25
+ new_resource.ipv6_enabled
26
+ end
27
+
28
+ def disabled?(new_resource)
29
+ # if either flag is found in the non-default boolean state
30
+ disable_flag = !(new_resource.enabled && !new_resource.disabled)
31
+
32
+ Chef::Log.warn("#{new_resource} has been disabled, not proceeding") if disable_flag
33
+ disable_flag
34
+ end
35
+
36
+ def ip_with_mask(new_resource, ip)
37
+ if ip.include?('/')
38
+ ip
39
+ elsif ipv4_rule?(new_resource)
40
+ "#{ip}/32"
41
+ elsif ipv6_rule?(new_resource)
42
+ "#{ip}/128"
43
+ else
44
+ ip
45
+ end
46
+ end
47
+
48
+ # ipv4-specific rule?
49
+ def ipv4_rule?(new_resource)
50
+ if (new_resource.source && IPAddr.new(new_resource.source).ipv4?) ||
51
+ (new_resource.destination && IPAddr.new(new_resource.destination).ipv4?)
52
+ true
53
+ else
54
+ false
55
+ end
56
+ end
57
+
58
+ # ipv6-specific rule?
59
+ def ipv6_rule?(new_resource)
60
+ if (new_resource.source && IPAddr.new(new_resource.source).ipv6?) ||
61
+ (new_resource.destination && IPAddr.new(new_resource.destination).ipv6?) ||
62
+ new_resource.protocol =~ /ipv6/ ||
63
+ new_resource.protocol =~ /icmpv6/
64
+ true
65
+ else
66
+ false
67
+ end
68
+ end
69
+
70
+ def debian?(current_node)
71
+ current_node['platform_family'] == 'debian'
72
+ end
73
+
74
+ def build_rule_file(rules)
75
+ contents = []
76
+ sorted_values = rules.values.sort.uniq
77
+ sorted_values.each do |sorted_value|
78
+ contents << "# position #{sorted_value}"
79
+ rules.each do |k, v|
80
+ next unless v == sorted_value
81
+
82
+ contents << if repeatable_directives(k)
83
+ k[/[^_]+/]
84
+ else
85
+ k
86
+ end
87
+ end
88
+ end
89
+ "#{contents.join("\n")}\n"
90
+ end
91
+
92
+ def repeatable_directives(s)
93
+ %w(:OUTPUT :INPUT :POSTROUTING :PREROUTING COMMIT).each do |special|
94
+ return true if s.start_with?(special)
95
+ end
96
+
97
+ false
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,116 @@
1
+ module FirewallCookbook
2
+ module Helpers
3
+ module Firewalld
4
+ include FirewallCookbook::Helpers
5
+ include Chef::Mixin::ShellOut
6
+
7
+ def firewalld_rules_filename
8
+ '/etc/sysconfig/firewalld-chef.rules'
9
+ end
10
+
11
+ def firewalld_rule!(cmd)
12
+ shell_out!(cmd, input: 'yes')
13
+ end
14
+
15
+ def firewalld_active?
16
+ cmd = shell_out('firewall-cmd', '--state')
17
+ cmd.stdout =~ /^running$/
18
+ end
19
+
20
+ def firewalld_default_zone?(z)
21
+ return false unless firewalld_active?
22
+
23
+ cmd = shell_out('firewall-cmd', '--get-default-zone')
24
+ cmd.stdout =~ /^#{z.to_s}$/
25
+ end
26
+
27
+ def firewalld_default_zone!(z)
28
+ raise 'firewalld not active' unless firewalld_active?
29
+
30
+ shell_out!('firewall-cmd', "--set-default-zone=#{z}")
31
+ end
32
+
33
+ def log_current_firewalld
34
+ shell_out!('firewall-cmd --direct --get-all-rules')
35
+ end
36
+
37
+ def firewalld_flush!
38
+ raise 'firewall not active' unless firewalld_active?
39
+
40
+ shell_out!('firewall-cmd', '--direct', '--remove-rules', 'ipv4', 'filter', 'INPUT')
41
+ shell_out!('firewall-cmd', '--direct', '--remove-rules', 'ipv4', 'filter', 'OUTPUT')
42
+ shell_out!('firewall-cmd', '--direct', '--permanent', '--remove-rules', 'ipv4', 'filter', 'INPUT')
43
+ shell_out!('firewall-cmd', '--direct', '--permanent', '--remove-rules', 'ipv4', 'filter', 'OUTPUT')
44
+ end
45
+
46
+ def firewalld_all_rules_permanent!
47
+ raise 'firewall not active' unless firewalld_active?
48
+
49
+ rules = shell_out!('firewall-cmd', '--direct', '--get-all-rules').stdout
50
+ perm_rules = shell_out!('firewall-cmd', '--direct', '--permanent', '--get-all-rules').stdout
51
+ rules == perm_rules
52
+ end
53
+
54
+ def firewalld_save!
55
+ raise 'firewall not active' unless firewalld_active?
56
+
57
+ shell_out!('firewall-cmd', '--direct', '--permanent', '--remove-rules', 'ipv4', 'filter', 'INPUT')
58
+ shell_out!('firewall-cmd', '--direct', '--permanent', '--remove-rules', 'ipv4', 'filter', 'OUTPUT')
59
+ shell_out!('firewall-cmd', '--direct', '--get-all-rules').stdout.lines do |line|
60
+ shell_out!("firewall-cmd --direct --permanent --add-rule #{line}")
61
+ end
62
+ end
63
+
64
+ def ip_versions(resource)
65
+ if ipv4_rule?(resource)
66
+ %w(ipv4)
67
+ elsif ipv6_rule?(resource)
68
+ %w(ipv6)
69
+ else # no source or destination address, add rules for both ipv4 and ipv6
70
+ %w(ipv4 ipv6)
71
+ end
72
+ end
73
+
74
+ CHAIN = { in: 'INPUT', out: 'OUTPUT', pre: 'PREROUTING', post: 'POSTROUTING' }.freeze unless defined? CHAIN # , nil => "FORWARD"}
75
+ TARGET = { allow: 'ACCEPT', reject: 'REJECT', deny: 'DROP', masquerade: 'MASQUERADE', redirect: 'REDIRECT', log: 'LOG --log-prefix \'iptables: \' --log-level 7' }.freeze unless defined? TARGET
76
+
77
+ def build_firewall_rule(new_resource, ip_version = 'ipv4')
78
+ return new_resource.raw.strip if new_resource.raw
79
+
80
+ type = new_resource.command
81
+ firewall_rule = if new_resource.direction
82
+ "#{ip_version} filter #{CHAIN[new_resource.direction.to_sym]} "
83
+ else
84
+ "#{ip_version} filter FORWARD "
85
+ end
86
+ firewall_rule << "#{new_resource.position} "
87
+
88
+ if [:pre, :post].include?(new_resource.direction)
89
+ firewall_rule << '-t nat '
90
+ end
91
+
92
+ # Firewalld order of prameters is important here see example output below:
93
+ # ipv4 filter INPUT 1 -s 1.2.3.4/32 -d 5.6.7.8/32 -i lo -p tcp -m tcp -m state --state NEW -m comment --comment "hello" -j DROP
94
+ firewall_rule << "-s #{ip_with_mask(new_resource, new_resource.source)} " if new_resource.source && new_resource.source != '0.0.0.0/0'
95
+ firewall_rule << "-d #{new_resource.destination} " if new_resource.destination
96
+
97
+ firewall_rule << "-i #{new_resource.interface} " if new_resource.interface
98
+ firewall_rule << "-o #{new_resource.dest_interface} " if new_resource.dest_interface
99
+
100
+ firewall_rule << "-p #{new_resource.protocol} " if new_resource.protocol && new_resource.protocol.to_s.to_sym != :none
101
+ firewall_rule << '-m tcp ' if new_resource.protocol && new_resource.protocol.to_s.to_sym == :tcp
102
+
103
+ # using multiport here allows us to simplify our greps and rule building
104
+ firewall_rule << "-m multiport --sports #{port_to_s(new_resource.source_port)} " if new_resource.source_port
105
+ firewall_rule << "-m multiport --dports #{port_to_s(dport_calc(new_resource))} " if dport_calc(new_resource)
106
+
107
+ firewall_rule << "-m state --state #{new_resource.stateful.is_a?(Array) ? new_resource.stateful.join(',').upcase : new_resource.stateful.to_s.upcase} " if new_resource.stateful
108
+ firewall_rule << "-m comment --comment '#{new_resource.description}' " if new_resource.include_comment
109
+ firewall_rule << "-j #{TARGET[type]} "
110
+ firewall_rule << "--to-ports #{new_resource.redirect_port} " if type == :redirect
111
+ firewall_rule.strip!
112
+ firewall_rule
113
+ end
114
+ end
115
+ end
116
+ end