|
| 1 | +#!/usr/bin/env python |
| 2 | +"""Integration test for proxy with real Snowflake connection. |
| 3 | +
|
| 4 | +Requirements: |
| 5 | + mitmproxy is installed automatically via the [development] extras in setup.cfg |
| 6 | + (Python 3.10+ only - mitmproxy has dependency conflicts on Python 3.9) |
| 7 | +
|
| 8 | +Important: |
| 9 | + When connecting through mitmproxy, you MUST set disable_ocsp_checks=True |
| 10 | + because mitmproxy performs MITM with self-signed certificates that cannot |
| 11 | + be validated via OCSP. |
| 12 | +""" |
| 13 | +from __future__ import annotations |
| 14 | + |
| 15 | +import sys |
| 16 | + |
| 17 | +import pytest |
| 18 | + |
| 19 | +from snowflake.connector.compat import IS_WINDOWS |
| 20 | + |
| 21 | +try: |
| 22 | + from snowflake.connector.util_text import random_string |
| 23 | +except ImportError: |
| 24 | + from ..randomize import random_string |
| 25 | + |
| 26 | + |
| 27 | +@pytest.mark.skipolddriver |
| 28 | +@pytest.mark.skipif( |
| 29 | + sys.version_info < (3, 10), |
| 30 | + reason="mitmproxy not installed for Python 3.9 due to dependency conflicts", |
| 31 | +) |
| 32 | +def test_put_with_https_proxy(conn_cnx, tmp_path, mitm_proxy, monkeypatch): |
| 33 | + test_file = tmp_path / "test_data.csv" |
| 34 | + test_file.write_text("col1,col2\n1,2\n3,4\n") |
| 35 | + |
| 36 | + # Explicitly configure environment to use the proxy |
| 37 | + mitm_proxy.set_env_vars(monkeypatch) |
| 38 | + |
| 39 | + # Connect to REAL Snowflake through mitmproxy |
| 40 | + # Must disable OCSP checks since mitmproxy presents self-signed certs for MITM |
| 41 | + with conn_cnx( |
| 42 | + disable_ocsp_checks=True, |
| 43 | + login_timeout=60, # Increase timeout for Windows proxy connection |
| 44 | + network_timeout=60, # Increase socket read timeout for proxy connections |
| 45 | + ) as conn: |
| 46 | + with conn.cursor() as cur: |
| 47 | + stage_name = random_string(5, "test_proxy_") |
| 48 | + cur.execute(f"CREATE TEMPORARY STAGE {stage_name}") |
| 49 | + |
| 50 | + # Use str().replace() for cross-platform file URI compatibility (like other tests) |
| 51 | + filename = str(test_file).replace("\\", "/") |
| 52 | + put_result = cur.execute( |
| 53 | + f"PUT 'file://{filename}' @{stage_name}" |
| 54 | + ).fetchall() |
| 55 | + |
| 56 | + assert len(put_result) > 0 |
| 57 | + assert put_result[0][6] == "UPLOADED" |
| 58 | + |
| 59 | + ls_result = cur.execute(f"LIST @{stage_name}").fetchall() |
| 60 | + assert len(ls_result) > 0 |
| 61 | + |
| 62 | + |
| 63 | +@pytest.mark.skipolddriver |
| 64 | +@pytest.mark.skipif( |
| 65 | + sys.version_info < (3, 10) or IS_WINDOWS, |
| 66 | + reason="mitmproxy not installed for Python 3.9 due to dependency conflicts", |
| 67 | +) |
| 68 | +def test_put_with_https_proxy_and_no_proxy_regression( |
| 69 | + conn_cnx, tmp_path, mitm_proxy, monkeypatch |
| 70 | +): |
| 71 | + """SNOW-2865839: PUT fails with TypeError when HTTPS_PROXY and NO_PROXY are set. |
| 72 | +
|
| 73 | + From bug report: |
| 74 | + "HTTPS_PROXY=http://localhost:8080 NO_PROXY='google.com' python test.py" |
| 75 | + causes TypeError during PUT operations. |
| 76 | +
|
| 77 | + Bug flow: |
| 78 | + 1. HTTPS_PROXY set (mitmproxy) |
| 79 | + 2. NO_PROXY set with ANY value (e.g., "google.com") |
| 80 | + 3. Execute PUT operation |
| 81 | + 4. storage_client passes bytes URL to use_session() |
| 82 | + 5. Without fix: TypeError: inet_aton() argument 1 must be str, not bytes |
| 83 | + 6. With fix: PUT succeeds |
| 84 | + """ |
| 85 | + test_file = tmp_path / "test_data.csv" |
| 86 | + test_file.write_text("col1,col2\n1,2\n3,4\n") |
| 87 | + |
| 88 | + # Configure environment to use mitmproxy |
| 89 | + mitm_proxy.set_env_vars(monkeypatch) |
| 90 | + |
| 91 | + # Set NO_PROXY with arbitrary value (from bug report) |
| 92 | + monkeypatch.setenv("NO_PROXY", "google.com") |
| 93 | + |
| 94 | + with conn_cnx( |
| 95 | + disable_ocsp_checks=True, |
| 96 | + login_timeout=60, # Increase timeout for Windows proxy connection |
| 97 | + network_timeout=60, # Increase socket read timeout for proxy connections for Windows |
| 98 | + ) as conn: |
| 99 | + with conn.cursor() as cur: |
| 100 | + stage_name = random_string(5, "test_no_proxy_") |
| 101 | + cur.execute(f"CREATE TEMPORARY STAGE {stage_name}") |
| 102 | + |
| 103 | + # This is where the bug occurs - storage_client passes bytes URL |
| 104 | + # Use str().replace() for cross-platform file URI compatibility (like other tests) |
| 105 | + filename = str(test_file).replace("\\", "/") |
| 106 | + put_result = cur.execute( |
| 107 | + f"PUT 'file://{filename}' @{stage_name}" |
| 108 | + ).fetchall() |
| 109 | + |
| 110 | + assert len(put_result) > 0 |
| 111 | + assert put_result[0][6] == "UPLOADED" |
| 112 | + |
| 113 | + ls_result = cur.execute(f"LIST @{stage_name}").fetchall() |
| 114 | + assert len(ls_result) > 0 |
0 commit comments