From 6d03c4c54370bb0ca91c4cec4ac929ab6a4275eb Mon Sep 17 00:00:00 2001 From: licanwei Date: Fri, 19 Jan 2018 00:56:04 -0800 Subject: [PATCH] check audit name length No more than 63 characters Change-Id: I52adbd7e9f12dd4a8b6977756d788ee0e5d6391a Closes-Bug: #1744231 --- watcher/api/controllers/v1/audit.py | 9 +++++++++ watcher/tests/api/v1/test_audits.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/watcher/api/controllers/v1/audit.py b/watcher/api/controllers/v1/audit.py index 2e296ab36..c72b382a5 100644 --- a/watcher/api/controllers/v1/audit.py +++ b/watcher/api/controllers/v1/audit.py @@ -37,6 +37,8 @@ import wsme from wsme import types as wtypes import wsmeext.pecan as wsme_pecan +from oslo_log import log + from watcher._i18n import _ from watcher.api.controllers import base from watcher.api.controllers import link @@ -49,6 +51,8 @@ from watcher.common import utils from watcher.decision_engine import rpcapi from watcher import objects +LOG = log.getLogger(__name__) + class AuditPostType(wtypes.Base): @@ -129,6 +133,11 @@ class AuditPostType(wtypes.Base): goal = objects.Goal.get(context, self.goal) self.name = "%s-%s" % (goal.name, datetime.datetime.utcnow().isoformat()) + # No more than 63 characters + if len(self.name) > 63: + LOG.warning("Audit: %s length exceeds 63 characters", + self.name) + self.name = self.name[0:63] return Audit( name=self.name, diff --git a/watcher/tests/api/v1/test_audits.py b/watcher/tests/api/v1/test_audits.py index 51db0e344..a82366d3a 100644 --- a/watcher/tests/api/v1/test_audits.py +++ b/watcher/tests/api/v1/test_audits.py @@ -806,6 +806,35 @@ class TestPost(api_base.FunctionalTest): strategy_id=strategy['id'], uuid=template_uuid, name=template_name) return audit_template + @mock.patch.object(deapi.DecisionEngineAPI, 'trigger_audit') + @mock.patch('oslo_utils.timeutils.utcnow') + def test_create_audit_with_name(self, mock_utcnow, mock_trigger_audit): + mock_trigger_audit.return_value = mock.ANY + test_time = datetime.datetime(2000, 1, 1, 0, 0) + mock_utcnow.return_value = test_time + + audit_dict = post_get_test_audit() + normal_name = 'this audit name is just for test' + # long_name length exceeds 63 characters + long_name = normal_name+audit_dict['uuid'] + del audit_dict['uuid'] + del audit_dict['state'] + del audit_dict['interval'] + del audit_dict['scope'] + del audit_dict['next_run_time'] + + audit_dict['name'] = normal_name + response = self.post_json('/audits', audit_dict) + self.assertEqual('application/json', response.content_type) + self.assertEqual(201, response.status_int) + self.assertEqual(normal_name, response.json['name']) + + audit_dict['name'] = long_name + response = self.post_json('/audits', audit_dict) + self.assertEqual('application/json', response.content_type) + self.assertEqual(201, response.status_int) + self.assertNotEqual(long_name, response.json['name']) + class TestDelete(api_base.FunctionalTest):