| 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_assume_role |
| 10 |
version_added: 1.0.0 |
| 11 |
version_added_collection: community.aws |
| 12 |
short_description: Assume a role using AWS Security Token Service and obtain temporary credentials |
| 13 |
description: |
| 14 |
- Assume a role using AWS Security Token Service and obtain temporary credentials. |
| 15 |
author: |
| 16 |
- Boris Ekelchik (@bekelchik) |
| 17 |
- Marek Piatek (@piontas) |
| 18 |
options: |
| 19 |
role_arn: |
| 20 |
description: |
| 21 |
- The Amazon Resource Name (ARN) of the role that the caller is |
| 22 |
assuming U(https://docs.aws.amazon.com/IAM/latest/UserGuide/Using_Identifiers.html#Identifiers_ARNs). |
| 23 |
required: true |
| 24 |
type: str |
| 25 |
role_session_name: |
| 26 |
description: |
| 27 |
- Name of the role's session - will be used by CloudTrail. |
| 28 |
required: true |
| 29 |
type: str |
| 30 |
policy: |
| 31 |
description: |
| 32 |
- Supplemental policy to use in addition to assumed role's policies. |
| 33 |
type: str |
| 34 |
duration_seconds: |
| 35 |
description: |
| 36 |
- The duration, in seconds, of the role session. The value can range from 900 seconds (15 minutes) to 43200 seconds (12 hours). |
| 37 |
- The max depends on the IAM role's sessions duration setting. |
| 38 |
- By default, the value is set to V(3600) seconds. |
| 39 |
type: int |
| 40 |
external_id: |
| 41 |
description: |
| 42 |
- A unique identifier that is used by third parties to assume a role in their customers' accounts. |
| 43 |
type: str |
| 44 |
mfa_serial_number: |
| 45 |
description: |
| 46 |
- The identification number of the MFA device that is associated with the user who is making the AssumeRole call. |
| 47 |
type: str |
| 48 |
mfa_token: |
| 49 |
description: |
| 50 |
- The value provided by the MFA device, if the trust policy of the role being assumed requires MFA. |
| 51 |
type: str |
| 52 |
notes: |
| 53 |
- In order to use the assumed role in a following playbook task you must pass the O(access_key), |
| 54 |
O(secret_key) and O(session_token) parameters to modules that should use the assumed credentials. |
| 55 |
extends_documentation_fragment: |
| 56 |
- amazon.aws.common.modules |
| 57 |
- amazon.aws.region.modules |
| 58 |
- amazon.aws.boto3 |
| 59 |
""" |
| 60 |
|
| 61 |
RETURN = r""" |
| 62 |
sts_creds: |
| 63 |
description: The temporary security credentials, which include an access key ID, a secret access key, and a security (or session) token. |
| 64 |
returned: always |
| 65 |
type: dict |
| 66 |
sample: |
| 67 |
access_key: XXXXXXXXXXXXXXXXXXXX |
| 68 |
expiration: '2017-11-11T11:11:11+00:00' |
| 69 |
secret_key: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
| 70 |
session_token: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
| 71 |
sts_user: |
| 72 |
description: The Amazon Resource Name (ARN) and the assumed role ID. |
| 73 |
returned: always |
| 74 |
type: dict |
| 75 |
sample: |
| 76 |
assumed_role_id: arn:aws:sts::123456789012:assumed-role/demo/Bob |
| 77 |
arn: ARO123EXAMPLE123:Bob |
| 78 |
changed: |
| 79 |
description: True if obtaining the credentials succeeds. |
| 80 |
type: bool |
| 81 |
returned: always |
| 82 |
""" |
| 83 |
|
| 84 |
EXAMPLES = r""" |
| 85 |
# Assume an existing role (more details: https://docs.aws.amazon.com/STS/latest/APIReference/API_AssumeRole.html) |
| 86 |
- amazon.aws.sts_assume_role: |
| 87 |
access_key: AKIA1EXAMPLE1EXAMPLE |
| 88 |
secret_key: 123456789abcdefghijklmnopqrstuvwxyzABCDE |
| 89 |
role_arn: "arn:aws:iam::123456789012:role/someRole" |
| 90 |
role_session_name: "someRoleSession" |
| 91 |
register: assumed_role |
| 92 |
|
| 93 |
# Use the assumed role above to tag an instance in account 123456789012 |
| 94 |
- amazon.aws.ec2_tag: |
| 95 |
access_key: "{{ assumed_role.sts_creds.access_key }}" |
| 96 |
secret_key: "{{ assumed_role.sts_creds.secret_key }}" |
| 97 |
session_token: "{{ assumed_role.sts_creds.session_token }}" |
| 98 |
resource: i-xyzxyz01 |
| 99 |
state: present |
| 100 |
tags: |
| 101 |
MyNewTag: value |
| 102 |
""" |
| 103 |
|
| 104 |
try: |
| 105 |
from botocore.exceptions import ClientError |
| 106 |
from botocore.exceptions import ParamValidationError |
| 107 |
except ImportError: |
| 108 |
pass # caught by AnsibleAWSModule |
| 109 |
|
| 110 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 111 |
|
| 112 |
from ansible_collections.amazon.aws.plugins.module_utils.botocore import is_boto3_error_code |
| 113 |
from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule |
| 114 |
|
| 115 |
|
| 116 |
def _parse_response(response): |
| 117 |
credentials = response.get("Credentials", {}) |
| 118 |
user = response.get("AssumedRoleUser", {}) |
| 119 |
|
| 120 |
sts_cred = { |
| 121 |
"access_key": credentials.get("AccessKeyId"), |
| 122 |
"secret_key": credentials.get("SecretAccessKey"), |
| 123 |
"session_token": credentials.get("SessionToken"), |
| 124 |
"expiration": credentials.get("Expiration"), |
| 125 |
} |
| 126 |
sts_user = camel_dict_to_snake_dict(user) |
| 127 |
return sts_cred, sts_user |
| 128 |
|
| 129 |
|
| 130 |
def assume_role_policy(connection, module): |
| 131 |
params = { |
| 132 |
"RoleArn": module.params.get("role_arn"), |
| 133 |
"RoleSessionName": module.params.get("role_session_name"), |
| 134 |
"Policy": module.params.get("policy"), |
| 135 |
"DurationSeconds": module.params.get("duration_seconds"), |
| 136 |
"ExternalId": module.params.get("external_id"), |
| 137 |
"SerialNumber": module.params.get("mfa_serial_number"), |
| 138 |
"TokenCode": module.params.get("mfa_token"), |
| 139 |
} |
| 140 |
changed = False |
| 141 |
|
| 142 |
kwargs = dict((k, v) for k, v in params.items() if v is not None) |
| 143 |
|
| 144 |
try: |
| 145 |
response = connection.assume_role(**kwargs) |
| 146 |
changed = True |
| 147 |
except is_boto3_error_code("MalformedPolicyDocument") as e: |
| 148 |
module.fail_json_aws(e, msg="Invalid policy document provided") |
| 149 |
except (ClientError, ParamValidationError) as e: |
| 150 |
module.fail_json_aws(e) |
| 151 |
|
| 152 |
sts_cred, sts_user = _parse_response(response) |
| 153 |
module.exit_json(changed=changed, sts_creds=sts_cred, sts_user=sts_user) |
| 154 |
|
| 155 |
|
| 156 |
def main(): |
| 157 |
argument_spec = dict( |
| 158 |
role_arn=dict(required=True), |
| 159 |
role_session_name=dict(required=True), |
| 160 |
duration_seconds=dict(required=False, default=None, type="int"), |
| 161 |
external_id=dict(required=False, default=None), |
| 162 |
policy=dict(required=False, default=None), |
| 163 |
mfa_serial_number=dict(required=False, default=None), |
| 164 |
mfa_token=dict(required=False, default=None, no_log=True), |
| 165 |
) |
| 166 |
|
| 167 |
module = AnsibleAWSModule(argument_spec=argument_spec) |
| 168 |
|
| 169 |
connection = module.client("sts") |
| 170 |
|
| 171 |
assume_role_policy(connection, module) |
| 172 |
|
| 173 |
|
| 174 |
if __name__ == "__main__": |
| 175 |
main() |