Skip to content

Commit b6d752b

Browse files
authored
SNOW-642635: Remove legacy ocsp cache code (#1292)
1 parent 88843f8 commit b6d752b

File tree

8 files changed

+227
-637
lines changed

8 files changed

+227
-637
lines changed

DESCRIPTION.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ Source code is also available at: https://github.com/snowflakedb/snowflake-conne
88

99
# Release Notes
1010

11+
- v2.8.2(Unreleased)
12+
13+
- Improved performance of OCSP response caching
14+
1115
- v2.8.1(October 30,2022)
1216

1317
- Bumped cryptography dependency from <37.0.0 to <39.0.0

src/snowflake/connector/cache.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ def __init__(
5959
self._lock = Lock()
6060
self._reset_telemetry()
6161

62+
def __len__(self) -> int:
63+
with self._lock:
64+
return len(self._cache)
65+
6266
@classmethod
6367
def from_dict(
6468
cls,
@@ -474,18 +478,21 @@ def _save(self, load_first: bool = True) -> bool:
474478
)
475479
with open(tmp_file, "wb") as w_file:
476480
pickle.dump(self, w_file)
481+
# We write to a tmp file and then move it to have atomic write
482+
os.replace(tmp_file_path, self.file_path)
483+
self.last_loaded = datetime.datetime.fromtimestamp(
484+
getmtime(self.file_path),
485+
)
486+
return True
477487
except OSError as o_err:
478488
raise PermissionError(
479489
o_err.errno,
480490
"Cache folder is not writeable",
481491
_dir,
482492
)
483-
# We write to a tmp file and then move it to have atomic write
484-
os.replace(tmp_file_path, self.file_path)
485-
self.last_loaded = datetime.datetime.fromtimestamp(
486-
getmtime(self.file_path),
487-
)
488-
return True
493+
finally:
494+
if os.path.exists(tmp_file_path) and os.path.isfile(tmp_file_path):
495+
os.unlink(tmp_file_path)
489496
except Timeout:
490497
logger.debug(
491498
f"acquiring {self._file_lock_path} timed out, skipping saving..."

src/snowflake/connector/compat.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
urlunsplit = urllib.parse.urlunsplit
5454
parse_qs = urllib.parse.parse_qs
5555
urlparse = urllib.parse.urlparse
56+
urlunparse = urllib.parse.urlunparse
5657

5758
NUM_DATA_TYPES += (int, float, decimal.Decimal)
5859

src/snowflake/connector/ocsp_asn1crypto.py

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@
4444
ER_OCSP_RESPONSE_STATUS_UNSUCCESSFUL,
4545
)
4646
from snowflake.connector.errors import RevocationCheckError
47-
from snowflake.connector.ocsp_snowflake import SnowflakeOCSP
48-
from snowflake.connector.ssd_internal_keys import ret_wildcard_hkey
47+
from snowflake.connector.ocsp_snowflake import SnowflakeOCSP, generate_cache_key
4948

5049
with warnings.catch_warnings():
5150
warnings.simplefilter("ignore")
@@ -80,12 +79,6 @@ class SnowflakeOCSPAsn1Crypto(SnowflakeOCSP):
8079
"sha512": hashes.SHA3_512,
8180
}
8281

83-
WILDCARD_CERTID = None
84-
85-
def __init__(self, **kwargs):
86-
super().__init__(**kwargs)
87-
self.WILDCARD_CERTID = self.encode_cert_id_key(ret_wildcard_hkey())
88-
8982
def encode_cert_id_key(self, hkey):
9083
issuer_name_hash, issuer_key_hash, serial_number = hkey
9184
issuer_name_hash = OctetString.load(issuer_name_hash)
@@ -103,12 +96,8 @@ def encode_cert_id_key(self, hkey):
10396
)
10497
return cert_id
10598

106-
def decode_cert_id_key(self, cert_id):
107-
return (
108-
cert_id["issuer_name_hash"].dump(),
109-
cert_id["issuer_key_hash"].dump(),
110-
cert_id["serial_number"].dump(),
111-
)
99+
def decode_cert_id_key(self, cert_id: CertId) -> tuple[bytes, bytes, bytes]:
100+
return generate_cache_key(cert_id)
112101

113102
def decode_cert_id_base64(self, cert_id_base64):
114103
return CertId.load(b64decode(cert_id_base64))
@@ -365,7 +354,6 @@ def process_ocsp_response(self, issuer, cert_id, ocsp_response):
365354
try:
366355
if cert_status == "good":
367356
self._process_good_status(single_response, cert_id, ocsp_response)
368-
SnowflakeOCSP.OCSP_CACHE.update_cache(self, cert_id, ocsp_response)
369357
elif cert_status == "revoked":
370358
self._process_revoked_status(single_response, cert_id)
371359
elif cert_status == "unknown":

0 commit comments

Comments
 (0)