| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright: (c) 2020, Hetzner Cloud GmbH <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = """ |
| 10 |
--- |
| 11 |
module: firewall |
| 12 |
short_description: Create and manage firewalls on the Hetzner Cloud. |
| 13 |
|
| 14 |
description: |
| 15 |
- Create, update and manage firewalls on the Hetzner Cloud. |
| 16 |
|
| 17 |
author: |
| 18 |
- Lukas Kaemmerling (@lkaemmerling) |
| 19 |
|
| 20 |
options: |
| 21 |
id: |
| 22 |
description: |
| 23 |
- The ID of the Hetzner Cloud Firewall to manage. |
| 24 |
- Only required if no firewall O(name) is given. |
| 25 |
type: int |
| 26 |
name: |
| 27 |
description: |
| 28 |
- The Name of the Hetzner Cloud Firewall to manage. |
| 29 |
- Only required if no firewall O(id) is given, or the firewall does not exist. |
| 30 |
type: str |
| 31 |
labels: |
| 32 |
description: |
| 33 |
- User-defined labels (key-value pairs). |
| 34 |
type: dict |
| 35 |
rules: |
| 36 |
description: |
| 37 |
- List of rules the firewall contain. |
| 38 |
type: list |
| 39 |
elements: dict |
| 40 |
suboptions: |
| 41 |
description: |
| 42 |
description: |
| 43 |
- User defined description of this rule. |
| 44 |
type: str |
| 45 |
direction: |
| 46 |
description: |
| 47 |
- The direction of the firewall rule. |
| 48 |
type: str |
| 49 |
choices: [in, out] |
| 50 |
protocol: |
| 51 |
description: |
| 52 |
- The protocol of the firewall rule. |
| 53 |
type: str |
| 54 |
choices: [icmp, tcp, udp, esp, gre] |
| 55 |
port: |
| 56 |
description: |
| 57 |
- The port or port range allowed by this rule. |
| 58 |
- A port range can be specified by separating two ports with a dash, e.g 1024-5000. |
| 59 |
- Only used if O(rules[].protocol=tcp) or O(rules[].protocol=udp). |
| 60 |
type: str |
| 61 |
source_ips: |
| 62 |
description: |
| 63 |
- List of CIDRs that are allowed within this rule. |
| 64 |
- Use 0.0.0.0/0 to allow all IPv4 addresses and ::/0 to allow all IPv6 addresses. |
| 65 |
- Only used if O(rules[].direction=in). |
| 66 |
type: list |
| 67 |
elements: str |
| 68 |
default: [] |
| 69 |
destination_ips: |
| 70 |
description: |
| 71 |
- List of CIDRs that are allowed within this rule. |
| 72 |
- Use 0.0.0.0/0 to allow all IPv4 addresses and ::/0 to allow all IPv6 addresses. |
| 73 |
- Only used if O(rules[].direction=out). |
| 74 |
type: list |
| 75 |
elements: str |
| 76 |
default: [] |
| 77 |
force: |
| 78 |
description: |
| 79 |
- Force the deletion of the Firewall when still in use. |
| 80 |
type: bool |
| 81 |
default: false |
| 82 |
state: |
| 83 |
description: |
| 84 |
- State of the firewall. |
| 85 |
default: present |
| 86 |
choices: [absent, present] |
| 87 |
type: str |
| 88 |
|
| 89 |
extends_documentation_fragment: |
| 90 |
- hetzner.hcloud.hcloud |
| 91 |
""" |
| 92 |
|
| 93 |
EXAMPLES = """ |
| 94 |
- name: Create a basic firewall |
| 95 |
hetzner.hcloud.firewall: |
| 96 |
name: my-firewall |
| 97 |
state: present |
| 98 |
|
| 99 |
- name: Create a firewall with rules |
| 100 |
hetzner.hcloud.firewall: |
| 101 |
name: my-firewall |
| 102 |
rules: |
| 103 |
- description: allow icmp from everywhere |
| 104 |
direction: in |
| 105 |
protocol: icmp |
| 106 |
source_ips: |
| 107 |
- 0.0.0.0/0 |
| 108 |
- ::/0 |
| 109 |
state: present |
| 110 |
|
| 111 |
- name: Create a firewall with labels |
| 112 |
hetzner.hcloud.firewall: |
| 113 |
name: my-firewall |
| 114 |
labels: |
| 115 |
key: value |
| 116 |
mylabel: 123 |
| 117 |
state: present |
| 118 |
|
| 119 |
- name: Ensure the firewall is absent (remove if needed) |
| 120 |
hetzner.hcloud.firewall: |
| 121 |
name: my-firewall |
| 122 |
state: absent |
| 123 |
""" |
| 124 |
|
| 125 |
RETURN = """ |
| 126 |
hcloud_firewall: |
| 127 |
description: The firewall instance. |
| 128 |
returned: always |
| 129 |
type: dict |
| 130 |
contains: |
| 131 |
id: |
| 132 |
description: Numeric identifier of the firewall. |
| 133 |
returned: always |
| 134 |
type: int |
| 135 |
sample: 1937415 |
| 136 |
name: |
| 137 |
description: Name of the firewall. |
| 138 |
returned: always |
| 139 |
type: str |
| 140 |
sample: my-firewall |
| 141 |
labels: |
| 142 |
description: User-defined labels (key-value pairs). |
| 143 |
returned: always |
| 144 |
type: dict |
| 145 |
rules: |
| 146 |
description: List of rules the firewall contain. |
| 147 |
returned: always |
| 148 |
type: list |
| 149 |
elements: dict |
| 150 |
contains: |
| 151 |
description: |
| 152 |
description: User defined description of this rule. |
| 153 |
type: str |
| 154 |
returned: always |
| 155 |
sample: allow http from anywhere |
| 156 |
direction: |
| 157 |
description: The direction of the firewall rule. |
| 158 |
type: str |
| 159 |
returned: always |
| 160 |
sample: in |
| 161 |
protocol: |
| 162 |
description: The protocol of the firewall rule. |
| 163 |
type: str |
| 164 |
returned: always |
| 165 |
sample: tcp |
| 166 |
port: |
| 167 |
description: The port or port range allowed by this rule. |
| 168 |
type: str |
| 169 |
returned: if RV(hcloud_firewall.rules[].protocol=tcp) or RV(hcloud_firewall.rules[].protocol=udp) |
| 170 |
sample: "80" |
| 171 |
source_ips: |
| 172 |
description: List of source CIDRs that are allowed within this rule. |
| 173 |
type: list |
| 174 |
elements: str |
| 175 |
returned: always |
| 176 |
sample: ["0.0.0.0/0", "::/0"] |
| 177 |
destination_ips: |
| 178 |
description: List of destination CIDRs that are allowed within this rule. |
| 179 |
type: list |
| 180 |
elements: str |
| 181 |
returned: always |
| 182 |
sample: [] |
| 183 |
applied_to: |
| 184 |
description: List of Resources the Firewall is applied to. |
| 185 |
returned: always |
| 186 |
type: list |
| 187 |
elements: dict |
| 188 |
contains: |
| 189 |
type: |
| 190 |
description: Type of the resource. |
| 191 |
type: str |
| 192 |
choices: [server, label_selector] |
| 193 |
sample: label_selector |
| 194 |
server: |
| 195 |
description: ID of the server. |
| 196 |
type: int |
| 197 |
sample: 12345 |
| 198 |
label_selector: |
| 199 |
description: Label selector value. |
| 200 |
type: str |
| 201 |
sample: env=prod |
| 202 |
applied_to_resources: |
| 203 |
description: List of Resources the Firewall label selector is applied to. |
| 204 |
returned: if RV(hcloud_firewall.applied_to[].type=label_selector) |
| 205 |
type: list |
| 206 |
elements: dict |
| 207 |
contains: |
| 208 |
type: |
| 209 |
description: Type of resource referenced. |
| 210 |
type: str |
| 211 |
choices: [server] |
| 212 |
sample: server |
| 213 |
server: |
| 214 |
description: ID of the Server. |
| 215 |
type: int |
| 216 |
sample: 12345 |
| 217 |
""" |
| 218 |
|
| 219 |
import time |
| 220 |
|
| 221 |
from ansible.module_utils.basic import AnsibleModule |
| 222 |
|
| 223 |
from ..module_utils._base import AnsibleHCloud |
| 224 |
from ..module_utils._ipaddress import normalize_ip |
| 225 |
from ..module_utils._vendor.hcloud import APIException, HCloudException |
| 226 |
from ..module_utils._vendor.hcloud.firewalls import ( |
| 227 |
BoundFirewall, |
| 228 |
FirewallResource, |
| 229 |
FirewallRule, |
| 230 |
) |
| 231 |
|
| 232 |
|
| 233 |
def normalize_rules(rules: list[dict]) -> list[dict]: |
| 234 |
for rule in rules: |
| 235 |
# Ensure ip addresses canonical representation |
| 236 |
rule["source_ips"] = [normalize_ip(o) for o in rule["source_ips"]] |
| 237 |
rule["destination_ips"] = [normalize_ip(o) for o in rule["destination_ips"]] |
| 238 |
return rules |
| 239 |
|
| 240 |
|
| 241 |
class AnsibleHCloudFirewall(AnsibleHCloud): |
| 242 |
represent = "hcloud_firewall" |
| 243 |
|
| 244 |
hcloud_firewall: BoundFirewall | None = None |
| 245 |
|
| 246 |
def _prepare_result(self): |
| 247 |
return { |
| 248 |
"id": self.hcloud_firewall.id, |
| 249 |
"name": self.hcloud_firewall.name, |
| 250 |
"rules": [self._prepare_result_rule(rule) for rule in self.hcloud_firewall.rules], |
| 251 |
"labels": self.hcloud_firewall.labels, |
| 252 |
"applied_to": [self._prepare_result_applied_to(resource) for resource in self.hcloud_firewall.applied_to], |
| 253 |
} |
| 254 |
|
| 255 |
def _prepare_result_rule(self, rule: FirewallRule): |
| 256 |
return { |
| 257 |
"direction": rule.direction, |
| 258 |
"protocol": rule.protocol, |
| 259 |
"port": rule.port, |
| 260 |
"source_ips": rule.source_ips, |
| 261 |
"destination_ips": rule.destination_ips, |
| 262 |
"description": rule.description, |
| 263 |
} |
| 264 |
|
| 265 |
def _prepare_result_applied_to(self, resource: FirewallResource): |
| 266 |
result = { |
| 267 |
"type": resource.type, |
| 268 |
"server": resource.server.id if resource.server is not None else None, |
| 269 |
"label_selector": resource.label_selector.selector if resource.label_selector is not None else None, |
| 270 |
} |
| 271 |
if resource.applied_to_resources is not None: |
| 272 |
result["applied_to_resources"] = [ |
| 273 |
{ |
| 274 |
"type": item.type, |
| 275 |
"server": item.server.id if item.server is not None else None, |
| 276 |
} |
| 277 |
for item in resource.applied_to_resources |
| 278 |
] |
| 279 |
return result |
| 280 |
|
| 281 |
def _get_firewall(self): |
| 282 |
try: |
| 283 |
if self.module.params.get("id") is not None: |
| 284 |
self.hcloud_firewall = self.client.firewalls.get_by_id(self.module.params.get("id")) |
| 285 |
elif self.module.params.get("name") is not None: |
| 286 |
self.hcloud_firewall = self.client.firewalls.get_by_name(self.module.params.get("name")) |
| 287 |
|
| 288 |
except HCloudException as exception: |
| 289 |
self.fail_json_hcloud(exception) |
| 290 |
|
| 291 |
def _create_firewall(self): |
| 292 |
self.module.fail_on_missing_params(required_params=["name"]) |
| 293 |
params = { |
| 294 |
"name": self.module.params.get("name"), |
| 295 |
"labels": self.module.params.get("labels"), |
| 296 |
} |
| 297 |
rules = self.module.params.get("rules") |
| 298 |
if rules is not None: |
| 299 |
rules = normalize_rules(rules) |
| 300 |
params["rules"] = [ |
| 301 |
FirewallRule( |
| 302 |
direction=rule["direction"], |
| 303 |
protocol=rule["protocol"], |
| 304 |
source_ips=rule["source_ips"] if rule["source_ips"] is not None else [], |
| 305 |
destination_ips=rule["destination_ips"] if rule["destination_ips"] is not None else [], |
| 306 |
port=rule["port"], |
| 307 |
description=rule["description"], |
| 308 |
) |
| 309 |
for rule in rules |
| 310 |
] |
| 311 |
|
| 312 |
if not self.module.check_mode: |
| 313 |
try: |
| 314 |
self.client.firewalls.create(**params) |
| 315 |
except HCloudException as exception: |
| 316 |
self.fail_json_hcloud(exception, params=params) |
| 317 |
|
| 318 |
self._mark_as_changed() |
| 319 |
self._get_firewall() |
| 320 |
|
| 321 |
def _update_firewall(self): |
| 322 |
name = self.module.params.get("name") |
| 323 |
if name is not None and self.hcloud_firewall.name != name: |
| 324 |
self.module.fail_on_missing_params(required_params=["id"]) |
| 325 |
if not self.module.check_mode: |
| 326 |
self.hcloud_firewall.update(name=name) |
| 327 |
self._mark_as_changed() |
| 328 |
|
| 329 |
labels = self.module.params.get("labels") |
| 330 |
if labels is not None and self.hcloud_firewall.labels != labels: |
| 331 |
if not self.module.check_mode: |
| 332 |
self.hcloud_firewall.update(labels=labels) |
| 333 |
self._mark_as_changed() |
| 334 |
|
| 335 |
rules = self.module.params.get("rules") |
| 336 |
if rules is not None: |
| 337 |
rules = normalize_rules(rules) |
| 338 |
|
| 339 |
if rules != [self._prepare_result_rule(rule) for rule in self.hcloud_firewall.rules]: |
| 340 |
if not self.module.check_mode: |
| 341 |
new_rules = [ |
| 342 |
FirewallRule( |
| 343 |
direction=rule["direction"], |
| 344 |
protocol=rule["protocol"], |
| 345 |
source_ips=rule["source_ips"] if rule["source_ips"] is not None else [], |
| 346 |
destination_ips=rule["destination_ips"] if rule["destination_ips"] is not None else [], |
| 347 |
port=rule["port"], |
| 348 |
description=rule["description"], |
| 349 |
) |
| 350 |
for rule in rules |
| 351 |
] |
| 352 |
self.hcloud_firewall.set_rules(new_rules) |
| 353 |
self._mark_as_changed() |
| 354 |
|
| 355 |
self._get_firewall() |
| 356 |
|
| 357 |
def present_firewall(self): |
| 358 |
self._get_firewall() |
| 359 |
if self.hcloud_firewall is None: |
| 360 |
self._create_firewall() |
| 361 |
else: |
| 362 |
self._update_firewall() |
| 363 |
|
| 364 |
def delete_firewall(self): |
| 365 |
self._get_firewall() |
| 366 |
if self.hcloud_firewall is not None: |
| 367 |
if not self.module.check_mode: |
| 368 |
if self.hcloud_firewall.applied_to: |
| 369 |
if self.module.params.get("force"): |
| 370 |
actions = self.hcloud_firewall.remove_from_resources(self.hcloud_firewall.applied_to) |
| 371 |
for action in actions: |
| 372 |
action.wait_until_finished() |
| 373 |
else: |
| 374 |
self.module.warn( |
| 375 |
f"Firewall {self.hcloud_firewall.name} is currently used by " |
| 376 |
"other resources. You need to unassign the resources before " |
| 377 |
"deleting the Firewall or use force=true." |
| 378 |
) |
| 379 |
|
| 380 |
retry_count = 0 |
| 381 |
while True: |
| 382 |
try: |
| 383 |
self.hcloud_firewall.delete() |
| 384 |
break |
| 385 |
except APIException as exception: |
| 386 |
if "is still in use" in exception.message and retry_count < 10: |
| 387 |
retry_count += 1 |
| 388 |
time.sleep(0.5 * retry_count) |
| 389 |
continue |
| 390 |
self.fail_json_hcloud(exception) |
| 391 |
except HCloudException as exception: |
| 392 |
self.fail_json_hcloud(exception) |
| 393 |
|
| 394 |
self._mark_as_changed() |
| 395 |
self.hcloud_firewall = None |
| 396 |
|
| 397 |
@classmethod |
| 398 |
def define_module(cls): |
| 399 |
return AnsibleModule( |
| 400 |
argument_spec=dict( |
| 401 |
id={"type": "int"}, |
| 402 |
name={"type": "str"}, |
| 403 |
labels={"type": "dict"}, |
| 404 |
rules=dict( |
| 405 |
type="list", |
| 406 |
elements="dict", |
| 407 |
options=dict( |
| 408 |
description={"type": "str"}, |
| 409 |
direction={"type": "str", "choices": ["in", "out"]}, |
| 410 |
protocol={"type": "str", "choices": ["icmp", "udp", "tcp", "esp", "gre"]}, |
| 411 |
port={"type": "str"}, |
| 412 |
source_ips={"type": "list", "elements": "str", "default": []}, |
| 413 |
destination_ips={"type": "list", "elements": "str", "default": []}, |
| 414 |
), |
| 415 |
required_together=[["direction", "protocol"]], |
| 416 |
required_if=[ |
| 417 |
["protocol", "udp", ["port"]], |
| 418 |
["protocol", "tcp", ["port"]], |
| 419 |
], |
| 420 |
), |
| 421 |
force={"type": "bool", "default": False}, |
| 422 |
state={ |
| 423 |
"choices": ["absent", "present"], |
| 424 |
"default": "present", |
| 425 |
}, |
| 426 |
**super().base_module_arguments(), |
| 427 |
), |
| 428 |
required_one_of=[["id", "name"]], |
| 429 |
required_if=[["state", "present", ["name"]]], |
| 430 |
supports_check_mode=True, |
| 431 |
) |
| 432 |
|
| 433 |
|
| 434 |
def main(): |
| 435 |
module = AnsibleHCloudFirewall.define_module() |
| 436 |
|
| 437 |
hcloud = AnsibleHCloudFirewall(module) |
| 438 |
state = module.params.get("state") |
| 439 |
if state == "absent": |
| 440 |
hcloud.delete_firewall() |
| 441 |
elif state == "present": |
| 442 |
hcloud.present_firewall() |
| 443 |
|
| 444 |
module.exit_json(**hcloud.get_result()) |
| 445 |
|
| 446 |
|
| 447 |
if __name__ == "__main__": |
| 448 |
main() |