From 86356bf6f13ff5a5394db6b4547b26cd6092b566 Mon Sep 17 00:00:00 2001 From: Ian Cordasco Date: Mon, 30 Jun 2014 14:49:03 -0500 Subject: [PATCH] Use rfc3986 library to validate URL paths and URIs More work needs to be done in rfc3986 to give the user more control over what they consider to be a valid URI in the context of RFC 3986. For example, a previous incarnation of these tests checked that "1" and "abc" were invalid when according to the RFC they are. Update the API samples and tests to use valid URIs DocImpact Change-Id: I288fbaead64990db1053b7a11e82904611b8498f --- doc/api_samples/os-agents/agent-post-req.json | 4 +- doc/api_samples/os-agents/agent-post-req.xml | 4 +- .../os-agents/agent-post-resp.json | 4 +- doc/api_samples/os-agents/agent-post-resp.xml | 4 +- .../os-agents/agent-update-put-req.json | 4 +- .../os-agents/agent-update-put-req.xml | 4 +- .../os-agents/agent-update-put-resp.json | 4 +- .../os-agents/agent-update-put-resp.xml | 4 +- .../os-agents/agents-get-resp.json | 4 +- doc/api_samples/os-agents/agents-get-resp.xml | 4 +- .../api_samples/os-agents/agent-post-req.json | 4 +- .../os-agents/agent-post-resp.json | 4 +- .../os-agents/agent-update-put-req.json | 2 +- .../os-agents/agent-update-put-resp.json | 4 +- .../os-agents/agents-get-resp.json | 4 +- nova/api/validation/validators.py | 7 +++ nova/api/validator.py | 21 ++----- .../openstack/compute/contrib/test_agents.py | 32 +++++----- .../compute/plugins/v3/test_agents.py | 46 +++++++-------- nova/tests/integrated/test_api_samples.py | 8 +-- .../os-agents/agent-post-resp.json.tpl | 4 +- .../os-agents/agent-update-put-resp.json.tpl | 4 +- .../os-agents/agents-get-resp.json.tpl | 4 +- nova/tests/integrated/v3/test_agents.py | 6 +- nova/tests/test_api_validation.py | 58 +++++++++++++++++++ requirements.txt | 1 + 26 files changed, 151 insertions(+), 98 deletions(-) diff --git a/doc/api_samples/os-agents/agent-post-req.json b/doc/api_samples/os-agents/agent-post-req.json index 217993b17f..1913498547 100644 --- a/doc/api_samples/os-agents/agent-post-req.json +++ b/doc/api_samples/os-agents/agent-post-req.json @@ -5,6 +5,6 @@ "architecture": "x86", "version": "8.0", "md5hash": "add6bb58e139be103324d04d82d8f545", - "url": "xxxxxxxxxxxx" + "url": "http://example.com/path/to/resource" } -} \ No newline at end of file +} diff --git a/doc/api_samples/os-agents/agent-post-req.xml b/doc/api_samples/os-agents/agent-post-req.xml index be93e97ce4..b7b7d036ba 100644 --- a/doc/api_samples/os-agents/agent-post-req.xml +++ b/doc/api_samples/os-agents/agent-post-req.xml @@ -5,5 +5,5 @@ x86 8.0 add6bb58e139be103324d04d82d8f545 - xxxxxxxxxxxx - \ No newline at end of file + http://example.com/path/to/resource + diff --git a/doc/api_samples/os-agents/agent-post-resp.json b/doc/api_samples/os-agents/agent-post-resp.json index f6c760cc67..24ddede90b 100644 --- a/doc/api_samples/os-agents/agent-post-resp.json +++ b/doc/api_samples/os-agents/agent-post-resp.json @@ -5,7 +5,7 @@ "hypervisor": "hypervisor", "md5hash": "add6bb58e139be103324d04d82d8f545", "os": "os", - "url": "xxxxxxxxxxxx", + "url": "http://example.com/path/to/resource", "version": "8.0" } -} \ No newline at end of file +} diff --git a/doc/api_samples/os-agents/agent-post-resp.xml b/doc/api_samples/os-agents/agent-post-resp.xml index 79f62b7fb9..abfe15f909 100644 --- a/doc/api_samples/os-agents/agent-post-resp.xml +++ b/doc/api_samples/os-agents/agent-post-resp.xml @@ -1,10 +1,10 @@ - xxxxxxxxxxxx + http://example.com/path/to/resource hypervisor add6bb58e139be103324d04d82d8f545 8.0 x86 os 1 - \ No newline at end of file + diff --git a/doc/api_samples/os-agents/agent-update-put-req.json b/doc/api_samples/os-agents/agent-update-put-req.json index e4eaf53525..f7398504d6 100644 --- a/doc/api_samples/os-agents/agent-update-put-req.json +++ b/doc/api_samples/os-agents/agent-update-put-req.json @@ -1,7 +1,7 @@ { "para": { - "url": "xxx://xxxx/xxx/xxx", + "url": "http://example.com/path/to/resource", "md5hash": "add6bb58e139be103324d04d82d8f545", "version": "7.0" } -} \ No newline at end of file +} diff --git a/doc/api_samples/os-agents/agent-update-put-req.xml b/doc/api_samples/os-agents/agent-update-put-req.xml index f759880c17..9a25cefdda 100644 --- a/doc/api_samples/os-agents/agent-update-put-req.xml +++ b/doc/api_samples/os-agents/agent-update-put-req.xml @@ -1,6 +1,6 @@ 7.0 - xxx://xxxx/xxx/xxx + http://example.com/path/to/resource add6bb58e139be103324d04d82d8f545 - \ No newline at end of file + diff --git a/doc/api_samples/os-agents/agent-update-put-resp.json b/doc/api_samples/os-agents/agent-update-put-resp.json index 6b67222c8c..2919d21388 100644 --- a/doc/api_samples/os-agents/agent-update-put-resp.json +++ b/doc/api_samples/os-agents/agent-update-put-resp.json @@ -2,7 +2,7 @@ "agent": { "agent_id": "1", "md5hash": "add6bb58e139be103324d04d82d8f545", - "url": "xxx://xxxx/xxx/xxx", + "url": "http://example.com/path/to/resource", "version": "7.0" } -} \ No newline at end of file +} diff --git a/doc/api_samples/os-agents/agent-update-put-resp.xml b/doc/api_samples/os-agents/agent-update-put-resp.xml index badf2750ea..ce62db3868 100644 --- a/doc/api_samples/os-agents/agent-update-put-resp.xml +++ b/doc/api_samples/os-agents/agent-update-put-resp.xml @@ -1,7 +1,7 @@ - xxx://xxxx/xxx/xxx + http://example.com/path/to/resource 7.0 1 add6bb58e139be103324d04d82d8f545 - \ No newline at end of file + diff --git a/doc/api_samples/os-agents/agents-get-resp.json b/doc/api_samples/os-agents/agents-get-resp.json index 73ba45c240..92e14e1dc5 100644 --- a/doc/api_samples/os-agents/agents-get-resp.json +++ b/doc/api_samples/os-agents/agents-get-resp.json @@ -6,8 +6,8 @@ "hypervisor": "hypervisor", "md5hash": "add6bb58e139be103324d04d82d8f545", "os": "os", - "url": "xxxxxxxxxxxx", + "url": "http://example.com/path/to/resource", "version": "8.0" } ] -} \ No newline at end of file +} diff --git a/doc/api_samples/os-agents/agents-get-resp.xml b/doc/api_samples/os-agents/agents-get-resp.xml index 4194f62c96..d804245305 100644 --- a/doc/api_samples/os-agents/agents-get-resp.xml +++ b/doc/api_samples/os-agents/agents-get-resp.xml @@ -1,4 +1,4 @@ - - \ No newline at end of file + + diff --git a/doc/v3/api_samples/os-agents/agent-post-req.json b/doc/v3/api_samples/os-agents/agent-post-req.json index 217993b17f..1913498547 100644 --- a/doc/v3/api_samples/os-agents/agent-post-req.json +++ b/doc/v3/api_samples/os-agents/agent-post-req.json @@ -5,6 +5,6 @@ "architecture": "x86", "version": "8.0", "md5hash": "add6bb58e139be103324d04d82d8f545", - "url": "xxxxxxxxxxxx" + "url": "http://example.com/path/to/resource" } -} \ No newline at end of file +} diff --git a/doc/v3/api_samples/os-agents/agent-post-resp.json b/doc/v3/api_samples/os-agents/agent-post-resp.json index f6c760cc67..24ddede90b 100644 --- a/doc/v3/api_samples/os-agents/agent-post-resp.json +++ b/doc/v3/api_samples/os-agents/agent-post-resp.json @@ -5,7 +5,7 @@ "hypervisor": "hypervisor", "md5hash": "add6bb58e139be103324d04d82d8f545", "os": "os", - "url": "xxxxxxxxxxxx", + "url": "http://example.com/path/to/resource", "version": "8.0" } -} \ No newline at end of file +} diff --git a/doc/v3/api_samples/os-agents/agent-update-put-req.json b/doc/v3/api_samples/os-agents/agent-update-put-req.json index e166abf9ee..89cbcaba39 100644 --- a/doc/v3/api_samples/os-agents/agent-update-put-req.json +++ b/doc/v3/api_samples/os-agents/agent-update-put-req.json @@ -1,6 +1,6 @@ { "agent": { - "url": "xxx://xxxx/xxx/xxx", + "url": "http://example.com/path/to/resource", "md5hash": "add6bb58e139be103324d04d82d8f545", "version": "7.0" } diff --git a/doc/v3/api_samples/os-agents/agent-update-put-resp.json b/doc/v3/api_samples/os-agents/agent-update-put-resp.json index 866994e4c9..2964c0f894 100644 --- a/doc/v3/api_samples/os-agents/agent-update-put-resp.json +++ b/doc/v3/api_samples/os-agents/agent-update-put-resp.json @@ -2,7 +2,7 @@ "agent": { "agent_id": 1, "md5hash": "add6bb58e139be103324d04d82d8f545", - "url": "xxx://xxxx/xxx/xxx", + "url": "http://example.com/path/to/resource", "version": "7.0" } -} \ No newline at end of file +} diff --git a/doc/v3/api_samples/os-agents/agents-get-resp.json b/doc/v3/api_samples/os-agents/agents-get-resp.json index 73ba45c240..92e14e1dc5 100644 --- a/doc/v3/api_samples/os-agents/agents-get-resp.json +++ b/doc/v3/api_samples/os-agents/agents-get-resp.json @@ -6,8 +6,8 @@ "hypervisor": "hypervisor", "md5hash": "add6bb58e139be103324d04d82d8f545", "os": "os", - "url": "xxxxxxxxxxxx", + "url": "http://example.com/path/to/resource", "version": "8.0" } ] -} \ No newline at end of file +} diff --git a/nova/api/validation/validators.py b/nova/api/validation/validators.py index ce74923756..0974fa177d 100644 --- a/nova/api/validation/validators.py +++ b/nova/api/validation/validators.py @@ -17,6 +17,7 @@ Internal implementation of request Body validating middleware. """ import jsonschema +import rfc3986 import six from nova import exception @@ -40,6 +41,12 @@ def _validate_uuid_format(instance): return uuidutils.is_uuid_like(instance) +@jsonschema.FormatChecker.cls_checks('uri') +def _validate_uri(instance): + return rfc3986.is_valid_uri(instance, require_scheme=True, + require_authority=True) + + class _SchemaValidator(object): """A validator class diff --git a/nova/api/validator.py b/nova/api/validator.py index 2e8462f0d2..cce621849d 100644 --- a/nova/api/validator.py +++ b/nova/api/validator.py @@ -14,8 +14,8 @@ # under the License. import base64 -import re +import rfc3986 import six from nova.openstack.common import log as logging @@ -24,21 +24,6 @@ from nova.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_str(max_length=None): def _do(val): @@ -69,7 +54,9 @@ def validate_url_path(val): if not validate_str()(val): return False - return VALIDATE_PATH_RE.match(val).end() == len(val) + uri = rfc3986.URIReference(None, None, val, None, None) + + return uri.path_is_valid() and val.startswith('/') def validate_image_path(val): diff --git a/nova/tests/api/openstack/compute/contrib/test_agents.py b/nova/tests/api/openstack/compute/contrib/test_agents.py index 7373efeb27..e287a4d2dd 100644 --- a/nova/tests/api/openstack/compute/contrib/test_agents.py +++ b/nova/tests/api/openstack/compute/contrib/test_agents.py @@ -24,25 +24,25 @@ from nova import test fake_agents_list = [{'hypervisor': 'kvm', 'os': 'win', 'architecture': 'x86', 'version': '7.0', - 'url': 'xxx://xxxx/xxx/xxx', + 'url': 'http://example.com/path/to/resource', 'md5hash': 'add6bb58e139be103324d04d82d8f545', 'id': 1}, {'hypervisor': 'kvm', 'os': 'linux', 'architecture': 'x86', 'version': '16.0', - 'url': 'xxx://xxxx/xxx/xxx1', + 'url': 'http://example.com/path/to/resource1', 'md5hash': 'add6bb58e139be103324d04d82d8f546', 'id': 2}, {'hypervisor': 'xen', 'os': 'linux', 'architecture': 'x86', 'version': '16.0', - 'url': 'xxx://xxxx/xxx/xxx2', + 'url': 'http://example.com/path/to/resource2', 'md5hash': 'add6bb58e139be103324d04d82d8f547', 'id': 3}, {'hypervisor': 'xen', 'os': 'win', 'architecture': 'power', 'version': '7.0', - 'url': 'xxx://xxxx/xxx/xxx3', + 'url': 'http://example.com/path/to/resource3', 'md5hash': 'add6bb58e139be103324d04d82d8f548', 'id': 4}, ] @@ -106,13 +106,13 @@ class AgentsTest(test.NoDBTestCase): 'os': 'win', 'architecture': 'x86', 'version': '7.0', - 'url': 'xxx://xxxx/xxx/xxx', + 'url': 'http://example.com/path/to/resource', 'md5hash': 'add6bb58e139be103324d04d82d8f545'}} response = {'agent': {'hypervisor': 'kvm', 'os': 'win', 'architecture': 'x86', 'version': '7.0', - 'url': 'xxx://xxxx/xxx/xxx', + 'url': 'http://example.com/path/to/resource', 'md5hash': 'add6bb58e139be103324d04d82d8f545', 'agent_id': 1}} res_dict = self.controller.create(req, body) @@ -151,7 +151,7 @@ class AgentsTest(test.NoDBTestCase): 'os': 'win', 'architecture': 'x86', 'version': '7.0', - 'url': 'xxx://xxxx/xxx/xxx', + 'url': 'http://example.com/path/to/resource', 'md5hash': 'add6bb58e139be103324d04d82d8f545'}} body['agent'][key] = 'x' * 256 self.assertRaises(webob.exc.HTTPBadRequest, @@ -185,25 +185,25 @@ class AgentsTest(test.NoDBTestCase): agents_list = [{'hypervisor': 'kvm', 'os': 'win', 'architecture': 'x86', 'version': '7.0', - 'url': 'xxx://xxxx/xxx/xxx', + 'url': 'http://example.com/path/to/resource', 'md5hash': 'add6bb58e139be103324d04d82d8f545', 'agent_id': 1}, {'hypervisor': 'kvm', 'os': 'linux', 'architecture': 'x86', 'version': '16.0', - 'url': 'xxx://xxxx/xxx/xxx1', + 'url': 'http://example.com/path/to/resource1', 'md5hash': 'add6bb58e139be103324d04d82d8f546', 'agent_id': 2}, {'hypervisor': 'xen', 'os': 'linux', 'architecture': 'x86', 'version': '16.0', - 'url': 'xxx://xxxx/xxx/xxx2', + 'url': 'http://example.com/path/to/resource2', 'md5hash': 'add6bb58e139be103324d04d82d8f547', 'agent_id': 3}, {'hypervisor': 'xen', 'os': 'win', 'architecture': 'power', 'version': '7.0', - 'url': 'xxx://xxxx/xxx/xxx3', + 'url': 'http://example.com/path/to/resource3', 'md5hash': 'add6bb58e139be103324d04d82d8f548', 'agent_id': 4}, ] @@ -215,13 +215,13 @@ class AgentsTest(test.NoDBTestCase): response = [{'hypervisor': 'kvm', 'os': 'win', 'architecture': 'x86', 'version': '7.0', - 'url': 'xxx://xxxx/xxx/xxx', + 'url': 'http://example.com/path/to/resource', 'md5hash': 'add6bb58e139be103324d04d82d8f545', 'agent_id': 1}, {'hypervisor': 'kvm', 'os': 'linux', 'architecture': 'x86', 'version': '16.0', - 'url': 'xxx://xxxx/xxx/xxx1', + 'url': 'http://example.com/path/to/resource1', 'md5hash': 'add6bb58e139be103324d04d82d8f546', 'agent_id': 2}, ] @@ -230,11 +230,11 @@ class AgentsTest(test.NoDBTestCase): def test_agents_update(self): req = FakeRequest() body = {'para': {'version': '7.0', - 'url': 'xxx://xxxx/xxx/xxx', + 'url': 'http://example.com/path/to/resource', 'md5hash': 'add6bb58e139be103324d04d82d8f545'}} response = {'agent': {'agent_id': 1, 'version': '7.0', - 'url': 'xxx://xxxx/xxx/xxx', + 'url': 'http://example.com/path/to/resource', 'md5hash': 'add6bb58e139be103324d04d82d8f545'}} res_dict = self.controller.update(req, 1, body) self.assertEqual(res_dict, response) @@ -258,7 +258,7 @@ class AgentsTest(test.NoDBTestCase): def _test_agents_update_with_invalid_length(self, key): req = FakeRequest() body = {'para': {'version': '7.0', - 'url': 'xxx://xxxx/xxx/xxx', + 'url': 'http://example.com/path/to/resource', 'md5hash': 'add6bb58e139be103324d04d82d8f545'}} body['para'][key] = 'x' * 256 self.assertRaises(webob.exc.HTTPBadRequest, diff --git a/nova/tests/api/openstack/compute/plugins/v3/test_agents.py b/nova/tests/api/openstack/compute/plugins/v3/test_agents.py index e7a07fed2d..a4b140214d 100644 --- a/nova/tests/api/openstack/compute/plugins/v3/test_agents.py +++ b/nova/tests/api/openstack/compute/plugins/v3/test_agents.py @@ -23,25 +23,25 @@ from nova import test fake_agents_list = [{'hypervisor': 'kvm', 'os': 'win', 'architecture': 'x86', 'version': '7.0', - 'url': 'xxx://xxxx/xxx/xxx', + 'url': 'http://example.com/path/to/resource', 'md5hash': 'add6bb58e139be103324d04d82d8f545', 'id': 1}, {'hypervisor': 'kvm', 'os': 'linux', 'architecture': 'x86', 'version': '16.0', - 'url': 'xxx://xxxx/xxx/xxx1', + 'url': 'http://example.com/path/to/resource1', 'md5hash': 'add6bb58e139be103324d04d82d8f546', 'id': 2}, {'hypervisor': 'xen', 'os': 'linux', 'architecture': 'x86', 'version': '16.0', - 'url': 'xxx://xxxx/xxx/xxx2', + 'url': 'http://example.com/path/to/resource2', 'md5hash': 'add6bb58e139be103324d04d82d8f547', 'id': 3}, {'hypervisor': 'xen', 'os': 'win', 'architecture': 'power', 'version': '7.0', - 'url': 'xxx://xxxx/xxx/xxx3', + 'url': 'http://example.com/path/to/resource3', 'md5hash': 'add6bb58e139be103324d04d82d8f548', 'id': 4}, ] @@ -109,13 +109,13 @@ class AgentsTest(test.NoDBTestCase): 'os': 'win', 'architecture': 'x86', 'version': '7.0', - 'url': 'xxx://xxxx/xxx/xxx', + 'url': 'http://example.com/path/to/resource', 'md5hash': 'add6bb58e139be103324d04d82d8f545'}} response = {'agent': {'hypervisor': 'kvm', 'os': 'win', 'architecture': 'x86', 'version': '7.0', - 'url': 'xxx://xxxx/xxx/xxx', + 'url': 'http://example.com/path/to/resource', 'md5hash': 'add6bb58e139be103324d04d82d8f545', 'agent_id': 1}} res_dict = self.controller.create(req, body=body) @@ -130,7 +130,7 @@ class AgentsTest(test.NoDBTestCase): 'os': 'win', 'architecture': 'x86', 'version': '7.0', - 'url': 'xxx://xxxx/xxx/xxx', + 'url': 'http://example.com/path/to/resource', 'md5hash': 'add6bb58e139be103324d04d82d8f545'}} self.assertRaises(exc.HTTPConflict, self.controller.create, req, body=body) @@ -141,7 +141,7 @@ class AgentsTest(test.NoDBTestCase): 'os': 'win', 'architecture': 'x86', 'version': '7.0', - 'url': 'xxx://xxxx/xxx/xxx'}} + 'url': 'http://example.com/path/to/resource'}} self.assertRaises(exception.ValidationError, self.controller.create, req, body=body) @@ -160,7 +160,7 @@ class AgentsTest(test.NoDBTestCase): body = {'agent': {'hypervisor': 'kvm', 'os': 'win', 'architecture': 'x86', - 'url': 'xxx://xxxx/xxx/xxx', + 'url': 'http://example.com/path/to/resource', 'md5hash': 'add6bb58e139be103324d04d82d8f545'}} self.assertRaises(exception.ValidationError, self.controller.create, req, body=body) @@ -170,7 +170,7 @@ class AgentsTest(test.NoDBTestCase): body = {'agent': {'hypervisor': 'kvm', 'os': 'win', 'version': '7.0', - 'url': 'xxx://xxxx/xxx/xxx', + 'url': 'http://example.com/path/to/resource', 'md5hash': 'add6bb58e139be103324d04d82d8f545'}} self.assertRaises(exception.ValidationError, self.controller.create, req, body=body) @@ -180,7 +180,7 @@ class AgentsTest(test.NoDBTestCase): body = {'agent': {'hypervisor': 'kvm', 'architecture': 'x86', 'version': '7.0', - 'url': 'xxx://xxxx/xxx/xxx', + 'url': 'http://example.com/path/to/resource', 'md5hash': 'add6bb58e139be103324d04d82d8f545'}} self.assertRaises(exception.ValidationError, self.controller.create, req, body=body) @@ -190,7 +190,7 @@ class AgentsTest(test.NoDBTestCase): body = {'agent': {'os': 'win', 'architecture': 'x86', 'version': '7.0', - 'url': 'xxx://xxxx/xxx/xxx', + 'url': 'http://example.com/path/to/resource', 'md5hash': 'add6bb58e139be103324d04d82d8f545'}} self.assertRaises(exception.ValidationError, self.controller.create, req, body=body) @@ -213,7 +213,7 @@ class AgentsTest(test.NoDBTestCase): 'os': 'win', 'architecture': 'x86', 'version': '7.0', - 'url': 'xxx://xxxx/xxx/xxx', + 'url': 'http://example.com/path/to/resource', 'md5hash': 'add6bb58e139be103324d04d82d8f545'}} body['agent'][key] = 'x' * 256 self.assertRaises(exception.ValidationError, self.controller.create, @@ -247,25 +247,25 @@ class AgentsTest(test.NoDBTestCase): agents_list = [{'hypervisor': 'kvm', 'os': 'win', 'architecture': 'x86', 'version': '7.0', - 'url': 'xxx://xxxx/xxx/xxx', + 'url': 'http://example.com/path/to/resource', 'md5hash': 'add6bb58e139be103324d04d82d8f545', 'agent_id': 1}, {'hypervisor': 'kvm', 'os': 'linux', 'architecture': 'x86', 'version': '16.0', - 'url': 'xxx://xxxx/xxx/xxx1', + 'url': 'http://example.com/path/to/resource1', 'md5hash': 'add6bb58e139be103324d04d82d8f546', 'agent_id': 2}, {'hypervisor': 'xen', 'os': 'linux', 'architecture': 'x86', 'version': '16.0', - 'url': 'xxx://xxxx/xxx/xxx2', + 'url': 'http://example.com/path/to/resource2', 'md5hash': 'add6bb58e139be103324d04d82d8f547', 'agent_id': 3}, {'hypervisor': 'xen', 'os': 'win', 'architecture': 'power', 'version': '7.0', - 'url': 'xxx://xxxx/xxx/xxx3', + 'url': 'http://example.com/path/to/resource3', 'md5hash': 'add6bb58e139be103324d04d82d8f548', 'agent_id': 4}, ] @@ -277,13 +277,13 @@ class AgentsTest(test.NoDBTestCase): response = [{'hypervisor': 'kvm', 'os': 'win', 'architecture': 'x86', 'version': '7.0', - 'url': 'xxx://xxxx/xxx/xxx', + 'url': 'http://example.com/path/to/resource', 'md5hash': 'add6bb58e139be103324d04d82d8f545', 'agent_id': 1}, {'hypervisor': 'kvm', 'os': 'linux', 'architecture': 'x86', 'version': '16.0', - 'url': 'xxx://xxxx/xxx/xxx1', + 'url': 'http://example.com/path/to/resource1', 'md5hash': 'add6bb58e139be103324d04d82d8f546', 'agent_id': 2}, ] @@ -292,11 +292,11 @@ class AgentsTest(test.NoDBTestCase): def test_agents_update(self): req = FakeRequest() body = {'agent': {'version': '7.0', - 'url': 'xxx://xxxx/xxx/xxx', + 'url': 'http://example.com/path/to/resource', 'md5hash': 'add6bb58e139be103324d04d82d8f545'}} response = {'agent': {'agent_id': 1, 'version': '7.0', - 'url': 'xxx://xxxx/xxx/xxx', + 'url': 'http://example.com/path/to/resource', 'md5hash': 'add6bb58e139be103324d04d82d8f545'}} res_dict = self.controller.update(req, 1, body=body) self.assertEqual(res_dict, response) @@ -304,7 +304,7 @@ class AgentsTest(test.NoDBTestCase): def test_agents_update_without_md5hash(self): req = FakeRequest() body = {'agent': {'version': '7.0', - 'url': 'xxx://xxxx/xxx/xxx'}} + 'url': 'http://example.com/path/to/resource'}} self.assertRaises(exception.ValidationError, self.controller.update, req, 1, body=body) @@ -335,7 +335,7 @@ class AgentsTest(test.NoDBTestCase): def _test_agents_update_with_invalid_length(self, key): req = FakeRequest() body = {'agent': {'version': '7.0', - 'url': 'xxx://xxxx/xxx/xxx', + 'url': 'http://example.com/path/to/resource', 'md5hash': 'add6bb58e139be103324d04d82d8f545'}} body['agent'][key] = 'x' * 256 self.assertRaises(exception.ValidationError, self.controller.update, diff --git a/nova/tests/integrated/test_api_samples.py b/nova/tests/integrated/test_api_samples.py index 4707a74889..f1be700c22 100644 --- a/nova/tests/integrated/test_api_samples.py +++ b/nova/tests/integrated/test_api_samples.py @@ -1378,7 +1378,7 @@ class AgentsJsonTest(ApiSampleTestBaseV2): def setUp(self): super(AgentsJsonTest, self).setUp() - fake_agents_list = [{'url': 'xxxxxxxxxxxx', + fake_agents_list = [{'url': 'http://example.com/path/to/resource', 'hypervisor': 'hypervisor', 'architecture': 'x86', 'os': 'os', @@ -1419,7 +1419,7 @@ class AgentsJsonTest(ApiSampleTestBaseV2): def test_agent_create(self): # Creates a new agent build. - project = {'url': 'xxxxxxxxxxxx', + project = {'url': 'http://example.com/path/to/resource', 'hypervisor': 'hypervisor', 'architecture': 'x86', 'os': 'os', @@ -1435,7 +1435,7 @@ class AgentsJsonTest(ApiSampleTestBaseV2): def test_agent_list(self): # Return a list of all agent builds. response = self._do_get('os-agents') - project = {'url': 'xxxxxxxxxxxx', + project = {'url': 'http://example.com/path/to/resource', 'hypervisor': 'hypervisor', 'architecture': 'x86', 'os': 'os', @@ -1449,7 +1449,7 @@ class AgentsJsonTest(ApiSampleTestBaseV2): # Update an existing agent build. agent_id = 1 subs = {'version': '7.0', - 'url': 'xxx://xxxx/xxx/xxx', + 'url': 'http://example.com/path/to/resource', 'md5hash': 'add6bb58e139be103324d04d82d8f545'} response = self._do_put('os-agents/%s' % agent_id, 'agent-update-put-req', subs) diff --git a/nova/tests/integrated/v3/api_samples/os-agents/agent-post-resp.json.tpl b/nova/tests/integrated/v3/api_samples/os-agents/agent-post-resp.json.tpl index f6c760cc67..24ddede90b 100644 --- a/nova/tests/integrated/v3/api_samples/os-agents/agent-post-resp.json.tpl +++ b/nova/tests/integrated/v3/api_samples/os-agents/agent-post-resp.json.tpl @@ -5,7 +5,7 @@ "hypervisor": "hypervisor", "md5hash": "add6bb58e139be103324d04d82d8f545", "os": "os", - "url": "xxxxxxxxxxxx", + "url": "http://example.com/path/to/resource", "version": "8.0" } -} \ No newline at end of file +} diff --git a/nova/tests/integrated/v3/api_samples/os-agents/agent-update-put-resp.json.tpl b/nova/tests/integrated/v3/api_samples/os-agents/agent-update-put-resp.json.tpl index 866994e4c9..2964c0f894 100644 --- a/nova/tests/integrated/v3/api_samples/os-agents/agent-update-put-resp.json.tpl +++ b/nova/tests/integrated/v3/api_samples/os-agents/agent-update-put-resp.json.tpl @@ -2,7 +2,7 @@ "agent": { "agent_id": 1, "md5hash": "add6bb58e139be103324d04d82d8f545", - "url": "xxx://xxxx/xxx/xxx", + "url": "http://example.com/path/to/resource", "version": "7.0" } -} \ No newline at end of file +} diff --git a/nova/tests/integrated/v3/api_samples/os-agents/agents-get-resp.json.tpl b/nova/tests/integrated/v3/api_samples/os-agents/agents-get-resp.json.tpl index 73ba45c240..92e14e1dc5 100644 --- a/nova/tests/integrated/v3/api_samples/os-agents/agents-get-resp.json.tpl +++ b/nova/tests/integrated/v3/api_samples/os-agents/agents-get-resp.json.tpl @@ -6,8 +6,8 @@ "hypervisor": "hypervisor", "md5hash": "add6bb58e139be103324d04d82d8f545", "os": "os", - "url": "xxxxxxxxxxxx", + "url": "http://example.com/path/to/resource", "version": "8.0" } ] -} \ No newline at end of file +} diff --git a/nova/tests/integrated/v3/test_agents.py b/nova/tests/integrated/v3/test_agents.py index 99a53bcf51..bdb3b4e220 100644 --- a/nova/tests/integrated/v3/test_agents.py +++ b/nova/tests/integrated/v3/test_agents.py @@ -24,7 +24,7 @@ class AgentsJsonTest(api_sample_base.ApiSampleTestBaseV3): def setUp(self): super(AgentsJsonTest, self).setUp() - fake_agents_list = [{'url': 'xxxxxxxxxxxx', + fake_agents_list = [{'url': 'http://example.com/path/to/resource', 'hypervisor': 'hypervisor', 'architecture': 'x86', 'os': 'os', @@ -65,7 +65,7 @@ class AgentsJsonTest(api_sample_base.ApiSampleTestBaseV3): def test_agent_create(self): # Creates a new agent build. - project = {'url': 'xxxxxxxxxxxx', + project = {'url': 'http://example.com/path/to/resource', 'hypervisor': 'hypervisor', 'architecture': 'x86', 'os': 'os', @@ -85,7 +85,7 @@ class AgentsJsonTest(api_sample_base.ApiSampleTestBaseV3): # Update an existing agent build. agent_id = 1 subs = {'version': '7.0', - 'url': 'xxx://xxxx/xxx/xxx', + 'url': 'http://example.com/path/to/resource', 'md5hash': 'add6bb58e139be103324d04d82d8f545'} response = self._do_put('os-agents/%s' % agent_id, 'agent-update-put-req', subs) diff --git a/nova/tests/test_api_validation.py b/nova/tests/test_api_validation.py index e055817489..a30a5fe92d 100644 --- a/nova/tests/test_api_validation.py +++ b/nova/tests/test_api_validation.py @@ -692,6 +692,64 @@ class UuidTestCase(APIValidationTestCase): expected_detail=detail) +class UriTestCase(APIValidationTestCase): + + def setUp(self): + super(UriTestCase, self).setUp() + schema = { + 'type': 'object', + 'properties': { + 'foo': { + 'type': 'string', + 'format': 'uri', + }, + }, + } + + @validation.schema(request_body_schema=schema) + def post(body): + return 'Validation succeeded.' + + self.post = post + + def test_validate_uri(self): + self.assertEqual('Validation succeeded.', + self.post( + body={'foo': 'http://localhost:8774/v2/servers'} + )) + self.assertEqual('Validation succeeded.', + self.post( + body={'foo': 'http://[::1]:8774/v2/servers'} + )) + + def test_validate_uri_fails(self): + base_detail = ("Invalid input for field/attribute foo. Value: {0}. " + "'{0}' is not a 'uri'") + invalid_uri = 'http://localhost:8774/v2/servers##' + self.check_validation_error(self.post, + body={'foo': invalid_uri}, + expected_detail=base_detail.format( + invalid_uri)) + + invalid_uri = 'http://[fdf8:01]:8774/v2/servers' + self.check_validation_error(self.post, + body={'foo': invalid_uri}, + expected_detail=base_detail.format( + invalid_uri)) + + invalid_uri = '1' + self.check_validation_error(self.post, + body={'foo': invalid_uri}, + expected_detail=base_detail.format( + invalid_uri)) + + invalid_uri = 'abc' + self.check_validation_error(self.post, + body={'foo': invalid_uri}, + expected_detail=base_detail.format( + invalid_uri)) + + class Ipv4TestCase(APIValidationTestCase): def setUp(self): diff --git a/requirements.txt b/requirements.txt index 7fb504cc45..9cf68daee2 100644 --- a/requirements.txt +++ b/requirements.txt @@ -37,3 +37,4 @@ pycadf>=0.5.1 oslo.messaging>=1.4.0.0a3 oslo.i18n>=0.1.0 # Apache-2.0 lockfile>=0.8 +rfc3986>=0.2.0 # Apache-2.0