1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

seport: minor refactor (#4471) (#4485)

* 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 7e6a2453d0)

Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>
This commit is contained in:
patchback[bot] 2022-04-10 18:48:44 +02:00 committed by GitHub
parent 2c106d66a4
commit 34682addb8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 14 deletions

View file

@ -0,0 +1,2 @@
minor_changes:
- seport - minor refactoring (https://github.com/ansible-collections/community.general/pull/4471).

View file

@ -113,7 +113,7 @@ from ansible.module_utils.common.text.converters import to_native
def get_runtime_status(ignore_selinux_state=False): 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): def semanage_port_get_ports(seport, setype, proto):
@ -161,10 +161,7 @@ def semanage_port_get_type(seport, port, proto):
key = (int(ports[0]), int(ports[1]), proto) key = (int(ports[0]), int(ports[1]), proto)
records = seport.get_all() records = seport.get_all()
if key in records: return records.get(key)
return records[key]
else:
return None
def semanage_port_add(module, ports, proto, setype, do_reload, serange='s0', sestore=''): def semanage_port_add(module, ports, proto, setype, do_reload, serange='s0', sestore=''):
@ -194,19 +191,23 @@ def semanage_port_add(module, ports, proto, setype, do_reload, serange='s0', ses
:rtype: bool :rtype: bool
:return: True if the policy was changed, otherwise False :return: True if the policy was changed, otherwise False
""" """
change = False
try: try:
seport = seobject.portRecords(sestore) seport = seobject.portRecords(sestore)
seport.set_reload(do_reload) seport.set_reload(do_reload)
change = False
ports_by_type = semanage_port_get_ports(seport, setype, proto) ports_by_type = semanage_port_get_ports(seport, setype, proto)
for port in ports: for port in ports:
if port not in ports_by_type: if port in ports_by_type:
change = True continue
port_type = semanage_port_get_type(seport, port, proto)
if port_type is None and not module.check_mode: change = True
seport.add(port, proto, serange, setype) if module.check_mode:
elif port_type is not None and not module.check_mode: continue
seport.modify(port, proto, serange, setype) 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: 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()) module.fail_json(msg="%s: %s\n" % (e.__class__.__name__, to_native(e)), exception=traceback.format_exc())
@ -238,10 +239,10 @@ def semanage_port_del(module, ports, proto, setype, do_reload, sestore=''):
:rtype: bool :rtype: bool
:return: True if the policy was changed, otherwise False :return: True if the policy was changed, otherwise False
""" """
change = False
try: try:
seport = seobject.portRecords(sestore) seport = seobject.portRecords(sestore)
seport.set_reload(do_reload) seport.set_reload(do_reload)
change = False
ports_by_type = semanage_port_get_ports(seport, setype, proto) ports_by_type = semanage_port_get_ports(seport, setype, proto)
for port in ports: for port in ports:
if port in ports_by_type: if port in ports_by_type: