Skip to content

Commit ceee75f

Browse files
author
Brian Flores
committed
fix observations
1 parent ba95504 commit ceee75f

File tree

8 files changed

+281
-1
lines changed

8 files changed

+281
-1
lines changed

SoftLayer/CLI/routes.py

+14
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,20 @@
331331
'SoftLayer.CLI.securitygroup.interface:remove'),
332332
('securitygroup:event-log', 'SoftLayer.CLI.securitygroup.event_log:get_by_request_id'),
333333

334+
('sshkey', 'SoftLayer.CLI.sshkey'),
335+
('sshkey:add', 'SoftLayer.CLI.security.sshkey_add:cli'),
336+
('sshkey:remove', 'SoftLayer.CLI.security.sshkey_remove:cli'),
337+
('sshkey:edit', 'SoftLayer.CLI.security.sshkey_edit:cli'),
338+
('sshkey:list', 'SoftLayer.CLI.security.sshkey_list:cli'),
339+
('sshkey:print', 'SoftLayer.CLI.security.sshkey_print:cli'),
340+
341+
('ssl', 'SoftLayer.CLI.ssl'),
342+
('ssl:add', 'SoftLayer.CLI.security.cert_add:cli'),
343+
('ssl:download', 'SoftLayer.CLI.security.cert_download:cli'),
344+
('ssl:edit', 'SoftLayer.CLI.security.cert_edit:cli'),
345+
('ssl:list', 'SoftLayer.CLI.security.cert_list:cli'),
346+
('ssl:remove', 'SoftLayer.CLI.security.cert_remove:cli'),
347+
334348
('security', 'SoftLayer.CLI.security'),
335349
('security:sshkey-add', 'SoftLayer.CLI.security.sshkey_add:cli'),
336350
('security:sshkey-remove', 'SoftLayer.CLI.security.sshkey_remove:cli'),

SoftLayer/CLI/sshkey/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""SSH Keys."""

SoftLayer/CLI/ssl/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""SSL Certificates."""

docs/cli/security.rst

+41-1
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,78 @@ SSH Keys and SSL Certificates
77
:prog: security sshkey-add
88
:show-nested:
99

10+
.. click:: SoftLayer.CLI.security.sshkey_add:cli
11+
:prog: sshkey add
12+
:show-nested:
13+
1014
.. click:: SoftLayer.CLI.security.sshkey_remove:cli
1115
:prog: security sshkey-remove
1216
:show-nested:
1317

18+
.. click:: SoftLayer.CLI.security.sshkey_remove:cli
19+
:prog: sshkey remove
20+
:show-nested:
21+
1422
.. click:: SoftLayer.CLI.security.sshkey_edit:cli
1523
:prog: security sshkey-edit
1624
:show-nested:
1725

26+
.. click:: SoftLayer.CLI.security.sshkey_edit:cli
27+
:prog: sshkey edit
28+
:show-nested:
29+
1830
.. click:: SoftLayer.CLI.security.sshkey_list:cli
1931
:prog: security sshkey-list
2032
:show-nested:
2133

34+
.. click:: SoftLayer.CLI.security.sshkey_list:cli
35+
:prog: sshkey list
36+
:show-nested:
37+
2238
.. click:: SoftLayer.CLI.security.sshkey_print:cli
2339
:prog: security sshkey-print
2440
:show-nested:
2541

42+
.. click:: SoftLayer.CLI.security.sshkey_print:cli
43+
:prog: sshkey print
44+
:show-nested:
45+
2646
.. click:: SoftLayer.CLI.security.cert_add:cli
2747
:prog: security cert-add
2848
:show-nested:
2949

50+
.. click:: SoftLayer.CLI.security.cert_add:cli
51+
:prog: ssl add
52+
:show-nested:
53+
3054
.. click:: SoftLayer.CLI.security.cert_download:cli
3155
:prog: security cert-download
3256
:show-nested:
3357

58+
.. click:: SoftLayer.CLI.security.cert_download:cli
59+
:prog: ssl download
60+
:show-nested:
61+
3462
.. click:: SoftLayer.CLI.security.cert_edit:cli
3563
:prog: security cert-edit
3664
:show-nested:
3765

66+
.. click:: SoftLayer.CLI.security.cert_edit:cli
67+
:prog: ssl edit
68+
:show-nested:
69+
3870
.. click:: SoftLayer.CLI.security.cert_list:cli
3971
:prog: security cert-list
4072
:show-nested:
4173

74+
.. click:: SoftLayer.CLI.security.cert_list:cli
75+
:prog: ssl list
76+
:show-nested:
77+
4278
.. click:: SoftLayer.CLI.security.cert_remove:cli
4379
:prog: security cert-remove
44-
:show-nested:
80+
:show-nested:
81+
82+
.. click:: SoftLayer.CLI.security.cert_remove:cli
83+
:prog: ssl remove
84+
:show-nested:

docs/cli/sshkey.rst

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
.. _cli_sshkey:
2+
3+
SSH Keys
4+
========
5+
6+
.. click:: SoftLayer.CLI.sshkey.add:cli
7+
This command is an alias for `slcli security sshkey-add`
8+
:prog: sshkey add
9+
:show-nested:
10+
11+
.. click:: SoftLayer.CLI.sshkey.remove:cli
12+
This command is an alias for `slcli security sshkey-remove`
13+
:prog: sshkey remove
14+
:show-nested:
15+
16+
.. click:: SoftLayer.CLI.sshkey.edit:cli
17+
This command is an alias for `slcli security sshkey-edit`
18+
:prog: sshkey edit
19+
:show-nested:
20+
21+
.. click:: SoftLayer.CLI.sshkey.list:cli
22+
This command is an alias for `slcli security sshkey-list`
23+
:prog: sshkey list
24+
:show-nested:
25+
26+
.. click:: SoftLayer.CLI.sshkey.print:cli
27+
This command is an alias for `slcli security sshkey-print`
28+
:prog: sshkey print
29+
:show-nested:

docs/cli/ssl.rst

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
.. _cli_ssl:
2+
3+
SSL Certificates
4+
================
5+
6+
.. click:: SoftLayer.CLI.ssl.add:cli
7+
This command is an alias for `slcli security cert_add`
8+
:prog: ssl add
9+
:show-nested:
10+
11+
.. click:: SoftLayer.CLI.ssl.download:cli
12+
This command is an alias for `slcli security cert_download`
13+
:prog: ssl download
14+
:show-nested:
15+
16+
.. click:: SoftLayer.CLI.ssl.edit:cli
17+
This command is an alias for `slcli security cert_edit`
18+
:prog: ssl edit
19+
:show-nested:
20+
21+
.. click:: SoftLayer.CLI.ssl.list:cli
22+
This command is an alias for `slcli security cert_list`
23+
:prog: ssl list
24+
:show-nested:
25+
26+
.. click:: SoftLayer.CLI.ssl.remove:cli
27+
This command is an alias for `slcli security cert_remove`
28+
:prog: ssl remove
29+
:show-nested:
30+

tests/CLI/modules/sshkey_tests.py

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
"""
2+
SoftLayer.tests.CLI.modules.sshkey_tests
3+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4+
5+
:license: MIT, see LICENSE for more details.
6+
"""
7+
import json
8+
import os.path
9+
import sys
10+
import tempfile
11+
12+
from unittest import mock as mock
13+
14+
from SoftLayer.CLI import exceptions
15+
from SoftLayer import testing
16+
17+
18+
class SshKeyTests(testing.TestCase):
19+
def test_add_without_key_errors(self):
20+
result = self.run_command(['sshkey', 'add', 'key1'])
21+
22+
self.assertEqual(result.exit_code, 2)
23+
self.assertIsInstance(result.exception, exceptions.ArgumentError)
24+
25+
def test_add_with_key_file_and_key_argument_errors(self):
26+
path = os.path.join(testing.FIXTURE_PATH, 'id_rsa.pub')
27+
result = self.run_command(['sshkey', 'add', 'key1',
28+
'--key=some_key',
29+
'--in-file=%s' % path])
30+
31+
self.assertEqual(result.exit_code, 2)
32+
self.assertIsInstance(result.exception, exceptions.ArgumentError)
33+
34+
def test_add_by_option(self):
35+
service = self.client['Security_Ssh_Key']
36+
mock_key = service.getObject()['key']
37+
38+
result = self.run_command(['sshkey', 'add', 'key1',
39+
'--key=%s' % mock_key,
40+
'--note=my key'])
41+
42+
self.assert_no_fail(result)
43+
self.assertEqual(json.loads(result.output),
44+
"SSH key added: aa:bb:cc:dd")
45+
self.assert_called_with('SoftLayer_Security_Ssh_Key', 'createObject',
46+
args=({'notes': 'my key',
47+
'key': mock_key,
48+
'label': 'key1'},))
49+
50+
def test_add_by_file(self):
51+
path = os.path.join(testing.FIXTURE_PATH, 'id_rsa.pub')
52+
53+
result = self.run_command(['sshkey', 'add', 'key1',
54+
'--in-file=%s' % path])
55+
56+
self.assert_no_fail(result)
57+
self.assertEqual(json.loads(result.output),
58+
"SSH key added: aa:bb:cc:dd")
59+
service = self.client['Security_Ssh_Key']
60+
mock_key = service.getObject()['key']
61+
self.assert_called_with('SoftLayer_Security_Ssh_Key', 'createObject',
62+
args=({'notes': None,
63+
'key': mock_key,
64+
'label': 'key1'},))
65+
66+
def test_remove_key(self):
67+
result = self.run_command(['--really', 'sshkey', 'remove', '1234'])
68+
69+
self.assert_no_fail(result)
70+
self.assert_called_with('SoftLayer_Security_Ssh_Key', 'deleteObject',
71+
identifier=1234)
72+
73+
@mock.patch('SoftLayer.CLI.formatting.no_going_back')
74+
def test_remove_key_fail(self, ngb_mock):
75+
ngb_mock.return_value = False
76+
result = self.run_command(['sshkey', 'remove', '1234'])
77+
78+
self.assertEqual(result.exit_code, 2)
79+
80+
def test_edit_key(self):
81+
result = self.run_command(['sshkey', 'edit', '1234',
82+
'--label=key1', '--note=my key'])
83+
84+
self.assert_no_fail(result)
85+
self.assert_called_with('SoftLayer_Security_Ssh_Key', 'editObject',
86+
args=({'notes': 'my key',
87+
'label': 'key1'},),
88+
identifier=1234)
89+
90+
def test_edit_key_fail(self):
91+
fixture = self.set_mock('SoftLayer_Security_Ssh_Key', 'editObject')
92+
fixture.return_value = False
93+
94+
result = self.run_command(['sshkey', 'edit', '1234',
95+
'--label=key1', '--note=my key'])
96+
97+
self.assertEqual(result.exit_code, 2)
98+
99+
def test_list_keys(self):
100+
result = self.run_command(['sshkey', 'list'])
101+
102+
self.assert_no_fail(result)
103+
self.assertEqual(json.loads(result.output),
104+
[{'notes': '-',
105+
'fingerprint': None,
106+
'id': '100',
107+
'label': 'Test 1'},
108+
{'notes': 'my key',
109+
'fingerprint': None,
110+
'id': '101',
111+
'label': 'Test 2'}])
112+
113+
def test_print_key(self):
114+
result = self.run_command(['sshkey', 'print', '1234'])
115+
116+
self.assert_no_fail(result)
117+
self.assertEqual(json.loads(result.output),
118+
{'id': 1234, 'label': 'label', 'notes': 'notes'})
119+
120+
def test_print_key_file(self):
121+
if sys.platform.startswith("win"):
122+
self.skipTest("Test doesn't work in Windows")
123+
with tempfile.NamedTemporaryFile() as sshkey_file:
124+
service = self.client['Security_Ssh_Key']
125+
mock_key = service.getObject()['key']
126+
result = self.run_command(['sshkey', 'print', '1234',
127+
'--out-file=%s' % sshkey_file.name])
128+
129+
self.assert_no_fail(result)
130+
self.assertEqual(mock_key, sshkey_file.read().decode("utf-8"))

tests/CLI/modules/ssl_tests.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""
2+
SoftLayer.tests.CLI.modules.ssl_tests
3+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4+
:license: MIT, see LICENSE for more details.
5+
"""
6+
from SoftLayer import testing
7+
8+
import json
9+
from unittest import mock as mock
10+
11+
12+
class SslTests(testing.TestCase):
13+
def test_list(self):
14+
result = self.run_command(['ssl', 'list', '--status', 'all'])
15+
self.assert_no_fail(result)
16+
self.assertEqual(json.loads(result.output), [
17+
{
18+
"id": 1234,
19+
"common_name": "cert",
20+
"days_until_expire": 0,
21+
"notes": None
22+
}
23+
])
24+
25+
@mock.patch('SoftLayer.CLI.formatting.no_going_back')
26+
def test_remove(self, confirm_mock):
27+
confirm_mock.return_value = True
28+
result = self.run_command(['ssl', 'remove', '123456'])
29+
self.assert_no_fail(result)
30+
self.assertEqual(result.exit_code, 0)
31+
32+
def test_download(self):
33+
result = self.run_command(['ssl', 'download', '123456'])
34+
self.assert_no_fail(result)
35+
self.assertEqual(result.exit_code, 0)

0 commit comments

Comments
 (0)