summaryrefslogtreecommitdiff
path: root/plugins/modules/vyos_firewall_rules.py
blob: daf391500d9caf38a59596eb8c8861c1eed90818 (plain)
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
#!/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_rules
short_description: Manage firewall rule sets on VyOS devices using REST API
description:
  - Manages named firewall rule sets on VyOS devices via the REST API.
  - Supports both IPv4 (C(ipv4)) and IPv6 (C(ipv6)) rule sets.
  - Uses REST API (C(connection=httpapi)) instead of CLI.
  - In VyOS 1.5+, firewall uses named rule sets under C(firewall.ipv4.name)
    and C(firewall.ipv6.name).
version_added: "1.0.0"
author:
  - VyOS Community (@vyos)
options:
  config:
    description: Firewall rule set configuration.
    type: list
    elements: dict
    suboptions:
      afi:
        description: Address family.
        type: str
        choices: [ipv4, ipv6]
        required: true
      rule_sets:
        description: Named rule sets for this address family.
        type: list
        elements: dict
        suboptions:
          name:
            description: Rule set name.
            type: str
            required: true
          default_action:
            description: Default action when no rule matches.
            type: str
            choices: [accept, drop, reject]
          description:
            description: Rule set description.
            type: str
          rules:
            description: Firewall rules in this rule set.
            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]
              source:
                description: Source match criteria.
                type: dict
                suboptions:
                  address:
                    description: Source IP address or prefix.
                    type: str
                  group:
                    description: Source group name.
                    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
                  group:
                    description: Destination group name.
                    type: str
                  port:
                    description: Destination port or range.
                    type: str
              log:
                description: Enable logging for this rule.
                type: bool
              icmp:
                description: ICMP type/code to match.
                type: dict
                suboptions:
                  type:
                    description: ICMP type.
                    type: int
                  code:
                    description: ICMP code.
                    type: int
  state:
    description:
      - Desired state of the firewall rules configuration.
      - C(merged) adds or updates without removing existing config.
      - C(replaced) replaces rule sets for named rule sets in config.
      - C(overridden) replaces all firewall rule sets.
      - C(deleted) removes firewall rule sets.
      - 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).
  - Rule sets are identified by AFI and name. Deleting a rule set removes
    all its rules.
  - The C(group) suboption can only reference an address-group. VyOS also
    supports network-group/port-group/domain-group references, which this
    module can read back (via C(gathered)) if already configured by other
    means, but cannot create -- the argspec has no group-type discriminator.
"""

EXAMPLES = r"""
- name: Merge firewall rules
  vyos.rest.vyos_firewall_rules:
    config:
      - afi: ipv4
        rule_sets:
          - name: RULE-SET1
            default_action: drop
            rules:
              - number: 10
                action: accept
                protocol: tcp
                source:
                  address: 192.168.1.0/24
                destination:
                  port: "80"
              - number: 20
                action: drop
                state: invalid
      - afi: ipv6
        rule_sets:
          - name: RULE-SET6
            default_action: accept
            rules:
              - number: 10
                action: accept
    state: merged

- name: Delete all firewall rules
  vyos.rest.vyos_firewall_rules:
    state: deleted

- name: Gather firewall rules
  vyos.rest.vyos_firewall_rules:
    state: gathered
"""

RETURN = r"""
before:
  description: Firewall rules configuration before this module ran.
  returned: always
  type: list
after:
  description: Firewall rules 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 rules 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"]
_AFIS = ("ipv4", "ipv6")

# Tag nodes VyOS's REST API can collapse to a bare string/list for a
# single entry with no other config -- "name" (rule sets, keyed by name)
# and "rule" (rules, keyed by number).
_TAG_KEYS = {"name", "rule"}


# ---------------------------------------------------------------------------
# want -> device / device -> argspec
#
# Every leaf here matches the device shape directly (action, description,
# disable, protocol, state, log, icmp.type/code) except one: "group".
# VyOS wraps a group reference under a literal group-kind key
# (address-group/network-group/...), not a flat value -- see the module
# note above on why this module can only ever *write* address-group.
# Rule-set/rule tag-node reshaping (keyed by name/number) is the other
# unavoidable structural work.
# ---------------------------------------------------------------------------


def _endpoint_to_device(ep):
    entry = autoclean({k: v for k, v in ep.items() if k != "group"})
    if ep.get("group"):
        entry["group"] = {"address-group": ep["group"]}
    return entry


def _endpoint_from_device(data):
    data = dict(data or {})
    group = data.pop("group", None)
    entry = from_device(data)
    if isinstance(group, dict) and group:
        entry["group"] = list(group.values())[0]
    elif isinstance(group, str):
        entry["group"] = group
    return entry


def _rules_to_device(rules):
    result = {}
    for r in rules or []:
        entry = autoclean(
            {k: v for k, v in r.items() if k not in ("number", "source", "destination")},
        )
        for endpoint in ("source", "destination"):
            if r.get(endpoint):
                entry[endpoint] = _endpoint_to_device(r[endpoint])
        result[str(r["number"])] = entry
    return result


def _rules_from_device(raw):
    result = []
    for num, data in sorted((raw or {}).items(), key=lambda kv: int(kv[0])):
        data = dict(data or {})
        src = data.pop("source", None)
        dst = data.pop("destination", None)
        entry = {"number": int(num), **from_device(data)}
        if src:
            entry["source"] = _endpoint_from_device(src)
        if dst:
            entry["destination"] = _endpoint_from_device(dst)
        result.append(entry)
    return result


def _rule_set_to_device(rs):
    entry = autoclean({k: v for k, v in rs.items() if k not in ("name", "rules")})
    if rs.get("rules"):
        entry["rule"] = _rules_to_device(rs["rules"])
    return entry


def _rule_set_from_device(name, data):
    data = dict(data or {})
    rules_raw = data.pop("rule", None) or {}
    entry = {"name": name, **from_device(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"]
        rule_sets = entry.get("rule_sets") or []
        if not rule_sets:
            continue
        result[afi] = {rs["name"]: _rule_set_to_device(rs) for rs in rule_sets}
    return result


def get_running_config(vyos):
    """Fetch each AFI's rule-set subtree directly at firewall.<afi>.name --
    the most targeted path available, deliberately not a broader fetch at
    firewall.<afi> or firewall itself (which would pull in the hook-filter
    and group subtrees owned by sibling modules for no benefit here).
    """
    result = {}
    for afi in _AFIS:
        raw = vyos.get_config(_BASE + [afi, "name"])
        if raw and isinstance(raw, dict):
            # Some VyOS REST responses wrap the result in an extra "name"
            # key even when fetched at a path already ending in "name";
            # unwrap defensively either way.
            raw = raw.get("name", raw)
            if raw and isinstance(raw, dict):
                result[afi] = raw
    return result


def _device_to_argspec(raw):
    raw = raw or {}
    result = []
    for afi in _AFIS:
        afi_raw = raw.get(afi) or {}
        rule_sets = [_rule_set_from_device(name, data) for name, data in sorted(afi_raw.items())]
        if rule_sets:
            result.append({"afi": afi, "rule_sets": rule_sets})
    return result


# ---------------------------------------------------------------------------
# Command building — dict_op scoped to _BASE + [afi, "name", rs_name] per
# rule set, never a blanket op at _BASE + [afi] or _BASE itself (which
# would risk vyos_firewall_interfaces's hook-filter subtree and
# vyos_firewall_global's group subtree under the same "firewall" root).
# ---------------------------------------------------------------------------


def build_commands(config, raw_have, state):
    raw_have = raw_have or {}
    config = config or []
    norm_have = {afi: normalize_have(data, _TAG_KEYS) for afi, data in raw_have.items()}

    if state == "deleted":
        commands = []
        if not config:
            for afi, rule_sets in raw_have.items():
                for name in rule_sets:
                    commands.append(("delete", _BASE + [afi, "name", name]))
        else:
            for entry in config:
                afi = entry["afi"]
                for rs in entry.get("rule_sets") or []:
                    if rs["name"] in (raw_have.get(afi) or {}):
                        commands.append(("delete", _BASE + [afi, "name", rs["name"]]))
        return commands

    want = _want_to_device(config)
    commands = []

    if state == "overridden":
        want_keys = {(afi, name) for afi, rule_sets in want.items() for name in rule_sets}
        for afi, rule_sets in raw_have.items():
            for name in rule_sets:
                if (afi, name) not in want_keys:
                    commands.append(("delete", _BASE + [afi, "name", name]))

    for afi, rule_sets in want.items():
        for name, want_rs in rule_sets.items():
            rsbase = _BASE + [afi, "name", name]
            have_rs = (norm_have.get(afi) or {}).get(name) or {}

            if state in ("replaced", "overridden"):
                commands += dict_op(want_rs, have_rs, rsbase, op="purge")
            commands += dict_op(want_rs, have_rs, rsbase, op="set")

    return commands


ARGUMENT_SPEC = dict(
    config=dict(
        type="list",
        elements="dict",
        options=dict(
            afi=dict(type="str", choices=["ipv4", "ipv6"], required=True),
            rule_sets=dict(
                type="list",
                elements="dict",
                options=dict(
                    name=dict(type="str", 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"),
                                    group=dict(type="str"),
                                    port=dict(type="str"),
                                ),
                            ),
                            destination=dict(
                                type="dict",
                                options=dict(
                                    address=dict(type="str"),
                                    group=dict(type="str"),
                                    port=dict(type="str"),
                                ),
                            ),
                            icmp=dict(
                                type="dict",
                                options=dict(
                                    type=dict(type="int"),
                                    code=dict(type="int"),
                                ),
                            ),
                        ),
                    ),
                ),
            ),
        ),
    ),
    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()