Skip to content
This repository was archived by the owner on May 16, 2023. It is now read-only.

Commit 87fd4b4

Browse files
authored
Merge pull request #4 from PokaInc/fix-path-based-resource
fix: Case where there is both a "resourcetype" and path in the "resou…
2 parents f796215 + 7c4713a commit 87fd4b4

File tree

2 files changed

+110
-9
lines changed

2 files changed

+110
-9
lines changed

arnparse/arnparse.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,13 @@ def arnparse(arn_str):
2626
raise MalformedArnError(arn_str)
2727

2828
elements = arn_str.split(':', 5)
29-
29+
service = elements[2]
3030
resource = elements[5]
31-
resource_type = None
3231

33-
service = elements[2]
34-
if service not in ['s3', 'sns', 'apigateway', 'execute-api']:
35-
if '/' in resource:
36-
resource_type, resource = resource.split('/', 1)
37-
elif ':' in resource:
38-
resource_type, resource = resource.split(':', 1)
32+
if service in ['s3', 'sns', 'apigateway', 'execute-api']:
33+
resource_type = None
34+
else:
35+
resource_type, resource = _parse_resource(resource)
3936

4037
return Arn(
4138
partition=elements[1],
@@ -45,3 +42,19 @@ def arnparse(arn_str):
4542
resource_type=resource_type,
4643
resource=resource,
4744
)
45+
46+
47+
def _parse_resource(resource):
48+
first_separator_index = -1
49+
for idx, c in enumerate(resource):
50+
if c in (':', '/'):
51+
first_separator_index = idx
52+
break
53+
54+
if first_separator_index != -1:
55+
resource_type = resource[:first_separator_index]
56+
resource = resource[first_separator_index + 1:]
57+
else:
58+
resource_type = None
59+
60+
return resource_type, resource

tests/test_arnparse.py

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,59 @@
11
import pytest
22

33
from arnparse import arnparse
4-
54
from arnparse.arnparse import MalformedArnError
65

76

7+
def test__definition():
8+
arn = arnparse('arn:partition:service:region:account-id:resource')
9+
assert arn.partition == 'partition'
10+
assert arn.service == 'service'
11+
assert arn.region == 'region'
12+
assert arn.account_id == 'account-id'
13+
assert arn.resource_type is None
14+
assert arn.resource == 'resource'
15+
16+
arn = arnparse('arn:partition:service:region:account-id:resourcetype/resource')
17+
assert arn.partition == 'partition'
18+
assert arn.service == 'service'
19+
assert arn.region == 'region'
20+
assert arn.account_id == 'account-id'
21+
assert arn.resource_type == 'resourcetype'
22+
assert arn.resource == 'resource'
23+
24+
arn = arnparse('arn:partition:service:region:account-id:resourcetype/resource/qualifier')
25+
assert arn.partition == 'partition'
26+
assert arn.service == 'service'
27+
assert arn.region == 'region'
28+
assert arn.account_id == 'account-id'
29+
assert arn.resource_type == 'resourcetype'
30+
assert arn.resource == 'resource/qualifier'
31+
32+
arn = arnparse('arn:partition:service:region:account-id:resourcetype/resource:qualifier')
33+
assert arn.partition == 'partition'
34+
assert arn.service == 'service'
35+
assert arn.region == 'region'
36+
assert arn.account_id == 'account-id'
37+
assert arn.resource_type == 'resourcetype'
38+
assert arn.resource == 'resource:qualifier'
39+
40+
arn = arnparse('arn:partition:service:region:account-id:resourcetype:resource')
41+
assert arn.partition == 'partition'
42+
assert arn.service == 'service'
43+
assert arn.region == 'region'
44+
assert arn.account_id == 'account-id'
45+
assert arn.resource_type == 'resourcetype'
46+
assert arn.resource == 'resource'
47+
48+
arn = arnparse('arn:partition:service:region:account-id:resourcetype:resource:qualifier')
49+
assert arn.partition == 'partition'
50+
assert arn.service == 'service'
51+
assert arn.region == 'region'
52+
assert arn.account_id == 'account-id'
53+
assert arn.resource_type == 'resourcetype'
54+
assert arn.resource == 'resource:qualifier'
55+
56+
857
def test__arnparse__resource_type_with_slash():
958
arn_str = 'arn:aws:ec2:us-east-1:123456789012:vpc/vpc-fd580e98'
1059

@@ -243,3 +292,42 @@ def test__s3():
243292
assert arn.account_id is None
244293
assert arn.resource_type is None
245294
assert arn.resource == 'my_corporate_bucket/Development/*'
295+
296+
297+
def test__cloudwatch_logs():
298+
arn_str = 'arn:aws:logs:us-east-1:123456789012:log-group:/aws/lambda/my-function:*'
299+
300+
arn = arnparse(arn_str)
301+
302+
assert arn.partition == 'aws'
303+
assert arn.service == 'logs'
304+
assert arn.region == 'us-east-1'
305+
assert arn.account_id == '123456789012'
306+
assert arn.resource_type == 'log-group'
307+
assert arn.resource == '/aws/lambda/my-function:*'
308+
309+
310+
def test__secrets_manager():
311+
arn_str = 'arn:aws:secretsmanager:us-east-1:123456789012:secret:myfolder/MyFirstSecret-ocq1Wq'
312+
313+
arn = arnparse(arn_str)
314+
315+
assert arn.partition == 'aws'
316+
assert arn.service == 'secretsmanager'
317+
assert arn.region == 'us-east-1'
318+
assert arn.account_id == '123456789012'
319+
assert arn.resource_type == 'secret'
320+
assert arn.resource == 'myfolder/MyFirstSecret-ocq1Wq'
321+
322+
323+
def test__batch():
324+
arn_str = 'arn:aws:batch:us-east-1:123456789012:job-definition/my-job-definition:1'
325+
326+
arn = arnparse(arn_str)
327+
328+
assert arn.partition == 'aws'
329+
assert arn.service == 'batch'
330+
assert arn.region == 'us-east-1'
331+
assert arn.account_id == '123456789012'
332+
assert arn.resource_type == 'job-definition'
333+
assert arn.resource == 'my-job-definition:1'

0 commit comments

Comments
 (0)