| 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: sts_session_token |
| 10 |
version_added: 1.0.0 |
| 11 |
short_description: obtain a session token from the AWS Security Token Service |
| 12 |
description: |
| 13 |
- Obtain a session token from the AWS Security Token Service. |
| 14 |
author: |
| 15 |
- Victor Costan (@pwnall) |
| 16 |
options: |
| 17 |
duration_seconds: |
| 18 |
description: |
| 19 |
- The duration, in seconds, of the session token. |
| 20 |
See U(https://docs.aws.amazon.com/STS/latest/APIReference/API_GetSessionToken.html#API_GetSessionToken_RequestParameters) |
| 21 |
for acceptable and default values. |
| 22 |
type: int |
| 23 |
mfa_serial_number: |
| 24 |
description: |
| 25 |
- The identification number of the MFA device that is associated with the user who is making the GetSessionToken call. |
| 26 |
type: str |
| 27 |
mfa_token: |
| 28 |
description: |
| 29 |
- The value provided by the MFA device, if the trust policy of the user requires MFA. |
| 30 |
type: str |
| 31 |
notes: |
| 32 |
- In order to use the session token in a following playbook task you must pass the I(access_key), |
| 33 |
I(secret_key) and I(session_token) parameters to modules that should use the session credentials. |
| 34 |
extends_documentation_fragment: |
| 35 |
- amazon.aws.common.modules |
| 36 |
- amazon.aws.region.modules |
| 37 |
- amazon.aws.boto3 |
| 38 |
""" |
| 39 |
|
| 40 |
RETURN = r""" |
| 41 |
sts_creds: |
| 42 |
description: The Credentials object returned by the AWS Security Token Service |
| 43 |
returned: always |
| 44 |
type: list |
| 45 |
sample: |
| 46 |
access_key: ASIAXXXXXXXXXXXXXXXX |
| 47 |
expiration: "2016-04-08T11:59:47+00:00" |
| 48 |
secret_key: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
| 49 |
session_token: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
| 50 |
changed: |
| 51 |
description: True if obtaining the credentials succeeds |
| 52 |
type: bool |
| 53 |
returned: always |
| 54 |
""" |
| 55 |
|
| 56 |
|
| 57 |
EXAMPLES = r""" |
| 58 |
# Note: These examples do not set authentication details, see the AWS Guide for details. |
| 59 |
|
| 60 |
# (more details: https://docs.aws.amazon.com/STS/latest/APIReference/API_GetSessionToken.html) |
| 61 |
- name: Get a session token |
| 62 |
community.aws.sts_session_token: |
| 63 |
access_key: AKIA1EXAMPLE1EXAMPLE |
| 64 |
secret_key: 123456789abcdefghijklmnopqrstuvwxyzABCDE |
| 65 |
duration_seconds: 3600 |
| 66 |
register: session_credentials |
| 67 |
|
| 68 |
- name: Use the session token obtained above to tag an instance in account 123456789012 |
| 69 |
amazon.aws.ec2_tag: |
| 70 |
access_key: "{{ session_credentials.sts_creds.access_key }}" |
| 71 |
secret_key: "{{ session_credentials.sts_creds.secret_key }}" |
| 72 |
session_token: "{{ session_credentials.sts_creds.session_token }}" |
| 73 |
resource: i-xyzxyz01 |
| 74 |
state: present |
| 75 |
tags: |
| 76 |
MyNewTag: value |
| 77 |
""" |
| 78 |
|
| 79 |
try: |
| 80 |
import botocore |
| 81 |
from botocore.exceptions import ClientError |
| 82 |
except ImportError: |
| 83 |
pass # Handled by AnsibleAWSModule |
| 84 |
|
| 85 |
from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule |
| 86 |
|
| 87 |
|
| 88 |
def normalize_credentials(credentials): |
| 89 |
access_key = credentials.get("AccessKeyId", None) |
| 90 |
secret_key = credentials.get("SecretAccessKey", None) |
| 91 |
session_token = credentials.get("SessionToken", None) |
| 92 |
expiration = credentials.get("Expiration", None) |
| 93 |
return { |
| 94 |
"access_key": access_key, |
| 95 |
"secret_key": secret_key, |
| 96 |
"session_token": session_token, |
| 97 |
"expiration": expiration, |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
def get_session_token(connection, module): |
| 102 |
duration_seconds = module.params.get("duration_seconds") |
| 103 |
mfa_serial_number = module.params.get("mfa_serial_number") |
| 104 |
mfa_token = module.params.get("mfa_token") |
| 105 |
changed = False |
| 106 |
|
| 107 |
args = {} |
| 108 |
if duration_seconds is not None: |
| 109 |
args["DurationSeconds"] = duration_seconds |
| 110 |
if mfa_serial_number is not None: |
| 111 |
args["SerialNumber"] = mfa_serial_number |
| 112 |
if mfa_token is not None: |
| 113 |
args["TokenCode"] = mfa_token |
| 114 |
|
| 115 |
try: |
| 116 |
response = connection.get_session_token(**args) |
| 117 |
changed = True |
| 118 |
except ClientError as e: |
| 119 |
module.fail_json(msg=e) |
| 120 |
|
| 121 |
credentials = normalize_credentials(response.get("Credentials", {})) |
| 122 |
module.exit_json(changed=changed, sts_creds=credentials) |
| 123 |
|
| 124 |
|
| 125 |
def main(): |
| 126 |
argument_spec = dict( |
| 127 |
duration_seconds=dict(required=False, default=None, type="int"), |
| 128 |
mfa_serial_number=dict(required=False, default=None), |
| 129 |
mfa_token=dict(required=False, default=None, no_log=True), |
| 130 |
) |
| 131 |
|
| 132 |
module = AnsibleAWSModule(argument_spec=argument_spec) |
| 133 |
|
| 134 |
try: |
| 135 |
connection = module.client("sts") |
| 136 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 137 |
module.fail_json_aws(e, msg="Failed to connect to AWS") |
| 138 |
|
| 139 |
get_session_token(connection, module) |
| 140 |
|
| 141 |
|
| 142 |
if __name__ == "__main__": |
| 143 |
main() |