Skip to content

Commit 82b12ae

Browse files
fix: state no longer required for masquerade and ICMP block inversion
- The above, and added a new error message for attempting to not specify state when using options that require state like source, port, port_forward -- Unit test case added for this error message - Removed state option from integration tests for masquerading and ICMP block, retaining the same fail conditions Fixes: #76
1 parent bf66a43 commit 82b12ae

File tree

6 files changed

+29
-12
lines changed

6 files changed

+29
-12
lines changed

library/firewall_lib.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@
154154
description:
155155
Ensure presence or absence of entries. Use C(present) and C(absent) only
156156
for zone-only operations, or for target operations.
157-
required: true
157+
required: false
158158
type: str
159159
choices: ["enabled", "disabled", "present", "absent"]
160160
__report_changed:
@@ -315,7 +315,9 @@ def main():
315315
],
316316
),
317317
state=dict(
318-
choices=["enabled", "disabled", "present", "absent"], required=True
318+
choices=["enabled", "disabled", "present", "absent"],
319+
required=False,
320+
default=None,
319321
),
320322
__report_changed=dict(required=False, type="bool", default=True),
321323
),
@@ -359,6 +361,19 @@ def main():
359361
runtime = module.params["runtime"]
360362
state = module.params["state"]
361363

364+
# All options that require state to be set
365+
state_required = any(
366+
(
367+
interface,
368+
source,
369+
service,
370+
source_port,
371+
port,
372+
forward_port,
373+
icmp_block,
374+
rich_rule,
375+
)
376+
)
362377
if permanent is None:
363378
runtime = True
364379
elif not any((permanent, runtime)):
@@ -460,6 +475,9 @@ def main():
460475
if len(source) > 0 and permanent is None:
461476
module.fail_json(msg="source cannot be set without permanent")
462477

478+
if state is None and state_required:
479+
module.fail_json(msg="Options invalid without state option set")
480+
463481
if not HAS_FIREWALLD:
464482
module.fail_json(msg="No firewalld")
465483

tasks/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
target: "{{ item.target | default(omit) }}"
5656
permanent: "{{ item.permanent | default(True) }}"
5757
runtime: "{{ item.runtime | default(True) }}"
58-
state: "{{ item.state }}"
58+
state: "{{ item.state | default(omit) }}"
5959
__report_changed: "{{ not __firewall_previous_replaced }}"
6060
loop: "{{ firewall is mapping | ternary([firewall], firewall) |
6161
map('dict2items') | map('difference', __previous) | select |

tests/tests_ansible.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,6 @@
200200
- name: Allow masquerading in permament dmz zone
201201
firewall_lib:
202202
masquerade: yes
203-
state: enabled
204203
permanent: yes
205204
zone: dmz
206205
register: result
@@ -209,7 +208,6 @@
209208
- name: Allow masquerading in permament dmz zone, again
210209
firewall_lib:
211210
masquerade: yes
212-
state: enabled
213211
permanent: yes
214212
zone: dmz
215213
register: result
@@ -226,7 +224,6 @@
226224
- name: Ensure ICMP block inversion in permanent drop zone
227225
firewall_lib:
228226
zone: drop
229-
state: enabled
230227
permanent: yes
231228
icmp_block_inversion: yes
232229
register: result
@@ -235,7 +232,6 @@
235232
- name: Ensure ICMP block inversion in permanent drop zone, again
236233
firewall_lib:
237234
zone: drop
238-
state: enabled
239235
permanent: yes
240236
icmp_block_inversion: yes
241237
register: result

tests/tests_purge_config.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
'448/tcp;;1.2.3.5']
1919
state: enabled
2020
- masquerade: yes
21-
state: enabled
2221
- service: http
2322
state: enabled
2423
tasks:
@@ -113,7 +112,6 @@
113112
vars:
114113
firewall:
115114
- set_default_zone: dmz
116-
state: enabled
117115

118116
- name: Verify role reports changed
119117
fail:
@@ -126,7 +124,6 @@
126124
vars:
127125
firewall:
128126
- set_default_zone: dmz
129-
state: enabled
130127

131128
- name: Verify role reports not changed
132129
fail:
@@ -140,7 +137,6 @@
140137
firewall:
141138
- previous: replaced
142139
- set_default_zone: dmz
143-
state: enabled
144140

145141
- name: Verify role reports not changed
146142
fail:

tests/tests_target.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
vars:
99
firewall:
1010
- set_default_zone: public
11-
state: enabled
1211
permanent: yes
1312
- target: DROP
1413
state: enabled

tests/unit/test_firewall_lib.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,14 @@ def test_main_error_no_params(self, am_class):
383383
"icmp_block_inversion, target, zone or set_default_zone needs to be set"
384384
)
385385

386+
@patch("firewall_lib.HAS_FIREWALLD", True)
387+
def test_main_error_state_required_for_options(self, am_class):
388+
am = am_class.return_value
389+
am.params = {"permanent": True, "source": ["192.0.2.0/24"]}
390+
with self.assertRaises(MockException):
391+
firewall_lib.main()
392+
am.fail_json.assert_called_with(msg="Options invalid without state option set")
393+
386394
@patch("firewall_lib.HAS_FIREWALLD", True)
387395
def test_main_error_timeout_icmp_block_inversion(self, am_class):
388396
am = am_class.return_value

0 commit comments

Comments
 (0)