2021-03-11 10:07:41 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
|
|
# Copyright: (c) 2020, Hetzner Cloud GmbH <info@hetzner-cloud.de>
|
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
|
|
|
|
|
2023-06-27 09:50:13 +00:00
|
|
|
DOCUMENTATION = """
|
2021-03-11 10:07:41 +00:00
|
|
|
---
|
|
|
|
module: hcloud_firewall
|
|
|
|
|
|
|
|
short_description: Create and manage firewalls on the Hetzner Cloud.
|
|
|
|
|
|
|
|
|
|
|
|
description:
|
|
|
|
- Create, update and manage firewalls on the Hetzner Cloud.
|
|
|
|
|
|
|
|
author:
|
|
|
|
- Lukas Kaemmerling (@lkaemmerling)
|
|
|
|
|
|
|
|
options:
|
|
|
|
id:
|
|
|
|
description:
|
|
|
|
- The ID of the Hetzner Cloud firewall to manage.
|
|
|
|
- Only required if no firewall I(name) is given
|
|
|
|
type: int
|
|
|
|
name:
|
|
|
|
description:
|
|
|
|
- The Name of the Hetzner Cloud firewall to manage.
|
2022-04-25 04:28:21 +00:00
|
|
|
- Only required if no firewall I(id) is given, or a firewall does not exist.
|
2021-03-11 10:07:41 +00:00
|
|
|
type: str
|
|
|
|
labels:
|
|
|
|
description:
|
|
|
|
- User-defined labels (key-value pairs)
|
|
|
|
type: dict
|
|
|
|
rules:
|
|
|
|
description:
|
|
|
|
- List of rules the firewall should contain.
|
|
|
|
type: list
|
|
|
|
elements: dict
|
|
|
|
suboptions:
|
|
|
|
direction:
|
|
|
|
description:
|
|
|
|
- The direction of the firewall rule.
|
|
|
|
type: str
|
|
|
|
choices: [ in, out ]
|
|
|
|
port:
|
|
|
|
description:
|
|
|
|
- The port of the firewall rule.
|
|
|
|
type: str
|
|
|
|
protocol:
|
|
|
|
description:
|
|
|
|
- The protocol of the firewall rule.
|
|
|
|
type: str
|
2022-11-07 12:58:43 +00:00
|
|
|
choices: [ icmp, tcp, udp, esp, gre ]
|
2021-03-11 10:07:41 +00:00
|
|
|
source_ips:
|
|
|
|
description:
|
|
|
|
- List of CIDRs that are allowed within this rule
|
|
|
|
type: list
|
|
|
|
elements: str
|
2022-11-07 12:58:43 +00:00
|
|
|
default: [ ]
|
2021-03-11 10:07:41 +00:00
|
|
|
destination_ips:
|
|
|
|
description:
|
|
|
|
- List of CIDRs that are allowed within this rule
|
|
|
|
type: list
|
|
|
|
elements: str
|
2022-11-07 12:58:43 +00:00
|
|
|
default: [ ]
|
2021-08-12 11:13:19 +00:00
|
|
|
description:
|
|
|
|
description:
|
|
|
|
- User defined description of this rule.
|
|
|
|
type: str
|
2021-03-11 10:07:41 +00:00
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- State of the firewall.
|
|
|
|
default: present
|
|
|
|
choices: [ absent, present ]
|
|
|
|
type: str
|
|
|
|
extends_documentation_fragment:
|
|
|
|
- hetzner.hcloud.hcloud
|
2023-06-27 09:50:13 +00:00
|
|
|
"""
|
2021-03-11 10:07:41 +00:00
|
|
|
|
|
|
|
EXAMPLES = """
|
|
|
|
- name: Create a basic firewall
|
2023-07-20 10:14:57 +00:00
|
|
|
hetzner.hcloud.hcloud_firewall:
|
2021-03-11 10:07:41 +00:00
|
|
|
name: my-firewall
|
|
|
|
state: present
|
|
|
|
|
|
|
|
- name: Create a firewall with rules
|
2023-07-20 10:14:57 +00:00
|
|
|
hetzner.hcloud.hcloud_firewall:
|
2021-03-11 10:07:41 +00:00
|
|
|
name: my-firewall
|
|
|
|
rules:
|
|
|
|
- direction: in
|
|
|
|
protocol: icmp
|
|
|
|
source_ips:
|
|
|
|
- 0.0.0.0/0
|
|
|
|
- ::/0
|
2021-08-12 11:13:19 +00:00
|
|
|
description: allow icmp in
|
2021-03-11 10:07:41 +00:00
|
|
|
state: present
|
|
|
|
|
|
|
|
- name: Create a firewall with labels
|
2023-07-20 10:14:57 +00:00
|
|
|
hetzner.hcloud.hcloud_firewall:
|
2021-03-11 10:07:41 +00:00
|
|
|
name: my-firewall
|
|
|
|
labels:
|
|
|
|
key: value
|
|
|
|
mylabel: 123
|
|
|
|
state: present
|
|
|
|
|
|
|
|
- name: Ensure the firewall is absent (remove if needed)
|
2023-07-20 10:14:57 +00:00
|
|
|
hetzner.hcloud.hcloud_firewall:
|
2021-03-11 10:07:41 +00:00
|
|
|
name: my-firewall
|
|
|
|
state: absent
|
|
|
|
"""
|
|
|
|
|
|
|
|
RETURN = """
|
|
|
|
hcloud_firewall:
|
|
|
|
description: The firewall instance
|
|
|
|
returned: Always
|
|
|
|
type: complex
|
|
|
|
contains:
|
|
|
|
id:
|
|
|
|
description: Numeric identifier of the firewall
|
|
|
|
returned: always
|
|
|
|
type: int
|
|
|
|
sample: 1937415
|
|
|
|
name:
|
|
|
|
description: Name of the firewall
|
|
|
|
returned: always
|
|
|
|
type: str
|
|
|
|
sample: my firewall
|
|
|
|
rules:
|
|
|
|
description: List of Rules within this Firewall
|
|
|
|
returned: always
|
|
|
|
type: complex
|
|
|
|
contains:
|
|
|
|
direction:
|
|
|
|
description: Direction of the Firewall Rule
|
|
|
|
type: str
|
|
|
|
returned: always
|
|
|
|
sample: in
|
|
|
|
protocol:
|
|
|
|
description: Protocol of the Firewall Rule
|
|
|
|
type: str
|
|
|
|
returned: always
|
|
|
|
sample: icmp
|
|
|
|
port:
|
|
|
|
description: Port of the Firewall Rule, None/Null if protocol is icmp
|
|
|
|
type: str
|
|
|
|
returned: always
|
|
|
|
sample: in
|
|
|
|
source_ips:
|
|
|
|
description: Source IPs of the Firewall
|
|
|
|
type: list
|
|
|
|
elements: str
|
|
|
|
returned: always
|
|
|
|
destination_ips:
|
|
|
|
description: Source IPs of the Firewall
|
|
|
|
type: list
|
|
|
|
elements: str
|
|
|
|
returned: always
|
2021-08-12 11:13:19 +00:00
|
|
|
description:
|
|
|
|
description: User defined description of the Firewall Rule
|
|
|
|
type: str
|
|
|
|
returned: always
|
2021-03-11 10:07:41 +00:00
|
|
|
labels:
|
|
|
|
description: User-defined labels (key-value pairs)
|
|
|
|
returned: always
|
|
|
|
type: dict
|
|
|
|
"""
|
|
|
|
|
2023-06-27 09:50:13 +00:00
|
|
|
import time
|
|
|
|
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
2023-06-27 11:17:21 +00:00
|
|
|
from ansible.module_utils.common.text.converters import to_native
|
2023-07-31 08:12:55 +00:00
|
|
|
|
2023-08-04 07:24:14 +00:00
|
|
|
from ..module_utils.hcloud import AnsibleHCloud
|
2023-07-31 08:12:55 +00:00
|
|
|
from ..module_utils.vendor.hcloud import APIException, HCloudException
|
|
|
|
from ..module_utils.vendor.hcloud.firewalls.domain import FirewallRule
|
2021-03-11 10:07:41 +00:00
|
|
|
|
|
|
|
|
2023-08-04 07:24:14 +00:00
|
|
|
class AnsibleHCloudFirewall(AnsibleHCloud):
|
2021-03-11 10:07:41 +00:00
|
|
|
def __init__(self, module):
|
2023-08-02 10:05:00 +00:00
|
|
|
super().__init__(module, "hcloud_firewall")
|
2021-03-11 10:07:41 +00:00
|
|
|
self.hcloud_firewall = None
|
|
|
|
|
|
|
|
def _prepare_result(self):
|
|
|
|
return {
|
|
|
|
"id": to_native(self.hcloud_firewall.id),
|
|
|
|
"name": to_native(self.hcloud_firewall.name),
|
|
|
|
"rules": [self._prepare_result_rule(rule) for rule in self.hcloud_firewall.rules],
|
2023-06-27 09:50:13 +00:00
|
|
|
"labels": self.hcloud_firewall.labels,
|
2021-03-11 10:07:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
def _prepare_result_rule(self, rule):
|
|
|
|
return {
|
|
|
|
"direction": rule.direction,
|
|
|
|
"protocol": to_native(rule.protocol),
|
|
|
|
"port": to_native(rule.port) if rule.port is not None else None,
|
|
|
|
"source_ips": [to_native(cidr) for cidr in rule.source_ips],
|
2021-08-12 11:13:19 +00:00
|
|
|
"destination_ips": [to_native(cidr) for cidr in rule.destination_ips],
|
|
|
|
"description": to_native(rule.description) if rule.description is not None else None,
|
2021-03-11 10:07:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
def _get_firewall(self):
|
|
|
|
try:
|
|
|
|
if self.module.params.get("id") is not None:
|
2023-06-27 09:50:13 +00:00
|
|
|
self.hcloud_firewall = self.client.firewalls.get_by_id(self.module.params.get("id"))
|
2021-03-11 10:07:41 +00:00
|
|
|
elif self.module.params.get("name") is not None:
|
2023-06-27 09:50:13 +00:00
|
|
|
self.hcloud_firewall = self.client.firewalls.get_by_name(self.module.params.get("name"))
|
2021-03-11 10:07:41 +00:00
|
|
|
|
2023-07-20 12:34:48 +00:00
|
|
|
except HCloudException as e:
|
|
|
|
self.fail_json_hcloud(e)
|
2021-03-11 10:07:41 +00:00
|
|
|
|
|
|
|
def _create_firewall(self):
|
2023-06-27 09:50:13 +00:00
|
|
|
self.module.fail_on_missing_params(required_params=["name"])
|
2021-03-11 10:07:41 +00:00
|
|
|
params = {
|
|
|
|
"name": self.module.params.get("name"),
|
2023-06-27 09:50:13 +00:00
|
|
|
"labels": self.module.params.get("labels"),
|
2021-03-11 10:07:41 +00:00
|
|
|
}
|
|
|
|
rules = self.module.params.get("rules")
|
|
|
|
if rules is not None:
|
|
|
|
params["rules"] = [
|
|
|
|
FirewallRule(
|
|
|
|
direction=rule["direction"],
|
|
|
|
protocol=rule["protocol"],
|
2021-05-27 06:13:57 +00:00
|
|
|
source_ips=rule["source_ips"] if rule["source_ips"] is not None else [],
|
|
|
|
destination_ips=rule["destination_ips"] if rule["destination_ips"] is not None else [],
|
2021-08-12 11:13:19 +00:00
|
|
|
port=rule["port"],
|
|
|
|
description=rule["description"],
|
2021-03-11 10:07:41 +00:00
|
|
|
)
|
|
|
|
for rule in rules
|
|
|
|
]
|
|
|
|
if not self.module.check_mode:
|
|
|
|
try:
|
|
|
|
self.client.firewalls.create(**params)
|
2023-07-20 12:34:48 +00:00
|
|
|
except HCloudException as e:
|
|
|
|
self.fail_json_hcloud(e, params=params)
|
2021-03-11 10:07:41 +00:00
|
|
|
self._mark_as_changed()
|
|
|
|
self._get_firewall()
|
|
|
|
|
|
|
|
def _update_firewall(self):
|
|
|
|
name = self.module.params.get("name")
|
|
|
|
if name is not None and self.hcloud_firewall.name != name:
|
2023-06-27 09:50:13 +00:00
|
|
|
self.module.fail_on_missing_params(required_params=["id"])
|
2021-03-11 10:07:41 +00:00
|
|
|
if not self.module.check_mode:
|
|
|
|
self.hcloud_firewall.update(name=name)
|
|
|
|
self._mark_as_changed()
|
|
|
|
|
|
|
|
labels = self.module.params.get("labels")
|
|
|
|
if labels is not None and self.hcloud_firewall.labels != labels:
|
|
|
|
if not self.module.check_mode:
|
|
|
|
self.hcloud_firewall.update(labels=labels)
|
|
|
|
self._mark_as_changed()
|
|
|
|
|
|
|
|
rules = self.module.params.get("rules")
|
2021-04-06 09:28:38 +00:00
|
|
|
if rules is not None and rules != [self._prepare_result_rule(rule) for rule in self.hcloud_firewall.rules]:
|
2021-03-11 10:07:41 +00:00
|
|
|
if not self.module.check_mode:
|
|
|
|
new_rules = [
|
|
|
|
FirewallRule(
|
|
|
|
direction=rule["direction"],
|
|
|
|
protocol=rule["protocol"],
|
2021-05-27 06:13:57 +00:00
|
|
|
source_ips=rule["source_ips"] if rule["source_ips"] is not None else [],
|
|
|
|
destination_ips=rule["destination_ips"] if rule["destination_ips"] is not None else [],
|
2021-08-12 11:13:19 +00:00
|
|
|
port=rule["port"],
|
|
|
|
description=rule["description"],
|
2021-03-11 10:07:41 +00:00
|
|
|
)
|
|
|
|
for rule in rules
|
|
|
|
]
|
|
|
|
self.hcloud_firewall.set_rules(new_rules)
|
|
|
|
self._mark_as_changed()
|
|
|
|
self._get_firewall()
|
|
|
|
|
|
|
|
def present_firewall(self):
|
|
|
|
self._get_firewall()
|
|
|
|
if self.hcloud_firewall is None:
|
|
|
|
self._create_firewall()
|
|
|
|
else:
|
|
|
|
self._update_firewall()
|
|
|
|
|
|
|
|
def delete_firewall(self):
|
|
|
|
self._get_firewall()
|
|
|
|
if self.hcloud_firewall is not None:
|
|
|
|
if not self.module.check_mode:
|
2022-11-08 13:05:13 +00:00
|
|
|
retry_count = 0
|
|
|
|
while retry_count < 10:
|
|
|
|
try:
|
|
|
|
self.client.firewalls.delete(self.hcloud_firewall)
|
|
|
|
break
|
|
|
|
except APIException as e:
|
|
|
|
if "is still in use" in e.message:
|
|
|
|
retry_count = retry_count + 1
|
|
|
|
time.sleep(0.5 * retry_count)
|
|
|
|
else:
|
2023-07-20 12:34:48 +00:00
|
|
|
self.fail_json_hcloud(e)
|
|
|
|
except HCloudException as e:
|
|
|
|
self.fail_json_hcloud(e)
|
2021-03-11 10:07:41 +00:00
|
|
|
self._mark_as_changed()
|
|
|
|
self.hcloud_firewall = None
|
|
|
|
|
2023-08-02 10:05:00 +00:00
|
|
|
@classmethod
|
|
|
|
def define_module(cls):
|
2021-03-11 10:07:41 +00:00
|
|
|
return AnsibleModule(
|
|
|
|
argument_spec=dict(
|
|
|
|
id={"type": "int"},
|
|
|
|
name={"type": "str"},
|
|
|
|
rules=dict(
|
|
|
|
type="list",
|
|
|
|
elements="dict",
|
|
|
|
options=dict(
|
|
|
|
direction={"type": "str", "choices": ["in", "out"]},
|
2022-11-07 12:58:43 +00:00
|
|
|
protocol={"type": "str", "choices": ["icmp", "udp", "tcp", "esp", "gre"]},
|
2021-03-11 10:07:41 +00:00
|
|
|
port={"type": "str"},
|
2021-05-27 06:13:57 +00:00
|
|
|
source_ips={"type": "list", "elements": "str", "default": []},
|
2021-04-06 09:28:38 +00:00
|
|
|
destination_ips={"type": "list", "elements": "str", "default": []},
|
2021-08-12 11:13:19 +00:00
|
|
|
description={"type": "str"},
|
2021-03-11 10:07:41 +00:00
|
|
|
),
|
2021-05-27 06:13:57 +00:00
|
|
|
required_together=[["direction", "protocol"]],
|
2021-03-11 10:07:41 +00:00
|
|
|
),
|
|
|
|
labels={"type": "dict"},
|
|
|
|
state={
|
|
|
|
"choices": ["absent", "present"],
|
|
|
|
"default": "present",
|
|
|
|
},
|
2023-08-02 10:05:00 +00:00
|
|
|
**super().base_module_arguments()
|
2021-03-11 10:07:41 +00:00
|
|
|
),
|
2023-06-27 09:50:13 +00:00
|
|
|
required_one_of=[["id", "name"]],
|
|
|
|
required_if=[["state", "present", ["name"]]],
|
2021-03-11 10:07:41 +00:00
|
|
|
supports_check_mode=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2023-08-04 07:24:14 +00:00
|
|
|
module = AnsibleHCloudFirewall.define_module()
|
2021-03-11 10:07:41 +00:00
|
|
|
|
2023-08-04 07:24:14 +00:00
|
|
|
hcloud = AnsibleHCloudFirewall(module)
|
2021-03-11 10:07:41 +00:00
|
|
|
state = module.params.get("state")
|
|
|
|
if state == "absent":
|
|
|
|
hcloud.delete_firewall()
|
|
|
|
elif state == "present":
|
|
|
|
hcloud.present_firewall()
|
|
|
|
|
|
|
|
module.exit_json(**hcloud.get_result())
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|