1
1
mirror of https://github.com/dbcli/pgcli.git synced 2024-10-04 01:08:51 +03:00

Merge pull request #873 from cfournie/remember_passwords

Remember prompted passwords using keyring
This commit is contained in:
Amjith Ramanujam 2018-05-16 17:49:31 -07:00 committed by GitHub
commit 8e12ea5e0f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 0 deletions

View File

@ -8,6 +8,7 @@ python:
install:
- pip install .
- pip install -r requirements-dev.txt
- pip install keyrings.alt>=3.1
script:
- set -e

View File

@ -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
@ -396,6 +397,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.
@ -417,6 +423,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):
@ -426,6 +434,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

View File

@ -21,6 +21,7 @@ install_requirements = [
'configobj >= 5.0.6',
'humanize >= 0.5.1',
'cli_helpers[styles] >= 1.0.1',
'keyring >= 12.2.0'
]