Clearer error handling on read_keys().

This commit is contained in:
kalzu 2023-01-01 23:25:02 +02:00
parent 8800275ed5
commit c02ea2849a
3 changed files with 15 additions and 20 deletions

View File

@ -11,6 +11,7 @@ import math
import json
import os
import time
import sys
import sqlite3
import binascii
import threading
@ -54,6 +55,11 @@ def read_keys():
Reads the declared KEYSFILE and returns "user_publickeys" from it.
Returns: json object
"""
empty_userkeys = {
"user_publickeys": {
"user_name": "user_ecdsa_public key"
}
}
# test if the file is there, we could make one if there is non
try:
# ascii is subset of UTF-8, this should be quite safe...
@ -61,20 +67,18 @@ def read_keys():
user_keys = json.load(cfile)
if 'user_publickeys' not in user_keys:
raise ValueError('Missing required property "user_publickeys" in config file')
print('Missing required property "user_publickeys" in config file')
sys.exit(1)
if user_keys == empty_userkeys:
print('Your userkeys.json file seems to not filled. Please insert your key there.')
sys.exit(1)
return user_keys["user_publickeys"]
except FileNotFoundError:
empty_userkeys = {
"user_publickeys": {
"user_name": "user_ecdsa_public key"
}
}
with open(KEYSFILE, "w", encoding='utf-8') as nfile:
print('You seem be lacking the users key file, created example for you. Exiting')
json.dump(empty_userkeys, nfile, indent=2)
raise FileNotFoundError
return {}
sys.exit(1)
def check_database():
"""
@ -421,11 +425,7 @@ if __name__ == "__main__":
database_lock = threading.Lock()
# Get the users public keys
try:
user_publickeys = read_keys()
except FileNotFoundError:
exit(1)
user_publickeys = read_keys()
# Start the data fetching backend process§
fetch_thread = threading.Thread(target=get_the_data)

View File

@ -1,5 +1,5 @@
{
"user_publickeys": {
"user_name": "user_ecdsa_public key"
"user1": "f1debc13fb21fe0eee54525aa4f8aae5733b201c755edaa55f8893c90aa375b261a62eaa3110651ac5d7705d402581256a37508b0a1ca28bd919ea44710d9c88"
}
}
}

View File

@ -1,5 +0,0 @@
{
"user_publickeys": {
"user1": "f1debc13fb21fe0eee54525aa4f8aae5733b201c755edaa55f8893c90aa375b261a62eaa3110651ac5d7705d402581256a37508b0a1ca28bd919ea44710d9c88"
}
}