[lambda] Add S3 validation for Code["S3Bucket"] and Code["S3Key"].
TODO: validate region.
This commit is contained in:
parent
4c10e54a36
commit
482751f731
@ -6,6 +6,8 @@ import hashlib
|
|||||||
|
|
||||||
import boto.awslambda
|
import boto.awslambda
|
||||||
from moto.core import BaseBackend
|
from moto.core import BaseBackend
|
||||||
|
from moto.s3.models import s3_backend
|
||||||
|
from moto.s3.exceptions import MissingBucket
|
||||||
|
|
||||||
|
|
||||||
class LambdaFunction(object):
|
class LambdaFunction(object):
|
||||||
@ -35,8 +37,23 @@ class LambdaFunction(object):
|
|||||||
self.code_size = len(code)
|
self.code_size = len(code)
|
||||||
self.code_sha_256 = hashlib.sha256(code).hexdigest()
|
self.code_sha_256 = hashlib.sha256(code).hexdigest()
|
||||||
else:
|
else:
|
||||||
self.code_size = 123
|
# validate s3 bucket
|
||||||
self.code_sha_256 = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
|
try:
|
||||||
|
# FIXME: does not validate bucket region
|
||||||
|
key = s3_backend.get_key(self.code['S3Bucket'], self.code['S3Key'])
|
||||||
|
except MissingBucket:
|
||||||
|
raise ValueError(
|
||||||
|
"InvalidParameterValueException",
|
||||||
|
"Error occurred while GetObject. S3 Error Code: NoSuchBucket. S3 Error Message: The specified bucket does not exist")
|
||||||
|
else:
|
||||||
|
# validate s3 key
|
||||||
|
if key is None:
|
||||||
|
raise ValueError(
|
||||||
|
"InvalidParameterValueException",
|
||||||
|
"Error occurred while GetObject. S3 Error Code: NoSuchKey. S3 Error Message: The specified key does not exist.")
|
||||||
|
else:
|
||||||
|
self.code_size = 123
|
||||||
|
self.code_sha_256 = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
|
||||||
self.function_arn = 'arn:aws:lambda:123456789012:function:{}'.format(self.function_name)
|
self.function_arn = 'arn:aws:lambda:123456789012:function:{}'.format(self.function_name)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -39,9 +39,13 @@ class LambdaResponse(BaseResponse):
|
|||||||
lambda_backend = self.get_lambda_backend(full_url)
|
lambda_backend = self.get_lambda_backend(full_url)
|
||||||
|
|
||||||
spec = json.loads(request.body)
|
spec = json.loads(request.body)
|
||||||
fn = lambda_backend.create_function(spec)
|
try:
|
||||||
config = fn.get_configuration()
|
fn = lambda_backend.create_function(spec)
|
||||||
return 201, headers, json.dumps(config)
|
except ValueError as e:
|
||||||
|
return 400, headers, json.dumps({"Error": {"Code": e.args[0], "Message": e.args[1]}})
|
||||||
|
else:
|
||||||
|
config = fn.get_configuration()
|
||||||
|
return 201, headers, json.dumps(config)
|
||||||
|
|
||||||
def _delete_function(self, request, full_url, headers):
|
def _delete_function(self, request, full_url, headers):
|
||||||
lambda_backend = self.get_lambda_backend(full_url)
|
lambda_backend = self.get_lambda_backend(full_url)
|
||||||
|
@ -8,7 +8,18 @@ import zipfile
|
|||||||
import sure # noqa
|
import sure # noqa
|
||||||
|
|
||||||
from freezegun import freeze_time
|
from freezegun import freeze_time
|
||||||
from moto import mock_lambda
|
from moto import mock_lambda, mock_s3
|
||||||
|
|
||||||
|
|
||||||
|
def get_test_zip_file():
|
||||||
|
zip_output = io.BytesIO()
|
||||||
|
with zipfile.ZipFile(zip_output, 'w') as f:
|
||||||
|
f.writestr('lambda_function.py', b'''\
|
||||||
|
def handler(event, context):
|
||||||
|
return "hello world"
|
||||||
|
''')
|
||||||
|
zip_output.seek(0)
|
||||||
|
return zip_output.read()
|
||||||
|
|
||||||
|
|
||||||
@mock_lambda
|
@mock_lambda
|
||||||
@ -22,7 +33,36 @@ def test_list_functions():
|
|||||||
|
|
||||||
@mock_lambda
|
@mock_lambda
|
||||||
@freeze_time('2015-01-01 00:00:00')
|
@freeze_time('2015-01-01 00:00:00')
|
||||||
|
def test_create_based_on_s3_with_missing_bucket():
|
||||||
|
conn = boto3.client('lambda', 'us-west-2')
|
||||||
|
|
||||||
|
conn.create_function.when.called_with(
|
||||||
|
FunctionName='testFunction',
|
||||||
|
Runtime='python2.7',
|
||||||
|
Role='test-iam-role',
|
||||||
|
Handler='lambda_function.handler',
|
||||||
|
Code={
|
||||||
|
'S3Bucket': 'this-bucket-does-not-exist',
|
||||||
|
'S3Key': 'test.zip',
|
||||||
|
},
|
||||||
|
Description='test lambda function',
|
||||||
|
Timeout=3,
|
||||||
|
MemorySize=128,
|
||||||
|
Publish=True,
|
||||||
|
VpcConfig={
|
||||||
|
"SecurityGroupIds": ["sg-123abc"],
|
||||||
|
"SubnetIds": ["subnet-123abc"],
|
||||||
|
},
|
||||||
|
).should.throw(botocore.client.ClientError)
|
||||||
|
|
||||||
|
|
||||||
|
@mock_lambda
|
||||||
|
@mock_s3
|
||||||
|
@freeze_time('2015-01-01 00:00:00')
|
||||||
def test_create_function_from_aws_bucket():
|
def test_create_function_from_aws_bucket():
|
||||||
|
s3_conn = boto3.client('s3', 'us-west-2')
|
||||||
|
s3_conn.create_bucket(Bucket='test-bucket')
|
||||||
|
s3_conn.put_object(Bucket='test-bucket', Key='test.zip', Body=get_test_zip_file())
|
||||||
conn = boto3.client('lambda', 'us-west-2')
|
conn = boto3.client('lambda', 'us-west-2')
|
||||||
|
|
||||||
result = conn.create_function(
|
result = conn.create_function(
|
||||||
@ -71,14 +111,7 @@ def test_create_function_from_aws_bucket():
|
|||||||
def test_create_function_from_zipfile():
|
def test_create_function_from_zipfile():
|
||||||
conn = boto3.client('lambda', 'us-west-2')
|
conn = boto3.client('lambda', 'us-west-2')
|
||||||
|
|
||||||
zip_output = io.BytesIO()
|
zip_content = get_test_zip_file()
|
||||||
with zipfile.ZipFile(zip_output, 'w') as f:
|
|
||||||
f.writestr('lambda_function.py', b'''\
|
|
||||||
def handler(event, context):
|
|
||||||
return "hello world"
|
|
||||||
''')
|
|
||||||
zip_output.seek(0)
|
|
||||||
zip_content = zip_output.read()
|
|
||||||
result = conn.create_function(
|
result = conn.create_function(
|
||||||
FunctionName='testFunction',
|
FunctionName='testFunction',
|
||||||
Runtime='python2.7',
|
Runtime='python2.7',
|
||||||
@ -115,8 +148,12 @@ def handler(event, context):
|
|||||||
|
|
||||||
|
|
||||||
@mock_lambda
|
@mock_lambda
|
||||||
|
@mock_s3
|
||||||
@freeze_time('2015-01-01 00:00:00')
|
@freeze_time('2015-01-01 00:00:00')
|
||||||
def test_get_function():
|
def test_get_function():
|
||||||
|
s3_conn = boto3.client('s3', 'us-west-2')
|
||||||
|
s3_conn.create_bucket(Bucket='test-bucket')
|
||||||
|
s3_conn.put_object(Bucket='test-bucket', Key='test.zip', Body=get_test_zip_file())
|
||||||
conn = boto3.client('lambda', 'us-west-2')
|
conn = boto3.client('lambda', 'us-west-2')
|
||||||
|
|
||||||
conn.create_function(
|
conn.create_function(
|
||||||
@ -165,7 +202,11 @@ def test_get_function():
|
|||||||
|
|
||||||
|
|
||||||
@mock_lambda
|
@mock_lambda
|
||||||
|
@mock_s3
|
||||||
def test_delete_function():
|
def test_delete_function():
|
||||||
|
s3_conn = boto3.client('s3', 'us-west-2')
|
||||||
|
s3_conn.create_bucket(Bucket='test-bucket')
|
||||||
|
s3_conn.put_object(Bucket='test-bucket', Key='test.zip', Body=get_test_zip_file())
|
||||||
conn = boto3.client('lambda', 'us-west-2')
|
conn = boto3.client('lambda', 'us-west-2')
|
||||||
|
|
||||||
conn.create_function(
|
conn.create_function(
|
||||||
@ -190,12 +231,16 @@ def test_delete_function():
|
|||||||
|
|
||||||
|
|
||||||
@mock_lambda
|
@mock_lambda
|
||||||
|
@mock_s3
|
||||||
@freeze_time('2015-01-01 00:00:00')
|
@freeze_time('2015-01-01 00:00:00')
|
||||||
def test_list_create_list_get_delete_list():
|
def test_list_create_list_get_delete_list():
|
||||||
"""
|
"""
|
||||||
test `list -> create -> list -> get -> delete -> list` integration
|
test `list -> create -> list -> get -> delete -> list` integration
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
s3_conn = boto3.client('s3', 'us-west-2')
|
||||||
|
s3_conn.create_bucket(Bucket='test-bucket')
|
||||||
|
s3_conn.put_object(Bucket='test-bucket', Key='test.zip', Body=get_test_zip_file())
|
||||||
conn = boto3.client('lambda', 'us-west-2')
|
conn = boto3.client('lambda', 'us-west-2')
|
||||||
|
|
||||||
conn.list_functions()['Functions'].should.have.length_of(0)
|
conn.list_functions()['Functions'].should.have.length_of(0)
|
||||||
|
Loading…
Reference in New Issue
Block a user