[Kimchi-devel] [RFC] Validating entries at login

Ramon Medeiros ramonn at linux.vnet.ibm.com
Fri Jan 27 14:24:12 UTC 2017


Propose: valid strings username and password with API.json

Issue: validator is not recognizing method


Just saw that src/wok/control/utils.py has a method validate_params, 
that reads API and validate the output of request_params. I've added 
this changes to code:

diff --git a/src/wok/API.json b/src/wok/API.json
index 8965db9..3faa31b 100644
--- a/src/wok/API.json
+++ b/src/wok/API.json
@@ -2,5 +2,24 @@
      "$schema": "http://json-schema.org/draft-03/schema#",
      "title": "Wok API",
      "description": "Json schema for Wok API",
-    "type": "object"
+    "type": "object",
+    "properties": {
+        "login": {
+            "type": "object",
+            "properties": {
+                "username": {
+                    "description": "Username",
+                    "required": true,
+                    "type": "string",
+                    "error": "WOKAUTH0003E"
+                },
+                "password": {
+                    "description": "Password",
+                    "required": true,
+                    "type": "string",
+                    "error": "WOKAUTH0003E"
+                }
+            }
+        }
+    }
  }
diff --git a/src/wok/root.py b/src/wok/root.py
index e4cecae..55e1886 100644
--- a/src/wok/root.py
+++ b/src/wok/root.py
@@ -32,7 +32,7 @@ from wok.i18n import messages
  from wok.config import paths as wok_paths
  from wok.control import sub_nodes
  from wok.control.base import Resource
-from wok.control.utils import parse_request
+from wok.control.utils import parse_request, validate_params
  from wok.exception import MissingParameter, UnauthorizedError
  from wok.reqlogger import log_request

@@ -170,6 +170,8 @@ class WokRoot(Root):

          try:
              params = parse_request()
+            validate_params(params, self, "login")
              username = params['username']
              password = params['password']
          except KeyError, item:


Debugging the code, i just saw that the action_name passed to 
validate_params (login) is not found by the validator:

  /root/WOK/src/wok/control/utils.py(109)validate_params()
-> validator.validate(request)
(Pdb) s
--Call--
 > /usr/lib/python2.7/site-packages/jsonschema/validators.py(121)validate()
-> def validate(self, *args, **kwargs):
(Pdb) n
 > /usr/lib/python2.7/site-packages/jsonschema/validators.py(122)validate()
-> for error in self.iter_errors(*args, **kwargs):
(Pdb) s
--Call--
 > 
/usr/lib/python2.7/site-packages/jsonschema/validators.py(78)iter_errors()
-> def iter_errors(self, instance, _schema=None):
(Pdb) n
 > 
/usr/lib/python2.7/site-packages/jsonschema/validators.py(79)iter_errors()
-> if _schema is None:
(Pdb)
 > 
/usr/lib/python2.7/site-packages/jsonschema/validators.py(80)iter_errors()
-> _schema = self.schema
(Pdb)
 > 
/usr/lib/python2.7/site-packages/jsonschema/validators.py(82)iter_errors()
-> scope = _schema.get(u"id")
(Pdb)
 > 
/usr/lib/python2.7/site-packages/jsonschema/validators.py(83)iter_errors()
-> if scope:
(Pdb) print _schema
{u'$schema': u'http://json-schema.org/draft-03/schema#', u'type': 
u'object', u'description': u'Json schema for Wok API', u'properties': 
{u'wokroot_login': {u'type': u'object', u'properties': {u'username': 
{u'required': True, u'type': u'string', u'description': u'Username', 
u'error': u'WOKAUTH0003E'}, u'password': {u'required': True, u'type': 
u'string', u'description': u'Password', u'error': u'WOKAUTH0003E'}}}}, 
u'title': u'Wok API'}
(Pdb) n
 > 
/usr/lib/python2.7/site-packages/jsonschema/validators.py(85)iter_errors()
-> try:
(Pdb)
 > 
/usr/lib/python2.7/site-packages/jsonschema/validators.py(86)iter_errors()
-> ref = _schema.get(u"$ref")

How i can know the correct one?



More information about the Kimchi-devel mailing list