Fix lambda function name validation for CreateFunction by joe4dev · Pull Request #9825 · localstack/localstack · GitHub
Skip to content
This repository was archived by the owner on Mar 23, 2026. It is now read-only.

Fix lambda function name validation for CreateFunction#9825

Merged
joe4dev merged 2 commits into
masterfrom
fix-lambda-function-name-validation
Dec 11, 2023
Merged

Fix lambda function name validation for CreateFunction#9825
joe4dev merged 2 commits into
masterfrom
fix-lambda-function-name-validation

Conversation

@joe4dev

@joe4dev joe4dev commented Dec 8, 2023

Copy link
Copy Markdown
Member

Motivation

The FunctionName in the Lambda CreateFunction operation is currently not validated and cannot handle function ARNs.
The AWS API reference describes that the operation can accept the following options for FunctionName:

  • Function name – my-function.
  • Function ARN – arn:aws:lambda:us-west-2:123456789012:function:my-function.
  • Partial ARN – 123456789012:function:my-function.

This issue came up in a support case where a function ARN crashed in the event_manager.py because it tried to create an SQS queue with an invalid name (containing :).

Changes

  • Add AWS-validated tests for exceptions and valid function ARNs
  • Fix validation of FunctionName for the Lambda CreateFunction operation

Testing

  • tests.aws.services.lambda_.test_lambda_api.TestLambdaFunction.test_function_arns

Notes

  • Other operations in the Lambda API also do not cover all validation implemented for CreateFunction now. However, they often use api_utils.get_name_and_qualifier, which supports function ARNs and handles some validations (i.e., qualified and region). If we want to improve server-side validations consistently, we need better boto-spec-based validation abstractions as suggested by @thrau here.
  • The FunctionName cannot be updated, so we don't need to consider renaming.

@joe4dev joe4dev added the semver: patch Non-breaking changes which can be included in patch releases label Dec 8, 2023
@joe4dev joe4dev self-assigned this Dec 8, 2023
@github-actions

github-actions Bot commented Dec 8, 2023

Copy link
Copy Markdown

@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 84.262% (-0.01%) from 84.274%
when pulling 9267413 on fix-lambda-function-name-validation
into 40266a1 on master.

@joe4dev joe4dev marked this pull request as ready for review December 8, 2023 14:06

@dfangl dfangl left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good! Do you think it makes sense to put that logic in a separate function, just to preserve readability in the CreateFunction body?

@joe4dev

joe4dev commented Dec 11, 2023

Copy link
Copy Markdown
Member Author

Looks good! Do you think it makes sense to put that logic in a separate function, just to preserve readability in the CreateFunction body?

Possibly. It is rather minor as long as we cannot reuse it. However, if the validation creep continues, we could consider reviving the idea of server-side "boto-spec-based validation abstractions as suggested by Thomas #7675 (comment).":

I did a quick try but it didn't work so far (doesn't produce validation errors):

from localstack.aws.protocol.validate import ParamValidator
from localstack.aws.spec import load_service

service = load_service("lambda")
operation = service.operation_model("CreateFunction")
shape = operation.input_shape.members["FunctionName"]
params = "invalid:fun"
validator = ParamValidator()
validation_errors = validator.validate(params, shape)

print(str(validation_errors))

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We would need some other type of AccessDeniedException to fix this tiny snapshot diff because AWS returns None but the exception translates the None message into an empty string:

/Error/Message None (type: <class 'NoneType'>) → '' (type: <class 'str'>)... (expected → actual)

@joe4dev joe4dev merged commit 4733a9b into master Dec 11, 2023
@joe4dev joe4dev deleted the fix-lambda-function-name-validation branch December 11, 2023 15:33
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

semver: patch Non-breaking changes which can be included in patch releases

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants