def validate_ascii_metadata(params, **kwargs): """Verify S3 Metadata only contains ascii characters. From: http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingMetadata.html "Amazon S3 stores user-defined metadata in lowercase. Each name, value pair must conform to US-ASCII when using REST and UTF-8 when using SOAP or browser-based uploads via POST." """ metadata = params.get('Metadata') if not metadata or not isinstance(metadata, dict): # We have to at least type check the metadata as a dict type # because this handler is called before param validation. # We'll go ahead and return because the param validator will # give a descriptive error message for us. # We might need a post-param validation event. return for key, value in metadata.items(): try: key.encode('ascii') value.encode('ascii') except UnicodeEncodeError as e: error_msg = ('Non ascii characters found in S3 metadata ' 'for key "%s", value: "%s". \nS3 metadata can only ' 'contain ASCII characters. ' % (key, value)) raise ParamValidationError(report=error_msg)
def validate_bucket_name(params, **kwargs): if 'Bucket' not in params: return bucket = params['Bucket'] if VALID_BUCKET.search(bucket) is None: error_msg = ('Invalid bucket name "%s": Bucket name must match ' 'the regex "%s"' % (bucket, VALID_BUCKET.pattern)) raise ParamValidationError(report=error_msg)
def serialize_to_request(self, parameters, operation_model): input_shape = operation_model.input_shape if input_shape is not None: report = self._param_validator.validate(parameters, operation_model.input_shape) if report.has_errors(): raise ParamValidationError(report=report.generate_report()) return self._serializer.serialize_to_request(parameters, operation_model)
def _quote_source_header_from_dict(source_dict): try: bucket = source_dict['Bucket'] key = percent_encode(source_dict['Key'], safe=SAFE_CHARS + '/') version_id = source_dict.get('VersionId') except KeyError as e: raise ParamValidationError(report='Missing required parameter: %s' % str(e)) final = '%s/%s' % (bucket, key) if version_id is not None: final += '?versionId=%s' % version_id return final
def _validate_response(self, operation_name, service_response): service_model = self.client.meta.service_model operation_model = service_model.operation_model(operation_name) output_shape = operation_model.output_shape # Remove ResponseMetadata so that the validator doesn't attempt to # perform validation on it. response = service_response if 'ResponseMetadata' in response: response = copy.copy(service_response) del response['ResponseMetadata'] if output_shape is not None: validate_parameters(response, output_shape) elif response: # If the output shape is None, that means the response should be # empty apart from ResponseMetadata raise ParamValidationError(report=( "Service response should only contain ResponseMetadata."))
def validate_ascii_metadata(params, **kwargs): """ """ metadata = params.get('Metadata') if not metadata or not isinstance(metadata, dict): # We have to at least type check the metadata as a dict type # because this handler is called before param validation. # We'll go ahead and return because the param validator will # give a descriptive error message for us. # We might need a post-param validation event. return for key, value in metadata.items(): try: key.encode('ascii') value.encode('ascii') except UnicodeEncodeError as e: error_msg = ('Non ascii characters found in S3 metadata ' 'for key "%s", value: "%s". \nS3 metadata can only ' 'contain ASCII characters. ' % (key, value)) raise ParamValidationError(report=error_msg)
def validate_parameters(params, shape): """Validates input parameters against a schema. This is a convenience function that validates parameters against a schema. You can also instantiate and use the ParamValidator class directly if you want more control. If there are any validation errors then a ParamValidationError will be raised. If there are no validation errors than no exception is raised and a value of None is returned. :param params: The user provided input parameters. :type shape: kscore.model.Shape :param shape: The schema which the input parameters should adhere to. :raise: ParamValidationError """ validator = ParamValidator() report = validator.validate(params, shape) if report.has_errors(): raise ParamValidationError(report=report.generate_report())