Properly cache get_server_verify_keys
This commit is contained in:
parent
92d3fe8deb
commit
b39b294d1f
|
@ -101,10 +101,10 @@ class Keyring(object):
|
||||||
server_name(str): The name of the server to fetch a key for.
|
server_name(str): The name of the server to fetch a key for.
|
||||||
keys_ids (list of str): The key_ids to check for.
|
keys_ids (list of str): The key_ids to check for.
|
||||||
"""
|
"""
|
||||||
cached = yield self.store.get_server_verify_key(server_name, key_ids[0])
|
cached = yield self.store.get_server_verify_keys(server_name, key_ids)
|
||||||
|
|
||||||
if cached:
|
if cached:
|
||||||
defer.returnValue(cached)
|
defer.returnValue(cached[0])
|
||||||
return
|
return
|
||||||
|
|
||||||
download = self.key_downloads.get(server_name)
|
download = self.key_downloads.get(server_name)
|
||||||
|
|
|
@ -71,24 +71,24 @@ class KeyStore(SQLBaseStore):
|
||||||
desc="store_server_certificate",
|
desc="store_server_certificate",
|
||||||
)
|
)
|
||||||
|
|
||||||
@cached(num_args=2)
|
@cached()
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def get_server_verify_key(self, server_name, key_id):
|
def get_all_server_verify_keys(self, server_name):
|
||||||
key_bytes = yield self._simple_select_one_onecol(
|
rows = yield self._simple_select_list(
|
||||||
table="server_signature_keys",
|
table="server_signature_keys",
|
||||||
keyvalues={
|
keyvalues={
|
||||||
"server_name": server_name,
|
"server_name": server_name,
|
||||||
"key_id": key_id
|
|
||||||
},
|
},
|
||||||
retcol="verify_key",
|
retcols=["key_id", "verify_key"],
|
||||||
desc="get_server_verify_key",
|
desc="get_all_server_verify_keys",
|
||||||
allow_none=True,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if key_bytes:
|
defer.returnValue({
|
||||||
defer.returnValue(decode_verify_key_bytes(key_id, str(key_bytes)))
|
row["key_id"]: decode_verify_key_bytes(
|
||||||
else:
|
row["key_id"], str(row["verify_key"])
|
||||||
defer.returnValue(None)
|
)
|
||||||
|
for row in rows
|
||||||
|
})
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def get_server_verify_keys(self, server_name, key_ids):
|
def get_server_verify_keys(self, server_name, key_ids):
|
||||||
|
@ -100,23 +100,8 @@ class KeyStore(SQLBaseStore):
|
||||||
Returns:
|
Returns:
|
||||||
(list of VerifyKey): The verification keys.
|
(list of VerifyKey): The verification keys.
|
||||||
"""
|
"""
|
||||||
sql = (
|
keys = yield self.get_all_server_verify_keys(server_name)
|
||||||
"SELECT key_id, verify_key FROM server_signature_keys"
|
defer.returnValue([keys[k] for k in key_ids if k in keys])
|
||||||
" WHERE server_name = ?"
|
|
||||||
" AND key_id in (" + ",".join("?" for key_id in key_ids) + ")"
|
|
||||||
)
|
|
||||||
|
|
||||||
rows = yield self._execute_and_decode(
|
|
||||||
"get_server_verify_keys", sql, server_name, *key_ids
|
|
||||||
)
|
|
||||||
|
|
||||||
keys = []
|
|
||||||
for row in rows:
|
|
||||||
key_id = row["key_id"]
|
|
||||||
key_bytes = row["verify_key"]
|
|
||||||
key = decode_verify_key_bytes(key_id, str(key_bytes))
|
|
||||||
keys.append(key)
|
|
||||||
defer.returnValue(keys)
|
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def store_server_verify_key(self, server_name, from_server, time_now_ms,
|
def store_server_verify_key(self, server_name, from_server, time_now_ms,
|
||||||
|
@ -129,12 +114,11 @@ class KeyStore(SQLBaseStore):
|
||||||
ts_now_ms (int): The time now in milliseconds
|
ts_now_ms (int): The time now in milliseconds
|
||||||
verification_key (VerifyKey): The NACL verify key.
|
verification_key (VerifyKey): The NACL verify key.
|
||||||
"""
|
"""
|
||||||
key_id = "%s:%s" % (verify_key.alg, verify_key.version)
|
|
||||||
yield self._simple_upsert(
|
yield self._simple_upsert(
|
||||||
table="server_signature_keys",
|
table="server_signature_keys",
|
||||||
keyvalues={
|
keyvalues={
|
||||||
"server_name": server_name,
|
"server_name": server_name,
|
||||||
"key_id": key_id,
|
"key_id": "%s:%s" % (verify_key.alg, verify_key.version),
|
||||||
},
|
},
|
||||||
values={
|
values={
|
||||||
"from_server": from_server,
|
"from_server": from_server,
|
||||||
|
@ -144,7 +128,7 @@ class KeyStore(SQLBaseStore):
|
||||||
desc="store_server_verify_key",
|
desc="store_server_verify_key",
|
||||||
)
|
)
|
||||||
|
|
||||||
self.get_server_verify_key.invalidate(server_name, key_id)
|
self.get_all_server_verify_keys.invalidate(server_name)
|
||||||
|
|
||||||
def store_server_keys_json(self, server_name, key_id, from_server,
|
def store_server_keys_json(self, server_name, key_id, from_server,
|
||||||
ts_now_ms, ts_expires_ms, key_json_bytes):
|
ts_now_ms, ts_expires_ms, key_json_bytes):
|
||||||
|
|
Loading…
Reference in New Issue