Skip to content

Commit 5c8faaf

Browse files
Zuulopenstack-gerrit
authored andcommitted
Merge "Remove SecretsHelper"
2 parents 1fd25bc + 8bfcd2b commit 5c8faaf

File tree

2 files changed

+0
-191
lines changed

2 files changed

+0
-191
lines changed

novaclient/shell.py

Lines changed: 0 additions & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
from __future__ import print_function
2222
import argparse
23-
import getpass
2423
import logging
2524
import sys
2625

@@ -32,14 +31,6 @@
3231

3332
osprofiler_profiler = importutils.try_import("osprofiler.profiler")
3433

35-
HAS_KEYRING = False
36-
all_errors = ValueError
37-
try:
38-
import keyring
39-
HAS_KEYRING = True
40-
except ImportError:
41-
pass
42-
4334
import novaclient
4435
from novaclient import api_versions
4536
from novaclient import client
@@ -210,133 +201,6 @@ def __call__(self, parser, namespace, values, option_string):
210201
action(parser, namespace, values, option_string)
211202

212203

213-
class SecretsHelper(object):
214-
def __init__(self, args, client):
215-
self.args = args
216-
self.client = client
217-
self.key = None
218-
self._password = None
219-
220-
def _validate_string(self, text):
221-
if text is None or len(text) == 0:
222-
return False
223-
return True
224-
225-
def _make_key(self):
226-
if self.key is not None:
227-
return self.key
228-
keys = [
229-
self.client.auth_url,
230-
self.client.projectid,
231-
self.client.user,
232-
self.client.region_name,
233-
self.client.endpoint_type,
234-
self.client.service_type,
235-
self.client.service_name,
236-
]
237-
for (index, key) in enumerate(keys):
238-
if key is None:
239-
keys[index] = '?'
240-
else:
241-
keys[index] = str(keys[index])
242-
self.key = "/".join(keys)
243-
return self.key
244-
245-
def _prompt_password(self, verify=True):
246-
pw = None
247-
if hasattr(sys.stdin, 'isatty') and sys.stdin.isatty():
248-
# Check for Ctl-D
249-
try:
250-
while True:
251-
pw1 = getpass.getpass('OS Password: ')
252-
if verify:
253-
pw2 = getpass.getpass('Please verify: ')
254-
else:
255-
pw2 = pw1
256-
if pw1 == pw2 and self._validate_string(pw1):
257-
pw = pw1
258-
break
259-
except EOFError:
260-
pass
261-
return pw
262-
263-
def save(self, auth_token, management_url, tenant_id):
264-
if not HAS_KEYRING or not self.args.os_cache:
265-
return
266-
if (auth_token == self.auth_token and
267-
management_url == self.management_url):
268-
# Nothing changed....
269-
return
270-
if not all([management_url, auth_token, tenant_id]):
271-
raise ValueError(_("Unable to save empty management url/auth "
272-
"token"))
273-
value = "|".join([str(auth_token),
274-
str(management_url),
275-
str(tenant_id)])
276-
keyring.set_password("novaclient_auth", self._make_key(), value)
277-
278-
@property
279-
def password(self):
280-
# Cache password so we prompt user at most once
281-
if self._password:
282-
pass
283-
elif self._validate_string(self.args.os_password):
284-
self._password = self.args.os_password
285-
else:
286-
verify_pass = strutils.bool_from_string(
287-
utils.env("OS_VERIFY_PASSWORD", default=False), True)
288-
self._password = self._prompt_password(verify_pass)
289-
if not self._password:
290-
raise exc.CommandError(
291-
'Expecting a password provided via either '
292-
'--os-password, env[OS_PASSWORD], or '
293-
'prompted response')
294-
return self._password
295-
296-
@property
297-
def management_url(self):
298-
if not HAS_KEYRING or not self.args.os_cache:
299-
return None
300-
management_url = None
301-
try:
302-
block = keyring.get_password('novaclient_auth', self._make_key())
303-
if block:
304-
_token, management_url, _tenant_id = block.split('|', 2)
305-
except all_errors:
306-
pass
307-
return management_url
308-
309-
@property
310-
def auth_token(self):
311-
# Now is where it gets complicated since we
312-
# want to look into the keyring module, if it
313-
# exists and see if anything was provided in that
314-
# file that we can use.
315-
if not HAS_KEYRING or not self.args.os_cache:
316-
return None
317-
token = None
318-
try:
319-
block = keyring.get_password('novaclient_auth', self._make_key())
320-
if block:
321-
token, _management_url, _tenant_id = block.split('|', 2)
322-
except all_errors:
323-
pass
324-
return token
325-
326-
@property
327-
def tenant_id(self):
328-
if not HAS_KEYRING or not self.args.os_cache:
329-
return None
330-
tenant_id = None
331-
try:
332-
block = keyring.get_password('novaclient_auth', self._make_key())
333-
if block:
334-
_token, _management_url, tenant_id = block.split('|', 2)
335-
except all_errors:
336-
pass
337-
return tenant_id
338-
339-
340204
class NovaClientArgumentParser(argparse.ArgumentParser):
341205

342206
def __init__(self, *args, **kwargs):
@@ -688,7 +552,6 @@ def main(self, argv):
688552

689553
# We may have either, both or none of these.
690554
# If we have both, we don't need USERNAME, PASSWORD etc.
691-
# Fill in the blanks from the SecretsHelper if possible.
692555
# Finally, authenticate unless we have both.
693556
# Note if we don't auth we probably don't have a tenant ID so we can't
694557
# cache the token.
@@ -847,27 +710,6 @@ def main(self, argv):
847710
user_domain_id=os_user_domain_id,
848711
user_domain_name=os_user_domain_name)
849712

850-
# Now check for the password/token of which pieces of the
851-
# identifying keyring key can come from the underlying client
852-
if must_auth:
853-
helper = SecretsHelper(args, self.cs.client)
854-
self.cs.client.keyring_saver = helper
855-
856-
tenant_id = helper.tenant_id
857-
# Allow commandline to override cache
858-
if not auth_token:
859-
auth_token = helper.auth_token
860-
endpoint_override = endpoint_override or helper.management_url
861-
if tenant_id and auth_token and endpoint_override:
862-
self.cs.client.tenant_id = tenant_id
863-
self.cs.client.auth_token = auth_token
864-
self.cs.client.management_url = endpoint_override
865-
self.cs.client.password_func = lambda: helper.password
866-
else:
867-
# We're missing something, so auth with user/pass and save
868-
# the result in our helper.
869-
self.cs.client.password = helper.password
870-
871713
args.func(self.cs, args)
872714

873715
if osprofiler_profiler and args.profile:

novaclient/tests/unit/test_shell.py

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -576,20 +576,6 @@ def test_password(self, mock_getpass, mock_stdin, m_requests):
576576
stdout, stderr = self.shell('list')
577577
self.assertEqual((stdout + stderr), ex)
578578

579-
@mock.patch('sys.stdin', side_effect=mock.MagicMock)
580-
@mock.patch('getpass.getpass', side_effect=EOFError)
581-
def test_no_password(self, mock_getpass, mock_stdin):
582-
required = ('Expecting a password provided'
583-
' via either --os-password, env[OS_PASSWORD],'
584-
' or prompted response',)
585-
self.make_env(exclude='OS_PASSWORD')
586-
try:
587-
self.shell('list')
588-
except exceptions.CommandError as message:
589-
self.assertEqual(required, message.args)
590-
else:
591-
self.fail('CommandError not raised')
592-
593579
def _test_service_type(self, version, service_type, mock_client):
594580
if version is None:
595581
cmd = 'list'
@@ -666,25 +652,6 @@ def test_osprofiler_not_installed(self, m_requests):
666652
self.assertIn('unrecognized arguments: --profile swordfish',
667653
stderr)
668654

669-
@mock.patch('novaclient.shell.SecretsHelper.tenant_id',
670-
return_value=True)
671-
@mock.patch('novaclient.shell.SecretsHelper.auth_token',
672-
return_value=True)
673-
@mock.patch('novaclient.shell.SecretsHelper.management_url',
674-
return_value=True)
675-
@requests_mock.Mocker()
676-
def test_keyring_saver_helper(self,
677-
sh_management_url_function,
678-
sh_auth_token_function,
679-
sh_tenant_id_function,
680-
m_requests):
681-
self.make_env(fake_env=FAKE_ENV)
682-
self.register_keystone_discovery_fixture(m_requests)
683-
self.shell('list')
684-
mock_client_instance = self.mock_client.return_value
685-
keyring_saver = mock_client_instance.client.keyring_saver
686-
self.assertIsInstance(keyring_saver, novaclient.shell.SecretsHelper)
687-
688655
def test_microversion_with_default_behaviour(self):
689656
self.make_env(fake_env=FAKE_ENV5)
690657
self.mock_server_version_range.return_value = (

0 commit comments

Comments
 (0)