diff options
author | Chris Fournier <chris.fournier@shopify.com> | 2018-05-14 11:30:50 -0400 |
---|---|---|
committer | Chris Fournier <chris.fournier@shopify.com> | 2018-05-14 11:30:50 -0400 |
commit | 09a43ccd49aa620f2b5f38541b173260d954cc0f (patch) | |
tree | 402e5a182d5a87ff1d113c7843542b39ae6b67b7 | |
parent | 2f344ca6d27312bc47c2caff35a273590fb7b2ac (diff) |
Remember prompted passwords using keyring
-rw-r--r-- | pgcli/main.py | 10 | ||||
-rw-r--r-- | setup.py | 1 |
2 files changed, 11 insertions, 0 deletions
diff --git a/pgcli/main.py b/pgcli/main.py index cfc3f2d7..15ea686b 100644 --- a/pgcli/main.py +++ b/pgcli/main.py @@ -40,6 +40,7 @@ from pygments.token import Token from pgspecial.main import (PGSpecial, NO_QUERY, PAGER_OFF) import pgspecial as special +import keyring from .pgcompleter import PGCompleter from .pgtoolbar import create_toolbar_tokens_func from .pgstyle import style_factory, style_factory_output @@ -383,6 +384,11 @@ class PGCli(object): if not self.force_passwd_prompt and not passwd: passwd = os.environ.get('PGPASSWORD', '') + # Find password from store + key = '%s@%s' % (user, host) + if not passwd: + passwd = keyring.get_password('pgcli', key) + # Prompt for a password immediately if requested via the -W flag. This # avoids wasting time trying to connect to the database and catching a # no-password exception. @@ -404,6 +410,8 @@ class PGCli(object): try: pgexecute = PGExecute(database, user, passwd, host, port, dsn, application_name='pgcli', **kwargs) + if passwd: + keyring.set_password('pgcli', key, passwd) except (OperationalError, InterfaceError) as e: if ('no password supplied' in utf8tounicode(e.args[0]) and auto_passwd_prompt): @@ -413,6 +421,8 @@ class PGCli(object): pgexecute = PGExecute(database, user, passwd, host, port, dsn, application_name='pgcli', **kwargs) + if passwd: + keyring.set_password('pgcli', key, passwd) else: raise e @@ -21,6 +21,7 @@ install_requirements = [ 'configobj >= 5.0.6', 'humanize >= 0.5.1', 'cli_helpers[styles] >= 1.0.1', + 'keyring >= 12.2.0' ] |