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

feat: TBD #3735

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions .cfnlintrc.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
templates:
- tests/translator/output/**/*.json
ignore_templates:
- tests/translator/output/**/function_with_function_url_config.json
- tests/translator/output/**/function_with_function_url_config_and_autopublishalias.json
- tests/translator/output/**/function_with_function_url_config_without_cors_config.json
- tests/translator/output/**/error_*.json # Fail by design
- tests/translator/output/**/api_http_paths_with_if_condition.json
- tests/translator/output/**/api_http_paths_with_if_condition_no_value_else_case.json
5 changes: 3 additions & 2 deletions samtranslator/compat.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# mypy: ignore-errors
try:
from pydantic import v1 as pydantic

@@ -7,9 +8,9 @@
except ImportError:
# Unfortunately mypy cannot handle this try/expect pattern, and "type: ignore"
# is the simplest work-around. See: https://github.com/python/mypy/issues/1153
import pydantic # type: ignore
import pydantic
except AttributeError:
# Pydantic v1.10.17+
import pydantic # type: ignore
import pydantic

__all__ = ["pydantic"]
1 change: 1 addition & 0 deletions samtranslator/model/lambda_.py
Original file line number Diff line number Diff line change
@@ -139,6 +139,7 @@ class LambdaPermission(Resource):
"SourceArn": GeneratedProperty(),
"EventSourceToken": GeneratedProperty(),
"FunctionUrlAuthType": GeneratedProperty(),
"InvokedViaFunctionUrl": GeneratedProperty(),
}


47 changes: 44 additions & 3 deletions samtranslator/model/sam_resources.py
Original file line number Diff line number Diff line change
@@ -321,8 +321,12 @@ def to_cloudformation(self, **kwargs): # type: ignore[no-untyped-def] # noqa: P
lambda_url = self._construct_function_url(lambda_function, lambda_alias, self.FunctionUrlConfig)
resources.append(lambda_url)
url_permission = self._construct_url_permission(lambda_function, lambda_alias, self.FunctionUrlConfig)
if url_permission:
invoke_dual_auth_permission = self._construct_invoke_dual_auth_permission(
lambda_function, lambda_alias, self.FunctionUrlConfig
)
if url_permission and invoke_dual_auth_permission:
resources.append(url_permission)
resources.append(invoke_dual_auth_permission)

self._validate_deployment_preference_and_add_update_policy(
kwargs.get("deployment_preference_collection", None),
@@ -332,7 +336,6 @@ def to_cloudformation(self, **kwargs): # type: ignore[no-untyped-def] # noqa: P
self.get_passthrough_resource_attributes(),
feature_toggle,
)

event_invoke_policies: List[Dict[str, Any]] = []
if self.EventInvokeConfig:
function_name = lambda_function.logical_id
@@ -1210,7 +1213,7 @@ def _construct_url_permission(
lambda_function : LambdaUrl
Lambda Function resource

llambda_alias : LambdaAlias
lambda_alias : LambdaAlias
Lambda Alias resource

Returns
@@ -1234,6 +1237,44 @@ def _construct_url_permission(
lambda_permission.FunctionUrlAuthType = auth_type
return lambda_permission

def _construct_invoke_dual_auth_permission(
self, lambda_function: LambdaFunction, lambda_alias: Optional[LambdaAlias], function_url_config: Dict[str, Any]
) -> Optional[LambdaPermission]:
"""
Construct the lambda permission associated with the function invoke resource in a case
for public access when AuthType is NONE

Parameters
----------
lambda_function : LambdaUrl
Lambda Function resource

lambda_alias : LambdaAlias
Lambda Alias resource

Returns
-------
LambdaPermission
The lambda permission appended to a function that allow function invoke only from Function URL
"""
# create lambda:InvokeFunction with InvokedViaFunctionUrl=True
auth_type = function_url_config.get("AuthType")

if auth_type not in ["NONE"] or is_intrinsic(function_url_config):
return None

logical_id = f"{lambda_function.logical_id}URLInvokeAllowPublicAccess"
lambda_permission_attributes = self.get_passthrough_resource_attributes()
lambda_invoke_permission = LambdaPermission(logical_id=logical_id, attributes=lambda_permission_attributes)
lambda_invoke_permission.Action = "lambda:InvokeFunction"
lambda_invoke_permission.Principal = "*"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not new in the PR, but I am curious why the service principal is *.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sync offline, this is by design meant for public (whoever).

lambda_invoke_permission.FunctionName = (
lambda_alias.get_runtime_attr("arn") if lambda_alias else lambda_function.get_runtime_attr("name")
)
lambda_invoke_permission.InvokedViaFunctionUrl = True

return lambda_invoke_permission


class SamApi(SamResourceMacro):
"""SAM rest API macro."""
28 changes: 23 additions & 5 deletions tests/model/test_sam_resources.py
Original file line number Diff line number Diff line change
@@ -583,11 +583,29 @@ def test_with_valid_function_url_config_with_lambda_permission(self):

cfnResources = function.to_cloudformation(**self.kwargs)
generatedUrlList = [x for x in cfnResources if isinstance(x, LambdaPermission)]
self.assertEqual(generatedUrlList.__len__(), 1)
self.assertEqual(generatedUrlList[0].Action, "lambda:InvokeFunctionUrl")
self.assertEqual(generatedUrlList[0].FunctionName, {"Ref": "foo"})
self.assertEqual(generatedUrlList[0].Principal, "*")
self.assertEqual(generatedUrlList[0].FunctionUrlAuthType, "NONE")
self.assertEqual(generatedUrlList.__len__(), 2)
for permission in generatedUrlList:
self.assertEqual(permission.FunctionName, {"Ref": "foo"})
self.assertEqual(permission.Principal, "*")
self.assertTrue(permission.Action in ["lambda:InvokeFunctionUrl", "lambda:InvokeFunction"])
if permission.Action == "lambda:InvokeFunctionUrl":
self.assertEqual(permission.FunctionUrlAuthType, "NONE")
if permission.Action == "lambda:InvokeFunction":
self.assertEqual(permission.InvokedViaFunctionUrl, True)

@patch("boto3.session.Session.region_name", "ap-southeast-1")
def test_with_aws_iam_function_url_config_with_lambda_permission(self):
function = SamFunction("foo")
function.CodeUri = "s3://foobar/foo.zip"
function.Runtime = "foo"
function.Handler = "bar"
# When create FURL with AWS_IAM
function.FunctionUrlConfig = {"AuthType": "AWS_IAM"}

cfnResources = function.to_cloudformation(**self.kwargs)
generatedUrlList = [x for x in cfnResources if isinstance(x, LambdaPermission)]
# Then no permisssion should be auto created
self.assertEqual(generatedUrlList.__len__(), 0)

@patch("boto3.session.Session.region_name", "ap-southeast-1")
def test_with_invalid_function_url_config_with_authorization_type_value_as_None(self):
Original file line number Diff line number Diff line change
@@ -58,6 +58,17 @@
},
"Type": "AWS::IAM::Role"
},
"MyFunctionURLInvokeAllowPublicAccess": {
"Properties": {
"Action": "lambda:InvokeFunction",
"FunctionName": {
"Ref": "MyFunction"
},
"InvokedViaFunctionUrl": true,
"Principal": "*"
},
"Type": "AWS::Lambda::Permission"
},
"MyFunctionUrl": {
"Properties": {
"AuthType": "NONE",
Original file line number Diff line number Diff line change
@@ -73,6 +73,17 @@
},
"Type": "AWS::IAM::Role"
},
"MyFunctionURLInvokeAllowPublicAccess": {
"Properties": {
"Action": "lambda:InvokeFunction",
"FunctionName": {
"Ref": "MyFunctionAliaslive"
},
"InvokedViaFunctionUrl": true,
"Principal": "*"
},
"Type": "AWS::Lambda::Permission"
},
"MyFunctionUrl": {
"Properties": {
"AuthType": "NONE",
Original file line number Diff line number Diff line change
@@ -68,6 +68,18 @@
},
"Type": "AWS::IAM::Role"
},
"MyFunctionURLInvokeAllowPublicAccess": {
"Condition": "MyCondition",
"Properties": {
"Action": "lambda:InvokeFunction",
"FunctionName": {
"Ref": "MyFunction"
},
"InvokedViaFunctionUrl": true,
"Principal": "*"
},
"Type": "AWS::Lambda::Permission"
},
"MyFunctionUrl": {
"Condition": "MyCondition",
"Properties": {
Original file line number Diff line number Diff line change
@@ -58,6 +58,17 @@
},
"Type": "AWS::IAM::Role"
},
"MyFunctionURLInvokeAllowPublicAccess": {
"Properties": {
"Action": "lambda:InvokeFunction",
"FunctionName": {
"Ref": "MyFunction"
},
"InvokedViaFunctionUrl": true,
"Principal": "*"
},
"Type": "AWS::Lambda::Permission"
},
"MyFunctionUrl": {
"Properties": {
"AuthType": "NONE",
Original file line number Diff line number Diff line change
@@ -58,6 +58,17 @@
},
"Type": "AWS::IAM::Role"
},
"MyFunctionURLInvokeAllowPublicAccess": {
"Properties": {
"Action": "lambda:InvokeFunction",
"FunctionName": {
"Ref": "MyFunction"
},
"InvokedViaFunctionUrl": true,
"Principal": "*"
},
"Type": "AWS::Lambda::Permission"
},
"MyFunctionUrl": {
"Properties": {
"AuthType": "NONE",
Original file line number Diff line number Diff line change
@@ -73,6 +73,17 @@
},
"Type": "AWS::IAM::Role"
},
"MyFunctionURLInvokeAllowPublicAccess": {
"Properties": {
"Action": "lambda:InvokeFunction",
"FunctionName": {
"Ref": "MyFunctionAliaslive"
},
"InvokedViaFunctionUrl": true,
"Principal": "*"
},
"Type": "AWS::Lambda::Permission"
},
"MyFunctionUrl": {
"Properties": {
"AuthType": "NONE",
Original file line number Diff line number Diff line change
@@ -68,6 +68,18 @@
},
"Type": "AWS::IAM::Role"
},
"MyFunctionURLInvokeAllowPublicAccess": {
"Condition": "MyCondition",
"Properties": {
"Action": "lambda:InvokeFunction",
"FunctionName": {
"Ref": "MyFunction"
},
"InvokedViaFunctionUrl": true,
"Principal": "*"
},
"Type": "AWS::Lambda::Permission"
},
"MyFunctionUrl": {
"Condition": "MyCondition",
"Properties": {
Original file line number Diff line number Diff line change
@@ -58,6 +58,17 @@
},
"Type": "AWS::IAM::Role"
},
"MyFunctionURLInvokeAllowPublicAccess": {
"Properties": {
"Action": "lambda:InvokeFunction",
"FunctionName": {
"Ref": "MyFunction"
},
"InvokedViaFunctionUrl": true,
"Principal": "*"
},
"Type": "AWS::Lambda::Permission"
},
"MyFunctionUrl": {
"Properties": {
"AuthType": "NONE",
11 changes: 11 additions & 0 deletions tests/translator/output/function_with_function_url_config.json
Original file line number Diff line number Diff line change
@@ -58,6 +58,17 @@
},
"Type": "AWS::IAM::Role"
},
"MyFunctionURLInvokeAllowPublicAccess": {
"Properties": {
"Action": "lambda:InvokeFunction",
"FunctionName": {
"Ref": "MyFunction"
},
"InvokedViaFunctionUrl": true,
"Principal": "*"
},
"Type": "AWS::Lambda::Permission"
},
"MyFunctionUrl": {
"Properties": {
"AuthType": "NONE",
Original file line number Diff line number Diff line change
@@ -73,6 +73,17 @@
},
"Type": "AWS::IAM::Role"
},
"MyFunctionURLInvokeAllowPublicAccess": {
"Properties": {
"Action": "lambda:InvokeFunction",
"FunctionName": {
"Ref": "MyFunctionAliaslive"
},
"InvokedViaFunctionUrl": true,
"Principal": "*"
},
"Type": "AWS::Lambda::Permission"
},
"MyFunctionUrl": {
"Properties": {
"AuthType": "NONE",
Original file line number Diff line number Diff line change
@@ -68,6 +68,18 @@
},
"Type": "AWS::IAM::Role"
},
"MyFunctionURLInvokeAllowPublicAccess": {
"Condition": "MyCondition",
"Properties": {
"Action": "lambda:InvokeFunction",
"FunctionName": {
"Ref": "MyFunction"
},
"InvokedViaFunctionUrl": true,
"Principal": "*"
},
"Type": "AWS::Lambda::Permission"
},
"MyFunctionUrl": {
"Condition": "MyCondition",
"Properties": {
Original file line number Diff line number Diff line change
@@ -58,6 +58,17 @@
},
"Type": "AWS::IAM::Role"
},
"MyFunctionURLInvokeAllowPublicAccess": {
"Properties": {
"Action": "lambda:InvokeFunction",
"FunctionName": {
"Ref": "MyFunction"
},
"InvokedViaFunctionUrl": true,
"Principal": "*"
},
"Type": "AWS::Lambda::Permission"
},
"MyFunctionUrl": {
"Properties": {
"AuthType": "NONE",