项目作者: voxpupuli

项目描述 :
Puppet module for managing firewalld
高级语言: Ruby
项目地址: git://github.com/voxpupuli/puppet-firewalld.git
创建时间: 2015-03-03T13:07:02Z
项目社区:https://github.com/voxpupuli/puppet-firewalld

开源协议:Apache License 2.0

下载


Module: firewalld

License
Build Status
Code Coverage
Puppet Forge
Puppet Forge - downloads
Puppet Forge - endorsement
Puppet Forge - scores

Description

This module manages firewalld, the userland interface that replaces
iptables and ships with RHEL7+. The module manages firewalld itself as
well as providing types and providers for managing firewalld zones,
policies, ports, and rich rules.

Compatibility

Latest versions of this module are only supported on Puppet
7.0+.

Usage

  1. class { 'firewalld': }

Parameters

  • package: Name of the package to install (default firewalld)
  • package_ensure: Default ‘installed’, can be any supported ensure type for
    the package resource
  • config_package: Name of the GUI package, default firewall-config
  • install_gui: Whether or not to install the config_package (default: false)
  • service_ensure: Whether the service should be running or not (default: running)
  • service_enable: Whether to enable the service
  • default_zone: Optional, set the default zone for interfaces (default: undef)
  • firewall_backend: Optional, set the firewall backend for firewalld (default:
    undef)
  • default_service_zone: Optional, set the default zone for services (default: undef)
  • default_port_zone: Optional, set the default zone for ports (default: undef)
  • default_port_protocol: Optional, set the default protocol for ports
    (default: undef)
  • log_denied: Optional, (firewalld-0.4.3.2-8+) Log denied packets, can be one
    of off, all, multicast, unicast, broadcast (default: undef)
  • zones: A hash of firewalld zones to configure
  • policies: A hash of firewalld policies to configure
  • ports: A hash of firewalld ports to configure
  • services: A hash of firewalld services to configure
  • rich_rules: A hash of firewalld rich rules to configure
  • custom_services: A hash of firewalld custom
    services
    to configure
  • direct_rules: A hash of firewalld direct rules to
    configure
  • direct_chains: A hash of firewalld direct chains
    to configure
  • direct_passthroughs: A hash of firewalld direct
    passthroughs
    to configure
  • purge_direct_rules: True or false, whether to purge firewalld direct
    rules
  • purge_direct_chains: True or false, whether to purge firewalld direct
    chains
  • purge_direct_passthroughs: True or false, whether to purge firewalld direct
    passthroughs

Resource Types

The firewalld module contains types and providers to manage zones,
services, ports, and rich rules by interfacing with the firewall-cmd
command. The following types are currently supported. Note that all
zone, service, port, and rule management is done in --permanent
mode, and a complete reload will be triggered anytime something
changes.

This module supports a number of resource types

Note, it is always recommended to include the ::firewalld class if
you are going to use any of these resources from another Puppet class
(eg: a profile) as it sets up the relationships between the
firewalld service resource and the exec resource to reload the
firewall upon change. Without the firewalld class included then the
firewall will not be reloaded upon change. The recommended pattern is
to put all resources into hiera and let the firewalld class set them
up. Examples of both forms are presented for the resource types below.

Firewalld Zones

Firewalld zones can be managed with the firewalld_zone resource type.

Example in Class:

  1. firewalld_zone { 'restricted':
  2. ensure => present,
  3. target => '%%REJECT%%',
  4. purge_rich_rules => true,
  5. purge_services => true,
  6. purge_ports => true,
  7. }

Example in Hiera:

  1. firewalld::zones:
  2. restricted:
  3. ensure: present
  4. target: '%%REJECT%%'
  5. purge_rich_rules: true
  6. purge_services: true
  7. purge_ports: true

Parameters (Firewalld Zones)

  • target: Specify the target of the zone.
  • interfaces: An array of interfaces for this zone
  • sources: An array of sources for the zone
  • protocols: An array of protocols for the zone
  • icmp_blocks: An array of ICMP blocks for the zone
  • masquerade: If set to true or false specifies whether or not
    to add masquerading to the zone
  • purge_rich_rules: Optional, and defaulted to false. When true any
    configured rich rules found in the zone that do not match what is in
    the Puppet catalog will be purged.
  • purge_services: Optional, and defaulted to false. When true any
    configured services found in the zone that do not match what is in
    the Puppet catalog will be purged. Warning: This includes the
    default ssh service, if you need SSH to access the box, make sure
    you add the service through either a rich firewall rule, port, or
    service (see below) or you will lock yourself out!
  • purge_ports: Optional, and defaulted to false. When true any
    configured ports found in the zone that do not match what is in the
    Puppet catalog will be purged. Warning: As with services, this
    includes the default ssh port. If you fail to specify the
    appropriate port, rich rule, or service, you will lock yourself out.

Firewalld policies

Firewalld policies can be managed with the firewalld_policy resource type.

Example in Class:

  1. firewalld_policy { 'anytorestricted':
  2. ensure => present,
  3. target => '%%REJECT%%',
  4. ingress_zones => ['ANY'],
  5. egress_zones => ['restricted'],
  6. purge_rich_rules => true,
  7. purge_services => true,
  8. purge_ports => true,
  9. }

Example in Hiera:

  1. firewalld::policies:
  2. anytorestricted:
  3. ensure: present
  4. target: '%%REJECT%%'
  5. ingress_zones:
  6. - 'ANY'
  7. egress_zones:
  8. - 'restricted'
  9. purge_rich_rules: true
  10. purge_services: true
  11. purge_ports: true

Parameters (Firewalld policies)

  • target: Specify the target of the policy.
  • ingress_zones: An array of ingress zones for this policy.
  • egress_zones: An array of egress zones for this policy.
  • priority: A non zero integer specifying the priority of this
    policy, policies with negative priorities apply before rules in
    zones, policies with positive priorities, after. Defaults to -1.
  • icmp_blocks: An array of ICMP blocks for the policy
  • masquerade: If set to true or false specifies whether or not
    to add masquerading to the policy
  • purge_rich_rules: Optional, and defaulted to false. When true any
    configured rich rules found in the policy that do not match what is in
    the Puppet catalog will be purged.
  • purge_services: Optional, and defaulted to false. When true any
    configured services found in the policy that do not match what is in
    the Puppet catalog will be purged.
  • purge_ports: Optional, and defaulted to false. When true any
    configured ports found in the policy that do not match what is in the
    Puppet catalog will be purged.

Firewalld Rich Rules

Firewalld rich rules are managed using the firewalld_rich_rule
resource type

Exactly one of the zone or policy parameters must be given

firewalld_rich_rules will autorequire the firewalld_zone specified
in the zone parameter or the firewalld_policy specified in the
policy parameter so there is no need to add dependencies for this

Example in Class:

  1. firewalld_rich_rule { 'Accept SSH from barny':
  2. ensure => present,
  3. zone => 'restricted',
  4. source => '192.168.1.2/32',
  5. service => 'ssh',
  6. action => 'accept',
  7. }

Example in Hiera:

  1. firewalld::rich_rules:
  2. 'Accept SSH from barny':
  3. ensure: present
  4. zone: restricted
  5. source: '192.168.1.2/32'
  6. service: 'ssh'
  7. action: 'accept'

Parameters (Firewalld Rich Rules)

  • zone: (Optional) Name of the zone this rich rule belongs to

  • policy: (Optional) Name of the policy this rich rule belongs to

  • family: Protocol family, defaults to ipv4

  • source: Source address information. This can be a hash containing
    the keys address or ipset and invert, or a string containing
    just the IP address

    1. source => '192.168.2.1',
    2. source => { 'address' => '192.168.1.1', 'invert' => true }
    3. source => { 'ipset' => 'whitelist', 'invert' => true }
    4. source => { 'ipset' => 'blacklist' }
  • dest: Destination address information. This can be a hash
    containing the keys address or ipset and invert, or a string
    containing just the IP address

    1. dest => '192.168.2.1',
    2. dest => { 'address' => '192.168.1.1', 'invert' => true }
    3. dest => { 'ipset' => 'whitelist', 'invert' => true }
    4. dest => { 'ipset' => 'blacklist' }
  • log: When set to true will enable logging, optionally this can
    be hash with prefix, level and limit

    1. log => { 'level' => 'debug', 'prefix' => 'foo' },
    2. log => true,
  • audit: When set to true will enable auditing, optionally this
    can be hash with limit

    1. audit => { 'limit' => '3/s' },
    2. audit => true,
  • action: A string containing the action accept, reject or
    drop. For reject it can be optionally supplied as a hash
    containing type

    1. action => 'accept'
    2. action => { 'action' => 'reject', 'type' => 'bad' }

The following paramters are the element of the rich rule, only one
may be used.

  • service: Name of the service

  • protocol: Protocol of the rich rule

  • port: A hash containing port and protocol values

    1. port => {
    2. 'port' => 80,
    3. 'protocol' => 'tcp',
    4. },
  • icmp_block: Specify an icmp-block for the rule

  • masquerade: Set to true or false to enable masquerading

  • forward_port: Set forward-port, this should be a hash containing
    port,protocol,to_port,to_addr

    1. forward_port => {
    2. 'port' => '8080',
    3. 'protocol' => 'tcp',
    4. 'to_addr' => '10.2.1.1',
    5. 'to_port' => '8993'
    6. },

Firewalld Custom Service

The firewalld_custom_service defined type creates and manages
custom services. It makes the service usable by firewalld, but does
not add it to any zones. To do that, use the firewalld::service type.

Example in Class:

  1. firewalld_custom_service{'puppet':
  2. short => 'puppet',
  3. description => 'Puppet Client access Puppet Server',
  4. ports => [
  5. {
  6. 'port' => '8140',
  7. 'protocol' => 'tcp',
  8. },
  9. {
  10. 'port' => '8140',
  11. 'protocol' => 'udp',
  12. },
  13. ],
  14. module => ['nf_conntrack_netbios_ns'],
  15. 'ipv4_destination' => '127.0.0.1',
  16. 'ipv6_destination' => '::1'
  17. }

Example in Hiera:

  1. firewalld::custom_services:
  2. puppet:
  3. short: 'puppet'
  4. description: 'Puppet Client access Puppet Server'
  5. ports:
  6. - port: 8140
  7. protocol: 'tcp'
  8. module: 'nf_conntrack_netbios_ns'
  9. ipv4_destination: '127.0.0.1'
  10. ipv6_destination: '::1'

This resource will create the following XML service definition in
/etc/firewalld/services/XZY.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <service>
  3. <short>puppet</short>
  4. <description>Puppet Client access Puppet Server</description>
  5. <port protocol="tcp" port="8140" ></port>
  6. <port protocol="udp" port="8140" ></port>
  7. <module name="nf_conntrack_netbios_ns"></module>
  8. <destination ipv4="127.0.0.1" ipv6="::1"></destination>
  9. </service>

and you will also see ‘puppet’ in the service list when you issue
firewall-cmd --permanent --get-services

Parameters (Firewalld Custom Service)

  • short: (namevar) The short name of the service (what you see in
    the firewalld command line output)

  • description: (Optional) A short description of the service

  • ports: (Optional) The protocol / port definitions for this service.
    Specified as an array of hashes, where each hash defines a protocol
    and/or port associated with this service. Each hash requires both
    port and protocol keys, even if the value is an empty string.
    Specifying a port only works for TCP & UDP, otherwise leave it empty
    and the entire protocol will be allowed. Valid protocols are tcp,
    udp, or any protocol defined in /etc/protocols

    1. ports => [{'port' => '1234', 'protocol' => 'tcp'}],
    2. ports => [{'port' => '4321', 'protocol' => 'udp'}, {'protocol' => 'rdp'}],

The ports parameter can also take a range of ports separated by a
colon or a dash (colons are replaced by dashes), for example:

  1. ports => [ {'port' => '8000:8002', 'protocol' => 'tcp']} ]

will produce:

  1. <port protocol="tcp" port="8000-8002" ></port>
  • protocols: (Optional) An array of protocols allowed by the service
    as defined in /etc/protocols.

    1. protocols => ['ospf'],
  • module: (Optional) An array of strings specifying netfilter kernel
    helper modules associated with this service

  • ipv4_destination: (Optional) A string specifying the destination
    network as a network IP address (optional with /mask), or a plain IP
    address.
    The use of hostnames is possible but not recommended,
    because these will only be resolved at service activation and
    transmitted to the kernel.

    1. ipv4_destination => '192.0.2.0/24',
  • ipv6_destination: (Optional) A string specifying the destination
    network as a network IP address (optional with /mask), or a plain IP
    address.
    The use of hostnames is possible but not recommended,
    because these will only be resolved at service activation and
    transmitted to the kernel.

    1. ipv4_destination => '2001:db8::/32',
  • config_dir: The location where the service definition XML files
    will be stored. Defaults to /etc/firewalld/services

Firewalld Service

The firewalld_service type is used to add or remove both built in
and custom services from zones.

Exactly one of the zone or policy parameters must be given.

firewalld_service will autorequire the firewalld_zone specified in
the zone parameter and the firewalld_custom_service specified in
the service parameter, so there is no need to add dependencies for
this

Example in Class:

  1. firewalld_service { 'Allow SSH from the external zone':
  2. ensure => 'present',
  3. service => 'ssh',
  4. zone => 'external',
  5. }

Example in Hiera:

  1. firewalld::services:
  2. 'Allow SSH from the external zone':
  3. ensure: present
  4. service: ssh
  5. zone: external
  6. dhcp:
  7. ensure: absent
  8. service: dhcp
  9. zone: public
  10. dhcpv6-client:
  11. ensure: present
  12. service: dhcpv6-client
  13. zone: public

Parameters (Firewalld Service)

  • service: Name of the service to manage, defaults to the resource
    name.

  • zone: Name of the zone in which you want to manage the service,
    defaults to parameter default_service_zone of class firewalld if
    specified.

  • policy: Name of the policy in which you want to manage the
    service. Make sure to set zone to unset if you use this and have
    specified default_service_zone for class firewalld.

  • ensure: Whether to add (present) or remove the service
    (absent), defaults to present.

Firewalld IPsets

Firewalld IPsets (on supported versions of firewalld) can be managed
using the firewalld_ipset resource type

Example:

  1. firewalld_ipset { 'whitelist':
  2. ensure => present,
  3. entries => [ '192.168.0.1', '192.168.0.2' ]
  4. }

Example in Hiera:

  1. firewalld::ipsets:
  2. whitelist:
  3. entries:
  4. - 192.168.0.1
  5. - 192.168.0.2

Parameters (Firewalld IPsets)

  • entries: An array of entries for the IPset
  • type: Type of ipset (default: hash:ip)
  • options: A hash of options for the IPset (eg:
    { "family" => "inet6"})

Note that type and options are parameters used when creating the
IPset and are not managed after creation - to change the type or
options of an ipset you must delete the existing ipset first.

Firewalld Ports

Firewalld ports can be managed with the firewalld_port resource
type.

Exactly one of the zone or policy parameters must be given.

firewalld_port will autorequire the firewalld_zone specified in the
zone parameter so there is no need to add dependencies for this

Example:

  1. firewalld_port { 'Open port 8080 in the public zone':
  2. ensure => present,
  3. zone => 'public',
  4. port => 8080,
  5. protocol => 'tcp',
  6. }

Example in Hiera:

  1. firewalld::ports:
  2. 'Open port 8080 in the public zone':
  3. ensure: present
  4. zone: public
  5. port: 8080
  6. protocol: 'tcp'

Parameters (Firewalld Ports)

  • zone: Name of the zone this port belongs to, defaults to parameter
    default_port_zone of class firewalld if specified.

  • policy: Name of the policy this port belongs to. Make sure to set
    zone to unset if you use this and have specified
    default_port_zone for class firewalld.

  • port: The port to manage, defaults to the resource name.

  • protocol: The protocol this port uses, e.g. tcp or udp,
    defaults to parameter default_port_protocol of class firewalld
    if specified.

  • ensure: Whether to add (present) or remove the service
    (absent), defaults to present.

Firewalld Direct Chains

Direct chains can be managed with the firewalld_direct_chain type

Example

  1. firewalld_direct_chain {'Add custom chain LOG_DROPS':
  2. name => 'LOG_DROPS',
  3. ensure => present,
  4. inet_protocol => 'ipv4',
  5. table => 'filter',
  6. }

The title can also be mapped to the types namevars using a colon
delimited string, so the above can also be represented as

  1. firewalld_direct_chain { 'ipv4:filter:LOG_DROPS':
  2. ensure => present,
  3. }

Example in hiera

  1. firewalld::direct_chains:
  2. 'Add custom chain LOG_DROPS':
  3. name: LOG_DROPS
  4. ensure: present
  5. inet_protocol: ipv4
  6. table: filter

Parameters (Firewalld Direct Chains)

  • name: name of the chain, eg LOG_DROPS (namevar)
  • inet_protocol: ipv4, ipv6 or eb, defaults to ipv4 (namevar)
  • table: The table (eg: filter) to apply the chain (namevar)

Firewalld Direct Rules

Direct rules can be applied using the firewalld_direct_rule type

Example (Firewalld Direct Rules)

  1. firewalld_direct_rule {'Allow outgoing SSH connection':
  2. ensure => 'present',
  3. inet_protocol => 'ipv4',
  4. table => 'filter',
  5. chain => 'OUTPUT',
  6. priority => 1,
  7. args => '-p tcp --dport=22 -j ACCEPT',
  8. }

Example in hiera (Firewalld Direct Rules)

  1. firewalld::direct_rules:
  2. 'Allow outgoing SSH connection':
  3. ensure: present
  4. inet_protocol: ipv4
  5. table: filter
  6. chain: OUTPUT
  7. priority: 1
  8. args: '-p tcp --dport=22 -j ACCEPT'

Parameters (Firewalld Direct Rules)

  • name: Resource name in Puppet
  • ensure: present or absent
  • inet_protocol: ipv4, ipv6 or eb, defaults to ipv4
  • table: Table (eg: filter) which to apply the rule
  • chain: Chain (eg: OUTPUT) which to apply the rule
  • priority: The priority number of the rule (e.g: 0, 1, 2, … 99)
  • args: Any iptables, ip6tables and ebtables command line arguments

Firewalld Direct Passthroughs

Direct passthroughs can be applied using the firewalld_direct_passthrough type

Example (Firewalld Direct Passthroughs)

  1. firewalld_direct_passthrough {'Forward traffic from OUTPUT to OUTPUT_filter':
  2. ensure => 'present',
  3. inet_protocol => 'ipv4',
  4. args => '-A OUTPUT -j OUTPUT_filter'
  5. }

Example in hiera (Firewalld Direct Passthroughs)

  1. firewalld::direct_passthroughs:
  2. 'Forward traffic from OUTPUT to OUTPUT_filter':
  3. ensure: present
  4. inet_protocol: ipv4
  5. args: '-A OUTPUT -j OUTPUT_filter'

Parameters (Firewalld Direct Passthroushs)

  • name: Resource name in Puppet
  • ensure: present or absent
  • inet_protocol: ipv4, ipv6 or eb, defaults to ipv4
  • args: Name of the passthroughhrough to add (e.g:
    -A OUTPUT -j OUTPUT_filter)

Testing

Unit Testing

Unit tests can be executed by running the following commands:

  • bundle install
  • bundle exec rake spec

Acceptance Testing

Acceptance tests are performed using
Beaker and require
Vagrant and
VirtualBox to run successfully.

It is HIGHLY RECOMMENDED that you use the upstream Vagrant package
and not one from your OS provider.

To run the acceptance tests:

  • bundle install
  • bundle exec rake beaker

To leave the Vagrant hosts running on failure for debugging:

  • BEAKER_destroy=onpass bundle exec rake beaker
  • cd .vagrant/beaker_vagrant_files/default.yml
  • vagrant ssh <host>

Author