| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: 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 |
--- |
| 9 |
module: ec2_win_password |
| 10 |
version_added: 1.0.0 |
| 11 |
short_description: Gets the default administrator password for EC2 Windows instances |
| 12 |
description: |
| 13 |
- Gets the default administrator password from any EC2 Windows instance. The instance is referenced by its id (e.g. C(i-XXXXXXX)). |
| 14 |
author: |
| 15 |
- "Rick Mendes (@rickmendes)" |
| 16 |
options: |
| 17 |
instance_id: |
| 18 |
description: |
| 19 |
- The instance id to get the password data from. |
| 20 |
required: true |
| 21 |
type: str |
| 22 |
key_file: |
| 23 |
description: |
| 24 |
- Path to the file containing the key pair used on the instance. |
| 25 |
- Conflicts with I(key_data). |
| 26 |
required: false |
| 27 |
type: path |
| 28 |
key_data: |
| 29 |
description: |
| 30 |
- The private key (usually stored in vault). |
| 31 |
- Conflicts with I(key_file), |
| 32 |
required: false |
| 33 |
type: str |
| 34 |
key_passphrase: |
| 35 |
description: |
| 36 |
- The passphrase for the instance key pair. The key must use DES or 3DES encryption for this module to decrypt it. You can use openssl to |
| 37 |
convert your password protected keys if they do not use DES or 3DES. ex) C(openssl rsa -in current_key -out new_key -des3). |
| 38 |
type: str |
| 39 |
wait: |
| 40 |
description: |
| 41 |
- Whether or not to wait for the password to be available before returning. |
| 42 |
type: bool |
| 43 |
default: false |
| 44 |
wait_timeout: |
| 45 |
description: |
| 46 |
- Number of seconds to wait before giving up. |
| 47 |
default: 120 |
| 48 |
type: int |
| 49 |
|
| 50 |
requirements: |
| 51 |
- cryptography |
| 52 |
|
| 53 |
extends_documentation_fragment: |
| 54 |
- amazon.aws.common.modules |
| 55 |
- amazon.aws.region.modules |
| 56 |
- amazon.aws.boto3 |
| 57 |
""" |
| 58 |
|
| 59 |
RETURN = r""" # """ |
| 60 |
|
| 61 |
EXAMPLES = r""" |
| 62 |
# Example of getting a password |
| 63 |
- name: get the Administrator password |
| 64 |
community.aws.ec2_win_password: |
| 65 |
profile: my-boto-profile |
| 66 |
instance_id: i-XXXXXX |
| 67 |
region: us-east-1 |
| 68 |
key_file: "~/aws-creds/my_test_key.pem" |
| 69 |
|
| 70 |
# Example of getting a password using a variable |
| 71 |
- name: get the Administrator password |
| 72 |
community.aws.ec2_win_password: |
| 73 |
profile: my-boto-profile |
| 74 |
instance_id: i-XXXXXX |
| 75 |
region: us-east-1 |
| 76 |
key_data: "{{ ec2_private_key }}" |
| 77 |
|
| 78 |
# Example of getting a password with a password protected key |
| 79 |
- name: get the Administrator password |
| 80 |
community.aws.ec2_win_password: |
| 81 |
profile: my-boto-profile |
| 82 |
instance_id: i-XXXXXX |
| 83 |
region: us-east-1 |
| 84 |
key_file: "~/aws-creds/my_protected_test_key.pem" |
| 85 |
key_passphrase: "secret" |
| 86 |
|
| 87 |
# Example of waiting for a password |
| 88 |
- name: get the Administrator password |
| 89 |
community.aws.ec2_win_password: |
| 90 |
profile: my-boto-profile |
| 91 |
instance_id: i-XXXXXX |
| 92 |
region: us-east-1 |
| 93 |
key_file: "~/aws-creds/my_test_key.pem" |
| 94 |
wait: true |
| 95 |
wait_timeout: 45 |
| 96 |
""" |
| 97 |
|
| 98 |
import datetime |
| 99 |
import time |
| 100 |
from base64 import b64decode |
| 101 |
|
| 102 |
try: |
| 103 |
from cryptography.hazmat.backends import default_backend |
| 104 |
from cryptography.hazmat.primitives.asymmetric.padding import PKCS1v15 |
| 105 |
from cryptography.hazmat.primitives.serialization import load_pem_private_key |
| 106 |
|
| 107 |
HAS_CRYPTOGRAPHY = True |
| 108 |
except ImportError: |
| 109 |
HAS_CRYPTOGRAPHY = False |
| 110 |
|
| 111 |
try: |
| 112 |
import botocore |
| 113 |
except ImportError: |
| 114 |
pass # Handled by AnsibleAWSModule |
| 115 |
|
| 116 |
from ansible.module_utils._text import to_bytes |
| 117 |
|
| 118 |
from ansible_collections.amazon.aws.plugins.module_utils.retries import AWSRetry |
| 119 |
|
| 120 |
from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule |
| 121 |
|
| 122 |
|
| 123 |
def setup_module_object(): |
| 124 |
argument_spec = dict( |
| 125 |
instance_id=dict(required=True), |
| 126 |
key_file=dict(required=False, default=None, type="path"), |
| 127 |
key_passphrase=dict(no_log=True, default=None, required=False), |
| 128 |
key_data=dict(no_log=True, default=None, required=False), |
| 129 |
wait=dict(type="bool", default=False, required=False), |
| 130 |
wait_timeout=dict(default=120, required=False, type="int"), |
| 131 |
) |
| 132 |
mutually_exclusive = [["key_file", "key_data"]] |
| 133 |
module = AnsibleAWSModule(argument_spec=argument_spec, mutually_exclusive=mutually_exclusive) |
| 134 |
return module |
| 135 |
|
| 136 |
|
| 137 |
def _get_password(module, client, instance_id): |
| 138 |
try: |
| 139 |
data = client.get_password_data(aws_retry=True, InstanceId=instance_id)["PasswordData"] |
| 140 |
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e: |
| 141 |
module.fail_json_aws(e, msg="Failed to get password data") |
| 142 |
return data |
| 143 |
|
| 144 |
|
| 145 |
def ec2_win_password(module): |
| 146 |
instance_id = module.params.get("instance_id") |
| 147 |
key_file = module.params.get("key_file") |
| 148 |
if module.params.get("key_passphrase") is None: |
| 149 |
b_key_passphrase = None |
| 150 |
else: |
| 151 |
b_key_passphrase = to_bytes(module.params.get("key_passphrase"), errors="surrogate_or_strict") |
| 152 |
if module.params.get("key_data") is None: |
| 153 |
b_key_data = None |
| 154 |
else: |
| 155 |
b_key_data = to_bytes(module.params.get("key_data"), errors="surrogate_or_strict") |
| 156 |
wait = module.params.get("wait") |
| 157 |
wait_timeout = module.params.get("wait_timeout") |
| 158 |
|
| 159 |
client = module.client("ec2", retry_decorator=AWSRetry.jittered_backoff()) |
| 160 |
|
| 161 |
if wait: |
| 162 |
start = datetime.datetime.now() |
| 163 |
end = start + datetime.timedelta(seconds=wait_timeout) |
| 164 |
|
| 165 |
while datetime.datetime.now() < end: |
| 166 |
data = _get_password(module, client, instance_id) |
| 167 |
decoded = b64decode(data) |
| 168 |
if not decoded: |
| 169 |
time.sleep(5) |
| 170 |
else: |
| 171 |
break |
| 172 |
else: |
| 173 |
data = _get_password(module, client, instance_id) |
| 174 |
decoded = b64decode(data) |
| 175 |
|
| 176 |
if wait and datetime.datetime.now() >= end: |
| 177 |
module.fail_json(msg=f"wait for password timeout after {int(wait_timeout)} seconds") |
| 178 |
|
| 179 |
if key_file is not None and b_key_data is None: |
| 180 |
try: |
| 181 |
with open(key_file, "rb") as f: |
| 182 |
key = load_pem_private_key(f.read(), b_key_passphrase, default_backend()) |
| 183 |
except IOError as e: |
| 184 |
# Handle bad files |
| 185 |
module.fail_json(msg=f"I/O error ({int(e.errno)}) opening key file: {e.strerror}") |
| 186 |
except (ValueError, TypeError): |
| 187 |
# Handle issues loading key |
| 188 |
module.fail_json(msg="unable to parse key file") |
| 189 |
elif b_key_data is not None and key_file is None: |
| 190 |
try: |
| 191 |
key = load_pem_private_key(b_key_data, b_key_passphrase, default_backend()) |
| 192 |
except (ValueError, TypeError): |
| 193 |
module.fail_json(msg="unable to parse key data") |
| 194 |
|
| 195 |
try: |
| 196 |
decrypted = key.decrypt(decoded, PKCS1v15()) |
| 197 |
except ValueError: |
| 198 |
decrypted = None |
| 199 |
|
| 200 |
if decrypted is None: |
| 201 |
module.fail_json(msg="unable to decrypt password", win_password="", changed=False) |
| 202 |
else: |
| 203 |
if wait: |
| 204 |
elapsed = datetime.datetime.now() - start |
| 205 |
module.exit_json(win_password=decrypted, changed=False, elapsed=elapsed.seconds) |
| 206 |
else: |
| 207 |
module.exit_json(win_password=decrypted, changed=False) |
| 208 |
|
| 209 |
|
| 210 |
def main(): |
| 211 |
module = setup_module_object() |
| 212 |
|
| 213 |
if not HAS_CRYPTOGRAPHY: |
| 214 |
module.fail_json(msg="cryptography package required for this module.") |
| 215 |
|
| 216 |
ec2_win_password(module) |
| 217 |
|
| 218 |
|
| 219 |
if __name__ == "__main__": |
| 220 |
main() |