def update_function_code(self, updated_spec): if "DryRun" in updated_spec and updated_spec["DryRun"]: return self.get_configuration() if "ZipFile" in updated_spec: self.code["ZipFile"] = updated_spec["ZipFile"] # using the "hackery" from __init__ because it seems to work # TODOs and FIXMEs included, because they'll need to be fixed # in both places now try: to_unzip_code = base64.b64decode( bytes(updated_spec["ZipFile"], "utf-8") ) except Exception: to_unzip_code = base64.b64decode(updated_spec["ZipFile"]) self.code_bytes = to_unzip_code self.code_size = len(to_unzip_code) self.code_sha_256 = hashlib.sha256(to_unzip_code).hexdigest() # TODO: we should be putting this in a lambda bucket self.code["UUID"] = str(uuid.uuid4()) self.code["S3Key"] = "{}-{}".format(self.function_name, self.code["UUID"]) elif "S3Bucket" in updated_spec and "S3Key" in updated_spec: key = None try: # FIXME: does not validate bucket region key = s3_backend.get_object( updated_spec["S3Bucket"], updated_spec["S3Key"] ) except MissingBucket: if do_validate_s3(): raise ValueError( "InvalidParameterValueException", "Error occurred while GetObject. S3 Error Code: NoSuchBucket. S3 Error Message: The specified bucket does not exist", ) except MissingKey: if do_validate_s3(): raise ValueError( "InvalidParameterValueException", "Error occurred while GetObject. S3 Error Code: NoSuchKey. S3 Error Message: The specified key does not exist.", ) if key: self.code_bytes = key.value self.code_size = key.size self.code_sha_256 = hashlib.sha256(key.value).hexdigest() self.code["S3Bucket"] = updated_spec["S3Bucket"] self.code["S3Key"] = updated_spec["S3Key"] return self.get_configuration()
def _validate_s3_bucket_and_key(data): key = None try: # FIXME: does not validate bucket region key = s3_backend.get_object(data["S3Bucket"], data["S3Key"]) except MissingBucket: if do_validate_s3(): raise InvalidParameterValueException( "Error occurred while GetObject. S3 Error Code: NoSuchBucket. S3 Error Message: The specified bucket does not exist" ) except MissingKey: if do_validate_s3(): raise ValueError( "InvalidParameterValueException", "Error occurred while GetObject. S3 Error Code: NoSuchKey. S3 Error Message: The specified key does not exist.", ) return key
def __init__(self, spec, region, validate_s3=True, version=1): # required self.region = region self.code = spec["Code"] self.function_name = spec["FunctionName"] self.handler = spec["Handler"] self.role = spec["Role"] self.run_time = spec["Runtime"] self.logs_backend = logs_backends[self.region] self.environment_vars = spec.get("Environment", {}).get("Variables", {}) self.docker_client = docker.from_env() self.policy = None self.state = "Active" # Unfortunately mocking replaces this method w/o fallback enabled, so we # need to replace it if we detect it's been mocked if requests.adapters.HTTPAdapter.send != _orig_adapter_send: _orig_get_adapter = self.docker_client.api.get_adapter def replace_adapter_send(*args, **kwargs): adapter = _orig_get_adapter(*args, **kwargs) if isinstance(adapter, requests.adapters.HTTPAdapter): adapter.send = functools.partial(_orig_adapter_send, adapter) return adapter self.docker_client.api.get_adapter = replace_adapter_send # optional self.description = spec.get("Description", "") self.memory_size = spec.get("MemorySize", 128) self.publish = spec.get("Publish", False) # this is ignored currently self.timeout = spec.get("Timeout", 3) self.logs_group_name = "/aws/lambda/{}".format(self.function_name) self.logs_backend.ensure_log_group(self.logs_group_name, []) # this isn't finished yet. it needs to find out the VpcId value self._vpc_config = spec.get("VpcConfig", { "SubnetIds": [], "SecurityGroupIds": [] }) # auto-generated self.version = version self.last_modified = datetime.datetime.utcnow().strftime( "%Y-%m-%d %H:%M:%S") if "ZipFile" in self.code: # more hackery to handle unicode/bytes/str in python3 and python2 - # argh! try: to_unzip_code = base64.b64decode( bytes(self.code["ZipFile"], "utf-8")) except Exception: to_unzip_code = base64.b64decode(self.code["ZipFile"]) self.code_bytes = to_unzip_code self.code_size = len(to_unzip_code) self.code_sha_256 = hashlib.sha256(to_unzip_code).hexdigest() # TODO: we should be putting this in a lambda bucket self.code["UUID"] = str(uuid.uuid4()) self.code["S3Key"] = "{}-{}".format(self.function_name, self.code["UUID"]) else: # validate s3 bucket and key key = None try: # FIXME: does not validate bucket region key = s3_backend.get_object(self.code["S3Bucket"], self.code["S3Key"]) except MissingBucket: if do_validate_s3(): raise InvalidParameterValueException( "Error occurred while GetObject. S3 Error Code: NoSuchBucket. S3 Error Message: The specified bucket does not exist" ) except MissingKey: if do_validate_s3(): raise ValueError( "InvalidParameterValueException", "Error occurred while GetObject. S3 Error Code: NoSuchKey. S3 Error Message: The specified key does not exist.", ) if key: self.code_bytes = key.value self.code_size = key.size self.code_sha_256 = hashlib.sha256(key.value).hexdigest() self.function_arn = make_function_arn(self.region, ACCOUNT_ID, self.function_name) self.tags = dict()