Skip to content

Commit 10c8348

Browse files
fix nit; better naming
1 parent 9d3fca4 commit 10c8348

File tree

2 files changed

+39
-28
lines changed

2 files changed

+39
-28
lines changed

src/snowflake/connector/crl.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -193,15 +193,17 @@ def __init__(
193193
self._read_timeout_ms = read_timeout_ms
194194
self._cache_validity_time = cache_validity_time
195195
self._cache_manager = cache_manager or CRLCacheManager.noop()
196-
self._validate_certificate_cache: dict[
197-
x509.Certificate, CRLValidationResult
198-
] = {}
199196

200197
# list of trusted CA and their certificates
201-
self._trusted_ca: dict[x509.Name, list(x509.Certificate)] = defaultdict(list)
198+
self._trusted_ca: dict[x509.Name, list[x509.Certificate]] = defaultdict(list)
202199
for cert in trusted_certificates:
203200
self._trusted_ca[cert.subject].append(cert)
204201

202+
# declaration of validate_certificate_is_not_revoked function cache
203+
self._cache_for__validate_certificate_is_not_revoked: dict[
204+
x509.Certificate, CRLValidationResult
205+
] = {}
206+
205207
@classmethod
206208
def from_config(
207209
cls,
@@ -337,7 +339,9 @@ def traverse_chain(cert: x509.Certificate) -> CRLValidationResult | None:
337339

338340
if trusted_ca_issuer := self._get_trusted_ca_issuer(cert):
339341
logger.debug("Certificate signed by trusted CA: %s", cert.subject)
340-
return self._validate_certificate_with_cache(cert, trusted_ca_issuer)
342+
return self._validate_certificate_is_not_revoked_with_cache(
343+
cert, trusted_ca_issuer
344+
)
341345

342346
if cert.issuer in is_being_visited:
343347
# cycle detected - invalid path
@@ -360,7 +364,9 @@ def traverse_chain(cert: x509.Certificate) -> CRLValidationResult | None:
360364
continue
361365
if ca_result == CRLValidationResult.UNREVOKED:
362366
# good path found
363-
return self._validate_certificate_with_cache(cert, ca_cert)
367+
return self._validate_certificate_is_not_revoked_with_cache(
368+
cert, ca_cert
369+
)
364370
valid_results.append((ca_result, ca_cert))
365371

366372
if len(valid_results) == 0:
@@ -371,7 +377,9 @@ def traverse_chain(cert: x509.Certificate) -> CRLValidationResult | None:
371377
# check if there exists an ERROR path
372378
for ca_result, ca_cert in valid_results:
373379
if ca_result == CRLValidationResult.ERROR:
374-
cert_result = self._validate_certificate_with_cache(cert, ca_cert)
380+
cert_result = self._validate_certificate_is_not_revoked_with_cache(
381+
cert, ca_cert
382+
)
375383
if cert_result == CRLValidationResult.REVOKED:
376384
return CRLValidationResult.REVOKED
377385
return CRLValidationResult.ERROR
@@ -418,17 +426,17 @@ def _verify_certificate_signature(
418426
except Exception:
419427
return False
420428

421-
def _validate_certificate_with_cache(
429+
def _validate_certificate_is_not_revoked_with_cache(
422430
self, cert: x509.Certificate, ca_cert: x509.Certificate
423431
) -> CRLValidationResult:
424432
# validate certificate can be called multiple times with the same certificate
425-
if cert not in self._validate_certificate_cache:
426-
self._validate_certificate_cache[cert] = self._validate_certificate(
427-
cert, ca_cert
433+
if cert not in self._cache_for__validate_certificate_is_not_revoked:
434+
self._cache_for__validate_certificate_is_not_revoked[cert] = (
435+
self._validate_certificate_is_not_revoked(cert, ca_cert)
428436
)
429-
return self._validate_certificate_cache[cert]
437+
return self._cache_for__validate_certificate_is_not_revoked[cert]
430438

431-
def _validate_certificate(
439+
def _validate_certificate_is_not_revoked(
432440
self, cert: x509.Certificate, ca_cert: x509.Certificate
433441
) -> CRLValidationResult:
434442
"""Validate a single certificate against CRL"""

test/unit/test_crl.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ def mock_validate(cert, _):
664664
return CRLValidationResult.REVOKED
665665
return CRLValidationResult.UNREVOKED
666666

667-
validator._validate_certificate = mock_validate
667+
validator._validate_certificate_is_not_revoked = mock_validate
668668

669669
assert (
670670
validator._validate_single_chain([chain.leafA, chain.BsignA, chain.rootA])
@@ -684,7 +684,7 @@ def test_validate_single_chain(cert_gen, session_manager):
684684

685685
# case 1: at least one valid path
686686
def mock_validate_with_special_cert(revoked_cert, error_result):
687-
validator._validate_certificate_with_cache = lambda cert, _: (
687+
validator._validate_certificate_is_not_revoked_with_cache = lambda cert, _: (
688688
error_result if cert == revoked_cert else CRLValidationResult.UNREVOKED
689689
)
690690

@@ -702,7 +702,7 @@ def mock_validate(cert, _):
702702
return CRLValidationResult.REVOKED
703703
return CRLValidationResult.UNREVOKED
704704

705-
validator._validate_certificate_with_cache = mock_validate
705+
validator._validate_certificate_is_not_revoked_with_cache = mock_validate
706706
assert validator._validate_single_chain(input_chain) == CRLValidationResult.REVOKED
707707

708708
# case 3: revoked + error should result in revoked\
@@ -711,14 +711,14 @@ def mock_validate(cert, _):
711711
return CRLValidationResult.REVOKED
712712
return CRLValidationResult.ERROR
713713

714-
validator._validate_certificate_with_cache = mock_validate
714+
validator._validate_certificate_is_not_revoked_with_cache = mock_validate
715715
assert validator._validate_single_chain(input_chain) == CRLValidationResult.REVOKED
716716

717717
# case 4: no path to trusted certificate
718718
def mock_validate(cert, _):
719719
return CRLValidationResult.UNREVOKED
720720

721-
validator._validate_certificate_with_cache = mock_validate
721+
validator._validate_certificate_is_not_revoked_with_cache = mock_validate
722722
assert (
723723
validator._validate_single_chain(
724724
[chain.leafA, chain.leafB, chain.AsignB, chain.BsignA]
@@ -734,7 +734,7 @@ def mock_validate(cert, _):
734734
return CRLValidationResult.ERROR
735735
return CRLValidationResult.UNREVOKED
736736

737-
validator._validate_certificate_with_cache = mock_validate
737+
validator._validate_certificate_is_not_revoked_with_cache = mock_validate
738738
assert (
739739
validator._validate_single_chain(
740740
[chain.leafA, chain.rootA, chain.leafB, chain.rootB, chain.BsignA]
@@ -1546,7 +1546,7 @@ def test_crl_validator_validate_certificate_with_cache_hit(
15461546
) as mock_check, mock_patch.object(
15471547
validator, "_verify_crl_signature", return_value=True
15481548
) as mock_verify:
1549-
result = validator._validate_certificate(cert, ca_cert)
1549+
result = validator._validate_certificate_is_not_revoked(cert, ca_cert)
15501550

15511551
# Should use cached CRL
15521552
assert result == CRLValidationResult.UNREVOKED
@@ -1591,7 +1591,7 @@ def test_crl_validator_validate_certificate_with_cache_miss(
15911591
mock_crl.next_update_utc = datetime.now(timezone.utc) + timedelta(days=7)
15921592
mock_crl.issuer = ca_cert.subject # Set the CRL issuer to match CA subject
15931593
mock_load_crl.return_value = mock_crl
1594-
result = validator._validate_certificate(cert, ca_cert)
1594+
result = validator._validate_certificate_is_not_revoked(cert, ca_cert)
15951595

15961596
# Should download CRL and cache it
15971597
assert result == CRLValidationResult.UNREVOKED
@@ -1780,7 +1780,9 @@ def test_crl_signature_verification_integration_with_validation_flow(
17801780
with mock_patch.object(
17811781
validator_enabled, "_fetch_crl_from_url", return_value=invalid_crl_bytes
17821782
):
1783-
result = validator_enabled._validate_certificate(cert, cert_gen.ca_certificate)
1783+
result = validator_enabled._validate_certificate_is_not_revoked(
1784+
cert, cert_gen.ca_certificate
1785+
)
17841786
assert result == CRLValidationResult.ERROR
17851787

17861788
# Test in ADVISORY mode - should also fail due to signature verification failure
@@ -1794,7 +1796,9 @@ def test_crl_signature_verification_integration_with_validation_flow(
17941796
with mock_patch.object(
17951797
validator_advisory, "_fetch_crl_from_url", return_value=invalid_crl_bytes
17961798
):
1797-
result = validator_advisory._validate_certificate(cert, cert_gen.ca_certificate)
1799+
result = validator_advisory._validate_certificate_is_not_revoked(
1800+
cert, cert_gen.ca_certificate
1801+
)
17981802
# Even in ADVISORY mode, signature verification failure should return ERROR
17991803
# We cannot trust a CRL whose signature cannot be verified
18001804
assert result == CRLValidationResult.ERROR
@@ -2066,16 +2070,15 @@ def test_validate_certificate_signatures_in_chain(cert_gen, session_manager):
20662070
)
20672071

20682072

2069-
def test_is_certificate_trusted_by_os(cert_gen):
2070-
"""Test OS certificate trust validation."""
2071-
# Create a test certificate chain
2073+
def test_trusted_certificates_helpers(cert_gen):
20722074
chain = cert_gen.create_simple_chain()
20732075

2074-
# Create a CRLValidator instance with SSL context
20752076
validator = CRLValidator(
20762077
session_manager=Mock(), trusted_certificates=[chain.root_cert]
20772078
)
20782079

2079-
# Test with a certificate that's in the CA certificates list
20802080
assert validator._is_certificate_trusted_by_os(chain.root_cert) is True
20812081
assert validator._is_certificate_trusted_by_os(chain.intermediate_cert) is False
2082+
2083+
assert validator._get_trusted_ca_issuer(chain.intermediate_cert) is chain.root_cert
2084+
assert validator._get_trusted_ca_issuer(chain.leaf_cert) is None

0 commit comments

Comments
 (0)