[core] The generation of RSA keys should be behind shared process locks.

This commit is contained in:
Fabio Manganiello 2024-07-25 00:05:53 +02:00
parent cf813e4197
commit 2cbb005c67
Signed by untrusted user: blacklight
GPG key ID: D90FBA7F76362774

View file

@ -14,6 +14,7 @@ import socket
import ssl import ssl
import time import time
import urllib.request import urllib.request
from collections import defaultdict
from importlib.machinery import SourceFileLoader from importlib.machinery import SourceFileLoader
from importlib.util import spec_from_loader, module_from_spec from importlib.util import spec_from_loader, module_from_spec
from multiprocessing import Lock as PLock from multiprocessing import Lock as PLock
@ -29,6 +30,7 @@ logger = logging.getLogger('utils')
Lock = Union[PLock, TLock] # type: ignore Lock = Union[PLock, TLock] # type: ignore
redis_pools: dict[Tuple[str, int], ConnectionPool] = {} redis_pools: dict[Tuple[str, int], ConnectionPool] = {}
key_locks: dict[str, Lock] = defaultdict(PLock)
def get_module_and_method_from_action(action): def get_module_and_method_from_action(action):
@ -564,6 +566,7 @@ def get_or_generate_stored_rsa_key_pair(
priv_key_file = os.path.join(keydir, os.path.basename(keyfile)) priv_key_file = os.path.join(keydir, os.path.basename(keyfile))
pub_key_file = priv_key_file + '.pub' pub_key_file = priv_key_file + '.pub'
with key_locks[keyfile]:
if os.path.isfile(priv_key_file) and os.path.isfile(pub_key_file): if os.path.isfile(priv_key_file) and os.path.isfile(pub_key_file):
with open(pub_key_file, 'r') as f1, open(priv_key_file, 'r') as f2: with open(pub_key_file, 'r') as f1, open(priv_key_file, 'r') as f2:
return ( return (