From 0c68b82a2f993e00230c9a20f0710b8b4b304dc9 Mon Sep 17 00:00:00 2001 From: Cameron Porter Date: Mon, 15 Jan 2018 15:27:57 -0600 Subject: [PATCH 1/2] Improve error conditions when adding SSH keys, for the case where neither an ssh key is provided via an argument nor from a file, and for the case where an ssh key is provided via an argument and from a file at the same time. --- SoftLayer/CLI/sshkey/add.py | 11 +++++++++++ SoftLayer/managers/sshkey.py | 1 + tests/CLI/modules/sshkey_tests.py | 15 +++++++++++++++ 3 files changed, 27 insertions(+) diff --git a/SoftLayer/CLI/sshkey/add.py b/SoftLayer/CLI/sshkey/add.py index 49920a070..a3a3c6ccb 100644 --- a/SoftLayer/CLI/sshkey/add.py +++ b/SoftLayer/CLI/sshkey/add.py @@ -6,6 +6,7 @@ import SoftLayer from SoftLayer.CLI import environment +from SoftLayer.CLI import exceptions @click.command() @@ -19,6 +20,16 @@ def cli(env, label, in_file, key, note): """Add a new SSH key.""" + if in_file is None and key is None: + raise exceptions.ArgumentError( + 'Either [-f | --in-file] or [-k | --key] arguments are required to add a key' + ) + + if in_file and key: + raise exceptions.ArgumentError( + '[-f | --in-file] is not allowed with [-k | --key]' + ) + if key: key_text = key else: diff --git a/SoftLayer/managers/sshkey.py b/SoftLayer/managers/sshkey.py index a294d5257..8aac63472 100644 --- a/SoftLayer/managers/sshkey.py +++ b/SoftLayer/managers/sshkey.py @@ -28,6 +28,7 @@ def add_key(self, key, label, notes=None): :param string key: The SSH key to add :param string label: The label for the key + :param string notes: Additional notes for the key :returns: A dictionary of the new key's information. """ order = { diff --git a/tests/CLI/modules/sshkey_tests.py b/tests/CLI/modules/sshkey_tests.py index e0e79b4cd..6b568839d 100644 --- a/tests/CLI/modules/sshkey_tests.py +++ b/tests/CLI/modules/sshkey_tests.py @@ -12,9 +12,24 @@ import mock from SoftLayer import testing +from SoftLayer.CLI import exceptions class SshKeyTests(testing.TestCase): + def test_add_without_key_errors(self): + result = self.run_command(['sshkey', 'add', 'key1']) + + self.assertEqual(result.exit_code, 2) + self.assertIsInstance(result.exception, exceptions.ArgumentError) + + def test_add_with_key_file_and_key_argument_errors(self): + path = os.path.join(testing.FIXTURE_PATH, 'id_rsa.pub') + result = self.run_command(['sshkey', 'add', 'key1', + '--key=some_key', + '--in-file=%s' % path]) + + self.assertEqual(result.exit_code, 2) + self.assertIsInstance(result.exception, exceptions.ArgumentError) def test_add_by_option(self): service = self.client['Security_Ssh_Key'] From 33ecb4a8d33d942757b7c51d700e8327fc53bb2a Mon Sep 17 00:00:00 2001 From: Cameron Porter Date: Mon, 15 Jan 2018 16:16:41 -0600 Subject: [PATCH 2/2] fix import order error --- tests/CLI/modules/sshkey_tests.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/CLI/modules/sshkey_tests.py b/tests/CLI/modules/sshkey_tests.py index 6b568839d..253309c08 100644 --- a/tests/CLI/modules/sshkey_tests.py +++ b/tests/CLI/modules/sshkey_tests.py @@ -11,8 +11,8 @@ import mock -from SoftLayer import testing from SoftLayer.CLI import exceptions +from SoftLayer import testing class SshKeyTests(testing.TestCase):