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
|
#!/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: Firewall interfaces resource module via REST API.
description:
- Attaches or detaches named firewall rule sets to/from interface
directions (in/out/local) using the VyOS HTTPS REST API.
version_added: "1.0.0"
author:
- VyOS Community (@vyos)
options:
config:
description: List of interface firewall attachment configurations.
type: list
elements: dict
suboptions:
name:
description: Interface name.
type: str
required: true
access_rules:
description: Firewall rule sets per AFI.
type: list
elements: dict
suboptions:
afi:
description: Address family.
type: str
choices: [ipv4, ipv6]
required: true
rules:
description: Rule sets and directions.
type: list
elements: dict
suboptions:
name:
description: Rule set name.
type: str
direction:
description: Traffic direction.
type: str
choices: [in, local, out]
required: true
state:
description: Desired state.
type: str
choices: [merged, replaced, overridden, deleted, gathered]
default: merged
hostname:
type: str
required: true
port:
type: int
default: 443
api_key:
type: str
required: true
no_log: true
timeout:
type: int
default: 30
verify_ssl:
type: bool
default: false
"""
RETURN = r"""
before:
returned: always
type: list
after:
returned: when changed
type: list
commands:
returned: always
type: list
"""
from ansible.module_utils.basic import AnsibleModule
from ansible_collections.vyos.rest.plugins.module_utils.vyos_rest import (
VYOS_REST_CONNECTION_ARGSPEC,
VyOSRestClient,
VyOSRestError,
)
_IFACE_TYPES = {
"eth": "ethernet",
"bond": "bonding",
"vti": "vti",
"vxlan": "vxlan",
"lo": "loopback",
"dummy": "dummy",
"br": "bridge",
"wg": "wireguard",
"tun": "tunnel",
}
def _itype(name):
for p, t in _IFACE_TYPES.items():
if name.startswith(p):
return t
return "ethernet"
def _fw_direction_key(afi, direction):
if afi == "ipv4":
return {"in": "in", "out": "out", "local": "local"}[direction]
return {"in": "in", "out": "out", "local": "local"}[direction]
def _apply(client, iface_cfg, commands):
name = iface_cfg["name"]
itype = _itype(name)
ibase = ["interfaces", itype, name, "firewall"]
for ar in iface_cfg.get("access_rules") or []:
afi = ar["afi"]
fw_key = "firewall" if afi == "ipv4" else "ipv6"
for rule in ar.get("rules") or []:
direction = rule["direction"]
rs_name = rule.get("name")
if rs_name:
path = ibase + [direction, fw_key if afi == "ipv6" else "name"]
client.configure_set(path, rs_name)
commands.append(
"set interfaces {t} {n} firewall {d} {k} '{rs}'".format(
t=itype,
n=name,
d=direction,
k="ipv6-name" if afi == "ipv6" else "name",
rs=rs_name,
),
)
def _delete_iface_fw(client, name, commands):
itype = _itype(name)
path = ["interfaces", itype, name, "firewall"]
try:
client.configure_delete(path)
commands.append("delete interfaces {t} {n} firewall".format(t=itype, n=name))
except VyOSRestError:
pass
def _get(client):
try:
result = client.retrieve_show_config(["interfaces"])
raw = result.get("data") or {}
out = []
for itype, itype_data in raw.items():
if not isinstance(itype_data, dict):
continue
for iname, idata in itype_data.items():
fw = idata.get("firewall") if isinstance(idata, dict) else None
if fw:
out.append({"name": iname, "firewall": fw})
return out
except VyOSRestError:
return []
def main():
argument_spec = dict(
config=dict(
type="list",
elements="dict",
options=dict(
name=dict(type="str", required=True),
access_rules=dict(
type="list",
elements="dict",
options=dict(
afi=dict(type="str", required=True, choices=["ipv4", "ipv6"]),
rules=dict(
type="list",
elements="dict",
options=dict(
name=dict(type="str"),
direction=dict(
type="str",
required=True,
choices=["in", "local", "out"],
),
),
),
),
),
),
),
state=dict(
type="str",
default="merged",
choices=["merged", "replaced", "overridden", "deleted", "gathered"],
),
)
argument_spec.update(VYOS_REST_CONNECTION_ARGSPEC)
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)
client = VyOSRestClient(module)
state = module.params["state"]
config = module.params.get("config") or []
commands = []
changed = False
before = _get(client)
if state == "gathered":
module.exit_json(changed=False, gathered=before, before=before, commands=[])
if module.check_mode:
module.exit_json(changed=True, before=before, commands=["(check mode)"])
try:
if state == "deleted":
targets = {i["name"] for i in config} if config else {i["name"] for i in before}
for iface in before:
if iface["name"] in targets:
_delete_iface_fw(client, iface["name"], commands)
changed = True
elif state in ("merged", "replaced", "overridden"):
if state in ("replaced", "overridden"):
for cfg in config:
_delete_iface_fw(client, cfg["name"], commands)
for cfg in config:
_apply(client, cfg, commands)
changed = True
except VyOSRestError as exc:
module.fail_json(msg=str(exc))
after = _get(client) if changed else before
module.exit_json(changed=changed, before=before, after=after, commands=commands)
if __name__ == "__main__":
main()
|