remove validator

Change-Id: I2847527fd2dd39dfb991e446d6d0ce3e65e5483c
This commit is contained in:
Andrey Pavlov 2015-01-08 15:50:40 +03:00
parent 4706689b21
commit 0db4e8ed8b
4 changed files with 2 additions and 132 deletions

View File

@ -29,7 +29,6 @@ import webob.exc
from ec2api.api import apirequest
from ec2api.api import ec2utils
from ec2api.api import faults
from ec2api.api import validator
from ec2api import context
from ec2api import exception
from ec2api.openstack.common.gettextutils import _
@ -329,27 +328,6 @@ class Requestify(wsgi.Middleware):
return self.application
class Validator(wsgi.Middleware):
validator.DEFAULT_VALIDATOR = {
}
def __init__(self, application):
super(Validator, self).__init__(application)
@webob.dec.wsgify(RequestClass=wsgi.Request)
def __call__(self, req):
try:
if validator.validate(req.environ['ec2.request'],
validator.DEFAULT_VALIDATOR):
return self.application
else:
raise webob.exc.HTTPBadRequest()
except Exception as ex:
return ec2_error_ex(
ex, req, unexpected=not isinstance(ex, exception.EC2Exception))
def exception_to_ec2code(ex):
"""Helper to extract EC2 error code from exception.

View File

@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import base64
import re
import netaddr
@ -25,25 +24,6 @@ from ec2api.openstack.common import log as logging
LOG = logging.getLogger(__name__)
def _get_path_validator_regex():
# rfc3986 path validator regex from
# http://jmrware.com/articles/2009/uri_regexp/URI_regex.html
pchar = "([A-Za-z0-9\-._~!$&'()*+,;=:@]|%[0-9A-Fa-f]{2})"
path = "((/{pchar}*)*|"
path += "/({pchar}+(/{pchar}*)*)?|"
path += "{pchar}+(/{pchar}*)*|"
path += "{pchar}+(/{pchar}*)*|)"
path = path.format(pchar=pchar)
return re.compile(path)
VALIDATE_PATH_RE = _get_path_validator_regex()
def validate_dummy(val, **kwargs):
return True
def validate_str(val, parameter_name, max_length=None):
if (isinstance(val, basestring) and
(max_length is None or max_length and len(val) <= max_length)):
@ -53,47 +33,6 @@ def validate_str(val, parameter_name, max_length=None):
"than 255 characters.") % parameter_name)
def validate_int(max_value=None):
def _do(val, **kwargs):
if not isinstance(val, int):
return False
if max_value and val > max_value:
return False
return True
return _do
def validate_url_path(val, parameter_name=None, **kwargs):
"""True if val is matched by the path component grammar in rfc3986."""
if not validate_str()(val, parameter_name):
return False
return VALIDATE_PATH_RE.match(val).end() == len(val)
def validate_image_path(val, parameter_name=None, **kwargs):
if not validate_str()(val, parameter_name):
return False
bucket_name = val.split('/')[0]
manifest_path = val[len(bucket_name) + 1:]
if not len(bucket_name) or not len(manifest_path):
return False
if val[0] == '/':
return False
# make sure the image path if rfc3986 compliant
# prepend '/' to make input validate
if not validate_url_path('/' + val):
return False
return True
def validate_list(items, parameter_name):
if not isinstance(items, list):
raise exception.InvalidParameterValue(
@ -102,15 +41,6 @@ def validate_list(items, parameter_name):
reason='Expected a list here')
def validate_user_data(user_data, **kwargs):
"""Check if the user_data is encoded properly."""
try:
user_data = base64.b64decode(user_data)
except TypeError:
return False
return True
def _is_valid_cidr(address):
"""Check if address is valid
@ -218,37 +148,3 @@ def validate_ipv4(address, parameter_name, **kwargs):
raise exception.InvalidParameterValue(
value=address, parameter=parameter_name,
reason=_('Not a valid IP address'))
def validate(request, validator):
"""Validate values of args against validators in validator.
:param args: Dict of values to be validated.
:param validator: A dict where the keys map to keys in args
and the values are validators.
Applies each validator to ``args[key]``
:returns: True if validation succeeds. Otherwise False.
A validator should be a callable which accepts 1 argument and which
returns True if the argument passes validation. False otherwise.
A validator should not raise an exception to indicate validity of the
argument.
Only validates keys which show up in both args and validator.
"""
args = request.args
for key in args:
if key not in validator:
continue
f = validator[key]
assert callable(f)
if not f(args[key], parameter_name=key, action=request.action):
LOG.debug(_("%(key)s with value %(value)s failed"
" validator %(name)s"),
{'key': key, 'value': args[key], 'name': f.__name__})
return False
return True

View File

@ -75,8 +75,7 @@ class ApiTestCase(test_base.BaseTestCase):
'ec2.request': ec2_request,
'ec2api.context': ec2_context}
request = ec2api.wsgi.Request(environ)
application = ec2api.api.Validator(ec2api.api.Executor())
response = request.send(application)
response = request.send(ec2api.api.Executor())
return self._check_and_transform_response(response, action)
def _create_context(self):

View File

@ -8,7 +8,7 @@ use = egg:Paste#urlmap
[composite:ec2apicloud]
use = call:ec2api.api.auth:pipeline_factory
keystone = ec2apifaultwrap logrequest ec2apikeystoneauth cloudrequest validator ec2apiexecutor
keystone = ec2apifaultwrap logrequest ec2apikeystoneauth cloudrequest ec2apiexecutor
[filter:ec2apifaultwrap]
paste.filter_factory = ec2api.api:FaultWrapper.factory
@ -22,9 +22,6 @@ paste.filter_factory = ec2api.api:EC2KeystoneAuth.factory
[filter:cloudrequest]
paste.filter_factory = ec2api.api:Requestify.factory
[filter:validator]
paste.filter_factory = ec2api.api:Validator.factory
[app:ec2apiexecutor]
paste.app_factory = ec2api.api:Executor.factory