Skip to content

Commit a9d959d

Browse files
committed
Unexpected output when working with ARNs that have a path in them
1 parent b280b0d commit a9d959d

File tree

3 files changed

+24
-17
lines changed

3 files changed

+24
-17
lines changed

policy_sentry/util/arns.py

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -107,32 +107,39 @@ def same_resource_type(self, arn_in_database):
107107
if i != "":
108108
non_empty_arn_format_list.append(i)
109109

110+
lower_resource_string = list(map(lambda x:x.lower(),split_resource_string_to_test))
110111
for i in non_empty_arn_format_list:
111112
# if i.lower() in [x.lower() for x in split_resource_string_to_test]:
112-
if i.lower() not in split_resource_string_to_test:
113+
if i.lower() not in lower_resource_string:
113114
return False
114115

115116
# 4c: See if the non-normalized fields match
116-
# for i in range(len(arn_format_list)):
117-
# # If the field is not normalized to empty string, then make sure the resource type segments match
118-
# # So, using table/${TableName}/backup/${BackupName} as an example:
119-
# # table should match, backup should match,
120-
# # and length of the arn_format_list should be the same as split_resource_string_to_test
121-
# # If all conditions match, then the ARN format is the same.
122-
# if arn_format_list[i] != "":
123-
# if arn_format_list[i] == split_resource_string_to_test[i]:
124-
# pass
125-
# elif split_resource_string_to_test[i] == "*":
126-
# pass
127-
# else:
128-
# return False
117+
for i in range(len(arn_format_list)):
118+
# If the field is not normalized to empty string, then make sure the resource type segments match
119+
# So, using table/${TableName}/backup/${BackupName} as an example:
120+
# table should match, backup should match,
121+
# and length of the arn_format_list should be the same as split_resource_string_to_test
122+
# If all conditions match, then the ARN format is the same.
123+
if arn_format_list[i] != "":
124+
if arn_format_list[i] == split_resource_string_to_test[i]:
125+
pass
126+
elif split_resource_string_to_test[i] == "*":
127+
pass
128+
else:
129+
return False
129130

130131
# 4. Special type for S3 bucket objects and CodeCommit repos
131132
# Note: Each service can only have one of these, so these are definitely exceptions
132-
exclusion_list = ["${ObjectName}", "${RepositoryName}", "${BucketName}"]
133+
exclusion_list = ["${ObjectName}", "${RepositoryName}", "${BucketName}", "table/${TableName}"]
133134
resource_path_arn_in_database = elements[5]
134135
if resource_path_arn_in_database in exclusion_list:
135136
logger.debug("Special type: %s", resource_path_arn_in_database)
137+
# handling special case table/${TableName}
138+
if resource_string_arn_in_database == "table/${TableName}":
139+
if len(self.resource_string.split('/')) == len(elements[5].split('/')):
140+
return True
141+
else:
142+
return False
136143
# If we've made it this far, then it is a special type
137144
# return True
138145
# Presence of / would mean it's an object in both so it matches

test/util/test_arns.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ class ArnPathTestCase(unittest.TestCase):
151151
# When paths are used
152152
def test_ssm_paths(self):
153153
parameter_1 = ARN("arn:aws:ssm:::parameter/dev/foo/bar*")
154-
parameter_2 = ARN("arn:aws:ssm:::parameter/dev")
154+
parameter_2 = "arn:aws:ssm:::parameter/dev"
155155
print(parameter_1.same_resource_type(parameter_2))
156156
self.assertTrue(parameter_1.same_resource_type(parameter_2))
157157

test/writing/test_write_policy_library_usage.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,4 +360,4 @@ def test_gh_237_ssm_arns_with_paths(self):
360360
# result = write_policy_with_template(crud_template)
361361
# print(json.dumps(result, indent=4))
362362
arn = ARN("arn:aws:ssm:::parameter/dev/foo/bar*")
363-
print(arn.same_resource_type("arn:aws:ssm:::parameter/dev"))
363+
self.assertTrue(arn.same_resource_type("arn:aws:ssm:::parameter/dev"))

0 commit comments

Comments
 (0)