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

refactor: preliminary changes for managed policy improvements #2910

Merged
merged 5 commits into from
Feb 15, 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
1 change: 1 addition & 0 deletions .cfnlintrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ ignore_templates:
- tests/translator/output/**/state_machine_with_schedule_dlq_retry_policy.json
- tests/translator/output/**/globals_for_function.json # RuntimeManagementConfig
- tests/translator/output/**/function_with_runtime_config.json # RuntimeManagementConfig
- tests/translator/output/**/managed_policies_minimal.json # Intentionally has non-existent managed policy name
ignore_checks:
- E2531 # Deprecated runtime; not relevant for transform tests
- W2531 # EOL runtime; not relevant for transform tests
Expand Down
9 changes: 7 additions & 2 deletions samtranslator/model/sam_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,10 @@ def to_cloudformation(self, **kwargs): # type: ignore[no-untyped-def]

execution_role = None
if lambda_function.Role is None:
execution_role = self._construct_role(managed_policy_map, event_invoke_policies)
execution_role = self._construct_role(
managed_policy_map,
event_invoke_policies,
)
lambda_function.Role = execution_role.get_runtime_attr("arn")
resources.append(execution_role)

Expand Down Expand Up @@ -559,7 +562,9 @@ def _add_event_invoke_managed_policy(
return {}

def _construct_role(
self, managed_policy_map: Dict[str, Any], event_invoke_policies: List[Dict[str, Any]]
self,
managed_policy_map: Dict[str, Any],
event_invoke_policies: List[Dict[str, Any]],
) -> IAMRole:
"""Constructs a Lambda execution role based on this SAM function's Policies property.

Expand Down
6 changes: 4 additions & 2 deletions samtranslator/translator/managed_policy_translator.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
from typing import Dict, cast

from samtranslator.metrics.method_decorator import cw_timer

Expand Down Expand Up @@ -30,7 +31,8 @@ def _load_policies_from_iam(self): # type: ignore[no-untyped-def]
LOG.info("Finished loading policies from IAM.")
self._policy_map = name_to_arn_map

def load(self): # type: ignore[no-untyped-def]
def load(self) -> Dict[str, str]:
if self._policy_map is None:
self._load_policies_from_iam()
return self._policy_map
# mypy doesn't realize that function above assigns non-None value
return cast(Dict[str, str], self._policy_map)
5 changes: 4 additions & 1 deletion samtranslator/translator/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ def transform(input_fragment, parameter_values, managed_policy_loader, feature_t

sam_parser = Parser()
to_py27_compatible_template(input_fragment, parameter_values)
translator = Translator(managed_policy_loader.load(), sam_parser) # type: ignore[no-untyped-call]
translator = Translator( # type: ignore[no-untyped-call]
managed_policy_loader.load(),
sam_parser,
)
transformed = translator.translate(
input_fragment,
parameter_values=parameter_values,
Expand Down
9 changes: 8 additions & 1 deletion samtranslator/translator/translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,14 @@
class Translator:
"""Translates SAM templates into CloudFormation templates"""

def __init__(self, managed_policy_map, sam_parser, plugins=None, boto_session=None, metrics=None): # type: ignore[no-untyped-def]
def __init__( # type: ignore[no-untyped-def]
self,
managed_policy_map,
sam_parser,
plugins=None,
boto_session=None,
metrics=None,
):
"""
:param dict managed_policy_map: Map of managed policy names to the ARNs
:param sam_parser: Instance of a SAM Parser
Expand Down
18 changes: 18 additions & 0 deletions tests/translator/input/managed_policies_minimal.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Resources:
MyFunction:
Type: AWS::Serverless::Function
Properties:
Runtime: python3.8
Handler: foo
InlineCode: bar
Policies:
- AnyNonOfficialManagedPolicy
- AmazonS3FullAccess

MyStateMachine:
Type: AWS::Serverless::StateMachine
Properties:
DefinitionUri: s3://foo/bar
Policies:
- AnyNonOfficialManagedPolicy
- AmazonS3FullAccess
110 changes: 110 additions & 0 deletions tests/translator/output/aws-cn/managed_policies_minimal.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
{
"Resources": {
"MyFunction": {
"Properties": {
"Code": {
"ZipFile": "bar"
},
"Handler": "foo",
"Role": {
"Fn::GetAtt": [
"MyFunctionRole",
"Arn"
]
},
"Runtime": "python3.8",
"Tags": [
{
"Key": "lambda:createdBy",
"Value": "SAM"
}
]
},
"Type": "AWS::Lambda::Function"
},
"MyFunctionRole": {
"Properties": {
"AssumeRolePolicyDocument": {
"Statement": [
{
"Action": [
"sts:AssumeRole"
],
"Effect": "Allow",
"Principal": {
"Service": [
"lambda.amazonaws.com"
]
}
}
],
"Version": "2012-10-17"
},
"ManagedPolicyArns": [
"arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole",
"AnyNonOfficialManagedPolicy",
"AmazonS3FullAccess"
],
"Tags": [
{
"Key": "lambda:createdBy",
"Value": "SAM"
}
]
},
"Type": "AWS::IAM::Role"
},
"MyStateMachine": {
"Properties": {
"DefinitionS3Location": {
"Bucket": "foo",
"Key": "bar"
},
"RoleArn": {
"Fn::GetAtt": [
"MyStateMachineRole",
"Arn"
]
},
"Tags": [
{
"Key": "stateMachine:createdBy",
"Value": "SAM"
}
]
},
"Type": "AWS::StepFunctions::StateMachine"
},
"MyStateMachineRole": {
"Properties": {
"AssumeRolePolicyDocument": {
"Statement": [
{
"Action": [
"sts:AssumeRole"
],
"Effect": "Allow",
"Principal": {
"Service": [
"states.amazonaws.com"
]
}
}
],
"Version": "2012-10-17"
},
"ManagedPolicyArns": [
"AnyNonOfficialManagedPolicy",
"AmazonS3FullAccess"
],
"Tags": [
{
"Key": "stateMachine:createdBy",
"Value": "SAM"
}
]
},
"Type": "AWS::IAM::Role"
}
}
}
110 changes: 110 additions & 0 deletions tests/translator/output/aws-us-gov/managed_policies_minimal.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
{
"Resources": {
"MyFunction": {
"Properties": {
"Code": {
"ZipFile": "bar"
},
"Handler": "foo",
"Role": {
"Fn::GetAtt": [
"MyFunctionRole",
"Arn"
]
},
"Runtime": "python3.8",
"Tags": [
{
"Key": "lambda:createdBy",
"Value": "SAM"
}
]
},
"Type": "AWS::Lambda::Function"
},
"MyFunctionRole": {
"Properties": {
"AssumeRolePolicyDocument": {
"Statement": [
{
"Action": [
"sts:AssumeRole"
],
"Effect": "Allow",
"Principal": {
"Service": [
"lambda.amazonaws.com"
]
}
}
],
"Version": "2012-10-17"
},
"ManagedPolicyArns": [
"arn:aws-us-gov:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole",
"AnyNonOfficialManagedPolicy",
"AmazonS3FullAccess"
],
"Tags": [
{
"Key": "lambda:createdBy",
"Value": "SAM"
}
]
},
"Type": "AWS::IAM::Role"
},
"MyStateMachine": {
"Properties": {
"DefinitionS3Location": {
"Bucket": "foo",
"Key": "bar"
},
"RoleArn": {
"Fn::GetAtt": [
"MyStateMachineRole",
"Arn"
]
},
"Tags": [
{
"Key": "stateMachine:createdBy",
"Value": "SAM"
}
]
},
"Type": "AWS::StepFunctions::StateMachine"
},
"MyStateMachineRole": {
"Properties": {
"AssumeRolePolicyDocument": {
"Statement": [
{
"Action": [
"sts:AssumeRole"
],
"Effect": "Allow",
"Principal": {
"Service": [
"states.amazonaws.com"
]
}
}
],
"Version": "2012-10-17"
},
"ManagedPolicyArns": [
"AnyNonOfficialManagedPolicy",
"AmazonS3FullAccess"
],
"Tags": [
{
"Key": "stateMachine:createdBy",
"Value": "SAM"
}
]
},
"Type": "AWS::IAM::Role"
}
}
}
Loading