| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: Contributors to the Ansible project |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
DOCUMENTATION = r""" |
| 8 |
module: route53_key_signing_key |
| 9 |
short_description: Manages a key-signing key (KSK) |
| 10 |
version_added: 9.2.0 |
| 11 |
description: |
| 12 |
- Creates a new key-signing key (KSK) associated with a hosted zone. |
| 13 |
You can only have two KSKs per hosted zone. |
| 14 |
- When O(state=absent), it deactivates and deletes a key-signing key (KSK). |
| 15 |
- Activates a key-signing key (KSK) so that it can be used for signing by DNSSEC. |
| 16 |
- Deactivates a key-signing key (KSK) so that it will not be used for signing by DNSSEC. |
| 17 |
options: |
| 18 |
state: |
| 19 |
description: |
| 20 |
- Whether or not the zone should exist. |
| 21 |
default: present |
| 22 |
choices: [ "present", "absent" ] |
| 23 |
type: str |
| 24 |
caller_reference: |
| 25 |
description: |
| 26 |
- A unique string that identifies the request. |
| 27 |
- Required when O(state=present). |
| 28 |
type: str |
| 29 |
hosted_zone_id: |
| 30 |
description: |
| 31 |
- The unique string (ID) used to identify a hosted zone. |
| 32 |
type: str |
| 33 |
required: true |
| 34 |
aliases: ["zone_id"] |
| 35 |
key_management_service_arn: |
| 36 |
description: |
| 37 |
- The Amazon resource name (ARN) for a customer managed key in Key Management Service (KMS). |
| 38 |
- Required when O(state=present). |
| 39 |
type: str |
| 40 |
aliases: ["kms_arn"] |
| 41 |
name: |
| 42 |
description: |
| 43 |
- A string used to identify a key-signing key (KSK). |
| 44 |
type: str |
| 45 |
required: true |
| 46 |
status: |
| 47 |
description: |
| 48 |
- A string specifying the initial status of the key-signing key (KSK). |
| 49 |
- When O(state=presnent), you can set the value to V(ACTIVE) or V(INACTIVE). |
| 50 |
type: str |
| 51 |
default: "ACTIVE" |
| 52 |
choices: ["ACTIVE", "INACTIVE"] |
| 53 |
wait: |
| 54 |
description: |
| 55 |
- Wait until the changes have been replicated. |
| 56 |
type: bool |
| 57 |
default: false |
| 58 |
wait_timeout: |
| 59 |
description: |
| 60 |
- How long to wait for the changes to be replicated, in seconds. |
| 61 |
default: 300 |
| 62 |
type: int |
| 63 |
extends_documentation_fragment: |
| 64 |
- amazon.aws.common.modules |
| 65 |
- amazon.aws.region.modules |
| 66 |
- amazon.aws.boto3 |
| 67 |
author: |
| 68 |
- Alina Buzachis (@alinabuzachis) |
| 69 |
""" |
| 70 |
|
| 71 |
EXAMPLES = r""" |
| 72 |
- name: Create a Key Signing Key Request |
| 73 |
amazon.aws.route53_ksk: |
| 74 |
name: "{{ resource_prefix }}-ksk" |
| 75 |
hosted_zone_id: "ZZZ1111112222" |
| 76 |
key_management_service_arn: "arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab" |
| 77 |
caller_reference: "arn:aws:iam::123456789012:user/SomeUser" |
| 78 |
status: "INACTIVE" |
| 79 |
state: present |
| 80 |
|
| 81 |
- name: Activate a Key Signing Key Request |
| 82 |
amazon.aws.route53_ksk: |
| 83 |
name: "{{ resource_prefix }}-ksk" |
| 84 |
hosted_zone_id: "ZZZ1111112222" |
| 85 |
key_management_service_arn: "arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab" |
| 86 |
caller_reference: "arn:aws:iam::123456789012:user/SomeUser" |
| 87 |
status: "ACTIVE" |
| 88 |
state: present |
| 89 |
|
| 90 |
- name: Delete a Key Signing Key Request and deactivate it |
| 91 |
amazon.aws.route53_ksk: |
| 92 |
name: "{{ resource_prefix }}-ksk" |
| 93 |
hosted_zone_id: "ZZZ1111112222" |
| 94 |
state: absent |
| 95 |
""" |
| 96 |
|
| 97 |
RETURN = r""" |
| 98 |
change_info: |
| 99 |
description: A dictionary that describes change information about changes made to the hosted zone. |
| 100 |
returned: when a Key Signing Key is created or it exists and is updated/deleted |
| 101 |
type: dict |
| 102 |
contains: |
| 103 |
id: |
| 104 |
description: Change ID. |
| 105 |
type: str |
| 106 |
status: |
| 107 |
description: The current state of the request. |
| 108 |
type: str |
| 109 |
submitted_at: |
| 110 |
description: The date and time that the change request was submitted in ISO 8601 format and Coordinated Universal Time (UTC). |
| 111 |
type: str |
| 112 |
comment: |
| 113 |
description: A comment you can provide. |
| 114 |
type: str |
| 115 |
sample: { |
| 116 |
"id": "/change/C090307813XORZJ5J3U4", |
| 117 |
"status": "PENDING", |
| 118 |
"submitted_at": "2024-12-04T15:15:36.743000+00:00" |
| 119 |
} |
| 120 |
location: |
| 121 |
description: The unique URL representing the new key-signing key (KSK). |
| 122 |
returned: when only a new Key Signing Key is created |
| 123 |
type: str |
| 124 |
sample: "https://route53.amazonaws.com/2013-04-01/keysigningkey/xxx/ansible-test-ksk" |
| 125 |
key_signing_key: |
| 126 |
description: The key-signing key (KSK) that the request creates. |
| 127 |
returned: always |
| 128 |
type: dict |
| 129 |
contains: |
| 130 |
name: |
| 131 |
description: A string used to identify a key-signing key (KSK). |
| 132 |
type: str |
| 133 |
kms_arn: |
| 134 |
description: The Amazon resource name (ARN) used to identify the customer managed key in Key Management Service (KMS). |
| 135 |
type: str |
| 136 |
flag: |
| 137 |
description: An integer that specifies how the key is used. |
| 138 |
type: int |
| 139 |
signing_algorithm_mnemonic: |
| 140 |
description: A string used to represent the signing algorithm. |
| 141 |
type: str |
| 142 |
signing_algorithm_type: |
| 143 |
description: An integer used to represent the signing algorithm. |
| 144 |
type: int |
| 145 |
digest_algorithm_mnemonic: |
| 146 |
description: A string used to represent the delegation signer digest algorithm. |
| 147 |
type: str |
| 148 |
digest_algorithm_type: |
| 149 |
description: An integer used to represent the delegation signer digest algorithm. |
| 150 |
type: int |
| 151 |
key_tag: |
| 152 |
description: An integer used to identify the DNSSEC record for the domain name. |
| 153 |
type: int |
| 154 |
digest_value: |
| 155 |
description: A cryptographic digest of a DNSKEY resource record (RR). |
| 156 |
type: str |
| 157 |
public_key: |
| 158 |
description: The public key, represented as a Base64 encoding. |
| 159 |
type: str |
| 160 |
ds_record: |
| 161 |
description: A string that represents a delegation signer (DS) record. |
| 162 |
type: str |
| 163 |
dnskey_record: |
| 164 |
description: A string that represents a DNSKEY record. |
| 165 |
type: str |
| 166 |
status: |
| 167 |
description: A string that represents the current key-signing key (KSK) status. |
| 168 |
type: str |
| 169 |
status_message: |
| 170 |
description: The status message provided for ACTION_NEEDED or INTERNAL_FAILURE statuses. |
| 171 |
type: str |
| 172 |
created_date: |
| 173 |
description: The date when the key-signing key (KSK) was created. |
| 174 |
type: str |
| 175 |
last_modified_date: |
| 176 |
description: The last time that the key-signing key (KSK) was changed. |
| 177 |
type: str |
| 178 |
sample: { |
| 179 |
"created_date": "2024-12-04T15:15:36.715000+00:00", |
| 180 |
"digest_algorithm_mnemonic": "SHA-256", |
| 181 |
"digest_algorithm_type": 2, |
| 182 |
"digest_value": "xxx", |
| 183 |
"dnskey_record": "xxx", |
| 184 |
"ds_record": "xxx", |
| 185 |
"flag": 257, |
| 186 |
"key_tag": 18948, |
| 187 |
"kms_arn": "arn:aws:kms:us-east-1:xxx:key/xxx", |
| 188 |
"last_modified_date": "2024-12-04T15:15:36.715000+00:00", |
| 189 |
"name": "ansible-test-44230979--ksk", |
| 190 |
"public_key": "xxxx", |
| 191 |
"signing_algorithm_mnemonic": "ECDSAP256SHA256", |
| 192 |
"signing_algorithm_type": 13, |
| 193 |
"status": "INACTIVE" |
| 194 |
} |
| 195 |
""" |
| 196 |
|
| 197 |
|
| 198 |
try: |
| 199 |
import botocore |
| 200 |
except ImportError: |
| 201 |
pass # Handled by AnsibleAWSModule |
| 202 |
|
| 203 |
from typing import Any |
| 204 |
from typing import Dict |
| 205 |
from typing import Tuple |
| 206 |
|
| 207 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 208 |
|
| 209 |
from ansible_collections.amazon.aws.plugins.module_utils.exceptions import AnsibleAWSError |
| 210 |
from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule |
| 211 |
from ansible_collections.amazon.aws.plugins.module_utils.waiters import get_waiter |
| 212 |
|
| 213 |
|
| 214 |
def deactivate_key_signing_key(client, hosted_zone_id: str, name: str) -> Dict[str, Any]: |
| 215 |
return client.deactivate_key_signing_key(HostedZoneId=hosted_zone_id, Name=name) |
| 216 |
|
| 217 |
|
| 218 |
def activate_key_signing_key(client, hosted_zone_id: str, name: str) -> Dict[str, Any]: |
| 219 |
return client.activate_key_signing_key(HostedZoneId=hosted_zone_id, Name=name) |
| 220 |
|
| 221 |
|
| 222 |
def get_change(client, change_id: str) -> Dict[str, Any]: |
| 223 |
return client.get_change(Id=change_id) |
| 224 |
|
| 225 |
|
| 226 |
def get_dnssec(client, hosted_zone_id: str) -> Dict[str, Any]: |
| 227 |
return client.get_dnssec(HostedZoneId=hosted_zone_id) |
| 228 |
|
| 229 |
|
| 230 |
def find_ksk(client, module: AnsibleAWSModule) -> Dict[str, Any]: |
| 231 |
hosted_zone_dnssec = get_dnssec(client, module.params.get("hosted_zone_id")) |
| 232 |
if hosted_zone_dnssec["KeySigningKeys"] != []: |
| 233 |
for ksk in hosted_zone_dnssec["KeySigningKeys"]: |
| 234 |
if ksk["Name"] == module.params.get("name"): |
| 235 |
return ksk |
| 236 |
return {} |
| 237 |
|
| 238 |
|
| 239 |
def wait(client, module: AnsibleAWSModule, change_id: str) -> None: |
| 240 |
try: |
| 241 |
waiter = get_waiter(client, "resource_record_sets_changed") |
| 242 |
waiter.wait( |
| 243 |
Id=change_id, |
| 244 |
WaiterConfig=dict( |
| 245 |
Delay=5, |
| 246 |
MaxAttempts=module.params.get("wait_timeout") // 5, |
| 247 |
), |
| 248 |
) |
| 249 |
except botocore.exceptions.WaiterError as e: |
| 250 |
module.fail_json_aws(e, msg="Timeout waiting for changes to be applied") |
| 251 |
|
| 252 |
|
| 253 |
def create_or_update(client, module: AnsibleAWSModule, ksk: Dict[str, Any]) -> Tuple[bool, Dict[str, Any]]: |
| 254 |
changed: bool = False |
| 255 |
zone_id = module.params.get("hosted_zone_id") |
| 256 |
name = module.params.get("name") |
| 257 |
status = module.params.get("status") |
| 258 |
response = {} |
| 259 |
|
| 260 |
if ksk: |
| 261 |
if ksk["Status"] != status: |
| 262 |
changed = True |
| 263 |
|
| 264 |
if module.check_mode: |
| 265 |
ksk["Status"] = status |
| 266 |
module.exit_json( |
| 267 |
changed=changed, |
| 268 |
key_signing_key=camel_dict_to_snake_dict(ksk), |
| 269 |
msg=f"Would have updated the Key Signing Key status to {status} if not in check_mode.", |
| 270 |
) |
| 271 |
|
| 272 |
if status == "ACTIVE": |
| 273 |
response.update(activate_key_signing_key(client, zone_id, name)) |
| 274 |
elif status == "INACTIVE": |
| 275 |
response.update(deactivate_key_signing_key(client, zone_id, name)) |
| 276 |
else: |
| 277 |
changed = True |
| 278 |
if module.check_mode: |
| 279 |
module.exit_json(changed=changed, msg="Would have created the Key Signing Key if not in check_mode.") |
| 280 |
|
| 281 |
response = client.create_key_signing_key( |
| 282 |
CallerReference=module.params.get("caller_reference"), |
| 283 |
KeyManagementServiceArn=module.params.get("key_management_service_arn"), |
| 284 |
HostedZoneId=zone_id, |
| 285 |
Name=name, |
| 286 |
Status=status, |
| 287 |
) |
| 288 |
|
| 289 |
return changed, response |
| 290 |
|
| 291 |
|
| 292 |
def delete(client, module: AnsibleAWSModule, ksk: Dict[str, Any]) -> Tuple[bool, Dict[str, Any]]: |
| 293 |
changed: bool = False |
| 294 |
zone_id = module.params.get("hosted_zone_id") |
| 295 |
name = module.params.get("name") |
| 296 |
response = {} |
| 297 |
|
| 298 |
if ksk: |
| 299 |
changed = True |
| 300 |
if module.check_mode: |
| 301 |
module.exit_json( |
| 302 |
changed=changed, |
| 303 |
key_signing_key={}, |
| 304 |
msg="Would have deleted the Key Signing Key if not in check_mode.", |
| 305 |
) |
| 306 |
|
| 307 |
result = deactivate_key_signing_key(client, zone_id, name) |
| 308 |
change_id = result["ChangeInfo"]["Id"] |
| 309 |
wait(client, module, change_id) |
| 310 |
|
| 311 |
response.update(client.delete_key_signing_key(HostedZoneId=zone_id, Name=name)) |
| 312 |
|
| 313 |
return changed, response |
| 314 |
|
| 315 |
|
| 316 |
def main() -> None: |
| 317 |
argument_spec = dict( |
| 318 |
caller_reference=dict(type="str"), |
| 319 |
hosted_zone_id=dict(type="str", aliases=["zone_id"], required=True), |
| 320 |
key_management_service_arn=dict(type="str", aliases=["kms_arn"], no_log=False), |
| 321 |
name=dict(type="str", required=True), |
| 322 |
status=dict(type="str", default="ACTIVE", choices=["ACTIVE", "INACTIVE"]), |
| 323 |
state=dict(default="present", choices=["present", "absent"]), |
| 324 |
wait=dict(type="bool", default=False), |
| 325 |
wait_timeout=dict(type="int", default=300), |
| 326 |
) |
| 327 |
|
| 328 |
module = AnsibleAWSModule( |
| 329 |
argument_spec=argument_spec, |
| 330 |
supports_check_mode=True, |
| 331 |
required_if=[["state", "present", ["caller_reference", "key_management_service_arn"]]], |
| 332 |
) |
| 333 |
|
| 334 |
try: |
| 335 |
client = module.client("route53") |
| 336 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 337 |
module.fail_json_aws(e, msg="Failed to connect to AWS.") |
| 338 |
|
| 339 |
changed = False |
| 340 |
state = module.params.get("state") |
| 341 |
|
| 342 |
try: |
| 343 |
ksk = find_ksk(client, module) |
| 344 |
if state == "present": |
| 345 |
changed, result = create_or_update(client, module, ksk) |
| 346 |
else: |
| 347 |
changed, result = delete(client, module, ksk) |
| 348 |
|
| 349 |
if module.params.get("wait") and result.get("ChangeInfo"): |
| 350 |
change_id = result["ChangeInfo"]["Id"] |
| 351 |
wait(client, module, change_id) |
| 352 |
result.update(get_change(client, change_id)) |
| 353 |
|
| 354 |
# Get updated information about KSK |
| 355 |
result["KeySigningKey"] = find_ksk(client, module) |
| 356 |
|
| 357 |
if "ResponseMetadata" in result: |
| 358 |
del result["ResponseMetadata"] |
| 359 |
|
| 360 |
except AnsibleAWSError as e: |
| 361 |
module.fail_json_aws_error(e) |
| 362 |
|
| 363 |
module.exit_json(changed=changed, **camel_dict_to_snake_dict(result)) |
| 364 |
|
| 365 |
|
| 366 |
if __name__ == "__main__": |
| 367 |
main() |