Validate parameters of agent API

The agent API has some parameters, but they are not validated.
Each parameter is stored to the table "agent_builds", and each column
is defined as varchar(255).

mysql> desc agent_builds;
+--------------+--------------+------+-----+---------+----------------+
| Field        | Type         | Null | Key | Default | Extra          |
+--------------+--------------+------+-----+---------+----------------+
| created_at   | datetime     | YES  |     | NULL    |                |
| updated_at   | datetime     | YES  |     | NULL    |                |
| deleted_at   | datetime     | YES  |     | NULL    |                |
| id           | int(11)      | NO   | PRI | NULL    | auto_increment |
| hypervisor   | varchar(255) | YES  | MUL | NULL    |                |
| os           | varchar(255) | YES  |     | NULL    |                |
| architecture | varchar(255) | YES  |     | NULL    |                |
| version      | varchar(255) | YES  |     | NULL    |                |
| url          | varchar(255) | YES  |     | NULL    |                |
| md5hash      | varchar(255) | YES  |     | NULL    |                |
| deleted      | int(11)      | YES  |     | NULL    |                |
+--------------+--------------+------+-----+---------+----------------+

This patch adds the validation with the type and the length.

Closes-Bug: 1240458

Change-Id: Id06235ebde62f7b96c55b0bd2be5d941bfd2e999
This commit is contained in:
Ken'ichi Ohmichi 2013-10-16 19:29:22 +09:00
parent 6affe67067
commit de7321d53f
4 changed files with 135 additions and 0 deletions

View File

@ -22,6 +22,7 @@ from nova.api.openstack import wsgi
from nova.api.openstack import xmlutil
from nova import db
from nova import exception
from nova import utils
authorize = extensions.extension_authorizer('compute', 'agents')
@ -101,6 +102,13 @@ class AgentController(object):
except (TypeError, KeyError):
raise webob.exc.HTTPUnprocessableEntity()
try:
utils.check_string_length(url, 'url', max_length=255)
utils.check_string_length(md5hash, 'md5hash', max_length=255)
utils.check_string_length(version, 'version', max_length=255)
except exception.InvalidInput as exc:
raise webob.exc.HTTPBadRequest(explanation=exc.format_message())
try:
db.agent_build_update(context, id,
{'version': version,
@ -138,6 +146,17 @@ class AgentController(object):
except (TypeError, KeyError):
raise webob.exc.HTTPUnprocessableEntity()
try:
utils.check_string_length(hypervisor, 'hypervisor', max_length=255)
utils.check_string_length(os, 'os', max_length=255)
utils.check_string_length(architecture, 'architecture',
max_length=255)
utils.check_string_length(version, 'version', max_length=255)
utils.check_string_length(url, 'url', max_length=255)
utils.check_string_length(md5hash, 'md5hash', max_length=255)
except exception.InvalidInput as exc:
raise webob.exc.HTTPBadRequest(explanation=exc.format_message())
try:
agent_build_ref = db.agent_build_create(context,
{'hypervisor': hypervisor,

View File

@ -23,6 +23,7 @@ from nova.api.openstack import xmlutil
from nova import db
from nova import exception
from nova.openstack.common.gettextutils import _
from nova import utils
ALIAS = "os-agents"
@ -108,6 +109,13 @@ class AgentController(object):
raise webob.exc.HTTPBadRequest(explanation=_(
"Could not find %s parameter in the request") % e.args[0])
try:
utils.check_string_length(url, 'url', max_length=255)
utils.check_string_length(md5hash, 'md5hash', max_length=255)
utils.check_string_length(version, 'version', max_length=255)
except exception.InvalidInput as exc:
raise webob.exc.HTTPBadRequest(explanation=exc.format_message())
try:
db.agent_build_update(context, id,
{'version': version,
@ -152,6 +160,17 @@ class AgentController(object):
raise webob.exc.HTTPBadRequest(explanation=_(
"Could not find %s parameter in the request") % e.args[0])
try:
utils.check_string_length(hypervisor, 'hypervisor', max_length=255)
utils.check_string_length(os, 'os', max_length=255)
utils.check_string_length(architecture, 'architecture',
max_length=255)
utils.check_string_length(version, 'version', max_length=255)
utils.check_string_length(url, 'url', max_length=255)
utils.check_string_length(md5hash, 'md5hash', max_length=255)
except exception.InvalidInput as exc:
raise webob.exc.HTTPBadRequest(explanation=exc.format_message())
try:
agent_build_ref = db.agent_build_create(context,
{'hypervisor': hypervisor,

View File

@ -12,6 +12,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import webob.exc
from nova.api.openstack.compute.contrib import agents
from nova import context
@ -116,6 +117,36 @@ class AgentsTest(test.NoDBTestCase):
res_dict = self.controller.create(req, body)
self.assertEqual(res_dict, response)
def _test_agents_create_with_invalid_length(self, key):
req = FakeRequest()
body = {'agent': {'hypervisor': 'kvm',
'os': 'win',
'architecture': 'x86',
'version': '7.0',
'url': 'xxx://xxxx/xxx/xxx',
'md5hash': 'add6bb58e139be103324d04d82d8f545'}}
body['agent'][key] = 'x' * 256
self.assertRaises(webob.exc.HTTPBadRequest,
self.controller.create, req, body)
def test_agents_create_with_invalid_length_hypervisor(self):
self._test_agents_create_with_invalid_length('hypervisor')
def test_agents_create_with_invalid_length_os(self):
self._test_agents_create_with_invalid_length('os')
def test_agents_create_with_invalid_length_architecture(self):
self._test_agents_create_with_invalid_length('architecture')
def test_agents_create_with_invalid_length_version(self):
self._test_agents_create_with_invalid_length('version')
def test_agents_create_with_invalid_length_url(self):
self._test_agents_create_with_invalid_length('url')
def test_agents_create_with_invalid_length_md5hash(self):
self._test_agents_create_with_invalid_length('md5hash')
def test_agents_delete(self):
req = FakeRequest()
self.controller.delete(req, 1)
@ -179,3 +210,21 @@ class AgentsTest(test.NoDBTestCase):
'md5hash': 'add6bb58e139be103324d04d82d8f545'}}
res_dict = self.controller.update(req, 1, body)
self.assertEqual(res_dict, response)
def _test_agents_update_with_invalid_length(self, key):
req = FakeRequest()
body = {'para': {'version': '7.0',
'url': 'xxx://xxxx/xxx/xxx',
'md5hash': 'add6bb58e139be103324d04d82d8f545'}}
body['para'][key] = 'x' * 256
self.assertRaises(webob.exc.HTTPBadRequest,
self.controller.update, req, 1, body)
def test_agents_update_with_invalid_length_version(self):
self._test_agents_update_with_invalid_length('version')
def test_agents_update_with_invalid_length_url(self):
self._test_agents_update_with_invalid_length('url')
def test_agents_update_with_invalid_length_md5hash(self):
self._test_agents_update_with_invalid_length('md5hash')

View File

@ -206,6 +206,36 @@ class AgentsTest(test.NoDBTestCase):
self.assertRaises(exc.HTTPBadRequest, self.controller.create,
req, body)
def _test_agents_create_with_invalid_length(self, key):
req = FakeRequest()
body = {'agent': {'hypervisor': 'kvm',
'os': 'win',
'architecture': 'x86',
'version': '7.0',
'url': 'xxx://xxxx/xxx/xxx',
'md5hash': 'add6bb58e139be103324d04d82d8f545'}}
body['agent'][key] = 'x' * 256
self.assertRaises(exc.HTTPBadRequest, self.controller.create,
req, body)
def test_agents_create_with_invalid_length_hypervisor(self):
self._test_agents_create_with_invalid_length('hypervisor')
def test_agents_create_with_invalid_length_os(self):
self._test_agents_create_with_invalid_length('os')
def test_agents_create_with_invalid_length_architecture(self):
self._test_agents_create_with_invalid_length('architecture')
def test_agents_create_with_invalid_length_version(self):
self._test_agents_create_with_invalid_length('version')
def test_agents_create_with_invalid_length_url(self):
self._test_agents_create_with_invalid_length('url')
def test_agents_create_with_invalid_length_md5hash(self):
self._test_agents_create_with_invalid_length('md5hash')
def test_agents_delete(self):
req = FakeRequest()
self.controller.delete(req, 1)
@ -300,3 +330,21 @@ class AgentsTest(test.NoDBTestCase):
body = {}
self.assertRaises(exc.HTTPBadRequest, self.controller.update,
req, 1, body)
def _test_agents_update_with_invalid_length(self, key):
req = FakeRequest()
body = {'agent': {'version': '7.0',
'url': 'xxx://xxxx/xxx/xxx',
'md5hash': 'add6bb58e139be103324d04d82d8f545'}}
body['agent'][key] = 'x' * 256
self.assertRaises(exc.HTTPBadRequest, self.controller.update,
req, 1, body)
def test_agents_update_with_invalid_length_version(self):
self._test_agents_update_with_invalid_length('version')
def test_agents_update_with_invalid_length_url(self):
self._test_agents_update_with_invalid_length('url')
def test_agents_update_with_invalid_length_md5hash(self):
self._test_agents_update_with_invalid_length('md5hash')