When someone tries to submit a metadata change on topology without formatting the request as json, we get an exception on the server and error 500 for the client:
prompt$ curl -s -X POST -d '{"description": "Switch1-Switch2"}' http://$KYTOS_IP:8181/api/kytos/topology/v3/links/f851f04affd30c78ed87509a7cda467bb45ca4d2fb784aee852d0e76eb6417e0/metadata
{"code":500,"description":"The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.","name":"Internal Server Error"}
From the server, we can see:
Dec 4 15:01:46 81f3b4c20007 kytos.core.controller:ERROR app:1892: Exception on /api/kytos/topology/v3/links/f851f04affd30c78ed87509a7cda467bb45ca4d2fb784aee852d0e76eb6417e0/metadata [POST]
Dec 4 15:01:46 81f3b4c20007 Traceback (most recent call last):
Dec 4 15:01:46 81f3b4c20007 File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 2447, in wsgi_app
Dec 4 15:01:46 81f3b4c20007 response = self.full_dispatch_request()
Dec 4 15:01:46 81f3b4c20007 File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1952, in full_dispatch_request
Dec 4 15:01:46 81f3b4c20007 rv = self.handle_user_exception(e)
Dec 4 15:01:46 81f3b4c20007 File "/usr/local/lib/python3.6/dist-packages/flask_cors/extension.py", line 161, in wrapped_function
Dec 4 15:01:46 81f3b4c20007 return cors_after_request(app.make_response(f(*args, **kwargs)))
Dec 4 15:01:46 81f3b4c20007 File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1821, in handle_user_exception
Dec 4 15:01:46 81f3b4c20007 reraise(exc_type, exc_value, tb)
Dec 4 15:01:46 81f3b4c20007 raise value
Dec 4 15:01:46 81f3b4c20007 File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1950, in full_dispatch_request
Dec 4 15:01:46 81f3b4c20007 rv = self.dispatch_request()
Dec 4 15:01:46 81f3b4c20007 File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1936, in dispatch_request
Dec 4 15:01:46 81f3b4c20007 return self.view_functions[rule.endpoint](**req.view_args)
Dec 4 15:01:46 81f3b4c20007 File "//var/lib/kytos/napps/kytos/topology/main.py", line 446, in add_link_metadata
Dec 4 15:01:46 81f3b4c20007 link.extend_metadata(metadata)
Dec 4 15:01:46 81f3b4c20007 File "/usr/local/lib/python3.6/dist-packages/kytos/core/common.py", line 116, in extend_metadata
Dec 4 15:01:46 81f3b4c20007 self.metadata.update(metadatas)
Dec 4 15:01:46 81f3b4c20007 TypeError: 'NoneType' object is not iterable
The current implementation does not validate the input and raise an 500 error: server side execution error. This situation should rather raises an 40x error: client side error (the user didnt provide the correct parameters, in this case the json encoding format).
For the record: the right way to submit the request would be:
Hi guys,
When someone tries to submit a metadata change on topology without formatting the request as json, we get an exception on the server and error 500 for the client:
From the server, we can see:
This happens because the topology API assume the received request contains a valid JSON data (https://github.com/kytos/topology/blob/0d8f3c182810ac43af33ba8f034e10ca82f6bda5/main.py#L279) and then it uses that value in subsequent internal code (https://github.com/kytos/topology/blob/0d8f3c182810ac43af33ba8f034e10ca82f6bda5/main.py#L285).
I suggest we run a sanity check and validate the user input before use it. An example of such validation is done in mef_eline: https://github.com/kytos/mef_eline/blob/master/main.py#L121-L131
The current implementation does not validate the input and raise an 500 error: server side execution error. This situation should rather raises an 40x error: client side error (the user didnt provide the correct parameters, in this case the json encoding format).
For the record: the right way to submit the request would be:
But again, this is enhancement suggestion for Kytos: error handling and input validation.