Skip to content

Commit 223fe6b

Browse files
Fpawlowski/snow 2011595 snowflake connector for python logging presigned ur ls (#2257)
1 parent fefa9a5 commit 223fe6b

File tree

6 files changed

+47
-38
lines changed

6 files changed

+47
-38
lines changed

.github/workflows/build_test.yml

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -178,19 +178,6 @@ jobs:
178178
run: python -m pip install -U setuptools pip wheel
179179
- name: Install tox
180180
run: python -m pip install tox>=4
181-
182-
- name: Run only the selected test
183-
run: python -m tox run -e py39-integ
184-
env:
185-
PYTEST_ADDOPTS: "test/integ/test_a_put_get_with_aws_token.py::test_put_get_with_aws"
186-
PYTHON_VERSION: ${{ matrix.python-version }}
187-
TOX_PARALLEL_NO_SPINNER: 1
188-
cloud_provider: [aws]
189-
shell: bash
190-
#
191-
# - name: Read logs
192-
# run: cat ./log_aws.log
193-
194181
- name: Run tests
195182
run: python -m tox run -e `echo py${PYTHON_VERSION/\./}-{extras,unit,integ,pandas,sso}-ci | sed 's/ /,/g'`
196183
env:

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ repos:
55
- id: check-hooks-apply
66
- id: check-useless-excludes
77
- repo: https://github.com/pre-commit/pre-commit-hooks
8-
rev: v5.0.0
8+
rev: v4.4.0
99
hooks:
1010
- id: trailing-whitespace
1111
exclude: >

test/integ/conftest.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -164,16 +164,22 @@ def init_test_schema(db_parameters) -> Generator[None]:
164164
165165
This is automatically called per test session.
166166
"""
167-
ret = db_parameters
168-
with snowflake.connector.connect(
169-
user=ret["user"],
170-
password=ret["password"],
171-
host=ret["host"],
172-
port=ret["port"],
173-
database=ret["database"],
174-
account=ret["account"],
175-
protocol=ret["protocol"],
176-
) as con:
167+
connection_params = {
168+
"user": db_parameters["user"],
169+
"password": db_parameters["password"],
170+
"host": db_parameters["host"],
171+
"port": db_parameters["port"],
172+
"database": db_parameters["database"],
173+
"account": db_parameters["account"],
174+
"protocol": db_parameters["protocol"],
175+
}
176+
177+
# Role may be needed when running on preprod, but is not present on Jenkins jobs
178+
optional_role = db_parameters.get("role")
179+
if optional_role is not None:
180+
connection_params.update(role=optional_role)
181+
182+
with snowflake.connector.connect(**connection_params) as con:
177183
con.cursor().execute(f"CREATE SCHEMA IF NOT EXISTS {TEST_SCHEMA}")
178184
yield
179185
con.cursor().execute(f"DROP SCHEMA IF EXISTS {TEST_SCHEMA}")

test/integ/test_large_result_set.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
#!/usr/bin/env python
22
from __future__ import annotations
33

4+
import logging
45
from unittest.mock import Mock
56

67
import pytest
78

9+
from snowflake.connector.secret_detector import SecretDetector
810
from snowflake.connector.telemetry import TelemetryField
911

1012
NUMBER_OF_ROWS = 50000
@@ -111,8 +113,9 @@ def test_query_large_result_set_n_threads(
111113

112114
@pytest.mark.aws
113115
@pytest.mark.skipolddriver
114-
def test_query_large_result_set(conn_cnx, db_parameters, ingest_data):
116+
def test_query_large_result_set(conn_cnx, db_parameters, ingest_data, caplog):
115117
"""[s3] Gets Large Result set."""
118+
caplog.set_level(logging.DEBUG)
116119
sql = "select * from {name} order by 1".format(name=db_parameters["name"])
117120
with conn_cnx() as cnx:
118121
telemetry_data = []
@@ -161,3 +164,17 @@ def test_query_large_result_set(conn_cnx, db_parameters, ingest_data):
161164
"Expected three telemetry logs (one per query) "
162165
"for log type {}".format(field.value)
163166
)
167+
168+
aws_request_present = False
169+
expected_token_prefix = "X-Amz-Signature="
170+
for line in caplog.text.splitlines():
171+
if expected_token_prefix in line:
172+
aws_request_present = True
173+
assert (
174+
expected_token_prefix + SecretDetector.SECRET_STARRED_MASK_STR
175+
in line
176+
), "connectionpool logger is leaking sensitive information"
177+
178+
assert (
179+
aws_request_present
180+
), "AWS URL was not found in logs, so it can't be assumed that no leaks happened in it"

test/integ/test_a_put_get_with_aws_token.py renamed to test/integ/test_put_get_with_aws_token.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import glob
55
import gzip
66
import os
7+
from logging import DEBUG
78

89
import pytest
910

@@ -43,6 +44,7 @@
4344
def test_put_get_with_aws(tmpdir, conn_cnx, from_path, caplog):
4445
"""[s3] Puts and Gets a small text using AWS S3."""
4546
# create a data file
47+
caplog.set_level(DEBUG)
4648
fname = str(tmpdir.join("test_put_get_with_aws_token.txt.gz"))
4749
original_contents = "123,test1\n456,test2\n"
4850
with gzip.open(fname, "wb") as f:
@@ -91,22 +93,17 @@ def test_put_get_with_aws(tmpdir, conn_cnx, from_path, caplog):
9193

9294
aws_request_present = False
9395
expected_token_prefix = "X-Amz-Signature="
94-
95-
print("\n\n")
96-
print(caplog.text)
97-
print("\n\n")
98-
99-
with open("./log_aws.log", "a") as f:
100-
f.write(caplog.text)
101-
10296
for line in caplog.text.splitlines():
103-
if expected_token_prefix in line:
97+
if ".amazonaws." in line:
10498
aws_request_present = True
99+
# getattr is used to stay compatible with old driver - before SECRET_STARRED_MASK_STR was added
105100
assert (
106-
expected_token_prefix + SecretDetector.SECRET_STARRED_MASK_STR in line
101+
expected_token_prefix
102+
+ getattr(SecretDetector, "SECRET_STARRED_MASK_STR", "****")
103+
in line
104+
or expected_token_prefix not in line
107105
), "connectionpool logger is leaking sensitive information"
108106

109-
# Connection pool is used on GitHub actions, but not always locally
110107
assert (
111108
aws_request_present
112109
), "AWS URL was not found in logs, so it can't be assumed that no leaks happened in it"

test/integ/test_put_get_with_azure_token.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,13 @@ def test_put_get_with_azure(tmpdir, conn_cnx, from_path, caplog):
8888
for line in caplog.text.splitlines():
8989
if "blob.core.windows.net" in line and expected_token_prefix in line:
9090
azure_request_present = True
91+
# getattr is used to stay compatible with old driver - before SECRET_STARRED_MASK_STR was added
9192
assert (
92-
expected_token_prefix + SecretDetector.SECRET_STARRED_MASK_STR in line
93+
expected_token_prefix
94+
+ getattr(SecretDetector, "SECRET_STARRED_MASK_STR", "****")
95+
in line
9396
), "connectionpool logger is leaking sensitive information"
9497

95-
# Connection pool is used on GitHub actions, but not always locally
9698
assert (
9799
azure_request_present
98100
), "Azure URL was not found in logs, so it can't be assumed that no leaks happened in it"

0 commit comments

Comments
 (0)