Skip to content

Commit 547b7fd

Browse files
martinpittrichm
authored andcommitted
fix: Fix "helpers" service option
Cause: The query check was inverted, and query/add used the wrong API (module, not helper). Consequence: Trying to set the "helpers:" option of a created service had no effect. Fix: Use the correct API and fix the check. Add an integration test which actually validates the created service in firewalld. The service helper API does not yet exist on RHEL 7, so skip the corresponding tests on RHEL < 8. Fixes #276
1 parent 24620c0 commit 547b7fd

File tree

3 files changed

+49
-9
lines changed

3 files changed

+49
-9
lines changed

library/firewall_lib.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -488,9 +488,9 @@ def set_service(
488488
fw_service_settings.addSourcePort(_port, _protocol)
489489
self.changed = True
490490
for _module in helper_module:
491-
if fw_service_settings.queryModule(_module):
491+
if not fw_service_settings.queryHelper(_module):
492492
if not self.module.check_mode:
493-
fw_service_settings.addModule(_module)
493+
fw_service_settings.addHelper(_module)
494494
self.changed = True
495495
if destination_ipv4:
496496
if not fw_service_settings.queryDestination(

tests/tests_ansible.yml

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -587,25 +587,49 @@
587587

588588
- name: Add custom service with all the elements at once
589589
firewall_lib:
590+
permanent: true
591+
state: present
590592
service: customservice
591593
short: Custom service
592594
port: 333/tcp
593595
source_port: 333/tcp
594596
protocol: ipv6
595-
helper_module: nf_conntrack_ftp
596597
description: Custom Service that does not do a single thing
597598
destination:
598599
- 123.45.6.78
599600
- "aaaa:aaaa:aaaa:aaa:aaaa:aaaa:aaaa::"
601+
# these two don't exist yet in RHEL 7
602+
helper_module: "{{ 'nf_conntrack_ftp'
603+
if ansible_distribution in ['RedHat', 'CentOS', 'Fedora']
604+
and ansible_distribution_major_version is version('8', '>=')
605+
else omit }}"
600606
includes: "{{ ['https', 'ldaps']
601607
if ansible_distribution in ['RedHat', 'CentOS', 'Fedora']
602608
and ansible_distribution_major_version is version('8', '>=')
603609
else omit }}"
604-
permanent: true
605-
state: present
606610
register: result
607611
failed_when: result is failed or result is not changed
608612

613+
- name: Verify service settings
614+
command: firewall-offline-cmd --info-service customservice
615+
register: info
616+
changed_when: false
617+
failed_when: >
618+
info.failed
619+
or "ports: 333/tcp" not in info.stdout
620+
or "source-ports: 333/tcp" not in info.stdout
621+
or "protocols: ipv6" not in info.stdout
622+
or "destination: ipv4:123.45.6.78 ipv6:aaaa:aaaa:aaaa:aaa:aaaa:aaaa:aaaa::"
623+
not in info.stdout
624+
625+
- name: Verify includes and helper service settings
626+
assert:
627+
that:
628+
- '"includes: https ldaps" in info.stdout'
629+
- '"helpers: nf_conntrack_ftp" in info.stdout'
630+
when: ansible_distribution in ['RedHat', 'CentOS', 'Fedora']
631+
and ansible_distribution_major_version is version('8', '>=')
632+
609633
- name: Add helper module that already is on customservice
610634
firewall_lib:
611635
service: customservice
@@ -614,6 +638,8 @@
614638
state: present
615639
register: result
616640
failed_when: result is failed or result is changed
641+
when: ansible_distribution in ['RedHat', 'CentOS', 'Fedora']
642+
and ansible_distribution_major_version is version('8', '>=')
617643

618644
- name: Forward port 40 to 0.0.0.0:8080 (string)
619645
firewall_lib:

tests/tests_service.yml

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,12 @@
124124
destination:
125125
- 1.1.1.1
126126
- 1::1
127-
helper_module: ftp
128127
protocol: icmp
128+
# these two don't exist yet in RHEL 7
129+
helper_module: "{{ 'ftp'
130+
if ansible_distribution in ['RedHat', 'CentOS', 'Fedora']
131+
and ansible_distribution_major_version is version('8', '>=')
132+
else omit }}"
129133
includes: "{{ ['ssh', 'ldaps']
130134
if ansible_distribution in ['RedHat', 'CentOS', 'Fedora']
131135
and ansible_distribution_major_version is version('8', '>=')
@@ -168,8 +172,12 @@
168172
destination:
169173
- 1.1.1.1
170174
- 1::1
171-
helper_module: ftp
172175
protocol: icmp
176+
# these two don't exist yet in RHEL 7
177+
helper_module: "{{ 'ftp'
178+
if ansible_distribution in ['RedHat', 'CentOS', 'Fedora']
179+
and ansible_distribution_major_version is version('8', '>=')
180+
else omit }}"
173181
includes: "{{ ['ssh', 'ldaps']
174182
if ansible_distribution in ['RedHat', 'CentOS', 'Fedora']
175183
and ansible_distribution_major_version is version('8', '>=')
@@ -247,8 +255,11 @@
247255
destination:
248256
- 1.1.1.1
249257
- 1::1
250-
helper_module: ftp
251258
protocol: icmp
259+
helper_module: "{{ 'ftp'
260+
if ansible_distribution in ['RedHat', 'CentOS', 'Fedora']
261+
and ansible_distribution_major_version is version('8', '>=')
262+
else omit }}"
252263
includes: "{{ ['ssh', 'ldaps']
253264
if ansible_distribution in ['RedHat', 'CentOS', 'Fedora']
254265
and ansible_distribution_major_version is version('8', '>=')
@@ -274,8 +285,11 @@
274285
destination:
275286
- 1.1.1.1
276287
- 1::1
277-
helper_module: ftp
278288
protocol: icmp
289+
helper_module: "{{ 'ftp'
290+
if ansible_distribution in ['RedHat', 'CentOS', 'Fedora']
291+
and ansible_distribution_major_version is version('8', '>=')
292+
else omit }}"
279293
includes: "{{ ['ssh', 'ldaps']
280294
if ansible_distribution in ['RedHat', 'CentOS', 'Fedora']
281295
and ansible_distribution_major_version is version('8', '>=')

0 commit comments

Comments
 (0)