|
1 | 1 | #!/usr/bin/env python |
2 | 2 | from __future__ import annotations |
3 | 3 |
|
| 4 | +import logging |
| 5 | +import sys |
4 | 6 | import time |
5 | | -from os import path |
| 7 | +from argparse import ArgumentParser, Namespace |
6 | 8 | from time import gmtime, strftime |
7 | 9 |
|
8 | 10 | from asn1crypto import ocsp as asn1crypto_ocsp |
9 | 11 |
|
10 | 12 | from snowflake.connector.compat import urlsplit |
11 | 13 | from snowflake.connector.ocsp_asn1crypto import SnowflakeOCSPAsn1Crypto as SFOCSP |
| 14 | +from snowflake.connector.ocsp_snowflake import OCSPTelemetryData |
12 | 15 | from snowflake.connector.ssl_wrap_socket import _openssl_connect |
13 | 16 |
|
14 | 17 |
|
| 18 | +def _parse_args() -> Namespace: |
| 19 | + parser = ArgumentParser( |
| 20 | + prog="dump_ocsp_response", |
| 21 | + description="Dump OCSP Response for the URLs (an internal tool).", |
| 22 | + ) |
| 23 | + parser.add_argument( |
| 24 | + "-o", |
| 25 | + "--output-file", |
| 26 | + required=False, |
| 27 | + help="Dump output file", |
| 28 | + type=str, |
| 29 | + default=None, |
| 30 | + ) |
| 31 | + parser.add_argument( |
| 32 | + "--log-level", |
| 33 | + required=False, |
| 34 | + help="Log level", |
| 35 | + choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"], |
| 36 | + ) |
| 37 | + parser.add_argument("--log-file", required=False, help="Log file", default=None) |
| 38 | + parser.add_argument("urls", nargs="+", help="URLs to dump OCSP Response for") |
| 39 | + return parser.parse_args() |
| 40 | + |
| 41 | + |
15 | 42 | def main() -> None: |
16 | 43 | """Internal Tool: OCSP response dumper.""" |
17 | | - |
18 | | - def help() -> None: |
19 | | - print("Dump OCSP Response for the URL. ") |
20 | | - print( |
21 | | - """ |
22 | | -Usage: {} <url> [<url> ...] |
23 | | -""".format( |
24 | | - path.basename(sys.argv[0]) |
| 44 | + args = _parse_args() |
| 45 | + if args.log_level: |
| 46 | + if args.log_file: |
| 47 | + logging.basicConfig( |
| 48 | + filename=args.log_file, level=getattr(logging, args.log_level.upper()) |
25 | 49 | ) |
26 | | - ) |
27 | | - sys.exit(2) |
28 | | - |
29 | | - import sys |
30 | | - |
31 | | - if len(sys.argv) < 2: |
32 | | - help() |
33 | | - |
34 | | - urls = sys.argv[1:] |
35 | | - dump_ocsp_response(urls, output_filename=None) |
| 50 | + else: |
| 51 | + logging.basicConfig(stream=sys.stdout, level=logging.DEBUG) |
| 52 | + dump_ocsp_response(args.urls, output_filename=args.output_file) |
36 | 53 |
|
37 | 54 |
|
38 | 55 | def dump_good_status(current_time, single_response) -> None: |
@@ -87,7 +104,7 @@ def dump_ocsp_response(urls, output_filename): |
87 | 104 | for issuer, subject in cert_data: |
88 | 105 | _, _ = ocsp.create_ocsp_request(issuer, subject) |
89 | 106 | _, _, _, cert_id, ocsp_response_der = ocsp.validate_by_direct_connection( |
90 | | - issuer, subject |
| 107 | + issuer, subject, OCSPTelemetryData() |
91 | 108 | ) |
92 | 109 | ocsp_response = asn1crypto_ocsp.OCSPResponse.load(ocsp_response_der) |
93 | 110 | print("------------------------------------------------------------") |
@@ -115,7 +132,7 @@ def dump_ocsp_response(urls, output_filename): |
115 | 132 |
|
116 | 133 | if output_filename: |
117 | 134 | SFOCSP.OCSP_CACHE.write_ocsp_response_cache_file(ocsp, output_filename) |
118 | | - return SFOCSP.OCSP_CACHE.CACHE |
| 135 | + return SFOCSP.OCSP_CACHE |
119 | 136 |
|
120 | 137 |
|
121 | 138 | if __name__ == "__main__": |
|
0 commit comments