Debian Codegraph @

ansible_collections/community/hrobot/plugins/modules/firewall_info.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright (c) 2019 Felix Fontein <[email protected]>
5 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
6 # SPDX-License-Identifier: GPL-3.0-or-later
7
8 from __future__ import absolute_import, division, print_function
9 __metaclass__ = type
10
11
12 DOCUMENTATION = r"""
13 module: firewall_info
14 short_description: Manage Hetzner's dedicated server firewall
15 author:
16 - Felix Fontein (@felixfontein)
17 description:
18 - Manage Hetzner's dedicated server firewall.
19 seealso:
20 - name: Firewall documentation
21 description: Hetzner's documentation on the stateless firewall for dedicated servers.
22 link: https://docs.hetzner.com/robot/dedicated-server/firewall/
23 - module: community.hrobot.firewall
24 description: Configure firewall.
25 extends_documentation_fragment:
26 - community.hrobot.robot
27 - community.hrobot.attributes
28 - community.hrobot.attributes.actiongroup_robot
29 - community.hrobot.attributes.idempotent_not_modify_state
30 - community.hrobot.attributes.info_module
31
32 attributes:
33 action_group:
34 version_added: 1.6.0
35
36 options:
37 server_ip:
38 description:
39 - The server's main IP address.
40 - Exactly one of O(server_ip) and O(server_number) must be specified.
41 - Note that Hetzner deprecated identifying the server's firewall by the server's main IP. Using this option can thus
42 stop working at any time in the future. Use O(server_number) instead.
43 type: str
44 server_number:
45 description:
46 - The server's number.
47 - Exactly one of O(server_ip) and O(server_number) must be specified.
48 type: int
49 version_added: 1.8.0
50 wait_for_configured:
51 description:
52 - Whether to wait until the firewall has been successfully configured before returning from the module.
53 - The API returns status C(in progress) when the firewall is currently being configured. If this happens, the module
54 will try again until the status changes to C(active) or C(disabled).
55 - Please note that there is a request limit. If you have to do multiple updates, it can be better to disable waiting,
56 and regularly use M(community.hrobot.firewall_info) to query status.
57 type: bool
58 default: true
59 wait_delay:
60 description:
61 - Delay to wait (in seconds) before checking again whether the firewall has been configured.
62 type: int
63 default: 10
64 timeout:
65 description:
66 - Timeout (in seconds) for waiting for firewall to be configured.
67 type: int
68 default: 180
69 """
70
71 EXAMPLES = r"""
72 ---
73 - name: Get firewall configuration for server with main IP 1.2.3.4
74 community.hrobot.firewall_info:
75 hetzner_user: foo
76 hetzner_password: bar
77 server_ip: 1.2.3.4
78 register: result
79
80 - ansible.builtin.debug:
81 msg: "{{ result.firewall }}"
82 """
83
84 RETURN = r"""
85 firewall:
86 description:
87 - The firewall configuration.
88 type: dict
89 returned: success
90 contains:
91 port:
92 description:
93 - Switch port of firewall.
94 - V(main) or V(kvm).
95 type: str
96 sample: main
97 filter_ipv6:
98 description:
99 - Whether the firewall rules apply to IPv6 as well or not.
100 type: bool
101 sample: false
102 server_ip:
103 description:
104 - Server's main IP address.
105 type: str
106 sample: 1.2.3.4
107 server_number:
108 description:
109 - Hetzner's internal server number.
110 type: int
111 sample: 12345
112 status:
113 description:
114 - Status of the firewall.
115 - V(active) or V(disabled).
116 - Will be V(in process) if the firewall is currently updated, and O(wait_for_configured) is set to V(false) or O(timeout)
117 to a too small value.
118 type: str
119 sample: active
120 allowlist_hos:
121 description:
122 - Whether Hetzner services have access.
123 type: bool
124 sample: true
125 version_added: 1.2.0
126 whitelist_hos:
127 description:
128 - Whether Hetzner services have access.
129 - Old name of return value V(allowlist_hos), will be removed eventually.
130 type: bool
131 sample: true
132 rules:
133 description:
134 - Firewall rules.
135 type: dict
136 contains:
137 input:
138 description:
139 - Input firewall rules.
140 type: list
141 elements: dict
142 contains:
143 name:
144 description:
145 - Name of the firewall rule.
146 type: str
147 sample: Allow HTTP access to server
148 ip_version:
149 description:
150 - Internet protocol version.
151 - No value means the rule applies both to IPv4 and IPv6.
152 type: str
153 sample: ipv4
154 dst_ip:
155 description:
156 - Destination IP address or subnet address.
157 - CIDR notation.
158 type: str
159 sample: 1.2.3.4/32
160 dst_port:
161 description:
162 - Destination port or port range.
163 type: str
164 sample: "443"
165 src_ip:
166 description:
167 - Source IP address or subnet address.
168 - CIDR notation.
169 type: str
170 sample:
171 src_port:
172 description:
173 - Source port or port range.
174 type: str
175 sample:
176 protocol:
177 description:
178 - Protocol above IP layer.
179 type: str
180 sample: tcp
181 tcp_flags:
182 description:
183 - TCP flags or logical combination of flags.
184 type: str
185 sample:
186 action:
187 description:
188 - Action if rule matches.
189 - V(accept) or V(discard).
190 type: str
191 sample: accept
192 choices:
193 - accept
194 - discard
195 output:
196 description:
197 - Output firewall rules.
198 type: list
199 elements: dict
200 contains:
201 name:
202 description:
203 - Name of the firewall rule.
204 type: str
205 sample: Allow HTTP access to server
206 ip_version:
207 description:
208 - Internet protocol version.
209 - No value means the rule applies both to IPv4 and IPv6.
210 type: str
211 sample:
212 dst_ip:
213 description:
214 - Destination IP address or subnet address.
215 - CIDR notation.
216 type: str
217 sample: 1.2.3.4/32
218 dst_port:
219 description:
220 - Destination port or port range.
221 type: str
222 sample: "443"
223 src_ip:
224 description:
225 - Source IP address or subnet address.
226 - CIDR notation.
227 type: str
228 sample:
229 src_port:
230 description:
231 - Source port or port range.
232 type: str
233 sample:
234 protocol:
235 description:
236 - Protocol above IP layer.
237 type: str
238 sample: tcp
239 tcp_flags:
240 description:
241 - TCP flags or logical combination of flags.
242 type: str
243 sample:
244 action:
245 description:
246 - Action if rule matches.
247 - V(accept) or V(discard).
248 type: str
249 sample: accept
250 choices:
251 - accept
252 - discard
253 """
254
255 from ansible.module_utils.basic import AnsibleModule
256 from ansible_collections.community.hrobot.plugins.module_utils.common import (
257 CheckDoneTimeoutException,
258 )
259 from ansible_collections.community.hrobot.plugins.module_utils.robot import (
260 ROBOT_DEFAULT_ARGUMENT_SPEC,
261 BASE_URL,
262 fetch_url_json,
263 fetch_url_json_with_retries,
264 )
265
266
267 def firewall_configured(result, error):
268 return result['firewall']['status'] != 'in process'
269
270
271 def main():
272 argument_spec = dict(
273 server_ip=dict(type='str'),
274 server_number=dict(type='int'),
275 wait_for_configured=dict(type='bool', default=True),
276 wait_delay=dict(type='int', default=10),
277 timeout=dict(type='int', default=180),
278 )
279 argument_spec.update(ROBOT_DEFAULT_ARGUMENT_SPEC)
280 module = AnsibleModule(
281 argument_spec=argument_spec,
282 supports_check_mode=True,
283 )
284
285 server_id = module.params['server_ip'] or module.params['server_number']
286
287 # https://robot.your-server.de/doc/webservice/en.html#get-firewall-server-ip
288 url = "{0}/firewall/{1}".format(BASE_URL, server_id)
289 if module.params['wait_for_configured']:
290 try:
291 result, error = fetch_url_json_with_retries(
292 module,
293 url,
294 check_done_callback=firewall_configured,
295 check_done_delay=module.params['wait_delay'],
296 check_done_timeout=module.params['timeout'],
297 )
298 except CheckDoneTimeoutException as dummy:
299 module.fail_json(msg='Timeout while waiting for firewall to be configured.')
300 else:
301 result, error = fetch_url_json(module, url)
302
303 firewall = result['firewall']
304 firewall['allowlist_hos'] = firewall.get('whitelist_hos', False)
305 if not firewall.get('rules'):
306 firewall['rules'] = dict()
307 for ruleset in ['input']:
308 firewall['rules'][ruleset] = []
309
310 module.exit_json(
311 changed=False,
312 firewall=firewall,
313 )
314
315
316 if __name__ == '__main__': # pragma: no cover
317 main() # pragma: no cover