From ba95504428db8722133224bcab605ed17f12e7d0 Mon Sep 17 00:00:00 2001 From: Brian Flores Date: Tue, 16 May 2023 10:08:03 -0400 Subject: [PATCH 1/2] added new command, unit tests and updated core test to work with commands that name starts with other command names --- SoftLayer/CLI/environment.py | 3 +- SoftLayer/CLI/routes.py | 24 +++---- SoftLayer/CLI/security/__init__.py | 2 + .../CLI/{ssl/add.py => security/cert_add.py} | 0 .../download.py => security/cert_download.py} | 0 .../{ssl/edit.py => security/cert_edit.py} | 0 .../{ssl/list.py => security/cert_list.py} | 0 .../remove.py => security/cert_remove.py} | 0 .../{sshkey/add.py => security/sshkey_add.py} | 0 .../edit.py => security/sshkey_edit.py} | 0 .../list.py => security/sshkey_list.py} | 0 .../print.py => security/sshkey_print.py} | 0 .../remove.py => security/sshkey_remove.py} | 0 SoftLayer/CLI/sshkey/__init__.py | 1 - SoftLayer/CLI/ssl/__init__.py | 1 - docs/cli/security.rst | 44 ++++++++++++ docs/cli/sshkey.rst | 24 ------- docs/cli/ssl.rst | 25 ------- .../{sshkey_tests.py => security_tests.py} | 72 ++++++++++++------- tests/CLI/modules/ssl_tests.py | 36 ---------- 20 files changed, 107 insertions(+), 125 deletions(-) create mode 100644 SoftLayer/CLI/security/__init__.py rename SoftLayer/CLI/{ssl/add.py => security/cert_add.py} (100%) rename SoftLayer/CLI/{ssl/download.py => security/cert_download.py} (100%) rename SoftLayer/CLI/{ssl/edit.py => security/cert_edit.py} (100%) rename SoftLayer/CLI/{ssl/list.py => security/cert_list.py} (100%) rename SoftLayer/CLI/{ssl/remove.py => security/cert_remove.py} (100%) rename SoftLayer/CLI/{sshkey/add.py => security/sshkey_add.py} (100%) rename SoftLayer/CLI/{sshkey/edit.py => security/sshkey_edit.py} (100%) rename SoftLayer/CLI/{sshkey/list.py => security/sshkey_list.py} (100%) rename SoftLayer/CLI/{sshkey/print.py => security/sshkey_print.py} (100%) rename SoftLayer/CLI/{sshkey/remove.py => security/sshkey_remove.py} (100%) delete mode 100644 SoftLayer/CLI/sshkey/__init__.py delete mode 100644 SoftLayer/CLI/ssl/__init__.py create mode 100644 docs/cli/security.rst delete mode 100644 docs/cli/sshkey.rst delete mode 100644 docs/cli/ssl.rst rename tests/CLI/modules/{sshkey_tests.py => security_tests.py} (63%) delete mode 100644 tests/CLI/modules/ssl_tests.py diff --git a/SoftLayer/CLI/environment.py b/SoftLayer/CLI/environment.py index 79964efb8..3d33fbc97 100644 --- a/SoftLayer/CLI/environment.py +++ b/SoftLayer/CLI/environment.py @@ -138,7 +138,8 @@ def list_commands(self, *path): # offset is used to exclude the path that the caller requested. offset = len(path_str) + 1 if path_str else 0 - commands.append(command[offset:]) + if ':' not in command[offset:]: + commands.append(command[offset:]) return sorted(commands) diff --git a/SoftLayer/CLI/routes.py b/SoftLayer/CLI/routes.py index 3922541f8..f756d8898 100644 --- a/SoftLayer/CLI/routes.py +++ b/SoftLayer/CLI/routes.py @@ -331,19 +331,17 @@ 'SoftLayer.CLI.securitygroup.interface:remove'), ('securitygroup:event-log', 'SoftLayer.CLI.securitygroup.event_log:get_by_request_id'), - ('sshkey', 'SoftLayer.CLI.sshkey'), - ('sshkey:add', 'SoftLayer.CLI.sshkey.add:cli'), - ('sshkey:remove', 'SoftLayer.CLI.sshkey.remove:cli'), - ('sshkey:edit', 'SoftLayer.CLI.sshkey.edit:cli'), - ('sshkey:list', 'SoftLayer.CLI.sshkey.list:cli'), - ('sshkey:print', 'SoftLayer.CLI.sshkey.print:cli'), - - ('ssl', 'SoftLayer.CLI.ssl'), - ('ssl:add', 'SoftLayer.CLI.ssl.add:cli'), - ('ssl:download', 'SoftLayer.CLI.ssl.download:cli'), - ('ssl:edit', 'SoftLayer.CLI.ssl.edit:cli'), - ('ssl:list', 'SoftLayer.CLI.ssl.list:cli'), - ('ssl:remove', 'SoftLayer.CLI.ssl.remove:cli'), + ('security', 'SoftLayer.CLI.security'), + ('security:sshkey-add', 'SoftLayer.CLI.security.sshkey_add:cli'), + ('security:sshkey-remove', 'SoftLayer.CLI.security.sshkey_remove:cli'), + ('security:sshkey-edit', 'SoftLayer.CLI.security.sshkey_edit:cli'), + ('security:sshkey-list', 'SoftLayer.CLI.security.sshkey_list:cli'), + ('security:sshkey-print', 'SoftLayer.CLI.security.sshkey_print:cli'), + ('security:cert-add', 'SoftLayer.CLI.security.cert_add:cli'), + ('security:cert-download', 'SoftLayer.CLI.security.cert_download:cli'), + ('security:cert-edit', 'SoftLayer.CLI.security.cert_edit:cli'), + ('security:cert-list', 'SoftLayer.CLI.security.cert_list:cli'), + ('security:cert-remove', 'SoftLayer.CLI.security.cert_remove:cli'), ('subnet', 'SoftLayer.CLI.subnet'), ('subnet:cancel', 'SoftLayer.CLI.subnet.cancel:cli'), diff --git a/SoftLayer/CLI/security/__init__.py b/SoftLayer/CLI/security/__init__.py new file mode 100644 index 000000000..3e7c83255 --- /dev/null +++ b/SoftLayer/CLI/security/__init__.py @@ -0,0 +1,2 @@ +"""SSH Keys and SSL Certificates.""" +# :license: MIT, see LICENSE for more details. diff --git a/SoftLayer/CLI/ssl/add.py b/SoftLayer/CLI/security/cert_add.py similarity index 100% rename from SoftLayer/CLI/ssl/add.py rename to SoftLayer/CLI/security/cert_add.py diff --git a/SoftLayer/CLI/ssl/download.py b/SoftLayer/CLI/security/cert_download.py similarity index 100% rename from SoftLayer/CLI/ssl/download.py rename to SoftLayer/CLI/security/cert_download.py diff --git a/SoftLayer/CLI/ssl/edit.py b/SoftLayer/CLI/security/cert_edit.py similarity index 100% rename from SoftLayer/CLI/ssl/edit.py rename to SoftLayer/CLI/security/cert_edit.py diff --git a/SoftLayer/CLI/ssl/list.py b/SoftLayer/CLI/security/cert_list.py similarity index 100% rename from SoftLayer/CLI/ssl/list.py rename to SoftLayer/CLI/security/cert_list.py diff --git a/SoftLayer/CLI/ssl/remove.py b/SoftLayer/CLI/security/cert_remove.py similarity index 100% rename from SoftLayer/CLI/ssl/remove.py rename to SoftLayer/CLI/security/cert_remove.py diff --git a/SoftLayer/CLI/sshkey/add.py b/SoftLayer/CLI/security/sshkey_add.py similarity index 100% rename from SoftLayer/CLI/sshkey/add.py rename to SoftLayer/CLI/security/sshkey_add.py diff --git a/SoftLayer/CLI/sshkey/edit.py b/SoftLayer/CLI/security/sshkey_edit.py similarity index 100% rename from SoftLayer/CLI/sshkey/edit.py rename to SoftLayer/CLI/security/sshkey_edit.py diff --git a/SoftLayer/CLI/sshkey/list.py b/SoftLayer/CLI/security/sshkey_list.py similarity index 100% rename from SoftLayer/CLI/sshkey/list.py rename to SoftLayer/CLI/security/sshkey_list.py diff --git a/SoftLayer/CLI/sshkey/print.py b/SoftLayer/CLI/security/sshkey_print.py similarity index 100% rename from SoftLayer/CLI/sshkey/print.py rename to SoftLayer/CLI/security/sshkey_print.py diff --git a/SoftLayer/CLI/sshkey/remove.py b/SoftLayer/CLI/security/sshkey_remove.py similarity index 100% rename from SoftLayer/CLI/sshkey/remove.py rename to SoftLayer/CLI/security/sshkey_remove.py diff --git a/SoftLayer/CLI/sshkey/__init__.py b/SoftLayer/CLI/sshkey/__init__.py deleted file mode 100644 index 725c04810..000000000 --- a/SoftLayer/CLI/sshkey/__init__.py +++ /dev/null @@ -1 +0,0 @@ -"""SSH Keys.""" diff --git a/SoftLayer/CLI/ssl/__init__.py b/SoftLayer/CLI/ssl/__init__.py deleted file mode 100644 index 63ef95227..000000000 --- a/SoftLayer/CLI/ssl/__init__.py +++ /dev/null @@ -1 +0,0 @@ -"""SSL Certificates.""" diff --git a/docs/cli/security.rst b/docs/cli/security.rst new file mode 100644 index 000000000..464bb707f --- /dev/null +++ b/docs/cli/security.rst @@ -0,0 +1,44 @@ +.. _cli_security: + +SSH Keys and SSL Certificates +======== + +.. click:: SoftLayer.CLI.security.sshkey_add:cli + :prog: security sshkey-add + :show-nested: + +.. click:: SoftLayer.CLI.security.sshkey_remove:cli + :prog: security sshkey-remove + :show-nested: + +.. click:: SoftLayer.CLI.security.sshkey_edit:cli + :prog: security sshkey-edit + :show-nested: + +.. click:: SoftLayer.CLI.security.sshkey_list:cli + :prog: security sshkey-list + :show-nested: + +.. click:: SoftLayer.CLI.security.sshkey_print:cli + :prog: security sshkey-print + :show-nested: + +.. click:: SoftLayer.CLI.security.cert_add:cli + :prog: security cert-add + :show-nested: + +.. click:: SoftLayer.CLI.security.cert_download:cli + :prog: security cert-download + :show-nested: + +.. click:: SoftLayer.CLI.security.cert_edit:cli + :prog: security cert-edit + :show-nested: + +.. click:: SoftLayer.CLI.security.cert_list:cli + :prog: security cert-list + :show-nested: + +.. click:: SoftLayer.CLI.security.cert_remove:cli + :prog: security cert-remove + :show-nested: \ No newline at end of file diff --git a/docs/cli/sshkey.rst b/docs/cli/sshkey.rst deleted file mode 100644 index d12d2f424..000000000 --- a/docs/cli/sshkey.rst +++ /dev/null @@ -1,24 +0,0 @@ -.. _cli_sshkey: - -SSH Keys -======== - -.. click:: SoftLayer.CLI.sshkey.add:cli - :prog: sshkey add - :show-nested: - -.. click:: SoftLayer.CLI.sshkey.remove:cli - :prog: sshkey remove - :show-nested: - -.. click:: SoftLayer.CLI.sshkey.edit:cli - :prog: sshkey edit - :show-nested: - -.. click:: SoftLayer.CLI.sshkey.list:cli - :prog: sshkey list - :show-nested: - -.. click:: SoftLayer.CLI.sshkey.print:cli - :prog: sshkey print - :show-nested: diff --git a/docs/cli/ssl.rst b/docs/cli/ssl.rst deleted file mode 100644 index f8c95f058..000000000 --- a/docs/cli/ssl.rst +++ /dev/null @@ -1,25 +0,0 @@ -.. _cli_ssl: - -SSL Certificates -================ - -.. click:: SoftLayer.CLI.ssl.add:cli - :prog: ssl add - :show-nested: - -.. click:: SoftLayer.CLI.ssl.download:cli - :prog: ssl download - :show-nested: - -.. click:: SoftLayer.CLI.ssl.edit:cli - :prog: ssl edit - :show-nested: - -.. click:: SoftLayer.CLI.ssl.list:cli - :prog: ssl list - :show-nested: - -.. click:: SoftLayer.CLI.ssl.remove:cli - :prog: ssl remove - :show-nested: - diff --git a/tests/CLI/modules/sshkey_tests.py b/tests/CLI/modules/security_tests.py similarity index 63% rename from tests/CLI/modules/sshkey_tests.py rename to tests/CLI/modules/security_tests.py index 858175900..8e14ac332 100644 --- a/tests/CLI/modules/sshkey_tests.py +++ b/tests/CLI/modules/security_tests.py @@ -1,5 +1,5 @@ """ - SoftLayer.tests.CLI.modules.sshkey_tests + SoftLayer.tests.CLI.modules.security_tests ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ :license: MIT, see LICENSE for more details. @@ -15,27 +15,27 @@ from SoftLayer import testing -class SshKeyTests(testing.TestCase): - def test_add_without_key_errors(self): - result = self.run_command(['sshkey', 'add', 'key1']) +class SecurityTests(testing.TestCase): + def test_add_sshkey_without_key_errors(self): + result = self.run_command(['security', '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): + def test_add_sshkey_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', + result = self.run_command(['security', '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): + def test_add_sshkey_by_option(self): service = self.client['Security_Ssh_Key'] mock_key = service.getObject()['key'] - result = self.run_command(['sshkey', 'add', 'key1', + result = self.run_command(['security', 'sshkey-add', 'key1', '--key=%s' % mock_key, '--note=my key']) @@ -47,10 +47,10 @@ def test_add_by_option(self): 'key': mock_key, 'label': 'key1'},)) - def test_add_by_file(self): + def test_add_sshkey_by_file(self): path = os.path.join(testing.FIXTURE_PATH, 'id_rsa.pub') - result = self.run_command(['sshkey', 'add', 'key1', + result = self.run_command(['security', 'sshkey-add', 'key1', '--in-file=%s' % path]) self.assert_no_fail(result) @@ -63,22 +63,22 @@ def test_add_by_file(self): 'key': mock_key, 'label': 'key1'},)) - def test_remove_key(self): - result = self.run_command(['--really', 'sshkey', 'remove', '1234']) + def test_remove_sshkey_key(self): + result = self.run_command(['--really', 'security', 'sshkey-remove', '1234']) self.assert_no_fail(result) self.assert_called_with('SoftLayer_Security_Ssh_Key', 'deleteObject', identifier=1234) @mock.patch('SoftLayer.CLI.formatting.no_going_back') - def test_remove_key_fail(self, ngb_mock): + def test_remove_sshkey_fail(self, ngb_mock): ngb_mock.return_value = False - result = self.run_command(['sshkey', 'remove', '1234']) + result = self.run_command(['security', 'sshkey-remove', '1234']) self.assertEqual(result.exit_code, 2) - def test_edit_key(self): - result = self.run_command(['sshkey', 'edit', '1234', + def test_edit_sshkey(self): + result = self.run_command(['security', 'sshkey-edit', '1234', '--label=key1', '--note=my key']) self.assert_no_fail(result) @@ -87,17 +87,17 @@ def test_edit_key(self): 'label': 'key1'},), identifier=1234) - def test_edit_key_fail(self): + def test_edit_sshkey_fail(self): fixture = self.set_mock('SoftLayer_Security_Ssh_Key', 'editObject') fixture.return_value = False - result = self.run_command(['sshkey', 'edit', '1234', + result = self.run_command(['security', 'sshkey-edit', '1234', '--label=key1', '--note=my key']) self.assertEqual(result.exit_code, 2) - def test_list_keys(self): - result = self.run_command(['sshkey', 'list']) + def test_list_sshkeys(self): + result = self.run_command(['security', 'sshkey-list']) self.assert_no_fail(result) self.assertEqual(json.loads(result.output), @@ -110,21 +110,45 @@ def test_list_keys(self): 'id': '101', 'label': 'Test 2'}]) - def test_print_key(self): - result = self.run_command(['sshkey', 'print', '1234']) + def test_print_sshkey(self): + result = self.run_command(['security', 'sshkey-print', '1234']) self.assert_no_fail(result) self.assertEqual(json.loads(result.output), {'id': 1234, 'label': 'label', 'notes': 'notes'}) - def test_print_key_file(self): + def test_print_sshkey_file(self): if sys.platform.startswith("win"): self.skipTest("Test doesn't work in Windows") with tempfile.NamedTemporaryFile() as sshkey_file: service = self.client['Security_Ssh_Key'] mock_key = service.getObject()['key'] - result = self.run_command(['sshkey', 'print', '1234', + result = self.run_command(['security', 'sshkey-print', '1234', '--out-file=%s' % sshkey_file.name]) self.assert_no_fail(result) self.assertEqual(mock_key, sshkey_file.read().decode("utf-8")) + + def test_list_certficates(self): + result = self.run_command(['security', 'cert-list', '--status', 'all']) + self.assert_no_fail(result) + self.assertEqual(json.loads(result.output), [ + { + "id": 1234, + "common_name": "cert", + "days_until_expire": 0, + "notes": None + } + ]) + + @mock.patch('SoftLayer.CLI.formatting.no_going_back') + def test_remove_certficate(self, confirm_mock): + confirm_mock.return_value = True + result = self.run_command(['security', 'cert-remove', '123456']) + self.assert_no_fail(result) + self.assertEqual(result.exit_code, 0) + + def test_download_certficate(self): + result = self.run_command(['security', 'cert-download', '123456']) + self.assert_no_fail(result) + self.assertEqual(result.exit_code, 0) diff --git a/tests/CLI/modules/ssl_tests.py b/tests/CLI/modules/ssl_tests.py deleted file mode 100644 index 2bcdff5ca..000000000 --- a/tests/CLI/modules/ssl_tests.py +++ /dev/null @@ -1,36 +0,0 @@ -""" - SoftLayer.tests.CLI.modules.ssl_tests - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - - :license: MIT, see LICENSE for more details. -""" -from SoftLayer import testing - -import json -from unittest import mock as mock - - -class SslTests(testing.TestCase): - def test_list(self): - result = self.run_command(['ssl', 'list', '--status', 'all']) - self.assert_no_fail(result) - self.assertEqual(json.loads(result.output), [ - { - "id": 1234, - "common_name": "cert", - "days_until_expire": 0, - "notes": None - } - ]) - - @mock.patch('SoftLayer.CLI.formatting.no_going_back') - def test_remove(self, confirm_mock): - confirm_mock.return_value = True - result = self.run_command(['ssl', 'remove', '123456']) - self.assert_no_fail(result) - self.assertEqual(result.exit_code, 0) - - def test_download(self): - result = self.run_command(['ssl', 'download', '123456']) - self.assert_no_fail(result) - self.assertEqual(result.exit_code, 0) From ceee75fef3cdf7757a4f69f9c99e5c64a3eb0c21 Mon Sep 17 00:00:00 2001 From: Brian Flores Date: Thu, 18 May 2023 15:33:32 -0400 Subject: [PATCH 2/2] fix observations --- SoftLayer/CLI/routes.py | 14 ++++ SoftLayer/CLI/sshkey/__init__.py | 1 + SoftLayer/CLI/ssl/__init__.py | 1 + docs/cli/security.rst | 42 +++++++++- docs/cli/sshkey.rst | 29 +++++++ docs/cli/ssl.rst | 30 +++++++ tests/CLI/modules/sshkey_tests.py | 130 ++++++++++++++++++++++++++++++ tests/CLI/modules/ssl_tests.py | 35 ++++++++ 8 files changed, 281 insertions(+), 1 deletion(-) create mode 100644 SoftLayer/CLI/sshkey/__init__.py create mode 100644 SoftLayer/CLI/ssl/__init__.py create mode 100644 docs/cli/sshkey.rst create mode 100644 docs/cli/ssl.rst create mode 100644 tests/CLI/modules/sshkey_tests.py create mode 100644 tests/CLI/modules/ssl_tests.py diff --git a/SoftLayer/CLI/routes.py b/SoftLayer/CLI/routes.py index f756d8898..c28011068 100644 --- a/SoftLayer/CLI/routes.py +++ b/SoftLayer/CLI/routes.py @@ -331,6 +331,20 @@ 'SoftLayer.CLI.securitygroup.interface:remove'), ('securitygroup:event-log', 'SoftLayer.CLI.securitygroup.event_log:get_by_request_id'), + ('sshkey', 'SoftLayer.CLI.sshkey'), + ('sshkey:add', 'SoftLayer.CLI.security.sshkey_add:cli'), + ('sshkey:remove', 'SoftLayer.CLI.security.sshkey_remove:cli'), + ('sshkey:edit', 'SoftLayer.CLI.security.sshkey_edit:cli'), + ('sshkey:list', 'SoftLayer.CLI.security.sshkey_list:cli'), + ('sshkey:print', 'SoftLayer.CLI.security.sshkey_print:cli'), + + ('ssl', 'SoftLayer.CLI.ssl'), + ('ssl:add', 'SoftLayer.CLI.security.cert_add:cli'), + ('ssl:download', 'SoftLayer.CLI.security.cert_download:cli'), + ('ssl:edit', 'SoftLayer.CLI.security.cert_edit:cli'), + ('ssl:list', 'SoftLayer.CLI.security.cert_list:cli'), + ('ssl:remove', 'SoftLayer.CLI.security.cert_remove:cli'), + ('security', 'SoftLayer.CLI.security'), ('security:sshkey-add', 'SoftLayer.CLI.security.sshkey_add:cli'), ('security:sshkey-remove', 'SoftLayer.CLI.security.sshkey_remove:cli'), diff --git a/SoftLayer/CLI/sshkey/__init__.py b/SoftLayer/CLI/sshkey/__init__.py new file mode 100644 index 000000000..725c04810 --- /dev/null +++ b/SoftLayer/CLI/sshkey/__init__.py @@ -0,0 +1 @@ +"""SSH Keys.""" diff --git a/SoftLayer/CLI/ssl/__init__.py b/SoftLayer/CLI/ssl/__init__.py new file mode 100644 index 000000000..63ef95227 --- /dev/null +++ b/SoftLayer/CLI/ssl/__init__.py @@ -0,0 +1 @@ +"""SSL Certificates.""" diff --git a/docs/cli/security.rst b/docs/cli/security.rst index 464bb707f..2450110de 100644 --- a/docs/cli/security.rst +++ b/docs/cli/security.rst @@ -7,38 +7,78 @@ SSH Keys and SSL Certificates :prog: security sshkey-add :show-nested: +.. click:: SoftLayer.CLI.security.sshkey_add:cli + :prog: sshkey add + :show-nested: + .. click:: SoftLayer.CLI.security.sshkey_remove:cli :prog: security sshkey-remove :show-nested: +.. click:: SoftLayer.CLI.security.sshkey_remove:cli + :prog: sshkey remove + :show-nested: + .. click:: SoftLayer.CLI.security.sshkey_edit:cli :prog: security sshkey-edit :show-nested: +.. click:: SoftLayer.CLI.security.sshkey_edit:cli + :prog: sshkey edit + :show-nested: + .. click:: SoftLayer.CLI.security.sshkey_list:cli :prog: security sshkey-list :show-nested: +.. click:: SoftLayer.CLI.security.sshkey_list:cli + :prog: sshkey list + :show-nested: + .. click:: SoftLayer.CLI.security.sshkey_print:cli :prog: security sshkey-print :show-nested: +.. click:: SoftLayer.CLI.security.sshkey_print:cli + :prog: sshkey print + :show-nested: + .. click:: SoftLayer.CLI.security.cert_add:cli :prog: security cert-add :show-nested: +.. click:: SoftLayer.CLI.security.cert_add:cli + :prog: ssl add + :show-nested: + .. click:: SoftLayer.CLI.security.cert_download:cli :prog: security cert-download :show-nested: +.. click:: SoftLayer.CLI.security.cert_download:cli + :prog: ssl download + :show-nested: + .. click:: SoftLayer.CLI.security.cert_edit:cli :prog: security cert-edit :show-nested: +.. click:: SoftLayer.CLI.security.cert_edit:cli + :prog: ssl edit + :show-nested: + .. click:: SoftLayer.CLI.security.cert_list:cli :prog: security cert-list :show-nested: +.. click:: SoftLayer.CLI.security.cert_list:cli + :prog: ssl list + :show-nested: + .. click:: SoftLayer.CLI.security.cert_remove:cli :prog: security cert-remove - :show-nested: \ No newline at end of file + :show-nested: + +.. click:: SoftLayer.CLI.security.cert_remove:cli + :prog: ssl remove + :show-nested: diff --git a/docs/cli/sshkey.rst b/docs/cli/sshkey.rst new file mode 100644 index 000000000..91a7b8c8a --- /dev/null +++ b/docs/cli/sshkey.rst @@ -0,0 +1,29 @@ +.. _cli_sshkey: + +SSH Keys +======== + +.. click:: SoftLayer.CLI.sshkey.add:cli + This command is an alias for `slcli security sshkey-add` + :prog: sshkey add + :show-nested: + +.. click:: SoftLayer.CLI.sshkey.remove:cli + This command is an alias for `slcli security sshkey-remove` + :prog: sshkey remove + :show-nested: + +.. click:: SoftLayer.CLI.sshkey.edit:cli + This command is an alias for `slcli security sshkey-edit` + :prog: sshkey edit + :show-nested: + +.. click:: SoftLayer.CLI.sshkey.list:cli + This command is an alias for `slcli security sshkey-list` + :prog: sshkey list + :show-nested: + +.. click:: SoftLayer.CLI.sshkey.print:cli + This command is an alias for `slcli security sshkey-print` + :prog: sshkey print + :show-nested: diff --git a/docs/cli/ssl.rst b/docs/cli/ssl.rst new file mode 100644 index 000000000..1b85dc772 --- /dev/null +++ b/docs/cli/ssl.rst @@ -0,0 +1,30 @@ +.. _cli_ssl: + +SSL Certificates +================ + +.. click:: SoftLayer.CLI.ssl.add:cli + This command is an alias for `slcli security cert_add` + :prog: ssl add + :show-nested: + +.. click:: SoftLayer.CLI.ssl.download:cli + This command is an alias for `slcli security cert_download` + :prog: ssl download + :show-nested: + +.. click:: SoftLayer.CLI.ssl.edit:cli + This command is an alias for `slcli security cert_edit` + :prog: ssl edit + :show-nested: + +.. click:: SoftLayer.CLI.ssl.list:cli + This command is an alias for `slcli security cert_list` + :prog: ssl list + :show-nested: + +.. click:: SoftLayer.CLI.ssl.remove:cli + This command is an alias for `slcli security cert_remove` + :prog: ssl remove + :show-nested: + diff --git a/tests/CLI/modules/sshkey_tests.py b/tests/CLI/modules/sshkey_tests.py new file mode 100644 index 000000000..858175900 --- /dev/null +++ b/tests/CLI/modules/sshkey_tests.py @@ -0,0 +1,130 @@ +""" + SoftLayer.tests.CLI.modules.sshkey_tests + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + :license: MIT, see LICENSE for more details. +""" +import json +import os.path +import sys +import tempfile + +from unittest import mock as mock + +from SoftLayer.CLI import exceptions +from SoftLayer import testing + + +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'] + mock_key = service.getObject()['key'] + + result = self.run_command(['sshkey', 'add', 'key1', + '--key=%s' % mock_key, + '--note=my key']) + + self.assert_no_fail(result) + self.assertEqual(json.loads(result.output), + "SSH key added: aa:bb:cc:dd") + self.assert_called_with('SoftLayer_Security_Ssh_Key', 'createObject', + args=({'notes': 'my key', + 'key': mock_key, + 'label': 'key1'},)) + + def test_add_by_file(self): + path = os.path.join(testing.FIXTURE_PATH, 'id_rsa.pub') + + result = self.run_command(['sshkey', 'add', 'key1', + '--in-file=%s' % path]) + + self.assert_no_fail(result) + self.assertEqual(json.loads(result.output), + "SSH key added: aa:bb:cc:dd") + service = self.client['Security_Ssh_Key'] + mock_key = service.getObject()['key'] + self.assert_called_with('SoftLayer_Security_Ssh_Key', 'createObject', + args=({'notes': None, + 'key': mock_key, + 'label': 'key1'},)) + + def test_remove_key(self): + result = self.run_command(['--really', 'sshkey', 'remove', '1234']) + + self.assert_no_fail(result) + self.assert_called_with('SoftLayer_Security_Ssh_Key', 'deleteObject', + identifier=1234) + + @mock.patch('SoftLayer.CLI.formatting.no_going_back') + def test_remove_key_fail(self, ngb_mock): + ngb_mock.return_value = False + result = self.run_command(['sshkey', 'remove', '1234']) + + self.assertEqual(result.exit_code, 2) + + def test_edit_key(self): + result = self.run_command(['sshkey', 'edit', '1234', + '--label=key1', '--note=my key']) + + self.assert_no_fail(result) + self.assert_called_with('SoftLayer_Security_Ssh_Key', 'editObject', + args=({'notes': 'my key', + 'label': 'key1'},), + identifier=1234) + + def test_edit_key_fail(self): + fixture = self.set_mock('SoftLayer_Security_Ssh_Key', 'editObject') + fixture.return_value = False + + result = self.run_command(['sshkey', 'edit', '1234', + '--label=key1', '--note=my key']) + + self.assertEqual(result.exit_code, 2) + + def test_list_keys(self): + result = self.run_command(['sshkey', 'list']) + + self.assert_no_fail(result) + self.assertEqual(json.loads(result.output), + [{'notes': '-', + 'fingerprint': None, + 'id': '100', + 'label': 'Test 1'}, + {'notes': 'my key', + 'fingerprint': None, + 'id': '101', + 'label': 'Test 2'}]) + + def test_print_key(self): + result = self.run_command(['sshkey', 'print', '1234']) + + self.assert_no_fail(result) + self.assertEqual(json.loads(result.output), + {'id': 1234, 'label': 'label', 'notes': 'notes'}) + + def test_print_key_file(self): + if sys.platform.startswith("win"): + self.skipTest("Test doesn't work in Windows") + with tempfile.NamedTemporaryFile() as sshkey_file: + service = self.client['Security_Ssh_Key'] + mock_key = service.getObject()['key'] + result = self.run_command(['sshkey', 'print', '1234', + '--out-file=%s' % sshkey_file.name]) + + self.assert_no_fail(result) + self.assertEqual(mock_key, sshkey_file.read().decode("utf-8")) diff --git a/tests/CLI/modules/ssl_tests.py b/tests/CLI/modules/ssl_tests.py new file mode 100644 index 000000000..73f2234cd --- /dev/null +++ b/tests/CLI/modules/ssl_tests.py @@ -0,0 +1,35 @@ +""" + SoftLayer.tests.CLI.modules.ssl_tests + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + :license: MIT, see LICENSE for more details. +""" +from SoftLayer import testing + +import json +from unittest import mock as mock + + +class SslTests(testing.TestCase): + def test_list(self): + result = self.run_command(['ssl', 'list', '--status', 'all']) + self.assert_no_fail(result) + self.assertEqual(json.loads(result.output), [ + { + "id": 1234, + "common_name": "cert", + "days_until_expire": 0, + "notes": None + } + ]) + + @mock.patch('SoftLayer.CLI.formatting.no_going_back') + def test_remove(self, confirm_mock): + confirm_mock.return_value = True + result = self.run_command(['ssl', 'remove', '123456']) + self.assert_no_fail(result) + self.assertEqual(result.exit_code, 0) + + def test_download(self): + result = self.run_command(['ssl', 'download', '123456']) + self.assert_no_fail(result) + self.assertEqual(result.exit_code, 0)