Debian Codegraph @

ansible_collections/amazon/aws/plugins/module_utils/arn.py

raw
1 # -*- coding: utf-8 -*-
2
3 # Copyright 2017 Michael De La Rue | Ansible
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 import re
7
8
9 def validate_aws_arn(
10 arn, partition=None, service=None, region=None, account_id=None, resource=None, resource_type=None, resource_id=None
11 ):
12 details = parse_aws_arn(arn)
13
14 if not details:
15 return False
16
17 if partition and details.get("partition") != partition:
18 return False
19 if service and details.get("service") != service:
20 return False
21 if region and details.get("region") != region:
22 return False
23 if account_id and details.get("account_id") != account_id:
24 return False
25 if resource and details.get("resource") != resource:
26 return False
27 if resource_type and details.get("resource_type") != resource_type:
28 return False
29 if resource_id and details.get("resource_id") != resource_id:
30 return False
31
32 return True
33
34
35 def parse_aws_arn(arn):
36 """
37 Based on https://docs.aws.amazon.com/IAM/latest/UserGuide/reference-arns.html
38
39 The following are the general formats for ARNs.
40 arn:partition:service:region:account-id:resource-id
41 arn:partition:service:region:account-id:resource-type/resource-id
42 arn:partition:service:region:account-id:resource-type:resource-id
43 The specific formats depend on the resource.
44 The ARNs for some resources omit the Region, the account ID, or both the Region and the account ID.
45
46 Note: resource_type handling is very naive, for complex cases it may be necessary to use
47 "resource" directly instead of resource_type, this will include the resource type and full ID,
48 including all paths.
49 """
50 m = re.search(r"arn:(aws(-([a-z\-]+))?):([\w-]+):([a-z0-9\-]*):(\d*|aws|aws-managed):(.*)", arn)
51 if m is None:
52 return None
53 result = dict()
54 result.update(dict(partition=m.group(1)))
55 result.update(dict(service=m.group(4)))
56 result.update(dict(region=m.group(5)))
57 result.update(dict(account_id=m.group(6)))
58 result.update(dict(resource=m.group(7)))
59
60 m2 = re.search(r"^([^:/]*)[:/](.+)$", m.group(7))
61 if m2 is None:
62 result.update(dict(resource_type=None, resource_id=m.group(7)))
63 else:
64 result.update(dict(resource_type=m2.group(1), resource_id=m2.group(2)))
65
66 return result
67
68
69 # An implementation of this used was originally in ec2.py, however Outposts
70 # aren't specific to the EC2 service
71 def is_outpost_arn(arn):
72 """
73 Validates that the ARN is for an AWS Outpost
74
75
76 API Specification Document:
77 https://docs.aws.amazon.com/outposts/latest/APIReference/API_Outpost.html
78 """
79 details = parse_aws_arn(arn)
80
81 if not details:
82 return False
83
84 service = details.get("service") or ""
85 if service.lower() != "outposts":
86 return False
87 resource = details.get("resource") or ""
88 if not re.match("^outpost/op-[a-f0-9]{17}$", resource):
89 return False
90
91 return True