Skip to content

Commit 803eab2

Browse files
authored
fix incorrect actions when querying for catalog resources (#564)
* fix incorrect actions when querying for catalog resources * also fix action table
1 parent f28365d commit 803eab2

File tree

7 files changed

+178
-144
lines changed

7 files changed

+178
-144
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ permissions:
1414

1515
env:
1616
JUST_VERSION: '1'
17-
PREK_VERSION: '0.2.12'
17+
PREK_VERSION: '0.2.13'
1818
TERRAFORM_VERSION: '1.13'
1919

2020
jobs:

policy_sentry/command/query.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,12 @@ def query_action_table(
148148
else:
149149
# Otherwise, leverage the datastore inside the python package
150150
logger.debug("Leveraging the bundled IAM Definition.")
151+
152+
if service == "catalog":
153+
# make sure nothing is returned when querying for `catalog`,
154+
# which has some special behaviour related to `servicecatalog`
155+
service = ""
156+
151157
# Actions on all services
152158
if service == "all":
153159
all_services = get_all_service_prefixes()

policy_sentry/querying/actions.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,12 @@ def get_action_data(service: str, action_name: str) -> dict[str, list[dict[str,
5959
Returns:
6060
List: A dictionary containing metadata about an IAM Action.
6161
"""
62-
action_data_results = {}
62+
action_data_results: dict[str, list[dict[str, Any]]] = {}
6363
try:
6464
service_prefix_data = get_service_prefix_data(service)
65+
if not service_prefix_data:
66+
return action_data_results
67+
6568
if action_name.endswith("*"):
6669
stripped_action_name = action_name.removesuffix("*")
6770
results = []
@@ -239,6 +242,8 @@ def get_actions_with_arn_type_and_access_level(
239242
results.extend(actions)
240243
else:
241244
service_prefix_data = get_service_prefix_data(service_prefix)
245+
# mainly needed for the use case of `catalog` -> `servicecatalog` mapping
246+
service_prefix = service_prefix_data["prefix"]
242247
for action_name, action_data in service_prefix_data["privileges"].items():
243248
if (
244249
action_data["access_level"] == access_level

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ Twitter = "https://twitter.com/kmcquade3"
4646
dev = [
4747
"coverage>=7.11.0,<8.0.0",
4848
"mypy>=1.18.0,<2.0.0",
49-
"prek>=0.2.10,<0.3.0",
49+
"prek>=0.2.13,<0.3.0",
5050
"pytest>=8.4.0,<9.0.0",
5151
"rust-just>=1.43.0,<2.0.0",
5252
"types-beautifulsoup4>=4.12.0,<5.0.0",

test/querying/test_all.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def test_GH_296_query_all_actions_with_wildcard_resources(self):
5050
access_level=None,
5151
condition=None,
5252
)
53-
self.assertTrue(len(result) > 3000)
53+
self.assertGreater(len(result), 3000)
5454

5555
def test_get_service_authorization_url(self):
5656
result = get_service_authorization_url("a4b")
@@ -59,3 +59,19 @@ def test_get_service_authorization_url(self):
5959
"https://docs.aws.amazon.com/service-authorization/latest/reference/list_alexaforbusiness.html"
6060
)
6161
self.assertTrue(result == expected_result)
62+
63+
def test_query_catalog_service(self):
64+
# given
65+
service = "catalog"
66+
67+
# when
68+
result = query_action_table(
69+
service=service,
70+
resource_type=None,
71+
name=None,
72+
access_level=None,
73+
condition=None,
74+
)
75+
76+
# then
77+
self.assertFalse(result)

test/querying/test_query_actions.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,13 @@ def test_get_actions_with_arn_type_and_access_level_case_5(self):
346346
output = get_actions_with_arn_type_and_access_level("s3", "object", "List")
347347
self.assertTrue("s3:ListMultipartUploadParts" in output)
348348

349+
def test_get_actions_with_arn_type_and_access_level_servicecatalog(self):
350+
# querying with `catalog` as a service prefix should yield `servicecatalog` as an action prefix
351+
# https://github.com/salesforce/policy_sentry/issues/563
352+
353+
output = get_actions_with_arn_type_and_access_level("catalog", "Portfolio", "List")
354+
self.assertIn("servicecatalog:ListPortfolioAccess", output)
355+
349356
def test_get_actions_matching_arn_type_case_1(self):
350357
"""querying.actions.get_actions_matching_arn_type"""
351358
expected_results = [

0 commit comments

Comments
 (0)