1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
|
#!/usr/bin/python
# -*- coding: utf-8 -*-
# GNU General Public License v3.0+
from __future__ import absolute_import, division, print_function
__metaclass__ = type
DOCUMENTATION = r"""
---
module: vyos_firewall_interfaces
short_description: Manage firewall hook filters on VyOS devices using REST API
description:
- Manages firewall hook filter configuration on VyOS devices via the REST API.
- In VyOS 1.5+, firewall hook filters (input/output/forward) replace the
per-interface firewall assignments used in VyOS 1.4.
- Hook filters apply globally to all traffic traversing that hook point.
- Uses REST API (C(connection=httpapi)) instead of CLI.
version_added: "1.0.0"
author:
- VyOS Community (@vyos)
options:
config:
description: Firewall hook filter configuration.
type: list
elements: dict
suboptions:
afi:
description: Address family.
type: str
choices: [ipv4, ipv6]
required: true
hooks:
description: Hook filter configurations for this address family.
type: list
elements: dict
suboptions:
hook:
description: Netfilter hook point.
type: str
choices: [input, output, forward]
required: true
default_action:
description: Default action when no rule matches.
type: str
choices: [accept, drop, reject]
description:
description: Filter description.
type: str
rules:
description: Rules in this hook filter.
type: list
elements: dict
suboptions:
number:
description: Rule number.
type: int
required: true
action:
description: Rule action.
type: str
choices: [accept, drop, reject, return, queue, continue]
description:
description: Rule description.
type: str
disable:
description: Disable this rule.
type: bool
protocol:
description: Protocol to match.
type: str
state:
description: Connection state to match.
type: str
choices: [established, invalid, new, related]
log:
description: Enable logging.
type: bool
source:
description: Source match criteria.
type: dict
suboptions:
address:
description: Source IP address or prefix.
type: str
port:
description: Source port or range.
type: str
destination:
description: Destination match criteria.
type: dict
suboptions:
address:
description: Destination IP address or prefix.
type: str
port:
description: Destination port or range.
type: str
state:
description:
- Desired state of the firewall hook filter configuration.
- C(merged) adds or updates without removing existing config.
- C(replaced) replaces hook filter config for named hooks in config.
- C(overridden) replaces all firewall hook filter config.
- C(deleted) removes firewall hook filter config.
- C(gathered) returns current configuration as structured data.
type: str
choices: [merged, replaced, overridden, deleted, gathered]
default: merged
notes:
- Requires C(ansible_connection=httpapi) with the VyOS httpapi plugin.
- C(ansible_network_os) must be set to C(vyos.rest.vyos).
- In VyOS 1.5+, hook filters apply globally rather than per-interface.
Use named rule sets (M(vyos.rest.vyos_firewall_rules)) for more granular
per-traffic control.
"""
EXAMPLES = r"""
- name: Merge firewall hook filter configuration
vyos.rest.vyos_firewall_interfaces:
config:
- afi: ipv4
hooks:
- hook: input
default_action: accept
rules:
- number: 10
action: accept
state: established
- number: 20
action: drop
state: invalid
- hook: forward
default_action: accept
- hook: output
default_action: accept
- afi: ipv6
hooks:
- hook: input
default_action: accept
state: merged
- name: Delete all firewall hook filter configuration
vyos.rest.vyos_firewall_interfaces:
state: deleted
- name: Gather firewall hook filter configuration
vyos.rest.vyos_firewall_interfaces:
state: gathered
"""
RETURN = r"""
before:
description: Firewall hook filter configuration before this module ran.
returned: always
type: list
after:
description: Firewall hook filter configuration after this module ran.
returned: when changed
type: list
commands:
description: List of API command tuples sent to the device.
returned: always
type: list
gathered:
description: Current firewall hook filter configuration as structured data.
returned: when state is gathered
type: list
saved:
description: Whether the config was saved after changes.
returned: when changed
type: bool
response:
description: Raw API response.
returned: always
type: dict
"""
from ansible.module_utils.basic import AnsibleModule
from ansible_collections.vyos.rest.plugins.module_utils.vyos import (
VyOSModule,
autoclean,
dict_op,
from_device,
normalize_have,
)
_BASE = ["firewall"]
# The only hook filter keys this module owns under firewall.<afi>. Sibling
# top-level keys under the same afi (e.g. firewall.<afi>.name, owned by
# vyos_firewall_rules) are never enumerated or touched -- this module
# only ever builds paths as _BASE + [afi, hook, "filter", ...] for hook
# drawn from this fixed set, never a blanket op at _BASE + [afi] itself.
_HOOKS = ("input", "output", "forward")
_AFIS = ("ipv4", "ipv6")
# "rule" is a genuine tag node (keyed by rule number) that VyOS's REST API
# can collapse to a bare value for a single rule with no other config.
_TAG_KEYS = {"rule"}
# ---------------------------------------------------------------------------
# want -> device / device -> argspec
#
# Every leaf here is a direct structural match between argspec and device
# shape (protocol, description, disable, state, log, source/destination
# both flowing through autoclean/from_device generically). The only
# unavoidable structural work: the "rule" tag-node reshape (keyed by
# number) and inserting the literal "filter" wrapper key that VyOS
# requires one level under each hook but the argspec omits (hook_entry's
# fields live directly on it, not nested under a "filter" key).
# ---------------------------------------------------------------------------
def _rules_to_device(rules):
return {
str(r["number"]): autoclean({k: v for k, v in r.items() if k != "number"})
for r in rules or []
}
def _rules_from_device(raw):
result = []
for num, data in sorted((raw or {}).items(), key=lambda kv: int(kv[0])):
entry = {"number": int(num), **from_device(data or {})}
result.append(entry)
return result
def _hook_filter_to_device(hook_entry):
entry = autoclean({k: v for k, v in hook_entry.items() if k not in ("hook", "rules")})
if hook_entry.get("rules"):
entry["rule"] = _rules_to_device(hook_entry["rules"])
return entry
def _hook_filter_from_device(hook, filter_data):
filter_data = dict(filter_data or {})
rules_raw = filter_data.pop("rule", None) or {}
entry = {"hook": hook, **from_device(filter_data)}
if rules_raw:
entry["rules"] = _rules_from_device(rules_raw)
return entry
def _want_to_device(config):
result = {}
for entry in config or []:
afi = entry["afi"]
hooks = entry.get("hooks") or []
if not hooks:
continue
result[afi] = {h["hook"]: {"filter": _hook_filter_to_device(h)} for h in hooks}
return result
def get_running_config(vyos):
return vyos.get_config(_BASE) or {}
def _device_to_argspec(raw):
raw = raw or {}
result = []
for afi in _AFIS:
afi_raw = raw.get(afi) or {}
hooks = []
for hook in _HOOKS:
filter_data = (afi_raw.get(hook) or {}).get("filter")
if filter_data:
hooks.append(_hook_filter_from_device(hook, filter_data))
if hooks:
result.append({"afi": afi, "hooks": hooks})
return result
# ---------------------------------------------------------------------------
# Command building — dict_op scoped to _BASE + [afi, hook, "filter"] only,
# per hook, never a blanket op at _BASE + [afi] or _BASE itself (which
# would risk vyos_firewall_rules's firewall.<afi>.name subtree, even
# though today the keys happen to differ -- staying scoped to the exact
# owned path is the same discipline established for the BGP modules).
# ---------------------------------------------------------------------------
def build_commands(config, raw_have, state):
raw_have = raw_have or {}
config = config or []
norm_have = normalize_have(raw_have, _TAG_KEYS)
if state == "deleted":
commands = []
# No config given -> delete every hook filter currently present.
# Config given -> delete only the (afi, hook) pairs it names.
targets = (
[(afi, hook) for afi in _AFIS for hook in _HOOKS]
if not config
else [(e["afi"], h["hook"]) for e in config for h in (e.get("hooks") or [])]
)
for afi, hook in targets:
if ((raw_have.get(afi) or {}).get(hook) or {}).get("filter"):
commands.append(("delete", _BASE + [afi, hook, "filter"]))
return commands
want = _want_to_device(config)
commands = []
if state == "overridden":
want_pairs = {(afi, hook) for afi, hooks in want.items() for hook in hooks}
for afi in _AFIS:
for hook in _HOOKS:
if (afi, hook) not in want_pairs and (
(raw_have.get(afi) or {}).get(hook) or {}
).get(
"filter",
):
commands.append(("delete", _BASE + [afi, hook, "filter"]))
for afi, hooks in want.items():
for hook, want_hook in hooks.items():
hbase = _BASE + [afi, hook, "filter"]
have_filter = ((norm_have.get(afi) or {}).get(hook) or {}).get("filter") or {}
want_filter = want_hook.get("filter", {})
if state in ("replaced", "overridden"):
commands += dict_op(want_filter, have_filter, hbase, op="purge")
commands += dict_op(want_filter, have_filter, hbase, op="set")
return commands
ARGUMENT_SPEC = dict(
config=dict(
type="list",
elements="dict",
options=dict(
afi=dict(type="str", choices=["ipv4", "ipv6"], required=True),
hooks=dict(
type="list",
elements="dict",
options=dict(
hook=dict(
type="str",
choices=["input", "output", "forward"],
required=True,
),
default_action=dict(
type="str",
choices=["accept", "drop", "reject"],
),
description=dict(type="str"),
rules=dict(
type="list",
elements="dict",
options=dict(
number=dict(type="int", required=True),
action=dict(
type="str",
choices=[
"accept",
"drop",
"reject",
"return",
"queue",
"continue",
],
),
description=dict(type="str"),
disable=dict(type="bool"),
protocol=dict(type="str"),
state=dict(
type="str",
choices=[
"established",
"invalid",
"new",
"related",
],
),
log=dict(type="bool"),
source=dict(
type="dict",
options=dict(
address=dict(type="str"),
port=dict(type="str"),
),
),
destination=dict(
type="dict",
options=dict(
address=dict(type="str"),
port=dict(type="str"),
),
),
),
),
),
),
),
),
state=dict(
default="merged",
choices=["merged", "replaced", "overridden", "deleted", "gathered"],
),
)
def main():
module = AnsibleModule(ARGUMENT_SPEC, supports_check_mode=True)
vyos = VyOSModule(module)
state = module.params["state"]
config = module.params.get("config") or []
raw_have = get_running_config(vyos)
have = _device_to_argspec(raw_have)
if state == "gathered":
module.exit_json(changed=False, gathered=have)
commands = build_commands(config, raw_have, state)
if module.check_mode:
module.exit_json(changed=bool(commands), commands=commands, before=have)
if commands:
response = vyos.apply_commands(commands)
saved = vyos.save_config()
module.exit_json(
changed=True,
before=have,
after=_device_to_argspec(get_running_config(vyos)),
commands=commands,
saved=saved,
response=response,
)
module.exit_json(changed=False, before=have, after=have, commands=[])
if __name__ == "__main__":
main()
|