|
1
|
|
|
# Make coding more python3-ish |
|
2
|
|
|
from __future__ import (absolute_import, division, print_function) |
|
3
|
|
|
__metaclass__ = type |
|
4
|
|
|
|
|
5
|
|
|
from argparse import ArgumentParser |
|
6
|
|
|
|
|
7
|
|
|
from ansible.compat.tests import unittest |
|
8
|
|
|
from ansible.compat.tests.mock import patch |
|
9
|
|
|
|
|
10
|
|
|
from ansible.errors import AnsibleError |
|
11
|
|
|
from ansible.module_utils import six |
|
12
|
|
|
from tpmstore.tpmstore import LookupModule |
|
13
|
|
|
from tpm import TpmApiv4 |
|
14
|
|
|
from tpm import TPMException |
|
15
|
|
|
from logging import getLogger |
|
16
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
log = getLogger(__name__) |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
class TestTpmstorePlugin(unittest.TestCase): |
|
22
|
|
|
|
|
23
|
|
|
def setUp(self): |
|
24
|
|
|
self.lookup_plugin = LookupModule() |
|
25
|
|
|
|
|
26
|
|
|
def test_least_arguments_exception(self): |
|
27
|
|
|
exception_error = 'At least 4 arguments required.' |
|
28
|
|
|
with self.assertRaises(AnsibleError) as context: |
|
29
|
|
|
self.lookup_plugin.run(['tpmurl', 'tpmuser']) |
|
30
|
|
|
log.debug("context exception: {}".format(context.exception)) |
|
31
|
|
|
self.assertTrue(exception_error in str(context.exception)) |
|
32
|
|
|
|
|
33
|
|
|
def test_create_argument_exception(self): |
|
34
|
|
|
wrong_term = 'Foo' |
|
35
|
|
|
exception_error = "create can only be True or False and not: {}".format(wrong_term) |
|
36
|
|
|
with self.assertRaises(AnsibleError) as context: |
|
37
|
|
|
self.lookup_plugin.run(['tpmurl', 'tpmuser', 'tpmass', 'create={}'.format(wrong_term)]) |
|
38
|
|
|
log.debug("context exception: {}".format(context.exception)) |
|
39
|
|
|
self.assertTrue(exception_error in str(context.exception)) |
|
40
|
|
|
|
|
41
|
|
|
def test_invalid_url_exception(self): |
|
42
|
|
|
wrong_url = 'ftp://foo.bar' |
|
43
|
|
|
exception_error = "First argument has to be a valid URL to TeamPasswordManager API: {}".format(wrong_url) |
|
44
|
|
|
with self.assertRaises(AnsibleError) as context: |
|
45
|
|
|
self.lookup_plugin.run(['{}'.format(wrong_url), 'tpmuser', 'tpmass', 'name=dostuff']) |
|
46
|
|
|
log.debug("context exception: {}".format(context.exception)) |
|
47
|
|
|
self.assertTrue(exception_error in str(context.exception)) |
|
48
|
|
|
|
|
49
|
|
|
def test_other_tpm_exception(self): |
|
50
|
|
|
wrong_url = 'https://does.not.exists.com' |
|
51
|
|
|
exception_error = "Connection error for " |
|
52
|
|
|
with self.assertRaises(AnsibleError) as context: |
|
53
|
|
|
self.lookup_plugin.run(['{}'.format(wrong_url), 'tpmuser', 'tpmass', 'name=dostuff']) |
|
54
|
|
|
log.debug("context exception: {}".format(context.exception)) |
|
55
|
|
|
self.assertTrue(exception_error in str(context.exception)) |
|
56
|
|
|
|
|
57
|
|
|
@patch('tpm.TpmApiv4.list_passwords_search', return_value=[{'id': 42}, {'id': 73}]) |
|
58
|
|
|
@patch('tpm.TpmApiv4.__init__', return_value=None) |
|
59
|
|
|
def test_too_many_results_exception(self, tpm_init_mock, tpm_list_mock): |
|
60
|
|
|
search_sring = "2result" |
|
61
|
|
|
exception_error = 'Found more then one match for the entry, please be more specific: {}'.format(search_sring) |
|
62
|
|
|
with self.assertRaises(AnsibleError) as context: |
|
63
|
|
|
self.lookup_plugin.run(['https://foo.bar', 'tpmuser', 'tpmass', 'name={}'.format(search_sring)]) |
|
64
|
|
|
log.debug("context exception: {}".format(context.exception)) |
|
65
|
|
|
self.assertTrue(exception_error in str(context.exception)) |
|
66
|
|
|
|
|
67
|
|
|
@patch('tpm.TpmApiv4.list_passwords_search', return_value=[]) |
|
68
|
|
|
@patch('tpm.TpmApiv4.__init__', return_value=None) |
|
69
|
|
|
def test_create_needs_projectid_exception(self, tpm_init_mock, tpm_list_mock): |
|
70
|
|
|
search_sring = "noresult" |
|
71
|
|
|
exception_error = 'To create a complete new entry, project_id is mandatory.' |
|
72
|
|
|
with self.assertRaises(AnsibleError) as context: |
|
73
|
|
|
self.lookup_plugin.run(['https://foo.bar', 'tpmuser', 'tpmass', 'name={}'.format(search_sring), 'create=True']) |
|
74
|
|
|
log.debug("context exception: {}".format(context.exception)) |
|
75
|
|
|
self.assertTrue(exception_error in str(context.exception)) |
|
76
|
|
|
|
|
77
|
|
|
def test_no_name_or_search_set_exception(self): |
|
78
|
|
|
exception_error = 'Either "name" or "search" have to be set.' |
|
79
|
|
|
with self.assertRaises(AnsibleError) as context: |
|
80
|
|
|
self.lookup_plugin.run(['https://foo.bar', 'tpmuser', 'tpmass', 'return_value=foobar']) |
|
81
|
|
|
log.debug("context exception: {}".format(context.exception)) |
|
82
|
|
|
self.assertTrue(exception_error in str(context.exception)) |
|
83
|
|
|
|
|
84
|
|
|
@patch('tpm.TpmApiv4.list_passwords_search', return_value=[]) |
|
85
|
|
|
@patch('tpm.TpmApiv4.__init__', return_value=None) |
|
86
|
|
|
def test_no_match_found_exception(self, tpm_init_mock, tpm_list_mock): |
|
87
|
|
View Code Duplication |
search_sring = "noresult" |
|
|
|
|
|
|
88
|
|
|
exception_error = "Found no match for: {}".format(search_sring) |
|
89
|
|
|
with self.assertRaises(AnsibleError) as context: |
|
90
|
|
|
self.lookup_plugin.run(['https://foo.bar', 'tpmuser', 'tpmass', 'name={}'.format(search_sring), 'create=False']) |
|
91
|
|
|
log.debug("context exception: {}".format(context.exception)) |
|
92
|
|
|
self.assertTrue(exception_error in str(context.exception)) |
|
93
|
|
|
|
|
94
|
|
|
@patch('tpm.TpmApiv4.show_password', return_value={'id': 73, 'name': 'A new Entry'}) |
|
95
|
|
|
@patch('tpm.TpmApiv4.create_password', side_effect=TPMException("Name already exists.")) |
|
96
|
|
|
@patch('tpm.TpmApiv4.list_passwords_search', return_value=[]) |
|
97
|
|
|
@patch('tpm.TpmApiv4.__init__', return_value=None) |
|
98
|
|
|
def test_exception_on_create(self, mock_init, mock_search, mock_create, mock_show): |
|
99
|
|
|
exception_error = "Name already exists." |
|
100
|
|
|
tpmurl = 'https://foo.bar' |
|
101
|
|
|
tpmuser = 'thatsme' |
|
102
|
|
|
tpmpass = 'mysecret' |
|
103
|
|
View Code Duplication |
search = 'A new Entry' |
|
|
|
|
|
|
104
|
|
|
project_id = '4' |
|
105
|
|
|
with self.assertRaises(AnsibleError) as context: |
|
106
|
|
|
self.lookup_plugin.run(['https://foo.bar', 'tpmuser', 'tpmass', 'name={}'.format(search), 'create=True', 'project_id={}'.format(project_id)]) |
|
107
|
|
|
log.debug("context exception: {}".format(context.exception)) |
|
108
|
|
|
self.assertTrue(exception_error in str(context.exception)) |
|
109
|
|
|
|
|
110
|
|
|
@patch('tpm.TpmApiv4.show_password', return_value={'id': 73, 'name': 'A new Entry'}) |
|
111
|
|
|
@patch('tpm.TpmApiv4.update_password', side_effect=TPMException("Can not update because of reasons.")) |
|
112
|
|
|
@patch('tpm.TpmApiv4.list_passwords_search', return_value=[{'id': 42}]) |
|
113
|
|
|
@patch('tpm.TpmApiv4.__init__', return_value=None) |
|
114
|
|
|
def test_exception_on_update(self, mock_init, mock_search, mock_create, mock_show): |
|
115
|
|
|
exception_error = "Can not update because of reasons." |
|
116
|
|
|
tpmurl = 'https://foo.bar' |
|
117
|
|
|
tpmuser = 'thatsme' |
|
118
|
|
|
tpmpass = 'mysecret' |
|
119
|
|
|
search = 'A new Entry' |
|
120
|
|
|
project_id = '4' |
|
121
|
|
|
with self.assertRaises(AnsibleError) as context: |
|
122
|
|
|
self.lookup_plugin.run(['https://foo.bar', 'tpmuser', 'tpmass', 'name={}'.format(search), 'create=True', 'project_id={}'.format(project_id)]) |
|
123
|
|
|
log.debug("context exception: {}".format(context.exception)) |
|
124
|
|
|
self.assertTrue(exception_error in str(context.exception)) |
|
125
|
|
|
|
|
126
|
|
|
@patch('tpm.TpmApiv4.list_passwords_search', return_value=[{'id': 42}]) |
|
127
|
|
|
@patch('tpm.TpmApiv4.show_password', return_value={'id': 42, 'password': 'foobar'}) |
|
128
|
|
|
@patch('tpm.TpmApiv4.__init__', return_value=None) |
|
129
|
|
|
def test_successful_default_result(self, tpm_init_mock, tpm_show_mock, tpm_list_mock): |
|
130
|
|
|
unlock_reason = 'to unlock' |
|
131
|
|
|
result = self.lookup_plugin.run(['https://foo.bar', 'tpmuser', 'tpmass', 'name=1result', 'reason={}'.format(unlock_reason)]) |
|
132
|
|
|
self.assertEqual(tpm_init_mock.call_args[1].get('unlock_reason'), unlock_reason) |
|
133
|
|
|
self.assertEqual(result, ['foobar']) |
|
134
|
|
|
|
|
135
|
|
|
@patch('tpm.TpmApiv4.list_passwords_search', return_value=[{'id': 42}]) |
|
136
|
|
|
@patch('tpm.TpmApiv4.show_password', return_value={'id': 42, 'password': 'foobar'}) |
|
137
|
|
|
@patch('tpm.TpmApiv4.__init__', return_value=None) |
|
138
|
|
|
def test_successful_search_result(self, mock_init, mock_show, mock_search): |
|
139
|
|
|
search = 'username:[foobar]' |
|
140
|
|
|
plugin_args = ['https://foo.bar', 'tpmuser', 'tpmass', 'search={}'.format(search)] |
|
141
|
|
|
result = self.lookup_plugin.run(plugin_args) |
|
142
|
|
|
self.assertEqual(mock_search.call_args[0][0], search) |
|
143
|
|
|
|
|
144
|
|
|
@patch('tpm.TpmApiv4.list_passwords_search', return_value=[{'id': 42}]) |
|
145
|
|
|
@patch('tpm.TpmApiv4.show_password', return_value={'id': 42, 'password': 'foobar', 'username': 'bar'}) |
|
146
|
|
|
@patch('tpm.TpmApiv4.__init__', return_value=None) |
|
147
|
|
|
def test_successful_return_value_result(self, mock_init, mock_show, mock_search): |
|
148
|
|
|
plugin_args = ['https://foo.bar', 'tpmuser', 'tpmass', 'name=1result', 'return_value=username'] |
|
149
|
|
|
result = self.lookup_plugin.run(plugin_args) |
|
150
|
|
|
self.assertEqual(result[0], 'bar') |
|
151
|
|
|
|
|
152
|
|
|
@patch('tpm.TpmApiv4.show_password', return_value={'id': 73, 'name': 'A new Entry'}) |
|
153
|
|
|
@patch('tpm.TpmApiv4.create_password', return_value={'id': 73}) |
|
154
|
|
|
@patch('tpm.TpmApiv4.generate_password', return_value={'password': 'random secret'}) |
|
155
|
|
|
@patch('tpm.TpmApiv4.list_passwords_search', return_value=[]) |
|
156
|
|
|
@patch('tpm.TpmApiv4.__init__', return_value=None) |
|
157
|
|
|
def test_verify_all_values_passed_at_create(self, mock_init, mock_search, mock_generate_pass, mock_create, mock_show): |
|
158
|
|
|
tpmurl = 'https://foo.bar' |
|
159
|
|
|
tpmuser = 'thatsme' |
|
160
|
|
|
tpmpass = 'mysecret' |
|
161
|
|
|
search = 'A new Entry' |
|
162
|
|
|
project_id = '4' |
|
163
|
|
|
random_password = "random secret" |
|
164
|
|
|
username = 'root' |
|
165
|
|
|
access_info = 'ssh://root@host' |
|
166
|
|
|
tags = 'root,ssh,aws,cloud' |
|
167
|
|
|
notes = 'Created by Ansible' |
|
168
|
|
|
reason = 'because I can' |
|
169
|
|
|
email = '[email protected]' |
|
170
|
|
|
expiry_date = "1983-04-25" |
|
171
|
|
|
result = self.lookup_plugin.run([tpmurl, tpmuser, tpmpass, 'name={}'.format(search), |
|
172
|
|
|
'create=True', 'project_id={}'.format(project_id), 'password=random', |
|
173
|
|
|
'username={}'.format(username), 'access_info={}'.format(access_info), |
|
174
|
|
|
'tags={}'.format(tags), 'notes={}'.format(notes), 'email={}'.format(email), |
|
175
|
|
|
'expiry_date={}'.format(expiry_date), 'reason={}'.format(reason)]) |
|
176
|
|
|
self.assertEqual(mock_init.call_args[0][0], tpmurl, 'tpmurl not passed') |
|
177
|
|
|
self.assertEqual(mock_init.call_args[1].get('username'), tpmuser, 'tpmuser not passed') |
|
178
|
|
|
self.assertEqual(mock_init.call_args[1].get('password'), tpmpass, 'tpmpass not passed') |
|
179
|
|
|
self.assertEqual(mock_init.call_args[1].get('unlock_reason'), reason, 'unlock_reason not passed') |
|
180
|
|
|
self.assertEqual(mock_search.call_args[0][0], 'name:[{}]'.format(search), 'search not passed') |
|
181
|
|
|
self.assertEqual(mock_create.call_args[0][0].get('project_id'), project_id, 'project_id not passed') |
|
182
|
|
|
self.assertEqual(mock_create.call_args[0][0].get('expiry_date'), expiry_date, 'expiry_date not passed') |
|
183
|
|
|
self.assertEqual(mock_create.call_args[0][0].get('password'), random_password, 'random_password not passed') |
|
184
|
|
|
self.assertEqual(mock_create.call_args[0][0].get('username'), username, 'username passed') |
|
185
|
|
|
self.assertEqual(mock_create.call_args[0][0].get('access_info'), access_info, 'access_info not passed') |
|
186
|
|
|
self.assertEqual(mock_create.call_args[0][0].get('tags'), tags, 'tags not passed') |
|
187
|
|
|
self.assertEqual(mock_create.call_args[0][0].get('notes'), notes, 'notes not passed') |
|
188
|
|
|
self.assertEqual(mock_create.call_args[0][0].get('email'), email, 'email not passed') |
|
189
|
|
|
|
|
190
|
|
|
|
|
191
|
|
|
@patch('tpm.TpmApiv4.show_password', return_value={'id': 73, 'name': 'An old entry'}) |
|
192
|
|
|
@patch('tpm.TpmApiv4.update_password', return_value={'id': 73}) |
|
193
|
|
|
@patch('tpm.TpmApiv4.generate_password', return_value={'password': 'random secret'}) |
|
194
|
|
|
@patch('tpm.TpmApiv4.list_passwords_search', return_value=[{'id': 73}]) |
|
195
|
|
|
@patch('tpm.TpmApiv4.__init__', return_value=None) |
|
196
|
|
|
def test_verify_all_values_passed_at_update(self, mock_init, mock_search, mock_generate_pass, mock_update_pass, mock_show): |
|
197
|
|
|
tpmurl = 'https://foo.bar' |
|
198
|
|
|
tpmuser = 'thatsme' |
|
199
|
|
|
tpmpass = 'mysecret' |
|
200
|
|
|
search = 'An old entry' |
|
201
|
|
|
project_id = '4' |
|
202
|
|
|
random_password = "random secret" |
|
203
|
|
|
username = 'root' |
|
204
|
|
|
access_info = 'ssh://root@host' |
|
205
|
|
|
tags = 'root,ssh,aws,cloud' |
|
206
|
|
|
notes = 'Created by Ansible' |
|
207
|
|
|
reason = 'because I can' |
|
208
|
|
|
email = '[email protected]' |
|
209
|
|
|
expiry_date = "1983-04-25" |
|
210
|
|
|
result = self.lookup_plugin.run([tpmurl, tpmuser, tpmpass, 'name={}'.format(search), |
|
211
|
|
|
'create=True', 'project_id={}'.format(project_id), 'password=random', |
|
212
|
|
|
'username={}'.format(username), 'access_info={}'.format(access_info), |
|
213
|
|
|
'tags={}'.format(tags), 'notes={}'.format(notes), 'email={}'.format(email), |
|
214
|
|
|
'expiry_date={}'.format(expiry_date), 'reason={}'.format(reason)]) |
|
215
|
|
|
log.debug(mock_update_pass.call_args[0][0]) |
|
216
|
|
|
self.assertEqual(mock_init.call_args[0][0], tpmurl) |
|
217
|
|
|
self.assertEqual(mock_init.call_args[1].get('username'), tpmuser) |
|
218
|
|
|
self.assertEqual(mock_init.call_args[1].get('password'), tpmpass) |
|
219
|
|
|
self.assertEqual(mock_init.call_args[1].get('unlock_reason'), reason) |
|
220
|
|
|
self.assertEqual(mock_search.call_args[0][0], 'name:[{}]'.format(search)) |
|
221
|
|
|
self.assertEqual(mock_update_pass.call_args[0][0], 73) |
|
222
|
|
|
self.assertEqual(mock_update_pass.call_args[0][1].get('project_id'), project_id) |
|
223
|
|
|
self.assertEqual(mock_update_pass.call_args[0][1].get('expiry_date'), expiry_date) |
|
224
|
|
|
self.assertEqual(mock_update_pass.call_args[0][1].get('password'), random_password) |
|
225
|
|
|
self.assertEqual(mock_update_pass.call_args[0][1].get('username'), username) |
|
226
|
|
|
self.assertEqual(mock_update_pass.call_args[0][1].get('access_info'), access_info) |
|
227
|
|
|
self.assertEqual(mock_update_pass.call_args[0][1].get('tags'), tags) |
|
228
|
|
|
self.assertEqual(mock_update_pass.call_args[0][1].get('notes'), notes) |
|
229
|
|
|
self.assertEqual(mock_update_pass.call_args[0][1].get('email'), email) |
|
230
|
|
|
|