| 1 |
#!/usr/bin/python |
| 2 |
# |
| 3 |
# Scaleway Security Group Rule management module |
| 4 |
# |
| 5 |
# Copyright (C) 2018 Antoine Barbare ([email protected]). |
| 6 |
# |
| 7 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 8 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 9 |
|
| 10 |
from __future__ import annotations |
| 11 |
|
| 12 |
DOCUMENTATION = r""" |
| 13 |
module: scaleway_security_group_rule |
| 14 |
short_description: Scaleway Security Group Rule management module |
| 15 |
author: Antoine Barbare (@abarbare) |
| 16 |
description: |
| 17 |
- This module manages Security Group Rule on Scaleway account U(https://developer.scaleway.com). |
| 18 |
extends_documentation_fragment: |
| 19 |
- community.general._scaleway |
| 20 |
- community.general._attributes |
| 21 |
- community.general._scaleway.actiongroup_scaleway |
| 22 |
|
| 23 |
attributes: |
| 24 |
check_mode: |
| 25 |
support: full |
| 26 |
diff_mode: |
| 27 |
support: none |
| 28 |
action_group: |
| 29 |
version_added: 11.3.0 |
| 30 |
|
| 31 |
options: |
| 32 |
state: |
| 33 |
type: str |
| 34 |
description: |
| 35 |
- Indicate desired state of the Security Group Rule. |
| 36 |
default: present |
| 37 |
choices: |
| 38 |
- present |
| 39 |
- absent |
| 40 |
|
| 41 |
region: |
| 42 |
type: str |
| 43 |
description: |
| 44 |
- Scaleway region to use (for example V(par1)). |
| 45 |
required: true |
| 46 |
choices: |
| 47 |
- ams1 |
| 48 |
- EMEA-NL-EVS |
| 49 |
- ams2 |
| 50 |
- ams3 |
| 51 |
- par1 |
| 52 |
- EMEA-FR-PAR1 |
| 53 |
- par2 |
| 54 |
- EMEA-FR-PAR2 |
| 55 |
- par3 |
| 56 |
- waw1 |
| 57 |
- EMEA-PL-WAW1 |
| 58 |
- waw2 |
| 59 |
- waw3 |
| 60 |
|
| 61 |
protocol: |
| 62 |
type: str |
| 63 |
description: |
| 64 |
- Network protocol to use. |
| 65 |
choices: |
| 66 |
- TCP |
| 67 |
- UDP |
| 68 |
- ICMP |
| 69 |
required: true |
| 70 |
|
| 71 |
port: |
| 72 |
description: |
| 73 |
- Port related to the rule, null value for all the ports. |
| 74 |
required: true |
| 75 |
type: int |
| 76 |
|
| 77 |
ip_range: |
| 78 |
type: str |
| 79 |
description: |
| 80 |
- IPV4 CIDR notation to apply to the rule. |
| 81 |
default: 0.0.0.0/0 |
| 82 |
|
| 83 |
direction: |
| 84 |
type: str |
| 85 |
description: |
| 86 |
- Rule direction. |
| 87 |
choices: |
| 88 |
- inbound |
| 89 |
- outbound |
| 90 |
required: true |
| 91 |
|
| 92 |
action: |
| 93 |
type: str |
| 94 |
description: |
| 95 |
- Rule action. |
| 96 |
choices: |
| 97 |
- accept |
| 98 |
- drop |
| 99 |
required: true |
| 100 |
|
| 101 |
security_group: |
| 102 |
type: str |
| 103 |
description: |
| 104 |
- Security Group unique identifier. |
| 105 |
required: true |
| 106 |
""" |
| 107 |
|
| 108 |
EXAMPLES = r""" |
| 109 |
- name: Create a Security Group Rule |
| 110 |
community.general.scaleway_security_group_rule: |
| 111 |
state: present |
| 112 |
region: par1 |
| 113 |
protocol: TCP |
| 114 |
port: 80 |
| 115 |
ip_range: 0.0.0.0/0 |
| 116 |
direction: inbound |
| 117 |
action: accept |
| 118 |
security_group: b57210ee-1281-4820-a6db-329f78596ecb |
| 119 |
register: security_group_rule_creation_task |
| 120 |
""" |
| 121 |
|
| 122 |
RETURN = r""" |
| 123 |
data: |
| 124 |
description: This is only present when O(state=present). |
| 125 |
returned: when O(state=present) |
| 126 |
type: dict |
| 127 |
sample: |
| 128 |
{ |
| 129 |
"scaleway_security_group_rule": { |
| 130 |
"direction": "inbound", |
| 131 |
"protocol": "TCP", |
| 132 |
"ip_range": "0.0.0.0/0", |
| 133 |
"dest_port_from": 80, |
| 134 |
"action": "accept", |
| 135 |
"position": 2, |
| 136 |
"dest_port_to": null, |
| 137 |
"editable": null, |
| 138 |
"id": "10cb0b9a-80f6-4830-abd7-a31cd828b5e9" |
| 139 |
} |
| 140 |
} |
| 141 |
""" |
| 142 |
|
| 143 |
from ansible.module_utils.basic import AnsibleModule |
| 144 |
|
| 145 |
from ansible_collections.community.general.plugins.module_utils._scaleway import ( |
| 146 |
SCALEWAY_LOCATION, |
| 147 |
Scaleway, |
| 148 |
payload_from_object, |
| 149 |
scaleway_argument_spec, |
| 150 |
) |
| 151 |
|
| 152 |
|
| 153 |
def get_sgr_from_api(security_group_rules, security_group_rule): |
| 154 |
"""Check if a security_group_rule specs are present in security_group_rules |
| 155 |
Return None if no rules match the specs |
| 156 |
Return the rule if found |
| 157 |
""" |
| 158 |
for sgr in security_group_rules: |
| 159 |
if ( |
| 160 |
sgr["ip_range"] == security_group_rule["ip_range"] |
| 161 |
and sgr["dest_port_from"] == security_group_rule["dest_port_from"] |
| 162 |
and sgr["direction"] == security_group_rule["direction"] |
| 163 |
and sgr["action"] == security_group_rule["action"] |
| 164 |
and sgr["protocol"] == security_group_rule["protocol"] |
| 165 |
): |
| 166 |
return sgr |
| 167 |
|
| 168 |
return None |
| 169 |
|
| 170 |
|
| 171 |
def present_strategy(api, security_group_id, security_group_rule): |
| 172 |
ret = {"changed": False} |
| 173 |
|
| 174 |
response = api.get(f"security_groups/{security_group_id}/rules") |
| 175 |
if not response.ok: |
| 176 |
api.module.fail_json( |
| 177 |
msg=f'Error getting security group rules "{response.info["msg"]}": "{response.json["message"]}" ({response.json})' |
| 178 |
) |
| 179 |
|
| 180 |
existing_rule = get_sgr_from_api(response.json["rules"], security_group_rule) |
| 181 |
|
| 182 |
if not existing_rule: |
| 183 |
ret["changed"] = True |
| 184 |
if api.module.check_mode: |
| 185 |
return ret |
| 186 |
|
| 187 |
# Create Security Group Rule |
| 188 |
response = api.post( |
| 189 |
f"/security_groups/{security_group_id}/rules", data=payload_from_object(security_group_rule) |
| 190 |
) |
| 191 |
|
| 192 |
if not response.ok: |
| 193 |
api.module.fail_json( |
| 194 |
msg=f'Error during security group rule creation: "{response.info["msg"]}": "{response.json["message"]}" ({response.json})' |
| 195 |
) |
| 196 |
ret["scaleway_security_group_rule"] = response.json["rule"] |
| 197 |
|
| 198 |
else: |
| 199 |
ret["scaleway_security_group_rule"] = existing_rule |
| 200 |
|
| 201 |
return ret |
| 202 |
|
| 203 |
|
| 204 |
def absent_strategy(api, security_group_id, security_group_rule): |
| 205 |
ret = {"changed": False} |
| 206 |
|
| 207 |
response = api.get(f"security_groups/{security_group_id}/rules") |
| 208 |
if not response.ok: |
| 209 |
api.module.fail_json( |
| 210 |
msg=f'Error getting security group rules "{response.info["msg"]}": "{response.json["message"]}" ({response.json})' |
| 211 |
) |
| 212 |
|
| 213 |
existing_rule = get_sgr_from_api(response.json["rules"], security_group_rule) |
| 214 |
|
| 215 |
if not existing_rule: |
| 216 |
return ret |
| 217 |
|
| 218 |
ret["changed"] = True |
| 219 |
if api.module.check_mode: |
| 220 |
return ret |
| 221 |
|
| 222 |
response = api.delete(f"/security_groups/{security_group_id}/rules/{existing_rule['id']}") |
| 223 |
if not response.ok: |
| 224 |
api.module.fail_json( |
| 225 |
msg=f'Error deleting security group rule "{response.info["msg"]}": "{response.json["message"]}" ({response.json})' |
| 226 |
) |
| 227 |
|
| 228 |
return ret |
| 229 |
|
| 230 |
|
| 231 |
def core(module): |
| 232 |
api = Scaleway(module=module) |
| 233 |
|
| 234 |
security_group_rule = { |
| 235 |
"protocol": module.params["protocol"], |
| 236 |
"dest_port_from": module.params["port"], |
| 237 |
"ip_range": module.params["ip_range"], |
| 238 |
"direction": module.params["direction"], |
| 239 |
"action": module.params["action"], |
| 240 |
} |
| 241 |
|
| 242 |
region = module.params["region"] |
| 243 |
module.params["api_url"] = SCALEWAY_LOCATION[region]["api_endpoint"] |
| 244 |
|
| 245 |
if module.params["state"] == "present": |
| 246 |
summary = present_strategy( |
| 247 |
api=api, security_group_id=module.params["security_group"], security_group_rule=security_group_rule |
| 248 |
) |
| 249 |
else: |
| 250 |
summary = absent_strategy( |
| 251 |
api=api, security_group_id=module.params["security_group"], security_group_rule=security_group_rule |
| 252 |
) |
| 253 |
module.exit_json(**summary) |
| 254 |
|
| 255 |
|
| 256 |
def main(): |
| 257 |
argument_spec = scaleway_argument_spec() |
| 258 |
argument_spec.update( |
| 259 |
state=dict(type="str", default="present", choices=["absent", "present"]), |
| 260 |
region=dict(type="str", required=True, choices=list(SCALEWAY_LOCATION.keys())), |
| 261 |
protocol=dict(type="str", required=True, choices=["TCP", "UDP", "ICMP"]), |
| 262 |
port=dict(type="int", required=True), |
| 263 |
ip_range=dict(type="str", default="0.0.0.0/0"), |
| 264 |
direction=dict(type="str", required=True, choices=["inbound", "outbound"]), |
| 265 |
action=dict(type="str", required=True, choices=["accept", "drop"]), |
| 266 |
security_group=dict(type="str", required=True), |
| 267 |
) |
| 268 |
module = AnsibleModule( |
| 269 |
argument_spec=argument_spec, |
| 270 |
supports_check_mode=True, |
| 271 |
) |
| 272 |
|
| 273 |
core(module) |
| 274 |
|
| 275 |
|
| 276 |
if __name__ == "__main__": |
| 277 |
main() |