| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright: (c) 2019, 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_info |
| 12 |
short_description: Gather infos about the Hetzner Cloud Firewalls. |
| 13 |
|
| 14 |
description: |
| 15 |
- Gather facts about your Hetzner Cloud Firewalls. |
| 16 |
|
| 17 |
author: |
| 18 |
- Jonas Lammler (@jooola) |
| 19 |
|
| 20 |
options: |
| 21 |
id: |
| 22 |
description: |
| 23 |
- The ID of the Firewall you want to get. |
| 24 |
- The module will fail if the provided ID is invalid. |
| 25 |
type: int |
| 26 |
name: |
| 27 |
description: |
| 28 |
- The name for the Firewall you want to get. |
| 29 |
type: str |
| 30 |
label_selector: |
| 31 |
description: |
| 32 |
- The label selector for the Firewalls you want to get. |
| 33 |
type: str |
| 34 |
|
| 35 |
extends_documentation_fragment: |
| 36 |
- hetzner.hcloud.hcloud |
| 37 |
""" |
| 38 |
|
| 39 |
EXAMPLES = """ |
| 40 |
- name: Gather hcloud Firewall infos |
| 41 |
hetzner.hcloud.firewall_info: |
| 42 |
register: output |
| 43 |
|
| 44 |
- name: Print the gathered infos |
| 45 |
debug: |
| 46 |
var: output |
| 47 |
""" |
| 48 |
|
| 49 |
RETURN = """ |
| 50 |
hcloud_firewall_info: |
| 51 |
description: List of Firewalls. |
| 52 |
returned: always |
| 53 |
type: list |
| 54 |
elements: dict |
| 55 |
contains: |
| 56 |
id: |
| 57 |
description: Numeric identifier of the firewall. |
| 58 |
returned: always |
| 59 |
type: int |
| 60 |
sample: 1937415 |
| 61 |
name: |
| 62 |
description: Name of the firewall. |
| 63 |
returned: always |
| 64 |
type: str |
| 65 |
sample: my-firewall |
| 66 |
labels: |
| 67 |
description: User-defined labels (key-value pairs). |
| 68 |
returned: always |
| 69 |
type: dict |
| 70 |
rules: |
| 71 |
description: List of rules the firewall contain. |
| 72 |
returned: always |
| 73 |
type: list |
| 74 |
elements: dict |
| 75 |
contains: |
| 76 |
description: |
| 77 |
description: User defined description of this rule. |
| 78 |
type: str |
| 79 |
returned: always |
| 80 |
sample: allow http from anywhere |
| 81 |
direction: |
| 82 |
description: The direction of the firewall rule. |
| 83 |
type: str |
| 84 |
returned: always |
| 85 |
sample: in |
| 86 |
protocol: |
| 87 |
description: The protocol of the firewall rule. |
| 88 |
type: str |
| 89 |
returned: always |
| 90 |
sample: tcp |
| 91 |
port: |
| 92 |
description: The port or port range allowed by this rule. |
| 93 |
type: str |
| 94 |
returned: if RV(hcloud_firewall_info[].rules[].protocol=tcp) or RV(hcloud_firewall_info[].rules[].protocol=udp) |
| 95 |
sample: "80" |
| 96 |
source_ips: |
| 97 |
description: List of source CIDRs that are allowed within this rule. |
| 98 |
type: list |
| 99 |
elements: str |
| 100 |
returned: always |
| 101 |
sample: ["0.0.0.0/0", "::/0"] |
| 102 |
destination_ips: |
| 103 |
description: List of destination CIDRs that are allowed within this rule. |
| 104 |
type: list |
| 105 |
elements: str |
| 106 |
returned: always |
| 107 |
sample: [] |
| 108 |
applied_to: |
| 109 |
description: List of Resources the Firewall is applied to. |
| 110 |
returned: always |
| 111 |
type: list |
| 112 |
elements: dict |
| 113 |
contains: |
| 114 |
type: |
| 115 |
description: Type of the resource. |
| 116 |
type: str |
| 117 |
choices: [server, label_selector] |
| 118 |
sample: label_selector |
| 119 |
server: |
| 120 |
description: ID of the server. |
| 121 |
type: int |
| 122 |
sample: 12345 |
| 123 |
label_selector: |
| 124 |
description: Label selector value. |
| 125 |
type: str |
| 126 |
sample: env=prod |
| 127 |
applied_to_resources: |
| 128 |
description: List of Resources the Firewall label selector is applied to. |
| 129 |
returned: if RV(hcloud_firewall_info[].applied_to[].type=label_selector) |
| 130 |
type: list |
| 131 |
elements: dict |
| 132 |
contains: |
| 133 |
type: |
| 134 |
description: Type of resource referenced. |
| 135 |
type: str |
| 136 |
choices: [server] |
| 137 |
sample: server |
| 138 |
server: |
| 139 |
description: ID of the Server. |
| 140 |
type: int |
| 141 |
sample: 12345 |
| 142 |
""" |
| 143 |
|
| 144 |
from ansible.module_utils.basic import AnsibleModule |
| 145 |
|
| 146 |
from ..module_utils._base import AnsibleHCloud |
| 147 |
from ..module_utils._vendor.hcloud import HCloudException |
| 148 |
from ..module_utils._vendor.hcloud.firewalls import ( |
| 149 |
BoundFirewall, |
| 150 |
FirewallResource, |
| 151 |
FirewallRule, |
| 152 |
) |
| 153 |
|
| 154 |
|
| 155 |
class AnsibleHCloudFirewallInfo(AnsibleHCloud): |
| 156 |
represent = "hcloud_firewall_info" |
| 157 |
|
| 158 |
hcloud_firewall_info: list[BoundFirewall] | None = None |
| 159 |
|
| 160 |
def _prepare_result(self): |
| 161 |
tmp = [] |
| 162 |
|
| 163 |
for firewall in self.hcloud_firewall_info: |
| 164 |
if firewall is None: |
| 165 |
continue |
| 166 |
|
| 167 |
tmp.append( |
| 168 |
{ |
| 169 |
"id": firewall.id, |
| 170 |
"name": firewall.name, |
| 171 |
"labels": firewall.labels, |
| 172 |
"rules": [self._prepare_result_rule(rule) for rule in firewall.rules], |
| 173 |
"applied_to": [self._prepare_result_applied_to(resource) for resource in firewall.applied_to], |
| 174 |
} |
| 175 |
) |
| 176 |
|
| 177 |
return tmp |
| 178 |
|
| 179 |
def _prepare_result_rule(self, rule: FirewallRule): |
| 180 |
return { |
| 181 |
"description": rule.description, |
| 182 |
"direction": rule.direction, |
| 183 |
"protocol": rule.protocol, |
| 184 |
"port": rule.port, |
| 185 |
"source_ips": rule.source_ips, |
| 186 |
"destination_ips": rule.destination_ips, |
| 187 |
} |
| 188 |
|
| 189 |
def _prepare_result_applied_to(self, resource: FirewallResource): |
| 190 |
result = { |
| 191 |
"type": resource.type, |
| 192 |
"server": resource.server.id if resource.server is not None else None, |
| 193 |
"label_selector": resource.label_selector.selector if resource.label_selector is not None else None, |
| 194 |
} |
| 195 |
if resource.applied_to_resources is not None: |
| 196 |
result["applied_to_resources"] = [ |
| 197 |
{ |
| 198 |
"type": item.type, |
| 199 |
"server": item.server.id if item.server is not None else None, |
| 200 |
} |
| 201 |
for item in resource.applied_to_resources |
| 202 |
] |
| 203 |
return result |
| 204 |
|
| 205 |
def get_firewalls(self): |
| 206 |
try: |
| 207 |
if self.module.params.get("id") is not None: |
| 208 |
self.hcloud_firewall_info = [self.client.firewalls.get_by_id(self.module.params.get("id"))] |
| 209 |
elif self.module.params.get("name") is not None: |
| 210 |
self.hcloud_firewall_info = [self.client.firewalls.get_by_name(self.module.params.get("name"))] |
| 211 |
elif self.module.params.get("label_selector") is not None: |
| 212 |
self.hcloud_firewall_info = self.client.firewalls.get_all( |
| 213 |
label_selector=self.module.params.get("label_selector") |
| 214 |
) |
| 215 |
else: |
| 216 |
self.hcloud_firewall_info = self.client.firewalls.get_all() |
| 217 |
|
| 218 |
except HCloudException as exception: |
| 219 |
self.fail_json_hcloud(exception) |
| 220 |
|
| 221 |
@classmethod |
| 222 |
def define_module(cls): |
| 223 |
return AnsibleModule( |
| 224 |
argument_spec=dict( |
| 225 |
id={"type": "int"}, |
| 226 |
name={"type": "str"}, |
| 227 |
label_selector={"type": "str"}, |
| 228 |
**super().base_module_arguments(), |
| 229 |
), |
| 230 |
supports_check_mode=True, |
| 231 |
) |
| 232 |
|
| 233 |
|
| 234 |
def main(): |
| 235 |
module = AnsibleHCloudFirewallInfo.define_module() |
| 236 |
hcloud = AnsibleHCloudFirewallInfo(module) |
| 237 |
|
| 238 |
hcloud.get_firewalls() |
| 239 |
module.exit_json(**hcloud.get_result()) |
| 240 |
|
| 241 |
|
| 242 |
if __name__ == "__main__": |
| 243 |
main() |