有没有办法在不提供主密码的情况下访问Windows中的密钥环?

Air*_*Air 6 python windows authentication passwords python-keyring

我正在与一个涉及连接数据库的同事开发脚本.我们希望保持代码独立于我们中的哪一个使用它,同时保持我们的密码私密,而不必在工作日期间反复进行身份验证.经过一些搜索(我们都是Python的新手)似乎我们可以为此目的使用密钥环,所以我从pip安装它(很可能是基于我的安装日期内存的1.2.2版本的库).

问题是当我尝试访问我存储的密码时,系统会提示我设置主密码以访问密钥环,如下所示(来自IDLE):

>>> import keyring
>>> keyring.set_password('Service', 'MyUsername', 'MyPassword')

Warning (from warnings module):
  File "C:\Python27\lib\getpass.py", line 92
  return fallback_getpass(prompt, stream)
GetPassWarning: Can not control echo on the terminal.
Warning: Password input may be echoed.
Please enter password for encrypted keyring:
Run Code Online (Sandbox Code Playgroud)

设置密码后,我可以轻松获取和设置密码,直到我重新启动shell.现在我必须再次输入主密码:

>>> ================================ RESTART ================================
>>> import keyring
>>> print keyring.get_password('Service', 'MyUsername')

Warning (from warnings module):
  File "C:\Python27\lib\getpass.py", line 92
    return fallback_getpass(prompt, stream)
GetPassWarning: Can not control echo on the terminal.
Warning: Password input may be echoed.
Please enter password for encrypted keyring:
Run Code Online (Sandbox Code Playgroud)

输入主密码后,此身份验证仅在当前会话期间/重新启动之间持续存在.从命令行运行脚本时,情况更糟 - 我必须在每次运行脚本时进行身份验证.在这一点上,密钥环不会节省我任何时间或精力,我怀疑它使我的密码更安全,而不是记忆和手动输入.

在搜索解决方案之后,如果主密码与用户帐户的密码相同,则密钥环看起来会在Unix上自动进行身份验证,但这在Windows上对我不起作用.

我是否试图让keyring做一些不应该做的事情,或者我的实现中是否存在错误?

我的经验似乎与另一个用户报告的情况冲突,当用户试图在相关问题中尝试访问密钥环时,他声称没有提示输入密码,python-keyring如何在Windows上运行?

Art*_*are 2

您使用哪个后端?检查使用:

>>> from keyring import get_keyring
>>> get_keyring()
[... what is output here?]
Run Code Online (Sandbox Code Playgroud)

如果输出这样:

<keyring.backends.file.EncryptedKeyring object at 0x.....>
Run Code Online (Sandbox Code Playgroud)

这就是它请求密码的原因。该模块无法找到任何可供使用的特定于平台的密钥环,因此它只是使用主密码加密您的密码并将其放入普通文件中。

就我个人而言,对我来说,我的脚本在无人值守的情况下运行比我的密码安全更重要,所以我编写了这个函数:

def configureKeyring():
    """
    EncryptedKeyring requires a master password to unlock it, which is not ideal
    for our scripts since we want them to run unattended. So if we're using
    EncryptedKeyring, automatically swap to a PlaintextKeyring instead.

    If we're using something else, say WinVaultKeyring, then that's more secure
    than PlaintextKeyring without being any less convenient, so we'll use it.
    """
    from keyring               import get_keyring, set_keyring
    from keyring.backends.file import EncryptedKeyring, PlaintextKeyring

    if isinstance(get_keyring(), EncryptedKeyring):
        set_keyring(PlaintextKeyring())
Run Code Online (Sandbox Code Playgroud)

在使用 或 之前keyringset_password如果您像我一样更看重不需要插入主密码而不是保证密码安全,get_password请运行此功能。configureKeyring()在执行此操作之前,请了解以明文形式存储密码的安全隐患。

一个更好的长期解决方案是可能调查所有其他可用的后端并安装适当的先决条件,以便可以使用不同的后端(如果存在不需要输入主密码并且只需登录即可)充足的。

注意:来自https://build.opensuse.org/package/view_file/openSUSE:Leap:42.2/python-keyring/python-keyring.changes?rev=a2e727a9e4a21a65c9496362d8cff27d

随着这些密钥环的移动,配置中明确指示的任何密钥环都需要更新以替换“keyring.backends”。与“keyrings.alt.”。例如,“keyring.backends.file.PlaintextKeyring”变为“keyrings.alt.file.PlaintextKeyring”

因此,根据已提供的解决方案中的版本,您可能需要这样做

from keyrings.alt.file import EncryptedKeyring, PlaintextKeyring 
Run Code Online (Sandbox Code Playgroud)