| 1 |
# This code is part of Ansible, but is an independent component. |
| 2 |
# This particular file snippet, and this file snippet only, is BSD licensed. |
| 3 |
# Modules you write using this snippet, which is embedded dynamically by Ansible |
| 4 |
# still belong to the author of the module, and may assign their own license |
| 5 |
# to the complete work. |
| 6 |
# |
| 7 |
# Copyright (c) 2017-present Alibaba Group Holding Limited. He Guimin <[email protected]> |
| 8 |
# |
| 9 |
# Simplified BSD License (see LICENSES/BSD-2-Clause.txt or https://opensource.org/licenses/BSD-2-Clause) |
| 10 |
# SPDX-License-Identifier: BSD-2-Clause |
| 11 |
|
| 12 |
# Note that this module util is **PRIVATE** to the collection. It can have breaking changes at any time. |
| 13 |
# Do not use this from other collections or standalone plugins/modules! |
| 14 |
|
| 15 |
from __future__ import annotations |
| 16 |
|
| 17 |
import json |
| 18 |
import os |
| 19 |
import traceback |
| 20 |
import typing as t |
| 21 |
|
| 22 |
from ansible.module_utils.basic import env_fallback |
| 23 |
|
| 24 |
if t.TYPE_CHECKING: |
| 25 |
from ansible.module_utils.basic import AnsibleModule |
| 26 |
|
| 27 |
try: |
| 28 |
import footmark |
| 29 |
import footmark.dns |
| 30 |
import footmark.ecs |
| 31 |
import footmark.ess |
| 32 |
import footmark.market |
| 33 |
import footmark.ram |
| 34 |
import footmark.rds |
| 35 |
import footmark.slb |
| 36 |
import footmark.sts |
| 37 |
import footmark.vpc |
| 38 |
|
| 39 |
FOOTMARK_IMP_ERR = None |
| 40 |
HAS_FOOTMARK = True |
| 41 |
except ImportError: |
| 42 |
FOOTMARK_IMP_ERR = traceback.format_exc() |
| 43 |
HAS_FOOTMARK = False |
| 44 |
|
| 45 |
|
| 46 |
class AnsibleACSError(Exception): |
| 47 |
pass |
| 48 |
|
| 49 |
|
| 50 |
def acs_common_argument_spec(): |
| 51 |
return dict( |
| 52 |
alicloud_access_key=dict( |
| 53 |
aliases=["access_key_id", "access_key"], |
| 54 |
no_log=True, |
| 55 |
fallback=(env_fallback, ["ALICLOUD_ACCESS_KEY", "ALICLOUD_ACCESS_KEY_ID"]), |
| 56 |
), |
| 57 |
alicloud_secret_key=dict( |
| 58 |
aliases=["secret_access_key", "secret_key"], |
| 59 |
no_log=True, |
| 60 |
fallback=(env_fallback, ["ALICLOUD_SECRET_KEY", "ALICLOUD_SECRET_ACCESS_KEY"]), |
| 61 |
), |
| 62 |
alicloud_security_token=dict( |
| 63 |
aliases=["security_token"], no_log=True, fallback=(env_fallback, ["ALICLOUD_SECURITY_TOKEN"]) |
| 64 |
), |
| 65 |
ecs_role_name=dict(aliases=["role_name"], fallback=(env_fallback, ["ALICLOUD_ECS_ROLE_NAME"])), |
| 66 |
) |
| 67 |
|
| 68 |
|
| 69 |
def ecs_argument_spec(): |
| 70 |
spec = acs_common_argument_spec() |
| 71 |
spec.update( |
| 72 |
dict( |
| 73 |
alicloud_region=dict( |
| 74 |
required=True, |
| 75 |
aliases=["region", "region_id"], |
| 76 |
fallback=(env_fallback, ["ALICLOUD_REGION", "ALICLOUD_REGION_ID"]), |
| 77 |
), |
| 78 |
alicloud_assume_role_arn=dict( |
| 79 |
fallback=(env_fallback, ["ALICLOUD_ASSUME_ROLE_ARN"]), aliases=["assume_role_arn"] |
| 80 |
), |
| 81 |
alicloud_assume_role_session_name=dict( |
| 82 |
fallback=(env_fallback, ["ALICLOUD_ASSUME_ROLE_SESSION_NAME"]), aliases=["assume_role_session_name"] |
| 83 |
), |
| 84 |
alicloud_assume_role_session_expiration=dict( |
| 85 |
type="int", |
| 86 |
fallback=(env_fallback, ["ALICLOUD_ASSUME_ROLE_SESSION_EXPIRATION"]), |
| 87 |
aliases=["assume_role_session_expiration"], |
| 88 |
), |
| 89 |
alicloud_assume_role=dict(type="dict", aliases=["assume_role"]), |
| 90 |
profile=dict(fallback=(env_fallback, ["ALICLOUD_PROFILE"])), |
| 91 |
shared_credentials_file=dict(fallback=(env_fallback, ["ALICLOUD_SHARED_CREDENTIALS_FILE"])), |
| 92 |
) |
| 93 |
) |
| 94 |
return spec |
| 95 |
|
| 96 |
|
| 97 |
def get_acs_connection_info(params): |
| 98 |
ecs_params = dict( |
| 99 |
acs_access_key_id=params.get("alicloud_access_key"), |
| 100 |
acs_secret_access_key=params.get("alicloud_secret_key"), |
| 101 |
security_token=params.get("alicloud_security_token"), |
| 102 |
ecs_role_name=params.get("ecs_role_name"), |
| 103 |
user_agent="Ansible-Provider-Alicloud", |
| 104 |
) |
| 105 |
return ecs_params |
| 106 |
|
| 107 |
|
| 108 |
def connect_to_acs(acs_module, region, **params): |
| 109 |
conn = acs_module.connect_to_region(region, **params) |
| 110 |
if not conn: |
| 111 |
if region not in [acs_module_region.id for acs_module_region in acs_module.regions()]: |
| 112 |
raise AnsibleACSError( |
| 113 |
f"Region {region} does not seem to be available for acs module {acs_module.__name__}." |
| 114 |
) |
| 115 |
else: |
| 116 |
raise AnsibleACSError( |
| 117 |
f"Unknown problem connecting to region {region} for acs module {acs_module.__name__}." |
| 118 |
) |
| 119 |
return conn |
| 120 |
|
| 121 |
|
| 122 |
def get_assume_role(params): |
| 123 |
"""Return new params""" |
| 124 |
sts_params = get_acs_connection_info(params) |
| 125 |
assume_role = {} |
| 126 |
if params.get("assume_role"): |
| 127 |
assume_role["alicloud_assume_role_arn"] = params["assume_role"].get("role_arn") |
| 128 |
assume_role["alicloud_assume_role_session_name"] = params["assume_role"].get("session_name") |
| 129 |
assume_role["alicloud_assume_role_session_expiration"] = params["assume_role"].get("session_expiration") |
| 130 |
assume_role["alicloud_assume_role_policy"] = params["assume_role"].get("policy") |
| 131 |
|
| 132 |
assume_role_params = { |
| 133 |
"role_arn": params.get("alicloud_assume_role_arn") |
| 134 |
if params.get("alicloud_assume_role_arn") |
| 135 |
else assume_role.get("alicloud_assume_role_arn"), |
| 136 |
"role_session_name": params.get("alicloud_assume_role_session_name") |
| 137 |
if params.get("alicloud_assume_role_session_name") |
| 138 |
else assume_role.get("alicloud_assume_role_session_name"), |
| 139 |
"duration_seconds": params.get("alicloud_assume_role_session_expiration") |
| 140 |
if params.get("alicloud_assume_role_session_expiration") |
| 141 |
else assume_role.get("alicloud_assume_role_session_expiration", 3600), |
| 142 |
"policy": assume_role.get("alicloud_assume_role_policy", {}), |
| 143 |
} |
| 144 |
|
| 145 |
try: |
| 146 |
sts = ( |
| 147 |
connect_to_acs(footmark.sts, params.get("alicloud_region"), **sts_params) |
| 148 |
.assume_role(**assume_role_params) |
| 149 |
.read() |
| 150 |
) |
| 151 |
sts_params["acs_access_key_id"], sts_params["acs_secret_access_key"], sts_params["security_token"] = ( |
| 152 |
sts["access_key_id"], |
| 153 |
sts["access_key_secret"], |
| 154 |
sts["security_token"], |
| 155 |
) |
| 156 |
except AnsibleACSError as e: |
| 157 |
params.fail_json(msg=str(e)) |
| 158 |
return sts_params |
| 159 |
|
| 160 |
|
| 161 |
def get_profile(params): |
| 162 |
if not params["alicloud_access_key"] and not params["ecs_role_name"] and params["profile"]: |
| 163 |
path = ( |
| 164 |
params["shared_credentials_file"] |
| 165 |
if params["shared_credentials_file"] |
| 166 |
else f"{os.getenv('HOME')}/.aliyun/config.json" |
| 167 |
) |
| 168 |
auth = {} |
| 169 |
with open(path) as f: |
| 170 |
for pro in json.load(f)["profiles"]: |
| 171 |
if params["profile"] == pro["name"]: |
| 172 |
auth = pro |
| 173 |
if auth: |
| 174 |
if auth["mode"] == "AK" and auth.get("access_key_id") and auth.get("access_key_secret"): |
| 175 |
params["alicloud_access_key"] = auth.get("access_key_id") |
| 176 |
params["alicloud_secret_key"] = auth.get("access_key_secret") |
| 177 |
params["alicloud_region"] = auth.get("region_id") |
| 178 |
params = get_acs_connection_info(params) |
| 179 |
elif ( |
| 180 |
auth["mode"] == "StsToken" |
| 181 |
and auth.get("access_key_id") |
| 182 |
and auth.get("access_key_secret") |
| 183 |
and auth.get("sts_token") |
| 184 |
): |
| 185 |
params["alicloud_access_key"] = auth.get("access_key_id") |
| 186 |
params["alicloud_secret_key"] = auth.get("access_key_secret") |
| 187 |
params["security_token"] = auth.get("sts_token") |
| 188 |
params["alicloud_region"] = auth.get("region_id") |
| 189 |
params = get_acs_connection_info(params) |
| 190 |
elif auth["mode"] == "EcsRamRole": |
| 191 |
params["ecs_role_name"] = auth.get("ram_role_name") |
| 192 |
params["alicloud_region"] = auth.get("region_id") |
| 193 |
params = get_acs_connection_info(params) |
| 194 |
elif auth["mode"] == "RamRoleArn" and auth.get("ram_role_arn"): |
| 195 |
params["alicloud_access_key"] = auth.get("access_key_id") |
| 196 |
params["alicloud_secret_key"] = auth.get("access_key_secret") |
| 197 |
params["security_token"] = auth.get("sts_token") |
| 198 |
params["ecs_role_name"] = auth.get("ram_role_name") |
| 199 |
params["alicloud_assume_role_arn"] = auth.get("ram_role_arn") |
| 200 |
params["alicloud_assume_role_session_name"] = auth.get("ram_session_name") |
| 201 |
params["alicloud_assume_role_session_expiration"] = auth.get("expired_seconds") |
| 202 |
params["alicloud_region"] = auth.get("region_id") |
| 203 |
params = get_assume_role(params) |
| 204 |
elif params.get("alicloud_assume_role_arn") or params.get("assume_role"): |
| 205 |
params = get_assume_role(params) |
| 206 |
else: |
| 207 |
params = get_acs_connection_info(params) |
| 208 |
return params |
| 209 |
|
| 210 |
|
| 211 |
def ecs_connect(module: AnsibleModule): |
| 212 |
"""Return an ecs connection""" |
| 213 |
ecs_params = get_profile(module.params) |
| 214 |
# If we have a region specified, connect to its endpoint. |
| 215 |
region = module.params.get("alicloud_region") |
| 216 |
if region: |
| 217 |
try: |
| 218 |
ecs = connect_to_acs(footmark.ecs, region, **ecs_params) |
| 219 |
except AnsibleACSError as e: |
| 220 |
module.fail_json(msg=str(e)) |
| 221 |
# Otherwise, no region so we fallback to the old connection method |
| 222 |
return ecs |
| 223 |
|
| 224 |
|
| 225 |
def slb_connect(module: AnsibleModule): |
| 226 |
"""Return an slb connection""" |
| 227 |
slb_params = get_profile(module.params) |
| 228 |
# If we have a region specified, connect to its endpoint. |
| 229 |
region = module.params.get("alicloud_region") |
| 230 |
if region: |
| 231 |
try: |
| 232 |
slb = connect_to_acs(footmark.slb, region, **slb_params) |
| 233 |
except AnsibleACSError as e: |
| 234 |
module.fail_json(msg=str(e)) |
| 235 |
# Otherwise, no region so we fallback to the old connection method |
| 236 |
return slb |
| 237 |
|
| 238 |
|
| 239 |
def dns_connect(module: AnsibleModule): |
| 240 |
"""Return an dns connection""" |
| 241 |
dns_params = get_profile(module.params) |
| 242 |
# If we have a region specified, connect to its endpoint. |
| 243 |
region = module.params.get("alicloud_region") |
| 244 |
if region: |
| 245 |
try: |
| 246 |
dns = connect_to_acs(footmark.dns, region, **dns_params) |
| 247 |
except AnsibleACSError as e: |
| 248 |
module.fail_json(msg=str(e)) |
| 249 |
# Otherwise, no region so we fallback to the old connection method |
| 250 |
return dns |
| 251 |
|
| 252 |
|
| 253 |
def vpc_connect(module: AnsibleModule): |
| 254 |
"""Return an vpc connection""" |
| 255 |
vpc_params = get_profile(module.params) |
| 256 |
# If we have a region specified, connect to its endpoint. |
| 257 |
region = module.params.get("alicloud_region") |
| 258 |
if region: |
| 259 |
try: |
| 260 |
vpc = connect_to_acs(footmark.vpc, region, **vpc_params) |
| 261 |
except AnsibleACSError as e: |
| 262 |
module.fail_json(msg=str(e)) |
| 263 |
# Otherwise, no region so we fallback to the old connection method |
| 264 |
return vpc |
| 265 |
|
| 266 |
|
| 267 |
def rds_connect(module: AnsibleModule): |
| 268 |
"""Return an rds connection""" |
| 269 |
rds_params = get_profile(module.params) |
| 270 |
# If we have a region specified, connect to its endpoint. |
| 271 |
region = module.params.get("alicloud_region") |
| 272 |
if region: |
| 273 |
try: |
| 274 |
rds = connect_to_acs(footmark.rds, region, **rds_params) |
| 275 |
except AnsibleACSError as e: |
| 276 |
module.fail_json(msg=str(e)) |
| 277 |
# Otherwise, no region so we fallback to the old connection method |
| 278 |
return rds |
| 279 |
|
| 280 |
|
| 281 |
def ess_connect(module: AnsibleModule): |
| 282 |
"""Return an ess connection""" |
| 283 |
ess_params = get_profile(module.params) |
| 284 |
# If we have a region specified, connect to its endpoint. |
| 285 |
region = module.params.get("alicloud_region") |
| 286 |
if region: |
| 287 |
try: |
| 288 |
ess = connect_to_acs(footmark.ess, region, **ess_params) |
| 289 |
except AnsibleACSError as e: |
| 290 |
module.fail_json(msg=str(e)) |
| 291 |
# Otherwise, no region so we fallback to the old connection method |
| 292 |
return ess |
| 293 |
|
| 294 |
|
| 295 |
def sts_connect(module: AnsibleModule): |
| 296 |
"""Return an sts connection""" |
| 297 |
sts_params = get_profile(module.params) |
| 298 |
# If we have a region specified, connect to its endpoint. |
| 299 |
region = module.params.get("alicloud_region") |
| 300 |
if region: |
| 301 |
try: |
| 302 |
sts = connect_to_acs(footmark.sts, region, **sts_params) |
| 303 |
except AnsibleACSError as e: |
| 304 |
module.fail_json(msg=str(e)) |
| 305 |
# Otherwise, no region so we fallback to the old connection method |
| 306 |
return sts |
| 307 |
|
| 308 |
|
| 309 |
def ram_connect(module: AnsibleModule): |
| 310 |
"""Return an ram connection""" |
| 311 |
ram_params = get_profile(module.params) |
| 312 |
# If we have a region specified, connect to its endpoint. |
| 313 |
region = module.params.get("alicloud_region") |
| 314 |
if region: |
| 315 |
try: |
| 316 |
ram = connect_to_acs(footmark.ram, region, **ram_params) |
| 317 |
except AnsibleACSError as e: |
| 318 |
module.fail_json(msg=str(e)) |
| 319 |
# Otherwise, no region so we fallback to the old connection method |
| 320 |
return ram |
| 321 |
|
| 322 |
|
| 323 |
def market_connect(module: AnsibleModule): |
| 324 |
"""Return an market connection""" |
| 325 |
market_params = get_profile(module.params) |
| 326 |
# If we have a region specified, connect to its endpoint. |
| 327 |
region = module.params.get("alicloud_region") |
| 328 |
if region: |
| 329 |
try: |
| 330 |
market = connect_to_acs(footmark.market, region, **market_params) |
| 331 |
except AnsibleACSError as e: |
| 332 |
module.fail_json(msg=str(e)) |
| 333 |
# Otherwise, no region so we fallback to the old connection method |
| 334 |
return market |