Skip to content

Commit 6f92249

Browse files
committed
MB-65338 Handle invalid argument to --encrypt-with-key
Change-Id: Ibc851c4e9af991b6bd1557cee0cf910c556408ba Reviewed-on: https://review.couchbase.org/c/couchbase-cli/+/224273 Tested-by: Build Bot <build@couchbase.com> Reviewed-by: Safian Ali <safian.ali@couchbase.com>
1 parent 5894724 commit 6f92249

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

cbmgr.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3358,6 +3358,13 @@ def _add_edit_parse_opts(self, opts):
33583358
if not opts.name:
33593359
_exit_if_errors(["--name must be specified"])
33603360

3361+
encrypt_with_key = 0
3362+
try:
3363+
if opts.encrypt_with_key:
3364+
encrypt_with_key = int(opts.encrypt_with_key)
3365+
except ValueError:
3366+
_exit_if_errors(["--encrypt-with-key's argument must be a number"])
3367+
33613368
usages = []
33623369
if opts.config_usage:
33633370
usages.append("config-encryption")
@@ -3409,7 +3416,7 @@ def _add_edit_parse_opts(self, opts):
34093416
data["encryptWith"] = "nodeSecretManager"
34103417
else:
34113418
data["encryptWith"] = "encryptionKey"
3412-
data["encryptWithKeyId"] = int(opts.encrypt_with_key)
3419+
data["encryptWithKeyId"] = encrypt_with_key
34133420

34143421
data["activeKey"] = {"kmipId": opts.kmip_key}
34153422
data["host"] = opts.kmip_host
@@ -3437,7 +3444,7 @@ def _add_edit_parse_opts(self, opts):
34373444
data["encryptWith"] = "nodeSecretManager"
34383445
else:
34393446
data["encryptWith"] = "encryptionKey"
3440-
data["encryptWithKeyId"] = int(opts.encrypt_with_key)
3447+
data["encryptWithKeyId"] = encrypt_with_key
34413448

34423449
if (opts.auto_rotate_every and not opts.auto_rotate_start) or \
34433450
(opts.auto_rotate_start and not opts.auto_rotate_every):

test/test_cli.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1482,6 +1482,16 @@ def test_add_edit_key_auto_one_rotation_opt(self):
14821482
self.system_exit_run(self.command + args, None, start_server=False)
14831483
self.assertIn('--auto-rotate-every must be provided with --auto-rotate-start-on', self.str_output)
14841484

1485+
def test_add_edit_key_invalid_id(self):
1486+
self.server.set_args(self.server_args)
1487+
self.server.run()
1488+
1489+
for base_args in [['--add-key'], ['--edit-key', '1']]:
1490+
args = base_args + ['--name', 'key01', '--kek-usage', '--key-type', 'auto-generated',
1491+
'--encrypt-with-key', 'foo']
1492+
self.system_exit_run(self.command + args, None, start_server=False)
1493+
self.assertIn("--encrypt-with-key's argument must be a number", self.str_output)
1494+
14851495
def test_add_key_auto(self):
14861496
self.server.set_args(self.server_args)
14871497
self.server.run()

0 commit comments

Comments
 (0)