diff --git a/cloudsplaining/scan/assume_role_policy_document.py b/cloudsplaining/scan/assume_role_policy_document.py index 1556009e..1492fe7f 100644 --- a/cloudsplaining/scan/assume_role_policy_document.py +++ b/cloudsplaining/scan/assume_role_policy_document.py @@ -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 ( @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/test/scanning/test_trust_policies.py b/test/scanning/test_trust_policies.py index 7156b1e0..c4245f9a 100644 --- a/test/scanning/test_trust_policies.py +++ b/test/scanning/test_trust_policies.py @@ -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, [])