Skip to content

Commit

Permalink
seport: minor refactor (#4471) (#4485)
Browse files Browse the repository at this point in the history
* seport: minor refactor

* added changelog fragment

* Update plugins/modules/system/seport.py

Co-authored-by: Felix Fontein <felix@fontein.de>

* Update plugins/modules/system/seport.py

Co-authored-by: Felix Fontein <felix@fontein.de>
(cherry picked from commit 7e6a245)

Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>
  • Loading branch information
patchback[bot] and russoz authored Apr 10, 2022
1 parent 2c106d6 commit 34682ad
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/4471-seport-refactor.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- seport - minor refactoring (https://github.com/ansible-collections/community.general/pull/4471).
29 changes: 15 additions & 14 deletions plugins/modules/system/seport.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@


def get_runtime_status(ignore_selinux_state=False):
return True if ignore_selinux_state is True else selinux.is_selinux_enabled()
return ignore_selinux_state or selinux.is_selinux_enabled()


def semanage_port_get_ports(seport, setype, proto):
Expand Down Expand Up @@ -161,10 +161,7 @@ def semanage_port_get_type(seport, port, proto):
key = (int(ports[0]), int(ports[1]), proto)

records = seport.get_all()
if key in records:
return records[key]
else:
return None
return records.get(key)


def semanage_port_add(module, ports, proto, setype, do_reload, serange='s0', sestore=''):
Expand Down Expand Up @@ -194,19 +191,23 @@ def semanage_port_add(module, ports, proto, setype, do_reload, serange='s0', ses
:rtype: bool
:return: True if the policy was changed, otherwise False
"""
change = False
try:
seport = seobject.portRecords(sestore)
seport.set_reload(do_reload)
change = False
ports_by_type = semanage_port_get_ports(seport, setype, proto)
for port in ports:
if port not in ports_by_type:
change = True
port_type = semanage_port_get_type(seport, port, proto)
if port_type is None and not module.check_mode:
seport.add(port, proto, serange, setype)
elif port_type is not None and not module.check_mode:
seport.modify(port, proto, serange, setype)
if port in ports_by_type:
continue

change = True
if module.check_mode:
continue
port_type = semanage_port_get_type(seport, port, proto)
if port_type is None:
seport.add(port, proto, serange, setype)
else:
seport.modify(port, proto, serange, setype)

except (ValueError, IOError, KeyError, OSError, RuntimeError) as e:
module.fail_json(msg="%s: %s\n" % (e.__class__.__name__, to_native(e)), exception=traceback.format_exc())
Expand Down Expand Up @@ -238,10 +239,10 @@ def semanage_port_del(module, ports, proto, setype, do_reload, sestore=''):
:rtype: bool
:return: True if the policy was changed, otherwise False
"""
change = False
try:
seport = seobject.portRecords(sestore)
seport.set_reload(do_reload)
change = False
ports_by_type = semanage_port_get_ports(seport, setype, proto)
for port in ports:
if port in ports_by_type:
Expand Down

0 comments on commit 34682ad

Please sign in to comment.