Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions cloudsplaining/scan/assume_role_policy_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from __future__ import annotations

import logging
from fnmatch import fnmatchcase
from typing import Any

from cloudsplaining.scan.resource_policy_document import (
Expand Down Expand Up @@ -124,12 +125,19 @@ def _assume_role_actions(self) -> list[str]:

return [actions]

def _allows_assume_role(self) -> bool:
"""Return whether an action pattern includes sts:AssumeRole.

IAM wildcards such as ``sts:*``, ``sts:AssumeRole*``, and ``*`` cover
``sts:AssumeRole``. An exact string match misses those trust policies.
"""
return any(fnmatchcase("sts:assumerole", action.lower()) for action in self.actions)

@property
def role_assumable_by_compute_services(self) -> list[str]:
"""Determines whether or not the role is assumed from a compute service, and if so which ones."""
# sts:AssumeRole must be there
lowercase_actions = [x.lower() for x in self.actions]
if "sts:AssumeRole".lower() not in lowercase_actions:
if not self._allows_assume_role():
return []

# Effect must be Allow
Expand All @@ -148,8 +156,7 @@ def role_assumable_by_compute_services(self) -> list[str]:
def role_assumable_by_cross_account_principals(self) -> list[str]:
"""Determines whether or not the role can be assumed from principals in other accounts, and if so which ones."""
# sts:AssumeRole must be there
lowercase_actions = [x.lower() for x in self.actions]
if "sts:AssumeRole".lower() not in lowercase_actions:
if not self._allows_assume_role():
return []

# Effect must be Allow
Expand All @@ -168,8 +175,7 @@ def role_assumable_by_cross_account_principals(self) -> list[str]:
def role_assumable_by_any_principal(self) -> list[str]:
"""Determines whether or not the role can be assumed by any principal (*) or any AWS account root."""
# sts:AssumeRole must be there
lowercase_actions = [x.lower() for x in self.actions]
if "sts:AssumeRole".lower() not in lowercase_actions:
if not self._allows_assume_role():
return []

# Effect must be Allow
Expand All @@ -187,8 +193,7 @@ def role_assumable_by_any_principal(self) -> list[str]:
def role_assumable_by_any_principal_with_conditions(self) -> list[str]:
"""Determines whether or not the role can be assumed by any principal (*) or any AWS account root with conditions."""
# sts:AssumeRole must be there
lowercase_actions = [x.lower() for x in self.actions]
if "sts:AssumeRole".lower() not in lowercase_actions:
if not self._allows_assume_role():
return []

# Effect must be Allow
Expand Down
42 changes: 42 additions & 0 deletions test/scanning/test_trust_policies.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,3 +456,45 @@ def test_assume_role_assumable_by_any_principal_with_conditions(self):
policy_doc = AssumeRolePolicyDocument(policy)
expected_policy = ["*", "arn:aws:iam::*:root"]
self.assertListEqual(policy_doc.role_assumable_by_any_principal_with_conditions, expected_policy)

def test_wildcard_actions_cover_assume_role(self):
"""sts:*, sts:AssumeRole*, and * include sts:AssumeRole."""
for action in ["sts:AssumeRole", "sts:*", "sts:AssumeRole*", "*"]:
any_principal = AssumeRoleStatement(
dict(Effect="Allow", Principal={"AWS": "*"}, Action=action)
)
self.assertListEqual(any_principal.role_assumable_by_any_principal, ["*"])

cross_account = AssumeRoleStatement(
dict(
Effect="Allow",
Principal={"AWS": "arn:aws:iam::999988887777:root"},
Action=action,
),
current_account_id="111122223333",
)
self.assertListEqual(
cross_account.role_assumable_by_cross_account_principals,
["arn:aws:iam::999988887777:root"],
)

compute = AssumeRoleStatement(
dict(Effect="Allow", Principal={"Service": "ec2.amazonaws.com"}, Action=action)
)
self.assertListEqual(compute.role_assumable_by_compute_services, ["ec2"])

with_condition = AssumeRoleStatement(
dict(
Effect="Allow",
Principal={"AWS": "*"},
Action=action,
Condition={"StringEquals": {"sts:ExternalId": "unique-id"}},
)
)
self.assertListEqual(with_condition.role_assumable_by_any_principal, [])
self.assertListEqual(with_condition.role_assumable_by_any_principal_with_conditions, ["*"])

not_assume_role = AssumeRoleStatement(
dict(Effect="Allow", Principal={"AWS": "*"}, Action="sts:AssumeRoleWithSAML")
)
self.assertListEqual(not_assume_role.role_assumable_by_any_principal, [])