Skip to content
This repository has been archived by the owner on Jan 13, 2024. It is now read-only.

Commit

Permalink
Fixes #325, use keyrings.cryptfile
Browse files Browse the repository at this point in the history
  • Loading branch information
sdpython committed Jan 10, 2021
1 parent b8c7590 commit 9b10d66
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 20 deletions.
19 changes: 7 additions & 12 deletions _unittests/ut_filehelper/test_transfer_ftp_true.py
Expand Up @@ -13,6 +13,7 @@
from pyquickhelper.filehelper import TransferFTP, FolderTransferFTP, FileTreeNode
from pyquickhelper.pycode import is_travis_or_appveyor, get_temp_folder, ExtTestCase
from pyquickhelper.loghelper.os_helper import get_machine, get_user
from pyquickhelper.loghelper import get_password


class TestTransferFTPTrue(ExtTestCase):
Expand All @@ -23,14 +24,11 @@ def test_transfer_ftp_true(self):
self._testMethodName,
OutputPrint=__name__ == "__main__")

with warnings.catch_warnings():
warnings.simplefilter('ignore', DeprecationWarning)
import keyring
prefix = "pyquickhelper,"
try:
user = keyring.get_password("web", prefix + "user")
pwd = keyring.get_password("web", prefix + "pwd")
ftpsite = keyring.get_password("web", prefix + "ftp")
user = get_password("web", prefix + "user")
pwd = get_password("web", prefix + "pwd")
ftpsite = get_password("web", prefix + "ftp")
except RuntimeError:
user = None
pwd = None
Expand Down Expand Up @@ -66,14 +64,11 @@ def test_transfer_ftp_start_transfering(self):
self._testMethodName,
OutputPrint=__name__ == "__main__")

with warnings.catch_warnings():
warnings.simplefilter('ignore', DeprecationWarning)
import keyring
prefix = "pyquickhelper,"
try:
user = keyring.get_password("web", prefix + "user")
pwd = keyring.get_password("web", prefix + "pwd")
ftpsite = keyring.get_password("web", prefix + "ftp")
user = get_password("web", prefix + "user")
pwd = get_password("web", prefix + "pwd")
ftpsite = get_password("web", prefix + "ftp")
except RuntimeError:
user = None
pwd = None
Expand Down
17 changes: 14 additions & 3 deletions _unittests/ut_loghelper/test_pwd_helper.py
Expand Up @@ -23,10 +23,21 @@ def test_exc(self):
@skipif_travis('stuck')
@skipif_circleci('stuck')
@skipif_azure('stuck')
def test_password(self):
def test_password_keyring(self):
pwd = 'bibi'
set_password('pyq', 'jj', pwd)
pwd2 = get_password('pyq', 'jj')
set_password('pyq', 'jj', pwd, lib='keyring')
pwd2 = get_password('pyq', 'jj', lib='keyring')
self.assertEqual(pwd, pwd2)

@skipif_appveyor('stuck')
@skipif_travis('stuck')
@skipif_circleci('stuck')
@skipif_azure('stuck')
def test_password_cryptfile(self):
os.environ['UTTESTPYQ'] = 'bypass'
pwd = 'bibi'
set_password('pyq', 'jj', pwd, env='UTTESTPYQ')
pwd2 = get_password('pyq', 'jj', env='UTTESTPYQ')
self.assertEqual(pwd, pwd2)


Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Expand Up @@ -18,6 +18,7 @@ jupyter_sphinx>=0.2.0
jyquickhelper>=0.3.128
keyring
keyrings.alt
keyrings.cryptfile
Mako
matplotlib
mistune
Expand Down
5 changes: 4 additions & 1 deletion setup.py
Expand Up @@ -262,7 +262,8 @@ def write_version():
extras_require={
'cli': ['tkinterquickhelper', 'pysftp', 'fire'],
# cryptography
'filehelper': ['cffi', 'cryptography', 'pylzma', 'pysftp'],
'filehelper': ['cffi', 'cryptography', 'pylzma', 'pysftp',
'keyrings.cryptfile'],
'jenkinshelper': ['python-jenkins>=1.0.0', 'pyyaml'],
'loghelper': ['psutil'],
'all': [
Expand All @@ -272,13 +273,15 @@ def write_version():
'cryptography',
"docformatter",
"docutils",
'flake8',
'fire',
"IPython>=5.0.0",
"jupyter",
"jupyter_client",
"jupyter_sphinx",
"jyquickhelper",
"keyring",
"keyrings.cryptfile",
"matplotlib",
"metakernel",
"multi_key_dict",
Expand Down
2 changes: 1 addition & 1 deletion src/pyquickhelper/__init__.py
Expand Up @@ -6,7 +6,7 @@
sphinx extension, jenkins helpers...
"""

__version__ = "1.10.3508"
__version__ = "1.10.3509"
__author__ = "Xavier Dupré"
__github__ = "https://github.com/sdpython/pyquickhelper"
__url__ = "http://www.xavierdupre.fr/app/pyquickhelper/helpsphinx/index.html"
Expand Down
1 change: 1 addition & 0 deletions src/pyquickhelper/helpgen/default_conf.py
Expand Up @@ -126,6 +126,7 @@ def get_epkg_dictionary():
'Jupyter Lab': 'https://jupyterlab.readthedocs.io/en/stable/',
'jupyter_sphinx': 'https://jupyter-sphinx.readthedocs.io/en/latest/index.html',
'keyring': 'https://github.com/jaraco/keyring',
'keyrings.cryptfile': 'https://github.com/frispete/keyrings.cryptfile',
'latex': 'https://en.wikipedia.org/wiki/LaTeX',
'LaTeX': 'https://en.wikipedia.org/wiki/LaTeX',
'LaTex': 'https://en.wikipedia.org/wiki/LaTeX',
Expand Down
29 changes: 26 additions & 3 deletions src/pyquickhelper/loghelper/pwd_helper.py
Expand Up @@ -2,17 +2,25 @@
@file
@brief Helpers to store and retrieve password.
"""
from getpass import getpass
from os import getenv


def set_password(system, username, password, lib='keyring'):
def set_password(system, username, password, lib='keyrings.cryptfile',
env='KEYRING_CRYPTFILE_PASSWORD'):
"""
Stores a password.
By default, uses :epkg:`keyring`.
By default, uses :epkg:`keyring` or
:epkg:`keyrings.cryptfile`.
:param system: system
:param username: username
:param password: password
:param lib: which lib to use to store the password
:param env: see below
If `lib == 'keyrings.cryptfile'`, the function used the environment
variable *env*, if present, no password is asked.
"""
if lib == 'keyring':
from keyring import (
Expand All @@ -25,19 +33,29 @@ def set_password(system, username, password, lib='keyring'):
"Unable to store a password with keyring for '{}', '{}'.".format(
system, username))
return
if lib == 'keyrings.cryptfile':
from keyrings.cryptfile.cryptfile import CryptFileKeyring
kr = CryptFileKeyring()
kr.keyring_key = getenv("KEYRING_CRYPTFILE_PASSWORD") or getpass()
kr.set_password(system, username, password)
raise RuntimeError(
"Unknown library '{}'.".format(lib))


def get_password(system, username, lib='keyring'):
def get_password(system, username, lib='keyrings.cryptfile',
env='KEYRING_CRYPTFILE_PASSWORD'):
"""
Restores a password.
By default, uses :epkg:`keyring`.
:param system: system
:param username: username
:param lib: which lib to use to store the password
:param env: see below
:return: password
If `lib == 'keyrings.cryptfile'`, the function used the environment
variable *env*, if present, no password is asked.
"""
if lib == 'keyring':
from keyring import get_password as lib_get_password
Expand All @@ -47,5 +65,10 @@ def get_password(system, username, lib='keyring'):
"Unable to restore a password with keyring for '{}', '{}'.".format(
system, username))
return pwd
if lib == 'keyrings.cryptfile':
from keyrings.cryptfile.cryptfile import CryptFileKeyring
kr = CryptFileKeyring()
kr.keyring_key = getenv("KEYRING_CRYPTFILE_PASSWORD") or getpass()
return kr.get_password(system, username)
raise RuntimeError(
"Unknown library '{}'.".format(lib))

0 comments on commit 9b10d66

Please sign in to comment.