Keyring

バックエンドとして各種OSのパスワード機構を使える

  • macOS Keychain
  • Freedesktop Secret Service supports many DE including GNOME (requires secretstorage)
  • KDE4 & KDE5 KWallet (requires dbus)
  • Windows Credential Locker

Linuxの場合、GnomeかKDEが稼働しているホストでなければならない。

keyring-cliツール

python:3-slimベースのContainer上ではNoKeyringErrorになる

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
$keyring
Usage: keyring [get|set|del] SERVICE USERNAME

Options:
-h, --help show this help message and exit
-p KEYRING_PATH, --keyring-path=KEYRING_PATH
Path to the keyring backend
-b KEYRING_BACKEND, --keyring-backend=KEYRING_BACKEND
Name of the keyring backend
--list-backends List keyring backends and exit
--disable Disable keyring and exit
$keyring set system test
Password for 'test' in 'system': ***********
Traceback (most recent call last):
File "/usr/local/bin/keyring", line 8, in <module>
sys.exit(main())
File "/usr/local/lib/python3.8/site-packages/keyring/cli.py", line 136, in main
return cli.run(argv)
File "/usr/local/lib/python3.8/site-packages/keyring/cli.py", line 89, in run
set_password(service, username, password)
File "/usr/local/lib/python3.8/site-packages/keyring/core.py", line 62, in set_password
_keyring_backend.set_password(service_name, username, password)
File "/usr/local/lib/python3.8/site-packages/keyring/backends/fail.py", line 25, in get_password
raise NoKeyringError(msg)
keyring.errors.NoKeyringError: No recommended backend was available. Install a recommended 3rd party backend package; or, install the keyrings.alt package if you want to use the non-recommended backends. See https://pypi.org/project/keyring for details.

Windows Credential Locker(Windows資格情報)をバックエンドにした場合

Windows資格情報に対する追加・削除・参照ができる。

1
2
3
4
5
6
7
import keyring

keyring.set_password('test', 'testuser', 'passpasstest')
print(keyring.get_password('test', 'testuser'))

keyring.delete_password('test', 'testuser')
print(keyring.get_password('test', 'testuser'))

実行結果

1
2
3
PS > python .\keyring-test.py
passpasstest
None