winter-telescope / winterapi

API interactions for WINTER
MIT License
1 stars 1 forks source link

NoKeyringError when trying to add a program #40

Closed geoffreymo closed 7 months ago

geoffreymo commented 7 months ago

I am running the notebooks in a new environment on Centos 7 and ran into a NoKeyringError when adding a program. Adding a user worked fine, but I noticed that the .winterapi.txt and .winterapi.txt.lock files were empty. Here is the traceback from adding a program:

---------------------------------------------------------------------------
NoKeyringError                            Traceback (most recent call last)
Cell In[13], line 4
      2 if len(winter.get_programs()) == 0:
      3     print("No programs found. Add one to get started:")
----> 4     winter.add_program(overwrite=True)

File /data/tgif/winterapi/winterapi/messenger.py:195, in WinterAPI.add_program(self, program_name, program_api_key, overwrite)
    192 program_dict = res.json()["body"]
    193 program_dict["prog_key"] = program_api_key
--> 195 self.fidelius.add_program(program_details=program_dict, overwrite=overwrite)

File /data/tgif/winterapi/winterapi/fidelius.py:110, in Fidelius.add_program(self, program_details, overwrite)
    102 def add_program(self, program_details: dict, overwrite=False):
    103     """
    104     Function to add a program to the credentials.
    105 
   (...)
    108     :return: None
    109     """
--> 110     self.reload_secrets()
    112     program_details.pop("puid")
    114     program_details = Program(**program_details)

File /data/tgif/winterapi/winterapi/fidelius.py:47, in Fidelius.reload_secrets(self)
     41 def reload_secrets(self):
     42     """
     43     Reload the secrets from the keyring.
     44 
     45     :return: None
     46     """
---> 47     self.credentials = self.load_secrets()

File /data/tgif/winterapi/winterapi/fidelius.py:38, in Fidelius.load_secrets()
     31 @staticmethod
     32 def load_secrets() -> WinterAPICredentials:
     33     """
     34     Load the secrets from the keyring.
     35 
     36     :return:
     37     """
---> 38     secret_dict = get_secrets()
     39     return WinterAPICredentials(**secret_dict)

File /data/tgif/winterapi/winterapi/credentials.py:125, in get_secrets(keyring_service, keyring_user)
    122     with open(secret_path, "rb") as in_f:
    123         encrypted = in_f.read()
    124         decrypted = json.loads(
--> 125             decrypt(
    126                 encrypted,
    127                 keyring_service=keyring_service,
    128                 keyring_user=keyring_user,
    129             )
    130         )
    131 else:
    132     decrypted = {}

File /data/tgif/winterapi/winterapi/credentials.py:102, in decrypt(ciphertext, keyring_service, keyring_user)
     92 """
     93 Function to decrypt a ciphertext message.
     94 
   (...)
     98 :return: Decrypted message.
     99 """
    100 # Decrypt the message
    101 fernet = Fernet(
--> 102     get_encryption_password(
    103         keyring_service=keyring_service, keyring_user=keyring_user
    104     )
    105 )
    106 plaintext = fernet.decrypt(ciphertext)
    108 return plaintext.decode("utf-8")

File /data/tgif/winterapi/winterapi/credentials.py:146, in get_encryption_password(keyring_service, keyring_user)
    136 def get_encryption_password(
    137     keyring_service: str = KEYRING_SERVICE, keyring_user: str = KEYRING_USER
    138 ) -> bytes:
    139     """
    140     Get the encryption password from the keyring.
    141 
   (...)
    144     :return: Password
    145     """
--> 146     str_password = keyring.get_password(keyring_service, keyring_user)
    147     if str_password is None:
    148         password = base64.urlsafe_b64encode(secrets.token_bytes(32))

File /data/tgif/envs/lib/python3.11/site-packages/keyring/core.py:56, in get_password(service_name, username)
     54 def get_password(service_name: str, username: str) -> typing.Optional[str]:
     55     """Get password from the specified service."""
---> 56     return get_keyring().get_password(service_name, username)

File /data/tgif/envs/lib/python3.11/site-packages/keyring/backends/fail.py:28, in Keyring.get_password(self, service, username, password)
     21 def get_password(self, service, username, password=None):
     22     msg = (
     23         "No recommended backend was available. Install a recommended 3rd "
     24         "party backend package; or, install the keyrings.alt package if "
     25         "you want to use the non-recommended backends. See "
     26         "https://pypi.org/project/keyring for details."
     27     )
---> 28     raise NoKeyringError(msg)

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.
geoffreymo commented 7 months ago

I installed keyrings.alt using pip, restarted my notebook kernel, deleted the .winterapi.txt and .winterapi.txt.lock files, and then everything worked!