|
32 | 32 | from snowflake.connector.compat import (urlsplit, OK) |
33 | 33 | from snowflake.connector.errorcode import ( |
34 | 34 | ER_INVALID_OCSP_RESPONSE, |
| 35 | + ER_SERVER_CERTIFICATE_UNKNOWN, |
35 | 36 | ER_SERVER_CERTIFICATE_REVOKED) |
36 | 37 | from snowflake.connector.errors import OperationalError |
37 | 38 |
|
@@ -337,7 +338,7 @@ def _lazy_read_ca_bundle(): |
337 | 338 | 'Set REQUESTS_CA_BUNDLE to the file.') |
338 | 339 |
|
339 | 340 |
|
340 | | -def _create_ocsp_request(issuer, subject): |
| 341 | +def create_ocsp_request(issuer, subject): |
341 | 342 | """ |
342 | 343 | Create CertId and OCSPRequest |
343 | 344 | """ |
@@ -510,7 +511,7 @@ def _process_unknown_status(cert_id): |
510 | 511 | del OCSP_VALIDATION_CACHE[hkey] |
511 | 512 | raise OperationalError( |
512 | 513 | msg=u"The certificate is in UNKNOWN revocation status.", |
513 | | - errno=ER_SERVER_CERTIFICATE_REVOKED, |
| 514 | + errno=ER_SERVER_CERTIFICATE_UNKNOWN, |
514 | 515 | ) |
515 | 516 |
|
516 | 517 |
|
@@ -656,7 +657,7 @@ def is_cert_id_in_cache(issuer, subject): |
656 | 657 | global OCSP_VALIDATION_CACHE |
657 | 658 | global OCSP_VALIDATION_CACHE_UPDATED |
658 | 659 |
|
659 | | - cert_id, req = _create_ocsp_request(issuer, subject) |
| 660 | + cert_id, req = create_ocsp_request(issuer, subject) |
660 | 661 | hkey = _decode_cert_id_key(cert_id) |
661 | 662 | with OCSP_VALIDATION_CACHE_LOCK: |
662 | 663 | current_time = int(time.time()) |
@@ -720,10 +721,8 @@ def validate_by_direct_connection(issuer, subject, do_retry=True): |
720 | 721 | "Retrying... %s/%s", ex, retry + 1, max_retry) |
721 | 722 | err = ex |
722 | 723 | cache_status = False |
723 | | - if err: |
724 | | - raise err |
725 | 724 |
|
726 | | - return True, cert_id, ocsp_response |
| 725 | + return err, issuer, subject, cert_id, ocsp_response |
727 | 726 |
|
728 | 727 |
|
729 | 728 | def _download_ocsp_response_cache(url, do_retry=True): |
@@ -776,7 +775,7 @@ def _validate_certificates_sequential(cert_data, do_retry=True): |
776 | 775 | results = [] |
777 | 776 | _check_ocsp_response_cache_server(cert_data) |
778 | 777 | for issuer, subject in cert_data: |
779 | | - r = validate_by_direct_connection(issuer, subject, do_retry) |
| 778 | + r = validate_by_direct_connection(issuer, subject, do_retry=do_retry) |
780 | 779 | results.append(r) |
781 | 780 | return results |
782 | 781 |
|
@@ -796,7 +795,7 @@ def _check_ocsp_response_cache_server(cert_data): |
796 | 795 | in_cache = True |
797 | 796 | for issuer, subject in cert_data: |
798 | 797 | # check if OCSP response is in cache |
799 | | - cert_id, _ = _create_ocsp_request(issuer, subject) |
| 798 | + cert_id, _ = create_ocsp_request(issuer, subject) |
800 | 799 | hkey = _decode_cert_id_key(cert_id) |
801 | 800 | with OCSP_VALIDATION_CACHE_LOCK: |
802 | 801 | if hkey not in OCSP_VALIDATION_CACHE: |
@@ -957,16 +956,19 @@ def __init__(self, ocsp_response_cache_uri=None, |
957 | 956 | _read_ocsp_response_cache(self._ocsp_response_cache_uri) |
958 | 957 | 'test'.encode("charmap") |
959 | 958 |
|
960 | | - def validate_certfile(self, cert_filename): |
| 959 | + def validate_certfile(self, cert_filename, no_exception=False): |
961 | 960 | """ |
962 | 961 | Validates the certificate is NOT revoked |
963 | 962 | """ |
964 | 963 | cert_map = {} |
965 | 964 | read_cert_bundle(cert_filename, cert_map) |
966 | 965 | cert_data = _create_pair_issuer_subject(cert_map) |
967 | | - return self._validate(None, cert_data, do_retry=False) |
| 966 | + return self._validate( |
| 967 | + None, cert_data, do_retry=False, no_exception=no_exception) |
968 | 968 |
|
969 | | - def validate(self, hostname, connection, ignore_no_ocsp=False): |
| 969 | + def validate( |
| 970 | + self, hostname, connection, |
| 971 | + ignore_no_ocsp=False, no_exception=False): |
970 | 972 | """ |
971 | 973 | Validates the certificate is not revoked using OCSP |
972 | 974 | """ |
@@ -976,14 +978,16 @@ def validate(self, hostname, connection, ignore_no_ocsp=False): |
976 | 978 | return True |
977 | 979 |
|
978 | 980 | cert_data = _extract_certificate_chain(connection) |
979 | | - return self._validate(hostname, cert_data) |
| 981 | + return self._validate(hostname, cert_data, no_exception=no_exception) |
980 | 982 |
|
981 | | - def _validate(self, hostname, cert_data, do_retry=True): |
| 983 | + def _validate( |
| 984 | + self, hostname, cert_data, do_retry=True, no_exception=False): |
982 | 985 | global OCSP_VALIDATION_CACHE_UPDATED |
983 | 986 | global OCSP_VALIDATION_CACHE |
984 | 987 |
|
985 | 988 | # Validate certs sequentially if OCSP response cache server is used |
986 | | - results = _validate_certificates_sequential(cert_data, do_retry) |
| 989 | + results = _validate_certificates_sequential( |
| 990 | + cert_data, do_retry=do_retry) |
987 | 991 |
|
988 | 992 | with OCSP_VALIDATION_CACHE_LOCK: |
989 | 993 | if OCSP_VALIDATION_CACHE_UPDATED: |
@@ -1000,5 +1004,12 @@ def _validate(self, hostname, cert_data, do_retry=True): |
1000 | 1004 | len(cert_data)), |
1001 | 1005 | errno=ER_INVALID_OCSP_RESPONSE |
1002 | 1006 | ) |
1003 | | - logger.debug('ok') |
1004 | | - return True |
| 1007 | + any_err = False |
| 1008 | + for err, issuer, subject, cert_id, ocsp_response in results: |
| 1009 | + if not no_exception and err is not None: |
| 1010 | + raise err |
| 1011 | + elif err is not None: |
| 1012 | + any_err = True |
| 1013 | + |
| 1014 | + logger.debug('ok' if not any_err else 'failed') |
| 1015 | + return results |