Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: do not load managed policies if already ARN #2964

Merged
merged 3 commits into from
Feb 27, 2023
Merged
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
6 changes: 6 additions & 0 deletions samtranslator/model/role_utils/role_constructor.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ def _get_managed_policy_arn(
if arn:
return arn

# If it's already an ARN, we're done
# https://docs.aws.amazon.com/general/latest/gr/aws-arns-and-namespaces.html
is_arn = name.startswith("arn:")
if is_arn:
return name

# Caller-provided function to get managed policy map (fallback)
if get_managed_policy_map:
fallback_managed_policy_map = get_managed_policy_map()
Expand Down
55 changes: 55 additions & 0 deletions tests/translator/test_translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,61 @@ def get_managed_policy_map():
self.assertEqual(function_arn, expected_arn)
self.assertEqual(sfn_arn, expected_arn)

# test to make sure with arn it doesnt load, with non-arn it does
@parameterized.expand(
[
([""], 1),
(["SomeNonArnThing"], 1),
(["SomeNonArnThing", "AnotherNonArnThing"], 1),
(["aws:looks:like:an:ARN:but-not-really"], 1),
(["arn:looks:like:an:ARN:foo", "Mixing_things_v2"], 1),
(["arn:looks:like:an:ARN:foo"], 0),
([{"Ref": "Foo"}], 0),
([{"SQSPollerPolicy": {"QueueName": "Bar"}}], 0),
(["arn:looks:like:an:ARN", "arn:aws:ec2:us-east-1:123456789012:vpc/vpc-0e9801d129EXAMPLE"], 0),
]
)
@patch("boto3.session.Session.region_name", "ap-southeast-1")
@patch("botocore.client.ClientEndpointBridge._check_default_region", mock_get_region)
def test_managed_policies_arn_not_loaded(self, policies, load_policy_count):
class ManagedPolicyLoader:
def __init__(self):
self.call_count = 0

def load(self):
self.call_count += 1
return {}

managed_policy_loader = ManagedPolicyLoader()

with patch("samtranslator.internal.managed_policies._BUNDLED_MANAGED_POLICIES", {}):
transform(
{
"Resources": {
"MyFunction": {
"Type": "AWS::Serverless::Function",
"Properties": {
"Handler": "foo",
"InlineCode": "bar",
"Runtime": "nodejs14.x",
"Policies": policies,
},
},
"MyStateMachine": {
"Type": "AWS::Serverless::StateMachine",
"Properties": {
"DefinitionUri": "s3://egg/baz",
"Policies": policies,
},
},
}
},
{},
managed_policy_loader,
)

self.assertEqual(load_policy_count, managed_policy_loader.call_count)

@parameterized.expand(
[
# All combinations, bundled map takes precedence
Expand Down