Skip to content

Commit 2e78257

Browse files
committed
refactor: logging improvements
1 parent 2898d86 commit 2e78257

File tree

8 files changed

+597
-23
lines changed

8 files changed

+597
-23
lines changed

redshift_connector/plugin/adfs_credentials_provider.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def form_based_authentication(self: "AdfsCredentialsProvider") -> str:
6161
_logger.error("A unknown error occurred when requesting SAML assertion to refresh credentials")
6262
raise InterfaceError(e)
6363

64-
_logger.debug(response.text)
64+
_logger.debug("ADFS form based authentication response length: {}".format(len(response.text)))
6565

6666
try:
6767
soup = bs4.BeautifulSoup(response.text, features="lxml")

redshift_connector/plugin/azure_credentials_provider.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def azure_oauth_based_authentication(self: "AzureCredentialsProvider") -> str:
103103
_logger.error("A unknown error occurred when requesting authentication from Azure.")
104104
raise InterfaceError(e)
105105

106-
_logger.debug(response.text)
106+
_logger.debug("Azure Oauth authentication response length: {}".format(len(response.text)))
107107

108108
# parse the JSON response to grab access_token field which contains Base64 encoded SAML
109109
# Assertion and decode it

redshift_connector/plugin/browser_azure_credentials_provider.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ def fetch_saml_response(self: "BrowserAzureCredentialsProvider", token):
148148
_logger.error("A unknown error occurred when requesting authentication from Azure")
149149
raise InterfaceError(e)
150150

151-
_logger.debug(response.text)
151+
_logger.debug("Azure authentication response length: {}".format(len(response.text)))
152152

153153
try:
154154
saml_assertion: str = response.json()["access_token"]

redshift_connector/plugin/ping_credentials_provider.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def get_saml_assertion(self: "PingCredentialsProvider") -> str:
6565
_logger.error("A unknown error occurred when requesting SAML assertion to refresh credentials")
6666
raise InterfaceError(e)
6767

68-
_logger.debug(response.content)
68+
_logger.debug("response length: {}".format(len(response.content)))
6969

7070
try:
7171
soup = bs4.BeautifulSoup(response.text)

redshift_connector/utils/logging_utils.py

Lines changed: 68 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,78 @@ def make_divider_block() -> str:
1212
def mask_secure_info_in_props(info: "RedshiftProperty") -> "RedshiftProperty":
1313
from redshift_connector import RedshiftProperty
1414

15+
logging_allow_list: typing.Tuple[str, ...] = (
16+
# "access_key_id",
17+
"allow_db_user_override",
18+
"app_id",
19+
"app_name",
20+
"application_name",
21+
"auth_profile",
22+
"auto_create",
23+
# "client_id",
24+
"client_protocol_version",
25+
# "client_secret",
26+
"cluster_identifier",
27+
"credentials_provider",
28+
"database_metadata_current_db_only",
29+
"db_groups",
30+
"db_name",
31+
"db_user",
32+
"duration",
33+
"endpoint_url",
34+
"force_lowercase",
35+
"group_federation",
36+
"host",
37+
"iam",
38+
"iam_disable_cache",
39+
"idp_host",
40+
"idpPort",
41+
"idp_response_timeout",
42+
"idp_tenant",
43+
"is_serverless",
44+
"listen_port",
45+
"login_url",
46+
"max_prepared_statements",
47+
"numeric_to_float",
48+
"partner_sp_id",
49+
# "password",
50+
"port",
51+
"preferred_role",
52+
"principal",
53+
"profile",
54+
"provider_name",
55+
"region",
56+
"replication",
57+
"role_arn",
58+
"role_session_name",
59+
"scope",
60+
# "secret_access_key",
61+
"serverless_acct_id",
62+
"serverless_work_group",
63+
# "session_token",
64+
"source_address",
65+
"ssl",
66+
"ssl_insecure",
67+
"sslmode",
68+
"tcp_keepalive",
69+
"timeout",
70+
"unix_sock",
71+
"user_name",
72+
# "web_identity_token",
73+
)
74+
1575
if info is None:
1676
return info
17-
secure_info_found: bool = False
18-
placeholder_value: str = "***"
1977

20-
temp: RedshiftProperty = copy.deepcopy(info)
78+
temp: RedshiftProperty = RedshiftProperty()
2179

2280
def is_populated(field: typing.Optional[str]):
2381
return field is not None and field != ""
2482

25-
if is_populated(temp.password):
26-
secure_info_found = True
27-
temp.password = placeholder_value
28-
if is_populated(temp.access_key_id):
29-
secure_info_found = True
30-
temp.access_key_id = placeholder_value
31-
if is_populated(temp.secret_access_key):
32-
secure_info_found = True
33-
temp.secret_access_key = placeholder_value
34-
if is_populated(temp.session_token):
35-
secure_info_found = True
36-
temp.session_token = placeholder_value
37-
38-
return temp if secure_info_found else info
83+
for parameter, value in info.__dict__.items():
84+
if parameter in logging_allow_list:
85+
temp.put(parameter, value)
86+
elif is_populated(value):
87+
temp.put(parameter, "***")
88+
89+
return temp

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ addopts =
2323
--cov-report term-missing
2424
--cov-report html:build/coverage
2525
--cov-report xml:build/coverage/coverage.xml
26-
testpaths=test
26+
test

test/unit/test_logging_utils.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,15 @@
33
from redshift_connector import RedshiftProperty
44
from redshift_connector.utils.logging_utils import mask_secure_info_in_props
55

6-
secret_rp_values = ("password", "access_key_id", "session_token", "secret_access_key")
6+
secret_rp_values = (
7+
"password",
8+
"access_key_id",
9+
"session_token",
10+
"secret_access_key",
11+
"client_id",
12+
"client_secret",
13+
"web_identity_token",
14+
)
715

816

917
@pytest.mark.parametrize("rp_arg", secret_rp_values)

0 commit comments

Comments
 (0)