Prevent null key setting for property

null key is the invalid input when setting property
in the openstack components.

Prevent null key setting for property in
openstackclient.

Change-Id: I8ec72664b5393ffef71fffa4bd1cd3c303e86a52
Related-Bug: #1558690
This commit is contained in:
qtang 2016-07-21 18:06:35 +08:00 committed by Dean Troyer
parent ef4a4f143b
commit 7dbc92b521
3 changed files with 36 additions and 16 deletions

View File

@ -33,7 +33,13 @@ class KeyValueAction(argparse.Action):
# Add value if an assignment else remove it
if '=' in values:
getattr(namespace, self.dest, {}).update([values.split('=', 1)])
values_list = values.split('=', 1)
# NOTE(qtang): Prevent null key setting in property
if '' == values_list[0]:
msg = _("Property key must be specified: %s")
raise argparse.ArgumentTypeError(msg % str(values))
else:
getattr(namespace, self.dest, {}).update([values_list])
else:
msg = _("Expected 'key=value' type, but got: %s")
raise argparse.ArgumentTypeError(msg % str(values))
@ -88,7 +94,13 @@ class MultiKeyValueAction(argparse.Action):
for kv in values.split(','):
# Add value if an assignment else raise ArgumentTypeError
if '=' in kv:
params.update([kv.split('=', 1)])
kv_list = kv.split('=', 1)
# NOTE(qtang): Prevent null key setting in property
if '' == kv_list[0]:
msg = _("Each property key must be specified: %s")
raise argparse.ArgumentTypeError(msg % str(kv))
else:
params.update([kv_list])
else:
msg = _(
"Expected comma separated 'key=value' pairs, but got: %s"

View File

@ -49,13 +49,14 @@ class TestKeyValueAction(utils.TestCase):
self.assertEqual(expect, actual)
def test_error_values(self):
self.assertRaises(
argparse.ArgumentTypeError,
self.parser.parse_args,
[
'--property', 'red',
]
)
data_list = [
['--property', 'red', ],
['--property', '=', ],
['--property', '=red', ]
]
for data in data_list:
self.assertRaises(argparse.ArgumentTypeError,
self.parser.parse_args, data)
class TestMultiKeyValueAction(utils.TestCase):
@ -115,13 +116,14 @@ class TestMultiKeyValueAction(utils.TestCase):
self.assertItemsEqual(expect, actual)
def test_error_values_with_comma(self):
self.assertRaises(
argparse.ArgumentTypeError,
self.parser.parse_args,
[
'--test', 'mmm,nnn=zzz',
]
)
data_list = [
['--test', 'mmm,nnn=zzz', ],
['--test', 'nnn=zzz,=', ],
['--test', 'nnn=zzz,=zzz', ]
]
for data in data_list:
self.assertRaises(argparse.ArgumentTypeError,
self.parser.parse_args, data)
def test_error_values_without_comma(self):
self.assertRaises(

View File

@ -0,0 +1,6 @@
---
fixes:
- |
Prevent null key setting for key-value pairs in the ``KeyValueAction``
and ``MultiKeyValueAction`` parser actions.
[Bug `1558690 <https://bugs.launchpad.net/bugs/1558690>`_]