From 0f3f2db16f90078355737d87d31f363a588bd5f8 Mon Sep 17 00:00:00 2001 From: Bhuvan Arumugam Date: Sun, 8 Jul 2012 16:06:32 -0700 Subject: [PATCH] Keyring support for openstackclient. If password is defined in keyring, use it; otherwise, prompt for the password. * openstackclient/shell.py import keyring OpenStackClient.authenticate_user(): Get password from keyring, if it is defined; otherwise, prompt for the password. If user enter a password, store it in keyring. OpenStackClient.get_password_from_keyring(): New method to get password from keyring. OpenStackClient.set_password_in_keyring(): New method go set password in keyring. * toos/pip-requires Define keyring as one of dependent. Change-Id: I36d3a63054658c0ef0553d68b38fefbc236930ef --- openstackclient/shell.py | 22 ++++++++++++++++++++++ tools/pip-requires | 1 + 2 files changed, 23 insertions(+) diff --git a/openstackclient/shell.py b/openstackclient/shell.py index 3d0adf9937..49e5bb6bf7 100644 --- a/openstackclient/shell.py +++ b/openstackclient/shell.py @@ -20,6 +20,7 @@ """ import getpass +import keyring import logging import os import sys @@ -33,6 +34,7 @@ VERSION = '0.1' +KEYRING_SERVICE = 'openstack' def env(*vars, **kwargs): @@ -149,12 +151,14 @@ def authenticate_user(self): "You must provide a username via" " either --os-username or env[OS_USERNAME]") + self.get_password_from_keyring() if not self.options.os_password: # No password, if we've got a tty, try prompting for it if hasattr(sys.stdin, 'isatty') and sys.stdin.isatty(): # Check for Ctl-D try: self.options.os_password = getpass.getpass() + self.set_password_in_keyring() except EOFError: pass # No password because we did't have a tty or the @@ -188,6 +192,24 @@ def authenticate_user(self): ) return + def get_password_from_keyring(self): + """Get password from keyring, if it's set""" + service = KEYRING_SERVICE + if not self.options.os_password: + password = keyring.get_password(service, self.options.os_username) + os.options.os_password = password + + def set_password_in_keyring(self): + """Set password in keyring for this user""" + service = KEYRING_SERVICE + if self.options.os_password: + password = keyring.get_password(service, self.options.os_username) + # either password is not set in keyring, or it is different + if password != self.options.os_password: + keyring.set_password(service, + self.options.os_username, + self.options.os_password) + def initialize_app(self, argv): """Global app init bits: diff --git a/tools/pip-requires b/tools/pip-requires index efcaf7fe01..4d9a2cd531 100644 --- a/tools/pip-requires +++ b/tools/pip-requires @@ -1,6 +1,7 @@ cliff argparse httplib2 +keyring prettytable python-keystoneclient>=0.1,<0.2 python-novaclient>=2,<3