Skip to content

Commit 1cac01f

Browse files
authored
Fixes #225 - S3 conditions querying (#227)
* Fixes #225 - S3 conditions querying * Version bump
1 parent 25a5773 commit 1cac01f

File tree

5 files changed

+82
-10
lines changed

5 files changed

+82
-10
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 0.8.8 (2020-09-15)
4+
* Fixes issue with querying condition keys (#225)
5+
* Adds get_region_from_arn back for our friends at Netflix :)
6+
37
## 0.8.7 (2020-09-06)
48
* Fixes elasticache query issue #223
59

policy_sentry/bin/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"""
33
Policy Sentry is a tool for generating least-privilege IAM Policies.
44
"""
5-
__version__ = "0.8.7"
5+
__version__ = "0.8.8"
66
import click
77
from policy_sentry import command
88

policy_sentry/querying/conditions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ def get_condition_keys_for_service(service_prefix):
2323
"""
2424
results = []
2525
service_prefix_data = get_service_prefix_data(service_prefix)
26-
for resource in service_prefix_data["resources"]:
27-
results.extend(resource["condition_keys"])
26+
for condition_key_entry in service_prefix_data["conditions"]:
27+
results.append(condition_key_entry["condition"])
2828
results = list(dict.fromkeys(results))
2929
return results
3030

policy_sentry/util/arns.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,17 @@ def get_service_from_arn(arn):
163163
return result["service"]
164164

165165

166+
def get_region_from_arn(arn):
167+
"""Given an ARN, return the region in the ARN, if it is available. In certain cases like S3 it is not"""
168+
result = parse_arn(arn)
169+
# Support S3 buckets with no values under region
170+
if result["region"] is None:
171+
result = ""
172+
else:
173+
result = result["region"]
174+
return result
175+
176+
166177
def get_account_from_arn(arn):
167178
"""Given an ARN, return the account ID in the ARN, if it is available. In certain cases like S3 it is not"""
168179
result = parse_arn(arn)

test/querying/test_query_conditions.py

Lines changed: 64 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import unittest
2+
import json
23
from policy_sentry.querying.conditions import (
34
get_condition_keys_for_service,
45
get_condition_key_details,
@@ -12,14 +13,21 @@ class QueryConditionsTestCase(unittest.TestCase):
1213
def test_get_condition_keys_for_service(self):
1314
"""querying.conditions.get_condition_keys_for_service test"""
1415
expected_results = [
15-
'aws:ResourceTag/${TagKey}',
16-
'ram:AllowsExternalPrincipals',
17-
'ram:ResourceShareName',
18-
'ram:PermissionArn'
16+
"aws:RequestTag/${TagKey}",
17+
"aws:ResourceTag/${TagKey}",
18+
"aws:TagKeys",
19+
"ram:AllowsExternalPrincipals",
20+
"ram:PermissionArn",
21+
"ram:Principal",
22+
"ram:RequestedAllowsExternalPrincipals",
23+
"ram:RequestedResourceType",
24+
"ram:ResourceArn",
25+
"ram:ResourceShareName",
26+
"ram:ShareOwnerAccountId"
1927
]
20-
result = get_condition_keys_for_service("ram")
21-
self.assertEqual(result, expected_results)
22-
28+
results = get_condition_keys_for_service("ram")
29+
# print(json.dumps(results, indent=4))
30+
self.assertEqual(results, expected_results)
2331

2432
def test_get_condition_keys_available_to_raw_arn(self):
2533
expected_results = [
@@ -71,3 +79,52 @@ def test_get_condition_value_type(self):
7179
self.maxDiff = None
7280
# print(result)
7381
self.assertEqual(desired_result, result)
82+
83+
def test_gh_225_s3_conditions(self):
84+
"""querying.actions.get_actions_matching_condition_key"""
85+
results = get_condition_keys_for_service("s3")
86+
# print(json.dumps(results, indent=4))
87+
expected_results = [
88+
"aws:RequestTag/${TagKey}",
89+
"aws:ResourceTag/${TagKey}",
90+
"aws:TagKeys",
91+
"s3:AccessPointNetworkOrigin",
92+
"s3:DataAccessPointAccount",
93+
"s3:DataAccessPointArn",
94+
"s3:ExistingJobOperation",
95+
"s3:ExistingJobPriority",
96+
"s3:ExistingObjectTag/<key>",
97+
"s3:JobSuspendedCause",
98+
"s3:LocationConstraint",
99+
"s3:RequestJobOperation",
100+
"s3:RequestJobPriority",
101+
"s3:RequestObjectTag/<key>",
102+
"s3:RequestObjectTagKeys",
103+
"s3:VersionId",
104+
"s3:authType",
105+
"s3:delimiter",
106+
"s3:locationconstraint",
107+
"s3:max-keys",
108+
"s3:object-lock-legal-hold",
109+
"s3:object-lock-mode",
110+
"s3:object-lock-remaining-retention-days",
111+
"s3:object-lock-retain-until-date",
112+
"s3:prefix",
113+
"s3:signatureAge",
114+
"s3:signatureversion",
115+
"s3:versionid",
116+
"s3:x-amz-acl",
117+
"s3:x-amz-content-sha256",
118+
"s3:x-amz-copy-source",
119+
"s3:x-amz-grant-full-control",
120+
"s3:x-amz-grant-read",
121+
"s3:x-amz-grant-read-acp",
122+
"s3:x-amz-grant-write",
123+
"s3:x-amz-grant-write-acp",
124+
"s3:x-amz-metadata-directive",
125+
"s3:x-amz-server-side-encryption",
126+
"s3:x-amz-server-side-encryption-aws-kms-key-id",
127+
"s3:x-amz-storage-class",
128+
"s3:x-amz-website-redirect-location"
129+
]
130+
self.assertListEqual(results, expected_results)

0 commit comments

Comments
 (0)