Vendor the external cookbooks
Knife-Zero doesn't include Berkshelf support, so vendoring everything in the repo is convenient again
This commit is contained in:
3
cookbooks/firewall/.foodcritic
Normal file
3
cookbooks/firewall/.foodcritic
Normal file
@@ -0,0 +1,3 @@
|
||||
~FC001
|
||||
~FC057
|
||||
~FC019
|
||||
291
cookbooks/firewall/CHANGELOG.md
Normal file
291
cookbooks/firewall/CHANGELOG.md
Normal file
@@ -0,0 +1,291 @@
|
||||
firewall Cookbook CHANGELOG
|
||||
=======================
|
||||
This file is used to list changes made in each version of the firewall cookbook.
|
||||
|
||||
## 2.6.5 (2018-07-24)
|
||||
|
||||
- use platform_family instead of platform to include all rhels
|
||||
|
||||
v2.6.4 (2018-07-01)
|
||||
-------------------
|
||||
* Stop including chef-sugar when it's >= 4.0.0 (#197)
|
||||
|
||||
v2.6.3 (2018-02-01)
|
||||
-------------------
|
||||
* Fix issue with deep merging of hashes and arrays in recent chef release (#185)
|
||||
|
||||
v2.6.2 (2017-06-01)
|
||||
-------------------
|
||||
* Incorrect file checking on Ubuntu, double file write (#173)
|
||||
* Added testing on CentOS 6.9
|
||||
* Clarify metadata that we're not working on Amazon Linux (#172)
|
||||
|
||||
v2.6.1 (2017-04-21)
|
||||
-------------------
|
||||
* Add recipe to disable firewall (#164)
|
||||
|
||||
v2.6.0 (2017-04-17)
|
||||
-------------------
|
||||
* Initial Chef 13.x support (#160, #159)
|
||||
* Allow loopback and icmp, when enabled (#161)
|
||||
* Address various newer rubocop and foodcritic complaints
|
||||
* Convert rule provider away from DSL (#159)
|
||||
|
||||
v2.5.4 (2017-02-13)
|
||||
-------------------
|
||||
* Update Test Kitchen platforms to the latest
|
||||
* Update copyright headers
|
||||
* Allow package options to be passed through to the package install for firewall
|
||||
* Define policy for Windows Firewall and use the attributes to set desired policy
|
||||
|
||||
v2.5.3 (2016-10-26)
|
||||
-------------------
|
||||
* Don't show firewall resource as updated (#133)
|
||||
* Add :off as a valid logging level (#129)
|
||||
* Add support for Ubuntu 16.04 (#149)
|
||||
|
||||
v2.5.2 (2016-06-02)
|
||||
-------------------
|
||||
* Don't issue commands when firewalld isn't active (#140)
|
||||
* Install iptables-services on CentOS >= 7 (#131)
|
||||
* Update Ruby version on Travis for listen gem
|
||||
|
||||
v2.5.1 (2016-05-31)
|
||||
-------------------
|
||||
* Protocol guard incorrectly prevents "none" protocol type on UFW helper (#128)
|
||||
* Fix wrongly ordered conditional for converting ports to strings using port_to_s
|
||||
* Fix notify_firewall attribute crashing firewall_rule provider (#130)
|
||||
* Add warning if firewall rule opens all traffic (#132)
|
||||
* Add ipv6 attribute respect to Ubuntu iptables (#138)
|
||||
|
||||
v2.5.0 (2016-03-08)
|
||||
-------------------
|
||||
* Don't modify parameter for port (#120)
|
||||
* Remove a reference to the wrong variable name under windows (#123)
|
||||
* Add support for mobile shell default firewall rule (#121)
|
||||
* New rubocop rules and style fixes
|
||||
* Correct a README.md example for `action :allow`
|
||||
|
||||
v2.4.0 (2016-01-28)
|
||||
-------------------
|
||||
* Expose default iptables ruleset so that raw rules can be used in conjunction
|
||||
with rulesets for other tables (#101).
|
||||
|
||||
v2.3.1 (2016-01-08)
|
||||
-------------------
|
||||
* Add raw rule support to the ufw firewall provider (#113).
|
||||
|
||||
v2.3.0 (2015-12-23)
|
||||
-------------------
|
||||
* Refactor logic so that firewall rules don't add a string rule to the firewall
|
||||
when their actions run. Just run the action once on the firewall itself. This is
|
||||
designed to prevent partial application of rules (#106)
|
||||
|
||||
* Switch to "enabled" (positive logic) instead of "disabled" (negative logic) on
|
||||
the firewall resource. It was difficult to reason with "disabled false" for some
|
||||
complicated recipes using firewall downstream. `disabled` is now deprecated.
|
||||
|
||||
* Add proper Windows testing and serverspec tests back into this cookbook.
|
||||
|
||||
* Fix the `port_to_s` function so it also works for Windows (#111)
|
||||
|
||||
* Fix typo checking action instead of command in iptables helper (#112)
|
||||
|
||||
* Remove testing ranges of ports on CentOS 5.x, as it's broken there.
|
||||
|
||||
v2.2.0 (2015-11-02)
|
||||
-------------------
|
||||
Added permanent as default option for RHEL 7 based systems using firewall-cmd.
|
||||
This defaults to turned off, but it will be enabled by default on the next major version bump.
|
||||
|
||||
v2.1.0 (2015-10-15)
|
||||
-------------------
|
||||
Minor feature release.
|
||||
* Ensure ICMPv6 is open when `['firewall']['allow_established']` is set to true (the default). ICMPv6 is critical for most IPv6 operations.
|
||||
|
||||
v2.0.5 (2015-10-05)
|
||||
-------------------
|
||||
Minor bugfix release.
|
||||
* Ensure provider filtering always yields 1 and only 1 provider, #97 & #98
|
||||
* Documentation update #96
|
||||
|
||||
v2.0.4 (2015-09-23)
|
||||
-------------------
|
||||
Minor bugfix release.
|
||||
* Allow override of filter chain policies, #94
|
||||
* Fix foodcrtitic and chefspec errors
|
||||
|
||||
v2.0.3 (2015-09-14)
|
||||
-------------------
|
||||
Minor bugfix release.
|
||||
* Fix wrong conditional for firewalld ports, #93
|
||||
* Fix ipv6 command logic under iptables, #91
|
||||
|
||||
v2.0.2 (2015-09-08)
|
||||
-------------------
|
||||
* Release with working CI, Chefspec matchers.
|
||||
|
||||
v2.0.1 (2015-09-01)
|
||||
-------------------
|
||||
* Add default related/established rule for iptables
|
||||
|
||||
v2.0.0 (2015-08-31)
|
||||
-------------------
|
||||
* #84, major rewrite:
|
||||
- Allow relative positioning of rules
|
||||
- Use delayed notifications to create one firewall ruleset instead of incremental changes
|
||||
- Remove poise dependency
|
||||
* #82 - Introduce Windows firewall support and test-kitchen platform.
|
||||
* #73 - Add the option to disable ipv6 commands on iptables
|
||||
* #78 - Use Chef-12 style `provides` to address provider mapping issues
|
||||
* Rubocop and foodcritic cleanup
|
||||
|
||||
v1.6.1 (2015-07-24)
|
||||
-------------------
|
||||
* #80 - Remove an extra space in port range
|
||||
|
||||
v1.6.0 (2015-07-15)
|
||||
-------------------
|
||||
* #68 - Install firewalld when it does not exist
|
||||
* #72 - Fix symbol that was a string, breaking comparisons
|
||||
|
||||
v1.5.2 (2015-07-15)
|
||||
-------------------
|
||||
* #75 - Use correct service in iptables save action, Add serverspec tests for iptables suite
|
||||
|
||||
v1.5.1 (2015-07-13)
|
||||
-------------------
|
||||
* #74 - add :save matcher for Chefspec
|
||||
|
||||
v1.5.0 (2015-07-06)
|
||||
-------------------
|
||||
|
||||
* #70 - Add chef service resource to ensure firewall-related services are enabled/disabled
|
||||
* - Add testing and support for iptables on ubuntu in iptables provider
|
||||
|
||||
v1.4.0 (2015-06-30)
|
||||
-------------------
|
||||
|
||||
* #69 - Support for CentOS/RHEL 5.x
|
||||
|
||||
v1.3.0 (2015-06-09)
|
||||
-------------------
|
||||
* #63 - Add support for protocol numbers
|
||||
|
||||
v1.2.0 (2015-05-28)
|
||||
-------------------
|
||||
* #64 - Support the newer version of poise
|
||||
|
||||
v1.1.2 (2015-05-19)
|
||||
-------------------
|
||||
* #60 - Always add /32 or /128 to ipv4 or ipv6 addresses, respectively.
|
||||
- Make comment quoting optional; iptables on Ubuntu strips quotes on strings without any spaces
|
||||
|
||||
v1.1.1 (2015-05-11)
|
||||
-------------------
|
||||
* #57 - Suppress warning: already initialized constant XXX while Chefspec
|
||||
|
||||
v1.1.0 (2015-04-27)
|
||||
-------------------
|
||||
* #56 - Better ipv6 support for firewalld and iptables
|
||||
* #54 - Document raw parameter
|
||||
|
||||
v1.0.2 (2015-04-03)
|
||||
-------------------
|
||||
* #52 - Typo in :masquerade action name
|
||||
|
||||
v1.0.1 (2015-03-28)
|
||||
-------------------
|
||||
* #49 - Fix position attribute of firewall_rule providers to be correctly used as a string in commands
|
||||
|
||||
v1.0.0 (2015-03-25)
|
||||
-------------------
|
||||
* Major upgrade and rewrite as HWRP using poise
|
||||
* Adds support for iptables and firewalld
|
||||
* Modernize tests and other files
|
||||
* Fix many bugs from ufw defaults to multiport suppot
|
||||
|
||||
v0.11.8 (2014-05-20)
|
||||
--------------------
|
||||
* Corrects issue where on a secondary converge would not distinguish between inbound and outbound rules
|
||||
|
||||
|
||||
v0.11.6 (2014-02-28)
|
||||
--------------------
|
||||
[COOK-4385] - UFW provider is broken
|
||||
|
||||
|
||||
v0.11.4 (2014-02-25)
|
||||
--------------------
|
||||
[COOK-4140] Only notify when a rule is actually added
|
||||
|
||||
|
||||
v0.11.2
|
||||
-------
|
||||
### Bug
|
||||
- **[COOK-3615](https://tickets.opscode.com/browse/COOK-3615)** - Install required UFW package on Debian
|
||||
|
||||
v0.11.0
|
||||
-------
|
||||
### Improvement
|
||||
- [COOK-2932]: ufw providers work on debian but cannot be used
|
||||
|
||||
v0.10.2
|
||||
-------
|
||||
- [COOK-2250] - improve readme
|
||||
|
||||
v0.10.0
|
||||
------
|
||||
- [COOK-1234] - allow multiple ports per rule
|
||||
|
||||
v0.9.2
|
||||
------
|
||||
- [COOK-1615] - Firewall example docs have incorrect direction syntax
|
||||
|
||||
v0.9.0
|
||||
------
|
||||
The default action for firewall LWRP is now :enable, the default action for firewall_rule LWRP is now :reject. This is in line with a "default deny" policy.
|
||||
|
||||
- [COOK-1429] - resolve foodcritic warnings
|
||||
|
||||
v0.8.0
|
||||
------
|
||||
- refactor all resources and providers into LWRPs
|
||||
- removed :reset action from firewall resource (couldn't find a good way to make it idempotent)
|
||||
- removed :logging action from firewall resource...just set desired level via the log_level attribute
|
||||
|
||||
v0.6.0
|
||||
------
|
||||
- [COOK-725] Firewall cookbook firewall_rule LWRP needs to support logging attribute.
|
||||
- Firewall cookbook firewall LWRP needs to support :logging
|
||||
|
||||
v0.5.7
|
||||
------
|
||||
- [COOK-696] Firewall cookbook firewall_rule LWRP needs to support interface
|
||||
- [COOK-697] Firewall cookbook firewall_rule LWRP needs to support the direction for the rules
|
||||
|
||||
v0.5.6
|
||||
------
|
||||
- [COOK-695] Firewall cookbook firewall_rule LWRP needs to support destination port
|
||||
|
||||
v0.5.5
|
||||
------
|
||||
- [COOK-709] fixed :nothing action for the 'firewall_rule' resource.
|
||||
|
||||
v0.5.4
|
||||
------
|
||||
- [COOK-694] added :reject action to the 'firewall_rule' resource.
|
||||
|
||||
v0.5.3
|
||||
------
|
||||
- [COOK-698] added :reset action to the 'firewall' resource.
|
||||
|
||||
v0.5.2
|
||||
------
|
||||
- Add missing 'requires' statements. fixes 'NameError: uninitialized constant' error.
|
||||
thanks to Ernad Husremović for the fix.
|
||||
|
||||
v0.5.0
|
||||
------
|
||||
- [COOK-686] create firewall and firewall_rule resources
|
||||
- [COOK-687] create UFW providers for all resources
|
||||
2
cookbooks/firewall/CONTRIBUTING.md
Normal file
2
cookbooks/firewall/CONTRIBUTING.md
Normal file
@@ -0,0 +1,2 @@
|
||||
Please refer to
|
||||
https://github.com/chef-cookbooks/community_cookbook_documentation/blob/master/CONTRIBUTING.MD
|
||||
19
cookbooks/firewall/MAINTAINERS.md
Normal file
19
cookbooks/firewall/MAINTAINERS.md
Normal file
@@ -0,0 +1,19 @@
|
||||
<!-- This is a generated file. Please do not edit directly -->
|
||||
|
||||
# Maintainers
|
||||
This file lists how this cookbook project is maintained. When making changes to the system, this
|
||||
file tells you who needs to review your patch - you need a simple majority of maintainers
|
||||
for the relevant subsystems to provide a :+1: on your pull request. Additionally, you need
|
||||
to not receive a veto from a Lieutenant or the Project Lead.
|
||||
|
||||
Check out [How Cookbooks are Maintained](https://github.com/chef-cookbooks/community_cookbook_documentation/blob/master/CONTRIBUTING.MD)
|
||||
for details on the process and how to become a maintainer or the project lead.
|
||||
|
||||
# Project Maintainer
|
||||
* [Martin Smith](https://github.com/martinb3)
|
||||
|
||||
# Maintainers
|
||||
* [Jennifer Davis](https://github.com/sigje)
|
||||
* [Tim Smith](https://github.com/tas50)
|
||||
* [Thom May](https://github.com/thommay)
|
||||
* [Martin Smith](https://github.com/martinb3)
|
||||
338
cookbooks/firewall/README.md
Normal file
338
cookbooks/firewall/README.md
Normal file
@@ -0,0 +1,338 @@
|
||||
firewall Cookbook
|
||||
=================
|
||||
|
||||
[](http://travis-ci.org/chef-cookbooks/firewall)
|
||||
[](https://supermarket.chef.io/cookbooks/firewall)
|
||||
|
||||
Provides a set of primitives for managing firewalls and associated rules.
|
||||
|
||||
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.
|
||||
|
||||
Requirements
|
||||
------------
|
||||
**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.:
|
||||
```
|
||||
depends 'firewall', '< 2.0'
|
||||
```
|
||||
|
||||
### Supported firewalls and platforms
|
||||
* UFW - Ubuntu, Debian
|
||||
* IPTables - Red Hat & CentOS, Ubuntu
|
||||
* FirewallD - Red Hat & CentOS >= 7.0 (IPv4 only support, [needs contributions/testing](https://github.com/chef-cookbooks/firewall/issues/86))
|
||||
* Windows Advanced Firewall - 2012 R2
|
||||
|
||||
Tested on:
|
||||
* Ubuntu 14.04, 16.04 with iptables, ufw
|
||||
* Debian 7, 8 with ufw
|
||||
* CentOS 6 with iptables
|
||||
* CentOS 7.1 with firewalld
|
||||
* Windows Server 2012r2 with Windows Advanced Firewall
|
||||
|
||||
By default, Ubuntu chooses ufw. To switch to iptables, set this in an attribute file:
|
||||
```
|
||||
default['firewall']['ubuntu_iptables'] = true
|
||||
```
|
||||
|
||||
By default, Red Hat & CentOS >= 7.0 chooses firewalld. To switch to iptables, set this in an attribute file:
|
||||
```
|
||||
default['firewall']['redhat7_iptables'] = true
|
||||
```
|
||||
|
||||
# Considerations that apply to all firewall providers and resources
|
||||
|
||||
This cookbook comes with two resources, firewall and firewall rule. The typical usage scenario is as follows:
|
||||
|
||||
- run the `:install` action on the `firewall` resource named 'default', which installs appropriate packages and configures services to start on boot and starts them
|
||||
|
||||
- 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.
|
||||
|
||||
- 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.
|
||||
|
||||
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.
|
||||
|
||||
# iptables considerations
|
||||
|
||||
If you need to use a table other than `*filter`, the best way to do so is like so:
|
||||
```
|
||||
node.default['firewall']['iptables']['defaults'][:ruleset] = {
|
||||
'*filter' => 1,
|
||||
':INPUT DROP' => 2,
|
||||
':FORWARD DROP' => 3,
|
||||
':OUTPUT ACCEPT_FILTER' => 4,
|
||||
'COMMIT_FILTER' => 100,
|
||||
'*nat' => 101,
|
||||
':PREROUTING DROP' => 102,
|
||||
':POSTROUTING DROP' => 103,
|
||||
':OUTPUT ACCEPT_NAT' => 104,
|
||||
'COMMIT_NAT' => 200
|
||||
}
|
||||
```
|
||||
|
||||
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.
|
||||
|
||||
Then it's trivial to add additional rules to the `*nat` table using the raw parameter:
|
||||
```
|
||||
firewall_rule "postroute" do
|
||||
raw "-A POSTROUTING -o eth1 -p tcp -d 172.28.128.21 -j SNAT --to-source 172.28.128.6"
|
||||
position 150
|
||||
end
|
||||
```
|
||||
|
||||
Note that any line starting with `COMMIT` will become just `COMMIT`, as hash
|
||||
keys must be unique but we need multiple commit lines.
|
||||
|
||||
# Recipes
|
||||
|
||||
### default
|
||||
The default recipe creates a firewall resource with action install.
|
||||
|
||||
### disable_firewall
|
||||
Used to disable platform specific firewall. Many clouds have their own firewall configured outside of the OS instance such as AWS Security Groups.
|
||||
|
||||
# Attributes
|
||||
|
||||
* `default['firewall']['allow_ssh'] = false`, set true to open port 22 for SSH when the default recipe runs
|
||||
* `default['firewall']['allow_mosh'] = false`, set to true to open UDP ports 60000 - 61000 for [Mosh][0] when the default recipe runs
|
||||
* `default['firewall']['allow_winrm'] = false`, set true to open port 5989 for WinRM when the default recipe runs
|
||||
* `default['firewall']['allow_loopback'] = false`, set to true to allow all traffic on the loopback interface
|
||||
* `default['firewall']['allow_icmp'] = false`, set true to allow icmp protocol on supported OSes (note: ufw and windows implementations don't support this)
|
||||
|
||||
* `default['firewall']['ubuntu_iptables'] = false`, set to true to use iptables on Ubuntu / Debian when using the default recipe
|
||||
* `default['firewall']['redhat7_iptables'] = false`, set to true to use iptables on Red Hat / CentOS 7 when using the default recipe
|
||||
|
||||
* `default['firewall']['ufw']['defaults']` hash for template `/etc/default/ufw`
|
||||
* `default['firewall']['iptables']['defaults']` hash for default policies for 'filter' table's chains`
|
||||
|
||||
* `default['firewall']['windows']['defaults']` hash to define inbound / outbound firewall policy on Windows platform
|
||||
|
||||
* `default['firewall']['allow_established'] = true`, set to false if you don't want a related/established default rule on iptables
|
||||
* `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)
|
||||
|
||||
* `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.
|
||||
|
||||
# Resources
|
||||
|
||||
### firewall
|
||||
|
||||
***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.
|
||||
|
||||
#### Actions
|
||||
- `:install` (*default action*): Install and Enable the firewall. This will ensure the appropriate packages are installed and that any services have been started.
|
||||
- `: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.
|
||||
- `:flush`: Flush all current rules. Also erase any internal state used to detect when rules should be applied.
|
||||
- `: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).
|
||||
|
||||
#### Parameters
|
||||
|
||||
- `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.
|
||||
- `ipv6_enabled` (default to `true`): If set to false, firewall will not perform any ipv6 related work. Currently only supported in iptables.
|
||||
- `log_level`: UFW only. Level of verbosity the firewall should log at. valid values are: :low, :medium, :high, :full, :off. default is :low.
|
||||
- `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.
|
||||
- `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.`
|
||||
- `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.`
|
||||
- `package_options`: Used to pass options to the package install of firewall
|
||||
|
||||
#### Examples
|
||||
|
||||
```ruby
|
||||
# all defaults
|
||||
firewall 'default'
|
||||
|
||||
# enable platform default firewall
|
||||
firewall 'default' do
|
||||
action :install
|
||||
end
|
||||
|
||||
# increase logging past default of 'low'
|
||||
firewall 'default' do
|
||||
log_level :high
|
||||
action :install
|
||||
end
|
||||
```
|
||||
|
||||
### firewall_rule
|
||||
|
||||
#### Actions
|
||||
- `: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.
|
||||
|
||||
#### Parameters
|
||||
|
||||
- `firewall_name`: the matching firewall resource that this rule applies to. Default value: `default`
|
||||
|
||||
- `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`.
|
||||
|
||||
- `description` (_default: same as rule name_): Used to provide a comment that will be included when adding the firewall rule.
|
||||
|
||||
- `include_comment` (_default: true_): Used to optionally exclude the comment in the rule.
|
||||
|
||||
- `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.
|
||||
|
||||
- `command`: What action to take on a particular packet
|
||||
|
||||
- `:allow` (_default action_): the rule should allow matching packets
|
||||
- `:deny`: the rule should deny matching packets
|
||||
- `:reject`: the rule should reject matching packets
|
||||
- `:masqerade`: Masquerade the matching packets
|
||||
- `:redirect`: Redirect the matching packets
|
||||
- `:log`: Configure logging
|
||||
|
||||
- `stateful`: a symbol or array of symbols, such as ``[:related, :established]` that will be passed to the state module in iptables or firewalld.
|
||||
|
||||
- `protocol`: `:tcp` (_default_), `:udp`, `:icmp`, `:none` or protocol number. Using protocol numbers is not supported using the ufw provider (default for debian/ubuntu systems).
|
||||
|
||||
- `direction`: For ufw, direction of the rule. valid values are: `:in` (_default_), `:out`, `:pre`, `:post`.
|
||||
|
||||
- `source` (_Default is `0.0.0.0/0` or `Anywhere`_): source ip address or subnet to filter.
|
||||
|
||||
- `source_port` (_Default is nil_): source port for filtering packets.
|
||||
|
||||
- `destination`: ip address or subnet to filter on packet destination, must be a valid IP
|
||||
|
||||
- `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).
|
||||
|
||||
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.
|
||||
|
||||
- `interface`: (source) interface to apply rule (ie. `eth0`).
|
||||
|
||||
- `dest_interface`: interface where packets may be destined to go
|
||||
|
||||
- `redirect_port`: redirected port for rules with command `:redirect`
|
||||
|
||||
- `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.
|
||||
|
||||
#### Examples
|
||||
|
||||
```ruby
|
||||
# open standard ssh port
|
||||
firewall_rule 'ssh' do
|
||||
port 22
|
||||
command :allow
|
||||
end
|
||||
|
||||
# open standard http port to tcp traffic only; insert as first rule
|
||||
firewall_rule 'http' do
|
||||
port 80
|
||||
protocol :tcp
|
||||
position 1
|
||||
command :allow
|
||||
end
|
||||
|
||||
# restrict port 13579 to 10.0.111.0/24 on eth0
|
||||
firewall_rule 'myapplication' do
|
||||
port 13579
|
||||
source '10.0.111.0/24'
|
||||
direction :in
|
||||
interface 'eth0'
|
||||
command :allow
|
||||
end
|
||||
|
||||
# specify a protocol number (supported on centos/redhat)
|
||||
firewall_rule 'vrrp' do
|
||||
protocol 112
|
||||
command :allow
|
||||
end
|
||||
|
||||
# use the iptables provider to specify protocol number on debian/ubuntu
|
||||
firewall_rule 'vrrp' do
|
||||
provider Chef::Provider::FirewallRuleIptables
|
||||
protocol 112
|
||||
command :allow
|
||||
end
|
||||
|
||||
# can use :raw command with UFW provider for VRRP
|
||||
firewall_rule "VRRP" do
|
||||
command :allow
|
||||
raw "allow to 224.0.0.18"
|
||||
end
|
||||
|
||||
# open UDP ports 60000..61000 for mobile shell (mosh.mit.edu), note
|
||||
# that the protocol attribute is required when using port_range
|
||||
firewall_rule 'mosh' do
|
||||
protocol :udp
|
||||
port 60000..61000
|
||||
command :allow
|
||||
end
|
||||
|
||||
# open multiple ports for http/https, note that the protocol
|
||||
# attribute is required when using ports
|
||||
firewall_rule 'http/https' do
|
||||
protocol :tcp
|
||||
port [80, 443]
|
||||
command :allow
|
||||
end
|
||||
|
||||
firewall 'default' do
|
||||
enabled false
|
||||
action :nothing
|
||||
end
|
||||
```
|
||||
|
||||
#### Providers
|
||||
|
||||
- See `libraries/z_provider_mapping.rb` for a full list of providers for each platform and version.
|
||||
|
||||
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.
|
||||
|
||||
# Troubleshooting
|
||||
|
||||
To figure out what the position values are for current rules, print the hash that contains the weights:
|
||||
```
|
||||
require pp
|
||||
default_firewall = resources(:firewall, 'default')
|
||||
pp default_firewall.rules
|
||||
```
|
||||
|
||||
# Development
|
||||
This section details "quick development" steps. For a detailed explanation, see [[Contributing.md]].
|
||||
|
||||
1. Clone this repository from GitHub:
|
||||
|
||||
$ git clone git@github.com:chef-cookbooks/firewall.git
|
||||
|
||||
2. Create a git branch
|
||||
|
||||
$ git checkout -b my_bug_fix
|
||||
|
||||
3. Install dependencies:
|
||||
|
||||
$ bundle install
|
||||
|
||||
4. Make your changes/patches/fixes, committing appropiately
|
||||
5. **Write tests**
|
||||
6. Run the tests:
|
||||
- `bundle exec foodcritic -f any .`
|
||||
- `bundle exec rspec`
|
||||
- `bundle exec rubocop`
|
||||
- `bundle exec kitchen test`
|
||||
|
||||
In detail:
|
||||
- Foodcritic will catch any Chef-specific style errors
|
||||
- RSpec will run the unit tests
|
||||
- Rubocop will check for Ruby-specific style errors
|
||||
- Test Kitchen will run and converge the recipes
|
||||
|
||||
|
||||
# License & Authors
|
||||
<!-- $ find -type f -iname "*.rb" -exec grep -i author '{}' \; | sort -k4 | uniq | sed 's/#/-/g' -->
|
||||
- Author:: Seth Chisamore (<schisamo@opscode.com>)
|
||||
- Author:: Ronald Doorn (<rdoorn@schubergphilis.com>)
|
||||
- Author:: Martin Smith (<martin@mbs3.org>)
|
||||
- Author:: Sander van Harmelen (<svanharmelen@schubergphilis.com>)
|
||||
|
||||
```text
|
||||
Copyright:: 2011-2015, Chef Software, Inc
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
```
|
||||
|
||||
[0]: https://mosh.mit.edu/
|
||||
5
cookbooks/firewall/attributes/default.rb
Normal file
5
cookbooks/firewall/attributes/default.rb
Normal file
@@ -0,0 +1,5 @@
|
||||
default['firewall']['allow_ssh'] = false
|
||||
default['firewall']['allow_winrm'] = false
|
||||
default['firewall']['allow_mosh'] = false
|
||||
default['firewall']['allow_loopback'] = false
|
||||
default['firewall']['allow_icmp'] = false
|
||||
1
cookbooks/firewall/attributes/firewalld.rb
Normal file
1
cookbooks/firewall/attributes/firewalld.rb
Normal file
@@ -0,0 +1 @@
|
||||
default['firewall']['firewalld']['permanent'] = false
|
||||
17
cookbooks/firewall/attributes/iptables.rb
Normal file
17
cookbooks/firewall/attributes/iptables.rb
Normal file
@@ -0,0 +1,17 @@
|
||||
default['firewall']['iptables']['defaults'][:policy] = {
|
||||
input: 'DROP',
|
||||
forward: 'DROP',
|
||||
output: 'ACCEPT',
|
||||
}
|
||||
default['firewall']['iptables']['defaults'][:ruleset] = {
|
||||
'*filter' => 1,
|
||||
":INPUT #{node['firewall']['iptables']['defaults'][:policy][:input]}" => 2,
|
||||
":FORWARD #{node['firewall']['iptables']['defaults'][:policy][:forward]}" => 3,
|
||||
":OUTPUT #{node['firewall']['iptables']['defaults'][:policy][:output]}" => 4,
|
||||
'COMMIT_FILTER' => 100,
|
||||
}
|
||||
|
||||
default['firewall']['ubuntu_iptables'] = false
|
||||
default['firewall']['redhat7_iptables'] = false
|
||||
default['firewall']['allow_established'] = true
|
||||
default['firewall']['ipv6_enabled'] = true
|
||||
12
cookbooks/firewall/attributes/ufw.rb
Normal file
12
cookbooks/firewall/attributes/ufw.rb
Normal file
@@ -0,0 +1,12 @@
|
||||
default['firewall']['ufw']['defaults'] = {
|
||||
ipv6: 'yes',
|
||||
manage_builtins: 'no',
|
||||
ipt_sysctl: '/etc/ufw/sysctl.conf',
|
||||
ipt_modules: 'nf_conntrack_ftp nf_nat_ftp nf_conntrack_netbios_ns',
|
||||
policy: {
|
||||
input: 'DROP',
|
||||
output: 'ACCEPT',
|
||||
forward: 'DROP',
|
||||
application: 'SKIP',
|
||||
},
|
||||
}
|
||||
8
cookbooks/firewall/attributes/windows.rb
Normal file
8
cookbooks/firewall/attributes/windows.rb
Normal file
@@ -0,0 +1,8 @@
|
||||
# Windows platform defult settings: block undefined inbould traffic, allow all outgoing traffic
|
||||
|
||||
default['firewall']['windows']['defaults'] = {
|
||||
policy: {
|
||||
input: 'blockinbound',
|
||||
output: 'allowoutbound',
|
||||
},
|
||||
}
|
||||
100
cookbooks/firewall/libraries/helpers.rb
Normal file
100
cookbooks/firewall/libraries/helpers.rb
Normal file
@@ -0,0 +1,100 @@
|
||||
module FirewallCookbook
|
||||
module Helpers
|
||||
def dport_calc(new_resource)
|
||||
new_resource.dest_port || new_resource.port
|
||||
end
|
||||
|
||||
def port_to_s(p)
|
||||
if p.is_a?(String)
|
||||
p
|
||||
elsif p && p.is_a?(Integer)
|
||||
p.to_s
|
||||
elsif p && p.is_a?(Array)
|
||||
p_strings = p.map { |o| port_to_s(o) }
|
||||
p_strings.sort.join(',')
|
||||
elsif p && p.is_a?(Range)
|
||||
if platform_family?('windows')
|
||||
"#{p.first}-#{p.last}"
|
||||
else
|
||||
"#{p.first}:#{p.last}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def ipv6_enabled?(new_resource)
|
||||
new_resource.ipv6_enabled
|
||||
end
|
||||
|
||||
def disabled?(new_resource)
|
||||
# if either flag is found in the non-default boolean state
|
||||
disable_flag = !(new_resource.enabled && !new_resource.disabled)
|
||||
|
||||
Chef::Log.warn("#{new_resource} has been disabled, not proceeding") if disable_flag
|
||||
disable_flag
|
||||
end
|
||||
|
||||
def ip_with_mask(new_resource, ip)
|
||||
if ip.include?('/')
|
||||
ip
|
||||
elsif ipv4_rule?(new_resource)
|
||||
"#{ip}/32"
|
||||
elsif ipv6_rule?(new_resource)
|
||||
"#{ip}/128"
|
||||
else
|
||||
ip
|
||||
end
|
||||
end
|
||||
|
||||
# ipv4-specific rule?
|
||||
def ipv4_rule?(new_resource)
|
||||
if (new_resource.source && IPAddr.new(new_resource.source).ipv4?) ||
|
||||
(new_resource.destination && IPAddr.new(new_resource.destination).ipv4?)
|
||||
true
|
||||
else
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
# ipv6-specific rule?
|
||||
def ipv6_rule?(new_resource)
|
||||
if (new_resource.source && IPAddr.new(new_resource.source).ipv6?) ||
|
||||
(new_resource.destination && IPAddr.new(new_resource.destination).ipv6?) ||
|
||||
new_resource.protocol =~ /ipv6/ ||
|
||||
new_resource.protocol =~ /icmpv6/
|
||||
true
|
||||
else
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
def ubuntu?(current_node)
|
||||
current_node['platform'] == 'ubuntu'
|
||||
end
|
||||
|
||||
def build_rule_file(rules)
|
||||
contents = []
|
||||
sorted_values = rules.values.sort.uniq
|
||||
sorted_values.each do |sorted_value|
|
||||
contents << "# position #{sorted_value}"
|
||||
rules.each do |k, v|
|
||||
next unless v == sorted_value
|
||||
|
||||
contents << if repeatable_directives(k)
|
||||
k[/[^_]+/]
|
||||
else
|
||||
k
|
||||
end
|
||||
end
|
||||
end
|
||||
"#{contents.join("\n")}\n"
|
||||
end
|
||||
|
||||
def repeatable_directives(s)
|
||||
%w(:OUTPUT :INPUT :POSTROUTING :PREROUTING COMMIT).each do |special|
|
||||
return true if s.start_with?(special)
|
||||
end
|
||||
|
||||
false
|
||||
end
|
||||
end
|
||||
end
|
||||
116
cookbooks/firewall/libraries/helpers_firewalld.rb
Normal file
116
cookbooks/firewall/libraries/helpers_firewalld.rb
Normal file
@@ -0,0 +1,116 @@
|
||||
module FirewallCookbook
|
||||
module Helpers
|
||||
module Firewalld
|
||||
include FirewallCookbook::Helpers
|
||||
include Chef::Mixin::ShellOut
|
||||
|
||||
def firewalld_rules_filename
|
||||
'/etc/sysconfig/firewalld-chef.rules'
|
||||
end
|
||||
|
||||
def firewalld_rule!(cmd)
|
||||
shell_out!(cmd, input: 'yes')
|
||||
end
|
||||
|
||||
def firewalld_active?
|
||||
cmd = shell_out('firewall-cmd', '--state')
|
||||
cmd.stdout =~ /^running$/
|
||||
end
|
||||
|
||||
def firewalld_default_zone?(z)
|
||||
return false unless firewalld_active?
|
||||
|
||||
cmd = shell_out('firewall-cmd', '--get-default-zone')
|
||||
cmd.stdout =~ /^#{z.to_s}$/
|
||||
end
|
||||
|
||||
def firewalld_default_zone!(z)
|
||||
raise 'firewalld not active' unless firewalld_active?
|
||||
|
||||
shell_out!('firewall-cmd', "--set-default-zone=#{z}")
|
||||
end
|
||||
|
||||
def log_current_firewalld
|
||||
shell_out!('firewall-cmd --direct --get-all-rules')
|
||||
end
|
||||
|
||||
def firewalld_flush!
|
||||
raise 'firewall not active' unless firewalld_active?
|
||||
|
||||
shell_out!('firewall-cmd', '--direct', '--remove-rules', 'ipv4', 'filter', 'INPUT')
|
||||
shell_out!('firewall-cmd', '--direct', '--remove-rules', 'ipv4', 'filter', 'OUTPUT')
|
||||
shell_out!('firewall-cmd', '--direct', '--permanent', '--remove-rules', 'ipv4', 'filter', 'INPUT')
|
||||
shell_out!('firewall-cmd', '--direct', '--permanent', '--remove-rules', 'ipv4', 'filter', 'OUTPUT')
|
||||
end
|
||||
|
||||
def firewalld_all_rules_permanent!
|
||||
raise 'firewall not active' unless firewalld_active?
|
||||
|
||||
rules = shell_out!('firewall-cmd', '--direct', '--get-all-rules').stdout
|
||||
perm_rules = shell_out!('firewall-cmd', '--direct', '--permanent', '--get-all-rules').stdout
|
||||
rules == perm_rules
|
||||
end
|
||||
|
||||
def firewalld_save!
|
||||
raise 'firewall not active' unless firewalld_active?
|
||||
|
||||
shell_out!('firewall-cmd', '--direct', '--permanent', '--remove-rules', 'ipv4', 'filter', 'INPUT')
|
||||
shell_out!('firewall-cmd', '--direct', '--permanent', '--remove-rules', 'ipv4', 'filter', 'OUTPUT')
|
||||
shell_out!('firewall-cmd', '--direct', '--get-all-rules').stdout.lines do |line|
|
||||
shell_out!("firewall-cmd --direct --permanent --add-rule #{line}")
|
||||
end
|
||||
end
|
||||
|
||||
def ip_versions(resource)
|
||||
if ipv4_rule?(resource)
|
||||
%w(ipv4)
|
||||
elsif ipv6_rule?(resource)
|
||||
%w(ipv6)
|
||||
else # no source or destination address, add rules for both ipv4 and ipv6
|
||||
%w(ipv4 ipv6)
|
||||
end
|
||||
end
|
||||
|
||||
CHAIN = { in: 'INPUT', out: 'OUTPUT', pre: 'PREROUTING', post: 'POSTROUTING' }.freeze unless defined? CHAIN # , nil => "FORWARD"}
|
||||
TARGET = { allow: 'ACCEPT', reject: 'REJECT', deny: 'DROP', masquerade: 'MASQUERADE', redirect: 'REDIRECT', log: 'LOG --log-prefix \'iptables: \' --log-level 7' }.freeze unless defined? TARGET
|
||||
|
||||
def build_firewall_rule(new_resource, ip_version = 'ipv4')
|
||||
return new_resource.raw.strip if new_resource.raw
|
||||
|
||||
type = new_resource.command
|
||||
firewall_rule = if new_resource.direction
|
||||
"#{ip_version} filter #{CHAIN[new_resource.direction.to_sym]} "
|
||||
else
|
||||
"#{ip_version} filter FORWARD "
|
||||
end
|
||||
firewall_rule << "#{new_resource.position} "
|
||||
|
||||
if [:pre, :post].include?(new_resource.direction)
|
||||
firewall_rule << '-t nat '
|
||||
end
|
||||
|
||||
# Firewalld order of prameters is important here see example output below:
|
||||
# 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
|
||||
firewall_rule << "-s #{ip_with_mask(new_resource, new_resource.source)} " if new_resource.source && new_resource.source != '0.0.0.0/0'
|
||||
firewall_rule << "-d #{new_resource.destination} " if new_resource.destination
|
||||
|
||||
firewall_rule << "-i #{new_resource.interface} " if new_resource.interface
|
||||
firewall_rule << "-o #{new_resource.dest_interface} " if new_resource.dest_interface
|
||||
|
||||
firewall_rule << "-p #{new_resource.protocol} " if new_resource.protocol && new_resource.protocol.to_s.to_sym != :none
|
||||
firewall_rule << '-m tcp ' if new_resource.protocol && new_resource.protocol.to_s.to_sym == :tcp
|
||||
|
||||
# using multiport here allows us to simplify our greps and rule building
|
||||
firewall_rule << "-m multiport --sports #{port_to_s(new_resource.source_port)} " if new_resource.source_port
|
||||
firewall_rule << "-m multiport --dports #{port_to_s(dport_calc(new_resource))} " if dport_calc(new_resource)
|
||||
|
||||
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
|
||||
firewall_rule << "-m comment --comment '#{new_resource.description}' " if new_resource.include_comment
|
||||
firewall_rule << "-j #{TARGET[type]} "
|
||||
firewall_rule << "--to-ports #{new_resource.redirect_port} " if type == :redirect
|
||||
firewall_rule.strip!
|
||||
firewall_rule
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
112
cookbooks/firewall/libraries/helpers_iptables.rb
Normal file
112
cookbooks/firewall/libraries/helpers_iptables.rb
Normal file
@@ -0,0 +1,112 @@
|
||||
module FirewallCookbook
|
||||
module Helpers
|
||||
module Iptables
|
||||
include FirewallCookbook::Helpers
|
||||
include Chef::Mixin::ShellOut
|
||||
|
||||
CHAIN = { in: 'INPUT', out: 'OUTPUT', pre: 'PREROUTING', post: 'POSTROUTING' }.freeze unless defined? CHAIN # , nil => "FORWARD"}
|
||||
TARGET = { allow: 'ACCEPT', reject: 'REJECT', deny: 'DROP', masquerade: 'MASQUERADE', redirect: 'REDIRECT', log: 'LOG --log-prefix "iptables: " --log-level 7' }.freeze unless defined? TARGET
|
||||
|
||||
def build_firewall_rule(current_node, rule_resource, ipv6 = false)
|
||||
el5 = current_node['platform_family'] == 'rhel' && Gem::Dependency.new('', '~> 5.0').match?('', current_node['platform_version'])
|
||||
|
||||
return rule_resource.raw.strip if rule_resource.raw
|
||||
firewall_rule = if rule_resource.direction
|
||||
"-A #{CHAIN[rule_resource.direction.to_sym]} "
|
||||
else
|
||||
'-A FORWARD '
|
||||
end
|
||||
|
||||
if [:pre, :post].include?(rule_resource.direction)
|
||||
firewall_rule << '-t nat '
|
||||
end
|
||||
|
||||
# Iptables order of prameters is important here see example output below:
|
||||
# -A INPUT -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
|
||||
firewall_rule << "-s #{ip_with_mask(rule_resource, rule_resource.source)} " if rule_resource.source && rule_resource.source != '0.0.0.0/0'
|
||||
firewall_rule << "-d #{rule_resource.destination} " if rule_resource.destination
|
||||
|
||||
firewall_rule << "-i #{rule_resource.interface} " if rule_resource.interface
|
||||
firewall_rule << "-o #{rule_resource.dest_interface} " if rule_resource.dest_interface
|
||||
|
||||
firewall_rule << "-p #{rule_resource.protocol} " if rule_resource.protocol && rule_resource.protocol.to_s.to_sym != :none
|
||||
firewall_rule << '-m tcp ' if rule_resource.protocol && rule_resource.protocol.to_s.to_sym == :tcp
|
||||
|
||||
# using multiport here allows us to simplify our greps and rule building
|
||||
firewall_rule << "-m multiport --sports #{port_to_s(rule_resource.source_port)} " if rule_resource.source_port
|
||||
firewall_rule << "-m multiport --dports #{port_to_s(dport_calc(rule_resource))} " if dport_calc(rule_resource)
|
||||
|
||||
firewall_rule << "-m state --state #{rule_resource.stateful.is_a?(Array) ? rule_resource.stateful.join(',').upcase : rule_resource.stateful.upcase} " if rule_resource.stateful
|
||||
# the comments extension is not available for ip6tables on rhel/centos 5
|
||||
unless el5 && ipv6
|
||||
firewall_rule << "-m comment --comment \"#{rule_resource.description}\" " if rule_resource.include_comment
|
||||
end
|
||||
|
||||
firewall_rule << "-j #{TARGET[rule_resource.command.to_sym]} "
|
||||
firewall_rule << "--to-ports #{rule_resource.redirect_port} " if rule_resource.command == :redirect
|
||||
firewall_rule.strip!
|
||||
firewall_rule
|
||||
end
|
||||
|
||||
def iptables_packages(new_resource)
|
||||
packages = if ipv6_enabled?(new_resource)
|
||||
%w(iptables iptables-ipv6)
|
||||
else
|
||||
%w(iptables)
|
||||
end
|
||||
|
||||
# centos 7 requires extra service
|
||||
if !ubuntu?(node) && node['platform_version'].to_i >= 7
|
||||
packages << %w(iptables-services)
|
||||
end
|
||||
|
||||
packages.flatten
|
||||
end
|
||||
|
||||
def iptables_commands(new_resource)
|
||||
if ipv6_enabled?(new_resource)
|
||||
%w(iptables ip6tables)
|
||||
else
|
||||
%w(iptables)
|
||||
end
|
||||
end
|
||||
|
||||
def log_iptables(new_resource)
|
||||
iptables_commands(new_resource).each do |cmd|
|
||||
shell_out!("#{cmd} -L -n")
|
||||
end
|
||||
rescue
|
||||
Chef::Log.info('log_iptables failed!')
|
||||
end
|
||||
|
||||
def iptables_flush!(new_resource)
|
||||
iptables_commands(new_resource).each do |cmd|
|
||||
shell_out!("#{cmd} -F")
|
||||
end
|
||||
end
|
||||
|
||||
def iptables_default_allow!(new_resource)
|
||||
iptables_commands(new_resource).each do |cmd|
|
||||
shell_out!("#{cmd} -P INPUT ACCEPT")
|
||||
shell_out!("#{cmd} -P OUTPUT ACCEPT")
|
||||
shell_out!("#{cmd} -P FORWARD ACCEPT")
|
||||
end
|
||||
end
|
||||
|
||||
def default_ruleset(current_node)
|
||||
current_node['firewall']['iptables']['defaults'][:ruleset].to_h
|
||||
end
|
||||
|
||||
def ensure_default_rules_exist(current_node, new_resource)
|
||||
input = new_resource.rules
|
||||
|
||||
# don't use iptables_commands here since we do populate the
|
||||
# hash regardless of ipv6 status
|
||||
%w(iptables ip6tables).each do |name|
|
||||
input[name] = {} unless input[name]
|
||||
input[name].merge!(default_ruleset(current_node).to_h)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
135
cookbooks/firewall/libraries/helpers_ufw.rb
Normal file
135
cookbooks/firewall/libraries/helpers_ufw.rb
Normal file
@@ -0,0 +1,135 @@
|
||||
module FirewallCookbook
|
||||
module Helpers
|
||||
module Ufw
|
||||
include FirewallCookbook::Helpers
|
||||
include Chef::Mixin::ShellOut
|
||||
|
||||
def ufw_rules_filename
|
||||
'/etc/default/ufw-chef.rules'
|
||||
end
|
||||
|
||||
def ufw_active?
|
||||
cmd = shell_out!('ufw', 'status')
|
||||
cmd.stdout =~ /^Status:\sactive/
|
||||
end
|
||||
|
||||
def ufw_disable!
|
||||
shell_out!('ufw', 'disable', input: 'yes')
|
||||
end
|
||||
|
||||
def ufw_enable!
|
||||
shell_out!('ufw', 'enable', input: 'yes')
|
||||
end
|
||||
|
||||
def ufw_reset!
|
||||
shell_out!('ufw', 'reset', input: 'yes')
|
||||
end
|
||||
|
||||
def ufw_logging!(param)
|
||||
shell_out!('ufw', 'logging', param.to_s)
|
||||
end
|
||||
|
||||
def ufw_rule!(cmd)
|
||||
shell_out!(cmd, input: 'yes')
|
||||
end
|
||||
|
||||
def build_rule(new_resource)
|
||||
Chef::Log.info("#{new_resource.name} apply_rule #{new_resource.command}")
|
||||
|
||||
# if we don't do this, we may see some bugs where traffic is opened on all ports to all hosts when only RELATED,ESTABLISHED was intended
|
||||
if new_resource.stateful
|
||||
msg = ''
|
||||
msg << "firewall_rule[#{new_resource.name}] was asked to "
|
||||
msg << "#{new_resource.command} a stateful rule using #{new_resource.stateful} "
|
||||
msg << 'but ufw does not support this kind of rule. Consider guarding by platform_family.'
|
||||
raise msg
|
||||
end
|
||||
|
||||
# if we don't do this, ufw will fail as it does not support protocol numbers, so we'll only allow it to run if specifying icmp/tcp/udp protocol types
|
||||
if new_resource.protocol && !new_resource.protocol.to_s.downcase.match('^(tcp|udp|esp|ah|ipv6|none)$')
|
||||
msg = ''
|
||||
msg << "firewall_rule[#{new_resource.name}] was asked to "
|
||||
msg << "#{new_resource.command} a rule using protocol #{new_resource.protocol} "
|
||||
msg << 'but ufw does not support this kind of rule. Consider guarding by platform_family.'
|
||||
raise msg
|
||||
end
|
||||
|
||||
# some examples:
|
||||
# ufw allow from 192.168.0.4 to any port 22
|
||||
# ufw deny proto tcp from 10.0.0.0/8 to 192.168.0.1 port 25
|
||||
# ufw insert 1 allow proto tcp from 0.0.0.0/0 to 192.168.0.1 port 25
|
||||
|
||||
if new_resource.raw
|
||||
"ufw #{new_resource.raw.strip}"
|
||||
else
|
||||
"ufw #{rule(new_resource)}"
|
||||
end
|
||||
end
|
||||
|
||||
def rule(new_resource)
|
||||
rule = ''
|
||||
rule << "#{new_resource.command} "
|
||||
rule << rule_interface(new_resource)
|
||||
rule << rule_logging(new_resource)
|
||||
rule << rule_proto(new_resource)
|
||||
rule << rule_dest_port(new_resource)
|
||||
rule << rule_source_port(new_resource)
|
||||
rule = rule.strip
|
||||
|
||||
if rule == 'ufw allow in proto tcp to any from any'
|
||||
Chef::Log.warn("firewall_rule[#{new_resource.name}] produced a rule that opens all traffic. This may be a logic error in your cookbook.")
|
||||
end
|
||||
|
||||
rule
|
||||
end
|
||||
|
||||
def rule_interface(new_resource)
|
||||
rule = ''
|
||||
rule << "#{new_resource.direction} " if new_resource.direction
|
||||
rule << "on #{new_resource.interface} " if new_resource.interface && new_resource.direction
|
||||
rule << "in on #{new_resource.interface} " if new_resource.interface && !new_resource.direction
|
||||
rule
|
||||
end
|
||||
|
||||
def rule_proto(new_resource)
|
||||
rule = ''
|
||||
rule << "proto #{new_resource.protocol} " if new_resource.protocol && new_resource.protocol.to_s.to_sym != :none
|
||||
rule
|
||||
end
|
||||
|
||||
def rule_dest_port(new_resource)
|
||||
rule = if new_resource.destination
|
||||
"to #{new_resource.destination} "
|
||||
else
|
||||
'to any '
|
||||
end
|
||||
rule << "port #{port_to_s(dport_calc(new_resource))} " if dport_calc(new_resource)
|
||||
rule
|
||||
end
|
||||
|
||||
def rule_source_port(new_resource)
|
||||
rule = if new_resource.source
|
||||
"from #{new_resource.source} "
|
||||
else
|
||||
'from any '
|
||||
end
|
||||
|
||||
if new_resource.source_port
|
||||
rule << "port #{port_to_s(new_resource.source_port)} "
|
||||
end
|
||||
rule
|
||||
end
|
||||
|
||||
def rule_logging(new_resource)
|
||||
case new_resource.logging && new_resource.logging.to_sym
|
||||
when :connections
|
||||
'log '
|
||||
when :packets
|
||||
'log-all '
|
||||
else
|
||||
''
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
130
cookbooks/firewall/libraries/helpers_windows.rb
Normal file
130
cookbooks/firewall/libraries/helpers_windows.rb
Normal file
@@ -0,0 +1,130 @@
|
||||
module FirewallCookbook
|
||||
module Helpers
|
||||
module Windows
|
||||
include FirewallCookbook::Helpers
|
||||
include Chef::Mixin::ShellOut
|
||||
|
||||
def fixup_cidr(str)
|
||||
newstr = str.clone
|
||||
newstr.gsub!('0.0.0.0/0', 'any') if newstr.include?('0.0.0.0/0')
|
||||
newstr.gsub!('/0', '') if newstr.include?('/0')
|
||||
newstr
|
||||
end
|
||||
|
||||
def windows_rules_filename
|
||||
"#{ENV['HOME']}/windows-chef.rules"
|
||||
end
|
||||
|
||||
def active?
|
||||
@active ||= begin
|
||||
cmd = shell_out!('netsh advfirewall show currentprofile')
|
||||
cmd.stdout =~ /^State\sON/
|
||||
end
|
||||
end
|
||||
|
||||
def enable!
|
||||
shell_out!('netsh advfirewall set currentprofile state on')
|
||||
end
|
||||
|
||||
def disable!
|
||||
shell_out!('netsh advfirewall set currentprofile state off')
|
||||
end
|
||||
|
||||
def reset!
|
||||
shell_out!('netsh advfirewall reset')
|
||||
end
|
||||
|
||||
def add_rule!(params)
|
||||
shell_out!("netsh advfirewall #{params}")
|
||||
end
|
||||
|
||||
def delete_all_rules!
|
||||
shell_out!('netsh advfirewall firewall delete rule name=all')
|
||||
end
|
||||
|
||||
def to_type(new_resource)
|
||||
cmd = new_resource.command
|
||||
type = if cmd == :reject || cmd == :deny
|
||||
:block
|
||||
else
|
||||
:allow
|
||||
end
|
||||
type
|
||||
end
|
||||
|
||||
def build_rule(new_resource)
|
||||
type = to_type(new_resource)
|
||||
parameters = {}
|
||||
|
||||
parameters['description'] = "\"#{new_resource.description}\""
|
||||
parameters['dir'] = new_resource.direction
|
||||
|
||||
new_resource.program && parameters['program'] = new_resource.program
|
||||
new_resource.service && parameters['service'] = new_resource.service
|
||||
parameters['protocol'] = new_resource.protocol
|
||||
|
||||
if new_resource.direction.to_sym == :out
|
||||
parameters['localip'] = new_resource.source ? fixup_cidr(new_resource.source) : 'any'
|
||||
parameters['localport'] = new_resource.source_port ? port_to_s(new_resource.source_port) : 'any'
|
||||
parameters['interfacetype'] = new_resource.interface ? new_resource.interface : 'any'
|
||||
parameters['remoteip'] = new_resource.destination ? fixup_cidr(new_resource.destination) : 'any'
|
||||
parameters['remoteport'] = new_resource.dest_port ? port_to_s(new_resource.dest_port) : 'any'
|
||||
else
|
||||
parameters['localip'] = new_resource.destination ? new_resource.destination : 'any'
|
||||
parameters['localport'] = dport_calc(new_resource) ? port_to_s(dport_calc(new_resource)) : 'any'
|
||||
parameters['interfacetype'] = new_resource.dest_interface ? new_resource.dest_interface : 'any'
|
||||
parameters['remoteip'] = new_resource.source ? fixup_cidr(new_resource.source) : 'any'
|
||||
parameters['remoteport'] = new_resource.source_port ? port_to_s(new_resource.source_port) : 'any'
|
||||
end
|
||||
|
||||
parameters['action'] = type.to_s
|
||||
|
||||
partial_command = parameters.map { |k, v| "#{k}=#{v}" }.join(' ')
|
||||
"firewall add rule name=\"#{new_resource.name}\" #{partial_command}"
|
||||
end
|
||||
|
||||
def rule_exists?(name)
|
||||
@exists ||= begin
|
||||
cmd = shell_out!("netsh advfirewall firewall show rule name=\"#{name}\"", returns: [0, 1])
|
||||
cmd.stdout !~ /^No rules match the specified criteria/
|
||||
end
|
||||
end
|
||||
|
||||
def show_all_rules!
|
||||
cmd = shell_out!('netsh advfirewall firewall show rule name=all')
|
||||
cmd.stdout.each_line do |line|
|
||||
Chef::Log.warn(line)
|
||||
end
|
||||
end
|
||||
|
||||
def rule_up_to_date?(name, type)
|
||||
@up_to_date ||= begin
|
||||
desired_parameters = rule_parameters(type)
|
||||
current_parameters = {}
|
||||
|
||||
cmd = shell_out!("netsh advfirewall firewall show rule name=\"#{name}\" verbose")
|
||||
cmd.stdout.each_line do |line|
|
||||
current_parameters['description'] = "\"#{Regexp.last_match(1).chomp}\"" if line =~ /^Description:\s+(.*)$/
|
||||
current_parameters['dir'] = Regexp.last_match(1).chomp if line =~ /^Direction:\s+(.*)$/
|
||||
current_parameters['program'] = Regexp.last_match(1).chomp if line =~ /^Program:\s+(.*)$/
|
||||
current_parameters['service'] = Regexp.last_match(1).chomp if line =~ /^Service:\s+(.*)$/
|
||||
current_parameters['protocol'] = Regexp.last_match(1).chomp if line =~ /^Protocol:\s+(.*)$/
|
||||
current_parameters['localip'] = Regexp.last_match(1).chomp if line =~ /^LocalIP:\s+(.*)$/
|
||||
current_parameters['localport'] = Regexp.last_match(1).chomp if line =~ /^LocalPort:\s+(.*)$/
|
||||
current_parameters['interfacetype'] = Regexp.last_match(1).chomp if line =~ /^InterfaceTypes:\s+(.*)$/
|
||||
current_parameters['remoteip'] = Regexp.last_match(1).chomp if line =~ /^RemoteIP:\s+(.*)$/
|
||||
current_parameters['remoteport'] = Regexp.last_match(1).chomp if line =~ /^RemotePort:\s+(.*)$/
|
||||
current_parameters['action'] = Regexp.last_match(1).chomp if line =~ /^Action:\s+(.*)$/
|
||||
end
|
||||
|
||||
up_to_date = true
|
||||
desired_parameters.each do |k, v|
|
||||
up_to_date = false if current_parameters[k] !~ /^["]?#{v}["]?$/i
|
||||
end
|
||||
|
||||
up_to_date
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
30
cookbooks/firewall/libraries/matchers.rb
Normal file
30
cookbooks/firewall/libraries/matchers.rb
Normal file
@@ -0,0 +1,30 @@
|
||||
if defined?(ChefSpec)
|
||||
ChefSpec.define_matcher(:firewall)
|
||||
ChefSpec.define_matcher(:firewall_rule)
|
||||
|
||||
# actions(:install, :restart, :disable, :flush, :save)
|
||||
|
||||
def install_firewall(resource)
|
||||
ChefSpec::Matchers::ResourceMatcher.new(:firewall, :install, resource)
|
||||
end
|
||||
|
||||
def restart_firewall(resource)
|
||||
ChefSpec::Matchers::ResourceMatcher.new(:firewall, :restart, resource)
|
||||
end
|
||||
|
||||
def disable_firewall(resource)
|
||||
ChefSpec::Matchers::ResourceMatcher.new(:firewall, :disable, resource)
|
||||
end
|
||||
|
||||
def flush_firewall(resource)
|
||||
ChefSpec::Matchers::ResourceMatcher.new(:firewall, :flush, resource)
|
||||
end
|
||||
|
||||
def save_firewall(resource)
|
||||
ChefSpec::Matchers::ResourceMatcher.new(:firewall, :save, resource)
|
||||
end
|
||||
|
||||
def create_firewall_rule(resource)
|
||||
ChefSpec::Matchers::ResourceMatcher.new(:firewall_rule, :create, resource)
|
||||
end
|
||||
end
|
||||
179
cookbooks/firewall/libraries/provider_firewall_firewalld.rb
Normal file
179
cookbooks/firewall/libraries/provider_firewall_firewalld.rb
Normal file
@@ -0,0 +1,179 @@
|
||||
#
|
||||
# Author:: Ronald Doorn (<rdoorn@schubergphilis.com>)
|
||||
# Cookbook:: firewall
|
||||
# Resource:: default
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
class Chef
|
||||
class Provider::FirewallFirewalld < Chef::Provider::LWRPBase
|
||||
include FirewallCookbook::Helpers::Firewalld
|
||||
|
||||
provides :firewall, os: 'linux', platform_family: %w(rhel fedora) do |node|
|
||||
node['platform_version'].to_f >= 7.0 && !node['firewall']['redhat7_iptables']
|
||||
end
|
||||
|
||||
def whyrun_supported?
|
||||
false
|
||||
end
|
||||
|
||||
def action_install
|
||||
return if disabled?(new_resource)
|
||||
|
||||
firewalld_package = package 'firewalld' do
|
||||
action :nothing
|
||||
options new_resource.package_options
|
||||
end
|
||||
firewalld_package.run_action(:install)
|
||||
new_resource.updated_by_last_action(firewalld_package.updated_by_last_action?)
|
||||
|
||||
unless ::File.exist?(firewalld_rules_filename)
|
||||
rules_file = lookup_or_create_rulesfile
|
||||
rules_file.content '# created by chef to allow service to start'
|
||||
rules_file.run_action(:create)
|
||||
new_resource.updated_by_last_action(rules_file.updated_by_last_action?)
|
||||
end
|
||||
|
||||
firewalld_service = lookup_or_create_service
|
||||
[:enable, :start].each do |a|
|
||||
firewalld_service.run_action(a)
|
||||
new_resource.updated_by_last_action(firewalld_service.updated_by_last_action?)
|
||||
end
|
||||
end
|
||||
|
||||
def action_restart
|
||||
return if disabled?(new_resource)
|
||||
|
||||
# ensure it's initialized
|
||||
new_resource.rules({}) unless new_resource.rules
|
||||
new_resource.rules['firewalld'] = {} unless new_resource.rules['firewalld']
|
||||
|
||||
# this populates the hash of rules from firewall_rule resources
|
||||
firewall_rules = Chef.run_context.resource_collection.select { |item| item.is_a?(Chef::Resource::FirewallRule) }
|
||||
firewall_rules.each do |firewall_rule|
|
||||
next unless firewall_rule.action.include?(:create) && !firewall_rule.should_skip?(:create)
|
||||
|
||||
ip_versions(firewall_rule).each do |ip_version|
|
||||
# build rules to apply with weight
|
||||
k = "firewall-cmd --direct --add-rule #{build_firewall_rule(firewall_rule, ip_version)}"
|
||||
v = firewall_rule.position
|
||||
|
||||
# unless we're adding them for the first time.... bail out.
|
||||
next if new_resource.rules['firewalld'].key?(k) && new_resource.rules['firewalld'][k] == v
|
||||
new_resource.rules['firewalld'][k] = v
|
||||
|
||||
# If persistent rules is enabled (default) make sure we add a permanent rule at the same time
|
||||
perm_rules = node && node['firewall'] && node['firewall']['firewalld'] && node['firewall']['firewalld']['permanent']
|
||||
if firewall_rule.permanent || perm_rules
|
||||
k = "firewall-cmd --permanent --direct --add-rule #{build_firewall_rule(firewall_rule, ip_version)}"
|
||||
new_resource.rules['firewalld'][k] = v
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# ensure a file resource exists with the current firewalld rules
|
||||
rules_file = lookup_or_create_rulesfile
|
||||
rules_file.content build_rule_file(new_resource.rules['firewalld'])
|
||||
rules_file.run_action(:create)
|
||||
|
||||
# ensure the service is running without waiting.
|
||||
firewalld_service = lookup_or_create_service
|
||||
[:enable, :start].each do |a|
|
||||
firewalld_service.run_action(a)
|
||||
new_resource.updated_by_last_action(firewalld_service.updated_by_last_action?)
|
||||
end
|
||||
|
||||
# mark updated if we changed the zone
|
||||
unless firewalld_default_zone?(new_resource.enabled_zone)
|
||||
firewalld_default_zone!(new_resource.enabled_zone)
|
||||
new_resource.updated_by_last_action(true)
|
||||
end
|
||||
|
||||
# if the file was changed, load new ruleset
|
||||
return unless rules_file.updated_by_last_action?
|
||||
firewalld_flush!
|
||||
# TODO: support logging
|
||||
|
||||
new_resource.rules['firewalld'].sort_by { |_k, v| v }.map { |k, _v| k }.each do |cmd|
|
||||
firewalld_rule!(cmd)
|
||||
end
|
||||
|
||||
new_resource.updated_by_last_action(true)
|
||||
end
|
||||
|
||||
def action_disable
|
||||
return if disabled?(new_resource)
|
||||
|
||||
if firewalld_active?
|
||||
firewalld_flush!
|
||||
firewalld_default_zone!(new_resource.disabled_zone)
|
||||
new_resource.updated_by_last_action(true)
|
||||
end
|
||||
|
||||
# ensure the service is stopped without waiting.
|
||||
firewalld_service = lookup_or_create_service
|
||||
[:disable, :stop].each do |a|
|
||||
firewalld_service.run_action(a)
|
||||
new_resource.updated_by_last_action(firewalld_service.updated_by_last_action?)
|
||||
end
|
||||
|
||||
rules_file = lookup_or_create_rulesfile
|
||||
rules_file.content '# created by chef to allow service to start'
|
||||
rules_file.run_action(:create)
|
||||
new_resource.updated_by_last_action(rules_file.updated_by_last_action?)
|
||||
end
|
||||
|
||||
def action_flush
|
||||
return if disabled?(new_resource)
|
||||
return unless firewalld_active?
|
||||
|
||||
firewalld_flush!
|
||||
new_resource.updated_by_last_action(true)
|
||||
|
||||
rules_file = lookup_or_create_rulesfile
|
||||
rules_file.content '# created by chef to allow service to start'
|
||||
rules_file.run_action(:create)
|
||||
new_resource.updated_by_last_action(rules_file.updated_by_last_action?)
|
||||
end
|
||||
|
||||
def action_save
|
||||
return if disabled?(new_resource)
|
||||
return if firewalld_all_rules_permanent!
|
||||
|
||||
firewalld_save!
|
||||
new_resource.updated_by_last_action(true)
|
||||
end
|
||||
|
||||
def lookup_or_create_service
|
||||
begin
|
||||
firewalld_service = Chef.run_context.resource_collection.find(service: 'firewalld')
|
||||
rescue
|
||||
firewalld_service = service 'firewalld' do
|
||||
action :nothing
|
||||
end
|
||||
end
|
||||
firewalld_service
|
||||
end
|
||||
|
||||
def lookup_or_create_rulesfile
|
||||
begin
|
||||
firewalld_file = Chef.run_context.resource_collection.find(file: firewalld_rules_filename)
|
||||
rescue
|
||||
firewalld_file = file firewalld_rules_filename do
|
||||
action :nothing
|
||||
end
|
||||
end
|
||||
firewalld_file
|
||||
end
|
||||
end
|
||||
end
|
||||
171
cookbooks/firewall/libraries/provider_firewall_iptables.rb
Normal file
171
cookbooks/firewall/libraries/provider_firewall_iptables.rb
Normal file
@@ -0,0 +1,171 @@
|
||||
#
|
||||
# Author:: Seth Chisamore (<schisamo@opscode.com>)
|
||||
# Cookbook:: firewall
|
||||
# Resource:: default
|
||||
#
|
||||
# Copyright:: 2011-2016, Chef Software, Inc.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
class Chef
|
||||
class Provider::FirewallIptables < Chef::Provider::LWRPBase
|
||||
include FirewallCookbook::Helpers
|
||||
include FirewallCookbook::Helpers::Iptables
|
||||
|
||||
provides :firewall, os: 'linux', platform_family: %w(rhel fedora) do |node|
|
||||
node['platform_version'].to_f < 7.0 || node['firewall']['redhat7_iptables']
|
||||
end
|
||||
|
||||
def whyrun_supported?
|
||||
false
|
||||
end
|
||||
|
||||
def action_install
|
||||
return if disabled?(new_resource)
|
||||
|
||||
# Ensure the package is installed
|
||||
iptables_packages(new_resource).each do |p|
|
||||
iptables_pkg = package p do
|
||||
action :nothing
|
||||
end
|
||||
iptables_pkg.run_action(:install)
|
||||
new_resource.updated_by_last_action(true) if iptables_pkg.updated_by_last_action?
|
||||
end
|
||||
|
||||
iptables_commands(new_resource).each do |svc|
|
||||
# must create empty file for service to start
|
||||
unless ::File.exist?("/etc/sysconfig/#{svc}")
|
||||
# must create empty file for service to start
|
||||
iptables_file = lookup_or_create_rulesfile(svc)
|
||||
iptables_file.content '# created by chef to allow service to start'
|
||||
iptables_file.run_action(:create)
|
||||
new_resource.updated_by_last_action(true) if iptables_file.updated_by_last_action?
|
||||
end
|
||||
|
||||
iptables_service = lookup_or_create_service(svc)
|
||||
[:enable, :start].each do |a|
|
||||
iptables_service.run_action(a)
|
||||
new_resource.updated_by_last_action(true) if iptables_service.updated_by_last_action?
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def action_restart
|
||||
return if disabled?(new_resource)
|
||||
|
||||
# prints all the firewall rules
|
||||
log_iptables(new_resource)
|
||||
|
||||
# ensure it's initialized
|
||||
new_resource.rules({}) unless new_resource.rules
|
||||
ensure_default_rules_exist(node, new_resource)
|
||||
|
||||
# this populates the hash of rules from firewall_rule resources
|
||||
firewall_rules = Chef.run_context.resource_collection.select { |item| item.is_a?(Chef::Resource::FirewallRule) }
|
||||
firewall_rules.each do |firewall_rule|
|
||||
next unless firewall_rule.action.include?(:create) && !firewall_rule.should_skip?(:create)
|
||||
|
||||
types = if ipv6_rule?(firewall_rule) # an ip4 specific rule
|
||||
%w(ip6tables)
|
||||
elsif ipv4_rule?(firewall_rule) # an ip6 specific rule
|
||||
%w(iptables)
|
||||
else # or not specific
|
||||
%w(iptables ip6tables)
|
||||
end
|
||||
|
||||
types.each do |iptables_type|
|
||||
# build rules to apply with weight
|
||||
k = build_firewall_rule(node, firewall_rule, iptables_type == 'ip6tables')
|
||||
v = firewall_rule.position
|
||||
|
||||
# unless we're adding them for the first time.... bail out.
|
||||
next if new_resource.rules[iptables_type].key?(k) && new_resource.rules[iptables_type][k] == v
|
||||
new_resource.rules[iptables_type][k] = v
|
||||
end
|
||||
end
|
||||
|
||||
iptables_commands(new_resource).each do |iptables_type|
|
||||
# this takes the commands in each hash entry and builds a rule file
|
||||
iptables_file = lookup_or_create_rulesfile(iptables_type)
|
||||
iptables_file.content build_rule_file(new_resource.rules[iptables_type])
|
||||
iptables_file.run_action(:create)
|
||||
|
||||
# if the file was unchanged, skip loop iteration, otherwise restart iptables
|
||||
next unless iptables_file.updated_by_last_action?
|
||||
|
||||
iptables_service = lookup_or_create_service(iptables_type)
|
||||
new_resource.notifies(:restart, iptables_service, :delayed)
|
||||
new_resource.updated_by_last_action(true)
|
||||
end
|
||||
end
|
||||
|
||||
def action_disable
|
||||
return if disabled?(new_resource)
|
||||
|
||||
iptables_flush!(new_resource)
|
||||
iptables_default_allow!(new_resource)
|
||||
new_resource.updated_by_last_action(true)
|
||||
|
||||
iptables_commands(new_resource).each do |svc|
|
||||
iptables_service = lookup_or_create_service(svc)
|
||||
[:disable, :stop].each do |a|
|
||||
iptables_service.run_action(a)
|
||||
new_resource.updated_by_last_action(true) if iptables_service.updated_by_last_action?
|
||||
end
|
||||
|
||||
# must create empty file for service to start
|
||||
iptables_file = lookup_or_create_rulesfile(svc)
|
||||
iptables_file.content '# created by chef to allow service to start'
|
||||
iptables_file.run_action(:create)
|
||||
new_resource.updated_by_last_action(true) if iptables_file.updated_by_last_action?
|
||||
end
|
||||
end
|
||||
|
||||
def action_flush
|
||||
return if disabled?(new_resource)
|
||||
|
||||
iptables_flush!(new_resource)
|
||||
new_resource.updated_by_last_action(true)
|
||||
|
||||
iptables_commands(new_resource).each do |svc|
|
||||
# must create empty file for service to start
|
||||
iptables_file = lookup_or_create_rulesfile(svc)
|
||||
iptables_file.content '# created by chef to allow service to start'
|
||||
iptables_file.run_action(:create)
|
||||
new_resource.updated_by_last_action(true) if iptables_file.updated_by_last_action?
|
||||
end
|
||||
end
|
||||
|
||||
def lookup_or_create_service(name)
|
||||
begin
|
||||
iptables_service = Chef.run_context.resource_collection.find(service: svc)
|
||||
rescue
|
||||
iptables_service = service name do
|
||||
action :nothing
|
||||
end
|
||||
end
|
||||
iptables_service
|
||||
end
|
||||
|
||||
def lookup_or_create_rulesfile(name)
|
||||
begin
|
||||
iptables_file = Chef.run_context.resource_collection.find(file: name)
|
||||
rescue
|
||||
iptables_file = file "/etc/sysconfig/#{name}" do
|
||||
action :nothing
|
||||
end
|
||||
end
|
||||
iptables_file
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,195 @@
|
||||
#
|
||||
# Author:: Seth Chisamore (<schisamo@opscode.com>)
|
||||
# Cookbook:: firewall
|
||||
# Resource:: default
|
||||
#
|
||||
# Copyright:: 2011-2016, Chef Software, Inc.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
class Chef
|
||||
class Provider::FirewallIptablesUbuntu < Chef::Provider::LWRPBase
|
||||
include FirewallCookbook::Helpers
|
||||
include FirewallCookbook::Helpers::Iptables
|
||||
|
||||
provides :firewall, os: 'linux', platform_family: %w(debian) do |node|
|
||||
node['platform_version'].to_f > 14.04 && node['firewall'] && node['firewall']['ubuntu_iptables']
|
||||
end
|
||||
|
||||
def whyrun_supported?
|
||||
false
|
||||
end
|
||||
|
||||
def action_install
|
||||
return if disabled?(new_resource)
|
||||
|
||||
# Ensure the package is installed
|
||||
pkg = package 'iptables-persistent' do
|
||||
action :nothing
|
||||
end
|
||||
pkg.run_action(:install)
|
||||
new_resource.updated_by_last_action(true) if pkg.updated_by_last_action?
|
||||
|
||||
rule_files = %w(rules.v4)
|
||||
rule_files << 'rules.v6' if ipv6_enabled?(new_resource)
|
||||
rule_files.each do |svc|
|
||||
next if ::File.exist?("/etc/iptables/#{svc}")
|
||||
|
||||
# must create empty file for service to start
|
||||
f = lookup_or_create_rulesfile(svc)
|
||||
f.content '# created by chef to allow service to start'
|
||||
f.run_action(:create)
|
||||
|
||||
new_resource.updated_by_last_action(true) if f.updated_by_last_action?
|
||||
end
|
||||
|
||||
iptables_service = lookup_or_create_service('netfilter-persistent')
|
||||
[:enable, :start].each do |act|
|
||||
# iptables-persistent isn't a real service
|
||||
iptables_service.status_command 'true'
|
||||
|
||||
iptables_service.run_action(act)
|
||||
new_resource.updated_by_last_action(true) if iptables_service.updated_by_last_action?
|
||||
end
|
||||
end
|
||||
|
||||
def action_restart
|
||||
return if disabled?(new_resource)
|
||||
|
||||
# prints all the firewall rules
|
||||
log_iptables(new_resource)
|
||||
|
||||
# ensure it's initialized
|
||||
new_resource.rules({}) unless new_resource.rules
|
||||
ensure_default_rules_exist(node, new_resource)
|
||||
|
||||
# this populates the hash of rules from firewall_rule resources
|
||||
firewall_rules = Chef.run_context.resource_collection.select { |item| item.is_a?(Chef::Resource::FirewallRule) }
|
||||
firewall_rules.each do |firewall_rule|
|
||||
next unless firewall_rule.action.include?(:create) && !firewall_rule.should_skip?(:create)
|
||||
|
||||
types = if ipv6_rule?(firewall_rule) # an ip4 specific rule
|
||||
%w(ip6tables)
|
||||
elsif ipv4_rule?(firewall_rule) # an ip6 specific rule
|
||||
%w(iptables)
|
||||
else # or not specific
|
||||
%w(iptables ip6tables)
|
||||
end
|
||||
|
||||
types.each do |iptables_type|
|
||||
# build rules to apply with weight
|
||||
k = build_firewall_rule(node, firewall_rule, iptables_type == 'ip6tables')
|
||||
v = firewall_rule.position
|
||||
|
||||
# unless we're adding them for the first time.... bail out.
|
||||
next if new_resource.rules[iptables_type].key?(k) && new_resource.rules[iptables_type][k] == v
|
||||
new_resource.rules[iptables_type][k] = v
|
||||
end
|
||||
end
|
||||
|
||||
rule_files = %w(iptables)
|
||||
rule_files << 'ip6tables' if ipv6_enabled?(new_resource)
|
||||
|
||||
rule_files.each do |iptables_type|
|
||||
iptables_filename = if iptables_type == 'ip6tables'
|
||||
'/etc/iptables/rules.v6'
|
||||
else
|
||||
'/etc/iptables/rules.v4'
|
||||
end
|
||||
|
||||
# ensure a file resource exists with the current iptables rules
|
||||
begin
|
||||
iptables_file = Chef.run_context.resource_collection.find(file: iptables_filename)
|
||||
rescue
|
||||
iptables_file = file iptables_filename do
|
||||
action :nothing
|
||||
end
|
||||
end
|
||||
iptables_file.content build_rule_file(new_resource.rules[iptables_type])
|
||||
iptables_file.run_action(:create)
|
||||
|
||||
# if the file was changed, restart iptables
|
||||
next unless iptables_file.updated_by_last_action?
|
||||
service_affected = service 'netfilter-persistent' do
|
||||
action :nothing
|
||||
end
|
||||
|
||||
new_resource.notifies(:restart, service_affected, :delayed)
|
||||
new_resource.updated_by_last_action(true)
|
||||
end
|
||||
end
|
||||
|
||||
def action_disable
|
||||
return if disabled?(new_resource)
|
||||
|
||||
iptables_flush!(new_resource)
|
||||
iptables_default_allow!(new_resource)
|
||||
new_resource.updated_by_last_action(true)
|
||||
|
||||
iptables_service = lookup_or_create_service('netfilter-persistent')
|
||||
[:disable, :stop].each do |act|
|
||||
iptables_service.run_action(act)
|
||||
new_resource.updated_by_last_action(true) if iptables_service.updated_by_last_action?
|
||||
end
|
||||
|
||||
%w(rules.v4 rules.v6).each do |svc|
|
||||
# must create empty file for service to start
|
||||
f = lookup_or_create_rulesfile(svc)
|
||||
f.content '# created by chef to allow service to start'
|
||||
f.run_action(:create)
|
||||
|
||||
new_resource.updated_by_last_action(true) if f.updated_by_last_action?
|
||||
end
|
||||
end
|
||||
|
||||
def action_flush
|
||||
return if disabled?(new_resource)
|
||||
|
||||
iptables_flush!(new_resource)
|
||||
new_resource.updated_by_last_action(true)
|
||||
|
||||
rule_files = %w(rules.v4)
|
||||
rule_files << 'rules.v6' if ipv6_enabled?(new_resource)
|
||||
rule_files.each do |svc|
|
||||
# must create empty file for service to start
|
||||
f = lookup_or_create_rulesfile(svc)
|
||||
f.content '# created by chef to allow service to start'
|
||||
f.run_action(:create)
|
||||
|
||||
new_resource.updated_by_last_action(true) if f.updated_by_last_action?
|
||||
end
|
||||
end
|
||||
|
||||
def lookup_or_create_service(name)
|
||||
begin
|
||||
iptables_service = Chef.run_context.resource_collection.find(service: svc)
|
||||
rescue
|
||||
iptables_service = service name do
|
||||
action :nothing
|
||||
end
|
||||
end
|
||||
iptables_service
|
||||
end
|
||||
|
||||
def lookup_or_create_rulesfile(name)
|
||||
begin
|
||||
iptables_file = Chef.run_context.resource_collection.find(file: name)
|
||||
rescue
|
||||
iptables_file = file "/etc/iptables/#{name}" do
|
||||
action :nothing
|
||||
end
|
||||
end
|
||||
iptables_file
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,195 @@
|
||||
#
|
||||
# Author:: Seth Chisamore (<schisamo@opscode.com>)
|
||||
# Cookbook Name:: firewall
|
||||
# Resource:: default
|
||||
#
|
||||
# Copyright:: 2011, Opscode, Inc.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
class Chef
|
||||
class Provider::FirewallIptablesUbuntu1404 < Chef::Provider::LWRPBase
|
||||
include FirewallCookbook::Helpers
|
||||
include FirewallCookbook::Helpers::Iptables
|
||||
|
||||
provides :firewall, os: 'linux', platform_family: %w(debian) do |node|
|
||||
node['platform_version'].to_f <= 14.04 && node['firewall'] && node['firewall']['ubuntu_iptables']
|
||||
end
|
||||
|
||||
def whyrun_supported?
|
||||
false
|
||||
end
|
||||
|
||||
def action_install
|
||||
return if disabled?(new_resource)
|
||||
|
||||
# Ensure the package is installed
|
||||
pkg = package 'iptables-persistent' do
|
||||
action :nothing
|
||||
end
|
||||
pkg.run_action(:install)
|
||||
new_resource.updated_by_last_action(true) if pkg.updated_by_last_action?
|
||||
|
||||
rule_files = %w(rules.v4)
|
||||
rule_files << 'rules.v6' if ipv6_enabled?(new_resource)
|
||||
rule_files.each do |svc|
|
||||
next if ::File.exist?("/etc/iptables/#{svc}")
|
||||
|
||||
# must create empty file for service to start
|
||||
f = lookup_or_create_rulesfile(svc)
|
||||
f.content '# created by chef to allow service to start'
|
||||
f.run_action(:create)
|
||||
|
||||
new_resource.updated_by_last_action(true) if f.updated_by_last_action?
|
||||
end
|
||||
|
||||
iptables_service = lookup_or_create_service('iptables-persistent')
|
||||
[:enable, :start].each do |act|
|
||||
# iptables-persistent isn't a real service
|
||||
iptables_service.status_command 'true'
|
||||
|
||||
iptables_service.run_action(act)
|
||||
new_resource.updated_by_last_action(true) if iptables_service.updated_by_last_action?
|
||||
end
|
||||
end
|
||||
|
||||
def action_restart
|
||||
return if disabled?(new_resource)
|
||||
|
||||
# prints all the firewall rules
|
||||
log_iptables(new_resource)
|
||||
|
||||
# ensure it's initialized
|
||||
new_resource.rules({}) unless new_resource.rules
|
||||
ensure_default_rules_exist(node, new_resource)
|
||||
|
||||
# this populates the hash of rules from firewall_rule resources
|
||||
firewall_rules = Chef.run_context.resource_collection.select { |item| item.is_a?(Chef::Resource::FirewallRule) }
|
||||
firewall_rules.each do |firewall_rule|
|
||||
next unless firewall_rule.action.include?(:create) && !firewall_rule.should_skip?(:create)
|
||||
|
||||
types = if ipv6_rule?(firewall_rule) # an ip4 specific rule
|
||||
%w(ip6tables)
|
||||
elsif ipv4_rule?(firewall_rule) # an ip6 specific rule
|
||||
%w(iptables)
|
||||
else # or not specific
|
||||
%w(iptables ip6tables)
|
||||
end
|
||||
|
||||
types.each do |iptables_type|
|
||||
# build rules to apply with weight
|
||||
k = build_firewall_rule(node, firewall_rule, iptables_type == 'ip6tables')
|
||||
v = firewall_rule.position
|
||||
|
||||
# unless we're adding them for the first time.... bail out.
|
||||
next if new_resource.rules[iptables_type].key?(k) && new_resource.rules[iptables_type][k] == v
|
||||
new_resource.rules[iptables_type][k] = v
|
||||
end
|
||||
end
|
||||
|
||||
rule_files = %w(iptables)
|
||||
rule_files << 'ip6tables' if ipv6_enabled?(new_resource)
|
||||
|
||||
rule_files.each do |iptables_type|
|
||||
iptables_filename = if iptables_type == 'ip6tables'
|
||||
'/etc/iptables/rules.v6'
|
||||
else
|
||||
'/etc/iptables/rules.v4'
|
||||
end
|
||||
|
||||
# ensure a file resource exists with the current iptables rules
|
||||
begin
|
||||
iptables_file = Chef.run_context.resource_collection.find(file: iptables_filename)
|
||||
rescue
|
||||
iptables_file = file iptables_filename do
|
||||
action :nothing
|
||||
end
|
||||
end
|
||||
iptables_file.content build_rule_file(new_resource.rules[iptables_type])
|
||||
iptables_file.run_action(:create)
|
||||
|
||||
# if the file was changed, restart iptables
|
||||
next unless iptables_file.updated_by_last_action?
|
||||
service_affected = service 'iptables-persistent' do
|
||||
action :nothing
|
||||
end
|
||||
|
||||
new_resource.notifies(:restart, service_affected, :delayed)
|
||||
new_resource.updated_by_last_action(true)
|
||||
end
|
||||
end
|
||||
|
||||
def action_disable
|
||||
return if disabled?(new_resource)
|
||||
|
||||
iptables_flush!(new_resource)
|
||||
iptables_default_allow!(new_resource)
|
||||
new_resource.updated_by_last_action(true)
|
||||
|
||||
iptables_service = lookup_or_create_service('iptables-persistent')
|
||||
[:disable, :stop].each do |act|
|
||||
iptables_service.run_action(act)
|
||||
new_resource.updated_by_last_action(true) if iptables_service.updated_by_last_action?
|
||||
end
|
||||
|
||||
%w(rules.v4 rules.v6).each do |svc|
|
||||
# must create empty file for service to start
|
||||
f = lookup_or_create_rulesfile(svc)
|
||||
f.content '# created by chef to allow service to start'
|
||||
f.run_action(:create)
|
||||
|
||||
new_resource.updated_by_last_action(true) if f.updated_by_last_action?
|
||||
end
|
||||
end
|
||||
|
||||
def action_flush
|
||||
return if disabled?(new_resource)
|
||||
|
||||
iptables_flush!(new_resource)
|
||||
new_resource.updated_by_last_action(true)
|
||||
|
||||
rule_files = %w(rules.v4)
|
||||
rule_files << 'rules.v6' if ipv6_enabled?(new_resource)
|
||||
rule_files.each do |svc|
|
||||
# must create empty file for service to start
|
||||
f = lookup_or_create_rulesfile(svc)
|
||||
f.content '# created by chef to allow service to start'
|
||||
f.run_action(:create)
|
||||
|
||||
new_resource.updated_by_last_action(true) if f.updated_by_last_action?
|
||||
end
|
||||
end
|
||||
|
||||
def lookup_or_create_service(name)
|
||||
begin
|
||||
iptables_service = Chef.run_context.resource_collection.find(service: svc)
|
||||
rescue
|
||||
iptables_service = service name do
|
||||
action :nothing
|
||||
end
|
||||
end
|
||||
iptables_service
|
||||
end
|
||||
|
||||
def lookup_or_create_rulesfile(name)
|
||||
begin
|
||||
iptables_file = Chef.run_context.resource_collection.find(file: name)
|
||||
rescue
|
||||
iptables_file = file "/etc/iptables/#{name}" do
|
||||
action :nothing
|
||||
end
|
||||
end
|
||||
iptables_file
|
||||
end
|
||||
end
|
||||
end
|
||||
34
cookbooks/firewall/libraries/provider_firewall_rule.rb
Normal file
34
cookbooks/firewall/libraries/provider_firewall_rule.rb
Normal file
@@ -0,0 +1,34 @@
|
||||
#
|
||||
# Author:: Ronald Doorn (<rdoorn@schubergphilis.com>)
|
||||
# Cookbook:: firewall
|
||||
# Provider:: rule_iptables
|
||||
#
|
||||
# Copyright:: 2015-2016, computerlyrik
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
class Chef
|
||||
class Provider::FirewallRuleGeneric < Chef::Provider::LWRPBase
|
||||
provides :firewall_rule
|
||||
|
||||
def action_create
|
||||
return unless new_resource.notify_firewall
|
||||
|
||||
firewall_resource = Chef.run_context.resource_collection.find(firewall: new_resource.firewall_name)
|
||||
raise 'could not find a firewall resource' unless firewall_resource
|
||||
|
||||
new_resource.notifies(:restart, firewall_resource, :delayed)
|
||||
new_resource.updated_by_last_action(true)
|
||||
end
|
||||
end
|
||||
end
|
||||
138
cookbooks/firewall/libraries/provider_firewall_ufw.rb
Normal file
138
cookbooks/firewall/libraries/provider_firewall_ufw.rb
Normal file
@@ -0,0 +1,138 @@
|
||||
#
|
||||
# Author:: Seth Chisamore (<schisamo@opscode.com>)
|
||||
# Cookbook:: firewall
|
||||
# Resource:: default
|
||||
#
|
||||
# Copyright:: 2011-2016, Chef Software, Inc.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
class Chef
|
||||
class Provider::FirewallUfw < Chef::Provider::LWRPBase
|
||||
include FirewallCookbook::Helpers::Ufw
|
||||
|
||||
provides :firewall, os: 'linux', platform_family: %w(debian) do |node|
|
||||
!(node['firewall'] && node['firewall']['ubuntu_iptables'])
|
||||
end
|
||||
|
||||
def whyrun_supported?
|
||||
false
|
||||
end
|
||||
|
||||
def action_install
|
||||
return if disabled?(new_resource)
|
||||
|
||||
pkg_ufw = package 'ufw' do
|
||||
action :nothing
|
||||
end
|
||||
pkg_ufw.run_action(:install)
|
||||
new_resource.updated_by_last_action(true) if pkg_ufw.updated_by_last_action?
|
||||
|
||||
defaults_ufw = template '/etc/default/ufw' do
|
||||
action :nothing
|
||||
owner 'root'
|
||||
group 'root'
|
||||
mode '0644'
|
||||
source 'ufw/default.erb'
|
||||
cookbook 'firewall'
|
||||
end
|
||||
defaults_ufw.run_action(:create)
|
||||
new_resource.updated_by_last_action(true) if defaults_ufw.updated_by_last_action?
|
||||
|
||||
return if ::File.exist?(ufw_rules_filename)
|
||||
|
||||
ufw_file = lookup_or_create_rulesfile
|
||||
ufw_file.content '# created by chef to allow service to start'
|
||||
ufw_file.run_action(:create)
|
||||
|
||||
new_resource.updated_by_last_action(true) if ufw_file.updated_by_last_action?
|
||||
end
|
||||
|
||||
def action_restart
|
||||
return if disabled?(new_resource)
|
||||
|
||||
# ensure it's initialized
|
||||
new_resource.rules({}) unless new_resource.rules
|
||||
new_resource.rules['ufw'] = {} unless new_resource.rules['ufw']
|
||||
|
||||
# this populates the hash of rules from firewall_rule resources
|
||||
firewall_rules = Chef.run_context.resource_collection.select { |item| item.is_a?(Chef::Resource::FirewallRule) }
|
||||
firewall_rules.each do |firewall_rule|
|
||||
next unless firewall_rule.action.include?(:create) && !firewall_rule.should_skip?(:create)
|
||||
|
||||
# build rules to apply with weight
|
||||
k = build_rule(firewall_rule)
|
||||
v = firewall_rule.position
|
||||
|
||||
# unless we're adding them for the first time.... bail out.
|
||||
unless new_resource.rules['ufw'].key?(k) && new_resource.rules['ufw'][k] == v
|
||||
new_resource.rules['ufw'][k] = v
|
||||
end
|
||||
end
|
||||
|
||||
# ensure a file resource exists with the current ufw rules
|
||||
ufw_file = lookup_or_create_rulesfile
|
||||
ufw_file.content build_rule_file(new_resource.rules['ufw'])
|
||||
ufw_file.run_action(:create)
|
||||
|
||||
# if the file was changed, restart iptables
|
||||
return unless ufw_file.updated_by_last_action?
|
||||
ufw_reset!
|
||||
ufw_logging!(new_resource.log_level) if new_resource.log_level
|
||||
|
||||
new_resource.rules['ufw'].sort_by { |_k, v| v }.map { |k, _v| k }.each do |cmd|
|
||||
ufw_rule!(cmd)
|
||||
end
|
||||
|
||||
# ensure it's enabled _after_ rules are inputted, to catch malformed rules
|
||||
ufw_enable! unless ufw_active?
|
||||
new_resource.updated_by_last_action(true)
|
||||
end
|
||||
|
||||
def action_disable
|
||||
return if disabled?(new_resource)
|
||||
|
||||
ufw_file = lookup_or_create_rulesfile
|
||||
ufw_file.content '# created by chef to allow service to start'
|
||||
ufw_file.run_action(:create)
|
||||
new_resource.updated_by_last_action(true) if ufw_file.updated_by_last_action?
|
||||
|
||||
return unless ufw_active?
|
||||
ufw_disable!
|
||||
new_resource.updated_by_last_action(true)
|
||||
end
|
||||
|
||||
def action_flush
|
||||
return if disabled?(new_resource)
|
||||
|
||||
ufw_reset!
|
||||
new_resource.updated_by_last_action(true)
|
||||
|
||||
ufw_file = lookup_or_create_rulesfile
|
||||
ufw_file.content '# created by chef to allow service to start'
|
||||
ufw_file.run_action(:create)
|
||||
new_resource.updated_by_last_action(true) if ufw_file.updated_by_last_action?
|
||||
end
|
||||
|
||||
def lookup_or_create_rulesfile
|
||||
begin
|
||||
ufw_file = Chef.run_context.resource_collection.find(file: ufw_rules_filename)
|
||||
rescue
|
||||
ufw_file = file ufw_rules_filename do
|
||||
action :nothing
|
||||
end
|
||||
end
|
||||
ufw_file
|
||||
end
|
||||
end
|
||||
end
|
||||
126
cookbooks/firewall/libraries/provider_firewall_windows.rb
Normal file
126
cookbooks/firewall/libraries/provider_firewall_windows.rb
Normal file
@@ -0,0 +1,126 @@
|
||||
#
|
||||
# Author:: Sander van Harmelen (<svanharmelen@schubergphilis.com>)
|
||||
# Cookbook:: firewall
|
||||
# Provider:: windows
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
|
||||
class Chef
|
||||
class Provider::FirewallWindows < Chef::Provider::LWRPBase
|
||||
include FirewallCookbook::Helpers::Windows
|
||||
|
||||
provides :firewall, os: 'windows'
|
||||
|
||||
def whyrun_supported?
|
||||
false
|
||||
end
|
||||
|
||||
def action_install
|
||||
return if disabled?(new_resource)
|
||||
|
||||
svc = service 'MpsSvc' do
|
||||
action :nothing
|
||||
end
|
||||
|
||||
[:enable, :start].each do |act|
|
||||
svc.run_action(act)
|
||||
new_resource.updated_by_last_action(true) if svc.updated_by_last_action?
|
||||
end
|
||||
end
|
||||
|
||||
def action_restart
|
||||
return if disabled?(new_resource)
|
||||
|
||||
# ensure it's initialized
|
||||
new_resource.rules({}) unless new_resource.rules
|
||||
new_resource.rules['windows'] = {} unless new_resource.rules['windows']
|
||||
|
||||
firewall_rules = Chef.run_context.resource_collection.select { |item| item.is_a?(Chef::Resource::FirewallRule) }
|
||||
firewall_rules.each do |firewall_rule|
|
||||
next unless firewall_rule.action.include?(:create) && !firewall_rule.should_skip?(:create)
|
||||
|
||||
# build rules to apply with weight
|
||||
k = build_rule(firewall_rule)
|
||||
v = firewall_rule.position
|
||||
|
||||
# unless we're adding them for the first time.... bail out.
|
||||
unless new_resource.rules['windows'].key?(k) && new_resource.rules['windows'][k] == v
|
||||
new_resource.rules['windows'][k] = v
|
||||
end
|
||||
end
|
||||
|
||||
input_policy = node['firewall']['windows']['defaults']['policy']['input']
|
||||
output_policy = node['firewall']['windows']['defaults']['policy']['output']
|
||||
unless new_resource.rules['windows'].key?("set currentprofile firewallpolicy #{input_policy},#{output_policy}")
|
||||
# Make this the possible last rule in the list
|
||||
new_resource.rules['windows']["set currentprofile firewallpolicy #{input_policy},#{output_policy}"] = 99999
|
||||
end
|
||||
|
||||
# ensure a file resource exists with the current rules
|
||||
begin
|
||||
windows_file = Chef.run_context.resource_collection.find(file: windows_rules_filename)
|
||||
rescue
|
||||
windows_file = file windows_rules_filename do
|
||||
action :nothing
|
||||
end
|
||||
end
|
||||
windows_file.content build_rule_file(new_resource.rules['windows'])
|
||||
windows_file.run_action(:create)
|
||||
|
||||
# if the file was changed, restart iptables
|
||||
return unless windows_file.updated_by_last_action?
|
||||
|
||||
disable! if active?
|
||||
delete_all_rules! # clear entirely
|
||||
reset! # populate default rules
|
||||
|
||||
new_resource.rules['windows'].sort_by { |_k, v| v }.map { |k, _v| k }.each do |cmd|
|
||||
add_rule!(cmd)
|
||||
end
|
||||
# ensure it's enabled _after_ rules are inputted, to catch malformed rules
|
||||
enable! unless active?
|
||||
|
||||
new_resource.updated_by_last_action(true)
|
||||
end
|
||||
|
||||
def action_disable
|
||||
return if disabled?(new_resource)
|
||||
|
||||
if active?
|
||||
disable!
|
||||
Chef::Log.info("#{new_resource} disabled.")
|
||||
new_resource.updated_by_last_action(true)
|
||||
else
|
||||
Chef::Log.debug("#{new_resource} already disabled.")
|
||||
end
|
||||
|
||||
svc = service 'MpsSvc' do
|
||||
action :nothing
|
||||
end
|
||||
|
||||
[:disable, :stop].each do |act|
|
||||
svc.run_action(act)
|
||||
new_resource.updated_by_last_action(true) if svc.updated_by_last_action?
|
||||
end
|
||||
end
|
||||
|
||||
def action_flush
|
||||
return if disabled?(new_resource)
|
||||
|
||||
reset!
|
||||
Chef::Log.info("#{new_resource} reset.")
|
||||
new_resource.updated_by_last_action(true)
|
||||
end
|
||||
end
|
||||
end
|
||||
26
cookbooks/firewall/libraries/resource_firewall.rb
Normal file
26
cookbooks/firewall/libraries/resource_firewall.rb
Normal file
@@ -0,0 +1,26 @@
|
||||
class Chef
|
||||
class Resource::Firewall < Chef::Resource::LWRPBase
|
||||
resource_name(:firewall)
|
||||
provides(:firewall)
|
||||
actions(:install, :restart, :disable, :flush, :save)
|
||||
default_action(:install)
|
||||
|
||||
# allow both kinds of logic -- eventually remove the :disabled one.
|
||||
# the positive logic is much easier to follow.
|
||||
attribute(:disabled, kind_of: [TrueClass, FalseClass], default: false)
|
||||
attribute(:enabled, kind_of: [TrueClass, FalseClass], default: true)
|
||||
|
||||
attribute(:log_level, kind_of: Symbol, equal_to: [:low, :medium, :high, :full, :off], default: :low)
|
||||
attribute(:rules, kind_of: Hash)
|
||||
|
||||
# for firewalld, specify the zone when firewall is disable and enabled
|
||||
attribute(:disabled_zone, kind_of: Symbol, default: :public)
|
||||
attribute(:enabled_zone, kind_of: Symbol, default: :drop)
|
||||
|
||||
# for firewall implementations where ipv6 can be skipped (currently iptables-specific)
|
||||
attribute(:ipv6_enabled, kind_of: [TrueClass, FalseClass], default: true)
|
||||
|
||||
# allow override of package options for firewalld package
|
||||
attribute(:package_options, kind_of: String, default: nil)
|
||||
end
|
||||
end
|
||||
52
cookbooks/firewall/libraries/resource_firewall_rule.rb
Normal file
52
cookbooks/firewall/libraries/resource_firewall_rule.rb
Normal file
@@ -0,0 +1,52 @@
|
||||
require 'ipaddr'
|
||||
|
||||
class Chef
|
||||
class Resource::FirewallRule < Chef::Resource::LWRPBase
|
||||
include FirewallCookbook::Helpers
|
||||
|
||||
resource_name(:firewall_rule)
|
||||
provides(:firewall_rule)
|
||||
actions(:create)
|
||||
default_action(:create)
|
||||
|
||||
attribute(:firewall_name, kind_of: String, default: 'default')
|
||||
|
||||
attribute(:command, kind_of: Symbol, equal_to: [:reject, :allow, :deny, :masquerade, :redirect, :log], default: :allow)
|
||||
|
||||
attribute(:protocol, kind_of: [Integer, Symbol], default: :tcp,
|
||||
callbacks: { 'must be either :tcp, :udp, :icmp, :\'ipv6-icmp\', :icmpv6, :none, or a valid IP protocol number' => lambda do |p|
|
||||
!!(p.to_s =~ /(udp|tcp|icmp|icmpv6|ipv6-icmp|esp|ah|ipv6|none)/ || (p.to_s =~ /^\d+$/ && p.between?(0, 142)))
|
||||
end })
|
||||
attribute(:direction, kind_of: Symbol, equal_to: [:in, :out, :pre, :post], default: :in)
|
||||
attribute(:logging, kind_of: Symbol, equal_to: [:connections, :packets])
|
||||
|
||||
attribute(:source, callbacks: { 'must be a valid ip address' => ->(ip) { !!IPAddr.new(ip) } })
|
||||
attribute(:source_port, kind_of: [Integer, Array, Range]) # source port
|
||||
attribute(:interface, kind_of: String)
|
||||
|
||||
attribute(:port, kind_of: [Integer, Array, Range]) # shorthand for dest_port
|
||||
attribute(:destination, callbacks: { 'must be a valid ip address' => ->(ip) { !!IPAddr.new(ip) } })
|
||||
attribute(:dest_port, kind_of: [Integer, Array, Range])
|
||||
attribute(:dest_interface, kind_of: String)
|
||||
|
||||
attribute(:position, kind_of: Integer, default: 50)
|
||||
attribute(:stateful, kind_of: [Symbol, Array])
|
||||
attribute(:redirect_port, kind_of: Integer)
|
||||
attribute(:description, kind_of: String, name_attribute: true)
|
||||
attribute(:include_comment, kind_of: [TrueClass, FalseClass], default: true)
|
||||
|
||||
# only used for firewalld
|
||||
attribute(:permanent, kind_of: [TrueClass, FalseClass], default: false)
|
||||
|
||||
# only used for Windows Firewalls
|
||||
attribute(:program, kind_of: String)
|
||||
attribute(:service, kind_of: String)
|
||||
|
||||
# for when you just want to pass a raw rule
|
||||
attribute(:raw, kind_of: String)
|
||||
|
||||
# do you want this rule to notify the firewall to recalculate
|
||||
# (and potentially reapply) the firewall_rule(s) it finds?
|
||||
attribute(:notify_firewall, kind_of: [TrueClass, FalseClass], default: true)
|
||||
end
|
||||
end
|
||||
1
cookbooks/firewall/metadata.json
Normal file
1
cookbooks/firewall/metadata.json
Normal file
File diff suppressed because one or more lines are too long
80
cookbooks/firewall/recipes/default.rb
Normal file
80
cookbooks/firewall/recipes/default.rb
Normal file
@@ -0,0 +1,80 @@
|
||||
#
|
||||
# Cookbook:: firewall
|
||||
# Recipe:: default
|
||||
#
|
||||
# Copyright:: 2011-2016, Chef Software, Inc.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
|
||||
chef_sugar_cookbook_version = Gem::Version.new(run_context.cookbook_collection['chef-sugar'].metadata.version)
|
||||
|
||||
include_recipe 'chef-sugar' if chef_sugar_cookbook_version < Gem::Version.new('4.0.0')
|
||||
|
||||
firewall 'default' do
|
||||
ipv6_enabled node['firewall']['ipv6_enabled']
|
||||
action :install
|
||||
end
|
||||
|
||||
# create a variable to use as a condition on some rules that follow
|
||||
iptables_firewall = rhel? || node['firewall']['ubuntu_iptables']
|
||||
|
||||
firewall_rule 'allow loopback' do
|
||||
interface 'lo'
|
||||
protocol :none
|
||||
command :allow
|
||||
only_if { linux? && node['firewall']['allow_loopback'] }
|
||||
end
|
||||
|
||||
firewall_rule 'allow icmp' do
|
||||
protocol :icmp
|
||||
command :allow
|
||||
# debian ufw doesn't allow 'icmp' protocol, but does open
|
||||
# icmp by default, so we skip it in default recipe
|
||||
only_if { (!debian? || iptables_firewall) && node['firewall']['allow_icmp'] }
|
||||
end
|
||||
|
||||
firewall_rule 'allow world to ssh' do
|
||||
port 22
|
||||
source '0.0.0.0/0'
|
||||
only_if { linux? && node['firewall']['allow_ssh'] }
|
||||
end
|
||||
|
||||
firewall_rule 'allow world to winrm' do
|
||||
port 5989
|
||||
source '0.0.0.0/0'
|
||||
only_if { windows? && node['firewall']['allow_winrm'] }
|
||||
end
|
||||
|
||||
firewall_rule 'allow world to mosh' do
|
||||
protocol :udp
|
||||
port 60000..61000
|
||||
source '0.0.0.0/0'
|
||||
only_if { linux? && node['firewall']['allow_mosh'] }
|
||||
end
|
||||
|
||||
# allow established connections, ufw defaults to this but iptables does not
|
||||
firewall_rule 'established' do
|
||||
stateful [:related, :established]
|
||||
protocol :none # explicitly don't specify protocol
|
||||
command :allow
|
||||
only_if { node['firewall']['allow_established'] && iptables_firewall }
|
||||
end
|
||||
|
||||
# ipv6 needs ICMP to reliably work, so ensure it's enabled if ipv6
|
||||
# allow established connections, ufw defaults to this but iptables does not
|
||||
firewall_rule 'ipv6_icmp' do
|
||||
protocol :'ipv6-icmp'
|
||||
command :allow
|
||||
only_if { node['firewall']['ipv6_enabled'] && node['firewall']['allow_established'] && iptables_firewall }
|
||||
end
|
||||
23
cookbooks/firewall/recipes/disable_firewall.rb
Normal file
23
cookbooks/firewall/recipes/disable_firewall.rb
Normal file
@@ -0,0 +1,23 @@
|
||||
#
|
||||
# Cookbook:: firewall
|
||||
# Recipe:: disable_firewall
|
||||
#
|
||||
# Copyright:: 2011-2016, Chef Software, Inc.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
|
||||
# Disable platform default firewall
|
||||
firewall 'default' do
|
||||
action :disable
|
||||
end
|
||||
13
cookbooks/firewall/templates/default/ufw/default.erb
Normal file
13
cookbooks/firewall/templates/default/ufw/default.erb
Normal file
@@ -0,0 +1,13 @@
|
||||
# /etc/default/ufw
|
||||
# This file is managed by Chef. Do not edit.
|
||||
|
||||
IPV6=<%= node['firewall']['ufw']['defaults']['ipv6'] %>
|
||||
MANAGE_BUILTINS=<%= node['firewall']['ufw']['defaults']['manage_builtins'] %>
|
||||
|
||||
<% node['firewall']['ufw']['defaults']['policy'].each do |policy, value| -%>
|
||||
<%= "DEFAULT_#{policy.upcase}_POLICY=\"#{value}\"" %>
|
||||
<% end -%>
|
||||
|
||||
IPT_SYSCTL="<%= node['firewall']['ufw']['defaults']['ipt_sysctl'] %>"
|
||||
|
||||
IPT_MODULES="<%= node['firewall']['ufw']['defaults']['ipt_modules'] %>"
|
||||
Reference in New Issue
Block a user