Add server name verification in instance search

There are Nova API calls that can accept a server name
as a regex parameter. For example, if the command
'nova list --name <some_pattern>' is invoked with
an incorrect value of the pattern, the nova-api makes
a request to database and returns Http 500 error. Seems
it's not convenient.

This fix checks the pattern. If it isn't correct,
the nova-api returns error 400 with a prompt of a bad regex.

Change-Id: Iad63c668d09ec6a82ace29700fb4949c1acfbe1c
Closes-Bug: #1603728
This commit is contained in:
Anton Kremenetsky 2016-07-20 13:24:43 -04:00
parent f076a12078
commit d01695886f
2 changed files with 15 additions and 0 deletions

View File

@ -14,6 +14,8 @@
# License for the specific language governing permissions and limitations
# under the License.
import re
from oslo_log import log as logging
import oslo_messaging as messaging
from oslo_utils import strutils
@ -260,6 +262,14 @@ class ServersController(wsgi.Controller):
msg = _("Only administrators may list deleted instances")
raise exc.HTTPForbidden(explanation=msg)
# Verify the value of the 'name' option is a correct regex.
if 'name' in search_opts:
try:
re.compile(search_opts['name'])
except re.error:
msg = _("The regex for server name is incorrect")
raise exc.HTTPBadRequest(explanation=msg)
if api_version_request.is_supported(req, min_version='2.26'):
for tag_filter in TAG_SEARCH_FILTERS:
if tag_filter in search_opts:

View File

@ -585,6 +585,11 @@ class ServersControllerTest(ControllerTest):
num_servers = len(res_dict['servers'])
self.assertEqual(0, num_servers)
def test_get_server_details_with_bad_name(self):
req = self.req('/fake/servers/detail?name=%2Binstance')
self.assertRaises(webob.exc.HTTPBadRequest,
self.controller.index, req)
def test_get_server_details_with_limit(self):
req = self.req('/fake/servers/detail?limit=3')
res = self.controller.detail(req)