quarksrus
quarksrus

Reputation: 11

How can I automate passing the password for keyring inside a python script?

I'm trying to configure Keyring for the first time.

My environment: O/S Platform : Headless RHEL 8.6 Python version : 3.9 backend I'm using keyrings.alt.

What I have done so far is:

  1. Created the keyring config file at this location: my home directory/.local/share/python_keyring/keyringrc.cfg and set the default backend like this: [backend] default-keyring=keyrings.alt.file.EncryptedKeyring

  2. Initialized the keyring by running 'python3.9' interactively and loading it with a sample 'username' and some password. It prompts me to set a password and the keyring file gets created in my home directory/.local/share/python_keyring/crypted_pass.cfg and shows the service name, the username = <hashed_password>.

The issue I'm having is when I run the following python script to retrieve the credentials, it keeps prompting me for the "Please enter password for encrypted keyring:" password when the keyring.get_credential() method is called. Is there anyway to automate this without providing the keyring password in plaintext in the python script?

From what I have read, this should not be happening because the Keyring file gets 'unlocked' when I login with my username and password. No such luck.

import keyring
import keyring.util.platform_ as keyring_platform
import keyring.backend
from keyrings.alt.file import EncryptedKeyring
import secretstorage


keyring.set_keyring(EncryptedKeyring())

print(keyring_platform.config_root())

print(keyring.get_keyring())


service_name = "loo"
username_var = "username"

cred = keyring.get_credential(service_name, username_var)

Best, QRUS

Upvotes: 0

Views: 74

Answers (0)

Related Questions