Browse Source

Handle test mode when adding rules

tags/v0.2.0
Alexandre Anriot 6 years ago
parent
commit
fcc8a36a4c
2 changed files with 13 additions and 5 deletions
  1. +5
    -1
      _modules/ufw.py
  2. +8
    -4
      _states/ufw.py

+ 5
- 1
_modules/ufw.py View File

__salt__['cmd.run'](cmd) __salt__['cmd.run'](cmd)


def add_rule(rule): def add_rule(rule):
cmd = "ufw " + rule
if __opts__['test']:
cmd = "ufw --dry-run " + rule
else:
cmd = "ufw " + rule
out = __salt__['cmd.run'](cmd, python_shell=True) out = __salt__['cmd.run'](cmd, python_shell=True)
return out return out


+ 8
- 4
_states/ufw.py View File

rule = _as_rule("allow", app=app, interface=interface, protocol=protocol, rule = _as_rule("allow", app=app, interface=interface, protocol=protocol,
from_addr=from_addr, from_port=from_port, to_addr=to_addr, to_port=to_port, comment=comment) from_addr=from_addr, from_port=from_port, to_addr=to_addr, to_port=to_port, comment=comment)


if __opts__['test']:
return _test(name, "{0}: {1}".format(name, rule))

try: try:
out = __salt__['ufw.add_rule'](rule) out = __salt__['ufw.add_rule'](rule)
except (CommandExecutionError, CommandNotFoundError) as e: except (CommandExecutionError, CommandNotFoundError) as e:
changes = False changes = False
for line in out.split('\n'): for line in out.split('\n'):
if line.startswith("Skipping"): if line.startswith("Skipping"):
continue
if __opts__['test']:
return _unchanged(name, "{0} was already allowed".format(name))
break
else:
continue
if line.startswith("Rule added") or line.startswith("Rules updated"): if line.startswith("Rule added") or line.startswith("Rules updated"):
changes = True changes = True
break break
if __opts__['test']:
return _test(name, "{0} would have been allowed".format(name))
break
return _error(name, line) return _error(name, line)


if changes: if changes:

Loading…
Cancel
Save