AWSLambda - raise correct error when publishing unknown function (#4934)

This commit is contained in:
Bert Blommers 2022-03-15 14:07:01 -01:00 committed by GitHub
parent cbe787add3
commit 5ae0ced349
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 39 additions and 4 deletions

View File

@ -43,3 +43,10 @@ class UnknownPolicyException(LambdaClientError):
"ResourceNotFoundException",
"No policy is associated with the given resource.",
)
class UnknownFunctionException(LambdaClientError):
code = 404
def __init__(self, arn):
super().__init__("ResourceNotFoundException", f"Function not found: {arn}")

View File

@ -36,6 +36,7 @@ from .exceptions import (
CrossAccountNotAllowed,
InvalidRoleFormat,
InvalidParameterValueException,
UnknownFunctionException,
)
from .utils import (
make_function_arn,
@ -932,10 +933,11 @@ class LambdaVersion(CloudFormationModel):
class LambdaStorage(object):
def __init__(self):
def __init__(self, region_name):
# Format 'func_name' {'alias': {}, 'versions': []}
self._functions = {}
self._arns = weakref.WeakValueDictionary()
self.region_name = region_name
def _get_latest(self, name):
return self._functions[name]["latest"]
@ -1011,6 +1013,12 @@ class LambdaStorage(object):
def publish_function(self, name_or_arn, description=""):
function = self.get_function_by_name_or_arn(name_or_arn)
if not function:
if name_or_arn.startswith("arn:aws"):
arn = name_or_arn
else:
arn = make_function_arn(self.region_name, ACCOUNT_ID, name_or_arn)
raise UnknownFunctionException(arn)
name = function.function_name
if name not in self._functions:
return None
@ -1180,7 +1188,7 @@ class LambdaBackend(BaseBackend):
"""
def __init__(self, region_name):
self._lambdas = LambdaStorage()
self._lambdas = LambdaStorage(region_name)
self._event_source_mappings = {}
self._layers = LayerStorage()
self.region_name = region_name

View File

@ -95,6 +95,7 @@ class LambdaResponse(BaseResponse):
else:
raise ValueError("Cannot handle request")
@error_handler
def versions(self, request, full_url, headers):
self.setup_class(request, full_url, headers)
if request.method == "GET":
@ -324,7 +325,7 @@ class LambdaResponse(BaseResponse):
return 404, {}, "{}"
def _publish_function(self):
function_name = self.path.rsplit("/", 2)[-2]
function_name = unquote(self.path.split("/")[-2])
description = self._get_param("Description")
fn = self.lambda_backend.publish_function(function_name, description)

View File

@ -7,7 +7,7 @@ response = LambdaResponse()
url_paths = {
r"{0}/(?P<api_version>[^/]+)/functions/?$": response.root,
r"{0}/(?P<api_version>[^/]+)/functions/(?P<function_name>[\w_:%-]+)/?$": response.function,
r"{0}/(?P<api_version>[^/]+)/functions/(?P<function_name>[\w_-]+)/versions/?$": response.versions,
r"{0}/(?P<api_version>[^/]+)/functions/(?P<function_name>[\w_:%-]+)/versions/?$": response.versions,
r"{0}/(?P<api_version>[^/]+)/event-source-mappings/?$": response.event_source_mappings,
r"{0}/(?P<api_version>[^/]+)/event-source-mappings/(?P<UUID>[\w_-]+)/?$": response.event_source_mapping,
r"{0}/(?P<api_version>[^/]+)/functions/(?P<function_name>[\w_-]+)/invocations/?$": response.invoke,

View File

@ -486,6 +486,25 @@ def test_delete_unknown_function():
).should.throw(botocore.client.ClientError)
@mock_lambda
@pytest.mark.parametrize(
"name",
[
"bad_function_name",
f"arn:aws:lambda:eu-west-1:{ACCOUNT_ID}:function:bad_function_name",
],
)
def test_publish_version_unknown_function(name):
client = boto3.client("lambda", "eu-west-1")
with pytest.raises(ClientError) as exc:
client.publish_version(FunctionName=name, Description="v2")
err = exc.value.response["Error"]
err["Code"].should.equal("ResourceNotFoundException")
err["Message"].should.equal(
f"Function not found: arn:aws:lambda:eu-west-1:{ACCOUNT_ID}:function:bad_function_name"
)
@mock_lambda
@mock_s3
def test_publish():