Added/corrected timeouts for sqlite3 database connections
Bumped mail plugin patch version for sqlite3 timeout change Code formatting corrections
This commit is contained in:
		
							parent
							
								
									2a7c933321
								
							
						
					
					
						commit
						0b2658374b
					
				
					 13 changed files with 158 additions and 123 deletions
				
			
		|  | @ -14,6 +14,7 @@ import nacl.exceptions | |||
| from coredb import keydb, dbfiles | ||||
| import onionrcrypto | ||||
| from onionrcrypto import getourkeypair | ||||
| from etc.onionrvalues import DATABASE_LOCK_TIMEOUT | ||||
| """ | ||||
|     This program is free software: you can redistribute it and/or modify | ||||
|     it under the terms of the GNU General Public License as published by | ||||
|  | @ -32,7 +33,8 @@ from onionrcrypto import getourkeypair | |||
| 
 | ||||
| def deleteExpiredKeys(): | ||||
|     # Fetch the keys we generated for the peer, that are still around | ||||
|     conn = sqlite3.connect(dbfiles.forward_keys_db, timeout=10) | ||||
|     conn = sqlite3.connect( | ||||
|         dbfiles.forward_keys_db, timeout=DATABASE_LOCK_TIMEOUT) | ||||
|     c = conn.cursor() | ||||
| 
 | ||||
|     curTime = epoch.get_epoch() | ||||
|  | @ -44,7 +46,8 @@ def deleteExpiredKeys(): | |||
| 
 | ||||
| 
 | ||||
| def deleteTheirExpiredKeys(pubkey): | ||||
|     conn = sqlite3.connect(dbfiles.user_id_info_db, timeout=10) | ||||
|     conn = sqlite3.connect( | ||||
|         dbfiles.user_id_info_db, timeout=DATABASE_LOCK_TIMEOUT) | ||||
|     c = conn.cursor() | ||||
| 
 | ||||
|     # Prepare the insert | ||||
|  | @ -63,12 +66,13 @@ class OnionrUser: | |||
| 
 | ||||
|     def __init__(self, publicKey, saveUser=False): | ||||
|         """ | ||||
|             OnionrUser is an abstraction for "users" of the network. | ||||
|         OnionrUser is an abstraction for "users" of the network. | ||||
| 
 | ||||
|             Takes a base32 encoded ed25519 public key, and a bool saveUser | ||||
|             saveUser determines if we should add a user to our peer database or not. | ||||
|         Takes a base32 encoded ed25519 public key, and a bool saveUser | ||||
|         saveUser determines if we should add a user to our peer database or not. | ||||
|         """ | ||||
|         publicKey = unpaddedbase32.repad(bytesconverter.str_to_bytes(publicKey)).decode() | ||||
|         publicKey = unpaddedbase32.repad( | ||||
|             bytesconverter.str_to_bytes(publicKey)).decode() | ||||
| 
 | ||||
|         self.trust = 0 | ||||
|         self.publicKey = publicKey | ||||
|  | @ -76,7 +80,7 @@ class OnionrUser: | |||
|         if saveUser and not publicKey == getourkeypair.get_keypair(): | ||||
|             try: | ||||
|                 keydb.addkeys.add_peer(publicKey) | ||||
|             except (AssertionError, ValueError) as e: | ||||
|             except (AssertionError, ValueError) as _: | ||||
|                 pass | ||||
| 
 | ||||
|         self.trust = keydb.userinfo.get_user_info(self.publicKey, 'trust') | ||||
|  | @ -102,11 +106,13 @@ class OnionrUser: | |||
|         return retData | ||||
| 
 | ||||
|     def encrypt(self, data): | ||||
|         encrypted = onionrcrypto.encryption.pub_key_encrypt(data, self.publicKey, encodedData=True) | ||||
|         encrypted = onionrcrypto.encryption.pub_key_encrypt( | ||||
|             data, self.publicKey, encodedData=True) | ||||
|         return encrypted | ||||
| 
 | ||||
|     def decrypt(self, data): | ||||
|         decrypted = onionrcrypto.encryption.pub_key_decrypt(data, self.publicKey, encodedData=True) | ||||
|         decrypted = onionrcrypto.encryption.pub_key_decrypt( | ||||
|             data, self.publicKey, encodedData=True) | ||||
|         return decrypted | ||||
| 
 | ||||
|     def forwardEncrypt(self, data): | ||||
|  | @ -115,33 +121,39 @@ class OnionrUser: | |||
|         retData = '' | ||||
|         forwardKey = self._getLatestForwardKey() | ||||
|         if stringvalidators.validate_pub_key(forwardKey[0]): | ||||
|             retData = onionrcrypto.encryption.pub_key_encrypt(data, forwardKey[0], encodedData=True) | ||||
|             retData = onionrcrypto.encryption.pub_key_encrypt( | ||||
|                 data, forwardKey[0], encodedData=True) | ||||
|         else: | ||||
|             raise onionrexceptions.InvalidPubkey("No valid forward secrecy key available for this user") | ||||
|         #self.generateForwardKey() | ||||
|             raise onionrexceptions.InvalidPubkey( | ||||
|                 "No valid forward secrecy key available for this user") | ||||
|         return (retData, forwardKey[0], forwardKey[1]) | ||||
| 
 | ||||
|     def forwardDecrypt(self, encrypted): | ||||
|         retData = "" | ||||
|         for key in self.getGeneratedForwardKeys(False): | ||||
|             try: | ||||
|                 retData = onionrcrypto.encryption.pub_key_decrypt(encrypted, privkey=key[1], encodedData=True) | ||||
|                 retData = onionrcrypto.encryption.pub_key_decrypt( | ||||
|                     encrypted, privkey=key[1], encodedData=True) | ||||
|             except nacl.exceptions.CryptoError: | ||||
|                 retData = False | ||||
|             else: | ||||
|                 break | ||||
|         else: | ||||
|             raise onionrexceptions.DecryptionError("Could not decrypt forward secrecy content") | ||||
|             raise onionrexceptions.DecryptionError( | ||||
|                 "Could not decrypt forward secrecy content") | ||||
|         return retData | ||||
| 
 | ||||
|     def _getLatestForwardKey(self): | ||||
|         # Get the latest forward secrecy key for a peer | ||||
|         key = "" | ||||
|         conn = sqlite3.connect(dbfiles.user_id_info_db, timeout=10) | ||||
|         conn = sqlite3.connect( | ||||
|             dbfiles.user_id_info_db, timeout=DATABASE_LOCK_TIMEOUT) | ||||
|         c = conn.cursor() | ||||
| 
 | ||||
|         # TODO: account for keys created at the same time (same epoch) | ||||
|         for row in c.execute("SELECT forwardKey, max(EXPIRE) FROM forwardKeys WHERE peerKey = ? ORDER BY expire DESC", (self.publicKey,)): | ||||
|         for row in c.execute( | ||||
|                 "SELECT forwardKey, max(EXPIRE) FROM forwardKeys WHERE peerKey = ? ORDER BY expire DESC",  # noqa | ||||
|                 (self.publicKey,)): | ||||
|             key = (row[0], row[1]) | ||||
|             break | ||||
| 
 | ||||
|  | @ -151,11 +163,14 @@ class OnionrUser: | |||
|         return key | ||||
| 
 | ||||
|     def _getForwardKeys(self): | ||||
|         conn = sqlite3.connect(dbfiles.user_id_info_db, timeout=10) | ||||
|         conn = sqlite3.connect( | ||||
|             dbfiles.user_id_info_db, timeout=DATABASE_LOCK_TIMEOUT) | ||||
|         c = conn.cursor() | ||||
|         keyList = [] | ||||
| 
 | ||||
|         for row in c.execute("SELECT forwardKey, date FROM forwardKeys WHERE peerKey = ? ORDER BY expire DESC", (self.publicKey,)): | ||||
|         for row in c.execute( | ||||
|                 "SELECT forwardKey, date FROM forwardKeys WHERE peerKey = ? ORDER BY expire DESC",  # noqa | ||||
|                 (self.publicKey,)): | ||||
|             keyList.append((row[0], row[1])) | ||||
| 
 | ||||
|         conn.commit() | ||||
|  | @ -166,7 +181,8 @@ class OnionrUser: | |||
|     def generateForwardKey(self, expire=DEFAULT_KEY_EXPIRE): | ||||
| 
 | ||||
|         # Generate a forward secrecy key for the peer | ||||
|         conn = sqlite3.connect(dbfiles.forward_keys_db, timeout=10) | ||||
|         conn = sqlite3.connect( | ||||
|             dbfiles.forward_keys_db, timeout=DATABASE_LOCK_TIMEOUT) | ||||
|         c = conn.cursor() | ||||
|         # Prepare the insert | ||||
|         time = epoch.get_epoch() | ||||
|  | @ -184,14 +200,16 @@ class OnionrUser: | |||
| 
 | ||||
|     def getGeneratedForwardKeys(self, genNew=True): | ||||
|         # Fetch the keys we generated for the peer, that are still around | ||||
|         conn = sqlite3.connect(dbfiles.forward_keys_db, timeout=10) | ||||
|         conn = sqlite3.connect( | ||||
|             dbfiles.forward_keys_db, timeout=DATABASE_LOCK_TIMEOUT) | ||||
|         c = conn.cursor() | ||||
|         pubkey = self.publicKey | ||||
|         pubkey = bytesconverter.bytes_to_str(pubkey) | ||||
|         command = (pubkey,) | ||||
|         keyList = [] # list of tuples containing pub, private for peer | ||||
|         keyList = []  # list of tuples containing pub, private for peer | ||||
| 
 | ||||
|         for result in c.execute("SELECT * FROM myForwardKeys WHERE peer = ?", command): | ||||
|         for result in c.execute( | ||||
|                 "SELECT * FROM myForwardKeys WHERE peer = ?", command): | ||||
|             keyList.append((result[1], result[2])) | ||||
| 
 | ||||
|         if len(keyList) == 0: | ||||
|  | @ -201,12 +219,14 @@ class OnionrUser: | |||
|         return list(keyList) | ||||
| 
 | ||||
|     def addForwardKey(self, newKey, expire=DEFAULT_KEY_EXPIRE): | ||||
|         newKey = bytesconverter.bytes_to_str(unpaddedbase32.repad(bytesconverter.str_to_bytes(newKey))) | ||||
|         newKey = bytesconverter.bytes_to_str( | ||||
|             unpaddedbase32.repad(bytesconverter.str_to_bytes(newKey))) | ||||
|         if not stringvalidators.validate_pub_key(newKey): | ||||
|             # Do not add if something went wrong with the key | ||||
|             raise onionrexceptions.InvalidPubkey(newKey) | ||||
| 
 | ||||
|         conn = sqlite3.connect(dbfiles.user_id_info_db, timeout=10) | ||||
|         conn = sqlite3.connect( | ||||
|             dbfiles.user_id_info_db, timeout=DATABASE_LOCK_TIMEOUT) | ||||
|         c = conn.cursor() | ||||
| 
 | ||||
|         # Get the time we're inserting the key at | ||||
|  | @ -218,7 +238,8 @@ class OnionrUser: | |||
|                 return False | ||||
|             if entry[1] == timeInsert: | ||||
|                 timeInsert += 1 | ||||
|                 time.sleep(1) # Sleep if our time is the same in order to prevent duplicate time records | ||||
|                 # Sleep if our time is the same to prevent dupe time records | ||||
|                 time.sleep(1) | ||||
| 
 | ||||
|         # Add a forward secrecy key for the peer | ||||
|         # Prepare the insert | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue