Browse Source

feat(state): allow deny rules to not be forced to the top

pull/27/head
Paul 3 years ago
parent
commit
6a9d4ccabb
6 changed files with 53 additions and 16 deletions
  1. +5
    -2
      _states/ufw.py
  2. +19
    -0
      docs/README.rst
  3. +3
    -1
      pillar.example
  4. +5
    -0
      test/salt/pillar/default.sls
  5. +9
    -5
      ufw/config/applications.sls
  6. +12
    -8
      ufw/config/services.sls

+ 5
- 2
_states/ufw.py View File





def deny(name, app=None, interface=None, protocol=None, def deny(name, app=None, interface=None, protocol=None,
from_addr=None, from_port=None, to_addr=None, to_port=None, comment=None):
from_addr=None, from_port=None, to_addr=None, to_port=None, comment=None, force_first=True):


return _add_rule('insert 1 deny', name, app, interface, protocol, from_addr, from_port, to_addr, to_port, comment)
if force_first is False:
return _add_rule('deny', name, app, interface, protocol, from_addr, from_port, to_addr, to_port, comment)
else:
return _add_rule('insert 1 deny', name, app, interface, protocol, from_addr, from_port, to_addr, to_port, comment)




def limit(name, app=None, interface=None, protocol=None, def limit(name, app=None, interface=None, protocol=None,

+ 19
- 0
docs/README.rst View File

applications: applications:
- OpenSSH - OpenSSH


Allow generic traffic on ens7, and allow 1 ip to access port 22 and explicitly block all others:

.. code-block::

ufw:
interfaces:
ens7:
services:
22:
protocol: tcp
to_port: 22
from_addr:
- 192.168.1.1
22/deny:
protocol: tcp
to_port: 22
deny: true
force_first: false

Testing Testing
------- -------



+ 3
- 1
pillar.example View File

protocol: tcp protocol: tcp
comment: Netbios comment: Netbios


# Deny from a specific port, by number.
# Deny from a specific port, by number, but don't force
# the rule as the first rule in the ufw state
140: 140:
protocol: tcp protocol: tcp
deny: true deny: true
force_first: false


# Deny everything from a specific ip address # Deny everything from a specific ip address
'*': '*':

+ 5
- 0
test/salt/pillar/default.sls View File

'443': '443':
protocol: tcp protocol: tcp
comment: Allow HTTPS comment: Allow HTTPS
'1000':
protocol: tcp
deny: true
force_first: false
comment: Deny 1000 not as first

+ 9
- 5
ufw/config/applications.sls View File

{%- set from_addrs = [from_addr_raw] if from_addr_raw is string else from_addr_raw %} {%- set from_addrs = [from_addr_raw] if from_addr_raw is string else from_addr_raw %}


{%- for from_addr in from_addrs %} {%- for from_addr in from_addrs %}
{%- set deny = app_details.get('deny', None) %}
{%- set limit = app_details.get('limit', None) %}
{%- set method = 'deny' if deny else ('limit' if limit else 'allow') %}
{%- set to_addr = app_details.get('to_addr', None) %}
{%- set comment = app_details.get('comment', None) %}
{%- set deny = app_details.get('deny', None) %}
{%- set force_first = app_details.get('force_first', None) %}
{%- set limit = app_details.get('limit', None) %}
{%- set method = 'deny' if deny else ('limit' if limit else 'allow') %}
{%- set to_addr = app_details.get('to_addr', None) %}
{%- set comment = app_details.get('comment', None) %}


{%- if from_addr is not none %} {%- if from_addr is not none %}
ufw-app-{{ method }}-{{ app_name }}-{{ from_addr }}: ufw-app-{{ method }}-{{ app_name }}-{{ from_addr }}:
{%- endif %} {%- endif %}
ufw.{{ method }}: ufw.{{ method }}:
- app: '"{{ app_name }}"' - app: '"{{ app_name }}"'
{%- if force_first is not none %}
- force_first: {{ force_first }}
{%- endif %}
{%- if from_addr is not none %} {%- if from_addr is not none %}
- from_addr: {{ from_addr }} - from_addr: {{ from_addr }}
{%- endif %} {%- endif %}

+ 12
- 8
ufw/config/services.sls View File

{%- set from_addrs = [from_addr_raw] if from_addr_raw is string else from_addr_raw %} {%- set from_addrs = [from_addr_raw] if from_addr_raw is string else from_addr_raw %}


{%- for from_addr in from_addrs %} {%- for from_addr in from_addrs %}
{%- set protocol = service_details.get('protocol', None) %}
{%- set deny = service_details.get('deny', None) %}
{%- set limit = service_details.get('limit', None) %}
{%- set method = 'deny' if deny else ('limit' if limit else 'allow') %}
{%- set from_port = service_details.get('from_port', None) %}
{%- set to_addr = service_details.get('to_addr', None) %}
{%- set to_port = service_details.get('to_port', service_name) %}
{%- set comment = service_details.get('comment', None) %}
{%- set protocol = service_details.get('protocol', None) %}
{%- set deny = service_details.get('deny', None) %}
{%- set force_first = service_details.get('force_first', None) %}
{%- set limit = service_details.get('limit', None) %}
{%- set method = 'deny' if deny else ('limit' if limit else 'allow') %}
{%- set from_port = service_details.get('from_port', None) %}
{%- set to_addr = service_details.get('to_addr', None) %}
{%- set to_port = service_details.get('to_port', service_name) %}
{%- set comment = service_details.get('comment', None) %}


ufw-svc-{{ method }}-{{ service_name }}-{{ from_addr }}: ufw-svc-{{ method }}-{{ service_name }}-{{ from_addr }}:
ufw.{{ method }}: ufw.{{ method }}:
{%- if protocol is not none %} {%- if protocol is not none %}
- protocol: {{ protocol }} - protocol: {{ protocol }}
{%- endif %} {%- endif %}
{%- if force_first is not none %}
- force_first: {{ force_first }}
{%- endif %}
{%- if from_addr is not none %} {%- if from_addr is not none %}
- from_addr: {{ from_addr }} - from_addr: {{ from_addr }}
{%- endif %} {%- endif %}

Loading…
Cancel
Save