Skip to content

Commit 8afb228

Browse files
authored
[LPTOCPCI-1164] Add pagination when retrieving roles from AWS (#413)
* Add pagination when retrieving roles from AWS. Previous limit was 100 roles. * Changes per review suggestions * Update get_roles function to remove mutable default arg * Add changes per PR review. Fixes potential infinite loop edge case. * Fix docstring * Make changes per PR review suggestions * Don't set is_truncated, only set marker var in while loop * Move max items variables to function, remove from global
1 parent cc17279 commit 8afb228

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

clouds/aws/roles/roles.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,31 @@ def iam_client(region=DEFAULT_AWS_REGION):
1919
return boto3.client(service_name="iam", region_name=region)
2020

2121

22+
def get_roles(client=None):
23+
"""
24+
Get all IAM roles.
25+
26+
Args:
27+
client (botocore.client.IAM, optional): A boto3 client for IAM. Defaults to None.
28+
29+
Returns:
30+
List[Dict[Any, Any]]: A list of IAM roles
31+
"""
32+
LOGGER.info("Retrieving all roles from IAM.")
33+
34+
iam_max_items = 1000
35+
client = client or iam_client()
36+
response = client.list_roles(MaxItems=iam_max_items)
37+
roles = response["Roles"]
38+
39+
while response["IsTruncated"]:
40+
marker = response["Marker"]
41+
response = client.list_roles(Marker=marker, MaxItems=iam_max_items)
42+
roles.extend(response["Roles"])
43+
44+
return roles
45+
46+
2247
def create_or_update_role_policy(role_name, policy_name, policy_document, region=DEFAULT_AWS_REGION):
2348
"""
2449
Create a new policy role or update an existing one.

0 commit comments

Comments
 (0)