forked from microsoft/mssql-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.py
More file actions
170 lines (133 loc) · 5.53 KB
/
helpers.py
File metadata and controls
170 lines (133 loc) · 5.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
"""
Copyright (c) Microsoft Corporation.
Licensed under the MIT license.
This module provides helper functions for the mssql_python package.
"""
from mssql_python import ddbc_bindings
from mssql_python.exceptions import raise_exception
from mssql_python.logging_config import get_logger
import platform
from pathlib import Path
from mssql_python.ddbc_bindings import normalize_architecture
logger = get_logger()
def add_driver_to_connection_str(connection_str):
"""
Add the DDBC driver to the connection string if not present.
Args:
connection_str (str): The original connection string.
Returns:
str: The connection string with the DDBC driver added.
Raises:
Exception: If the connection string is invalid.
"""
driver_name = "Driver={ODBC Driver 18 for SQL Server}"
try:
# Strip any leading or trailing whitespace from the connection string
connection_str = connection_str.strip()
connection_str = add_driver_name_to_app_parameter(connection_str)
# Split the connection string into individual attributes
connection_attributes = connection_str.split(";")
final_connection_attributes = []
# Iterate through the attributes and exclude any existing driver attribute
for attribute in connection_attributes:
if attribute.lower().split("=")[0] == "driver":
continue
final_connection_attributes.append(attribute)
# Join the remaining attributes back into a connection string
connection_str = ";".join(final_connection_attributes)
# Insert the driver attribute at the beginning of the connection string
final_connection_attributes.insert(0, driver_name)
connection_str = ";".join(final_connection_attributes)
except Exception as e:
raise Exception(
"Invalid connection string, Please follow the format: "
"Server=server_name;Database=database_name;UID=user_name;PWD=password"
) from e
return connection_str
def check_error(handle_type, handle, ret):
"""
Check for errors and raise an exception if an error is found.
Args:
handle_type: The type of the handle (e.g., SQL_HANDLE_ENV, SQL_HANDLE_DBC).
handle: The SqlHandle object associated with the operation.
ret: The return code from the DDBC function call.
Raises:
RuntimeError: If an error is found.
"""
if ret < 0:
error_info = ddbc_bindings.DDBCSQLCheckError(handle_type, handle, ret)
if logger:
logger.error("Error: %s", error_info.ddbcErrorMsg)
raise_exception(error_info.sqlState, error_info.ddbcErrorMsg)
def add_driver_name_to_app_parameter(connection_string):
"""
Modifies the input connection string by appending the APP name.
Args:
connection_string (str): The input connection string.
Returns:
str: The modified connection string.
"""
# Split the input string into key-value pairs
parameters = connection_string.split(";")
# Initialize variables
app_found = False
modified_parameters = []
# Iterate through the key-value pairs
for param in parameters:
if param.lower().startswith("app="):
# Overwrite the value with 'MSSQL-Python'
app_found = True
key, _ = param.split("=", 1)
modified_parameters.append(f"{key}=MSSQL-Python")
else:
# Keep other parameters as is
modified_parameters.append(param)
# If APP key is not found, append it
if not app_found:
modified_parameters.append("APP=MSSQL-Python")
# Join the parameters back into a connection string
return ";".join(modified_parameters) + ";"
def sanitize_connection_string(conn_str: str) -> str:
"""
Sanitize the connection string by removing sensitive information.
Args:
conn_str (str): The connection string to sanitize.
Returns:
str: The sanitized connection string.
"""
# Remove sensitive information from the connection string, Pwd section
# Replace Pwd=...; or Pwd=... (end of string) with Pwd=***;
import re
return re.sub(r"(Pwd\s*=\s*)[^;]*", r"\1***", conn_str, flags=re.IGNORECASE)
def sanitize_user_input(user_input: str, max_length: int = 50) -> str:
"""
Sanitize user input for safe logging by removing control characters,
limiting length, and ensuring safe characters only.
Args:
user_input (str): The user input to sanitize.
max_length (int): Maximum length of the sanitized output.
Returns:
str: The sanitized string safe for logging.
"""
if not isinstance(user_input, str):
return "<non-string>"
# Remove control characters and non-printable characters
import re
# Allow alphanumeric, dash, underscore, and dot (common in encoding names)
sanitized = re.sub(r'[^\w\-\.]', '', user_input)
# Limit length to prevent log flooding
if len(sanitized) > max_length:
sanitized = sanitized[:max_length] + "..."
# Return placeholder if nothing remains after sanitization
return sanitized if sanitized else "<invalid>"
def log(level: str, message: str, *args) -> None:
"""
Universal logging helper that gets a fresh logger instance.
Args:
level: Log level ('debug', 'info', 'warning', 'error')
message: Log message with optional format placeholders
*args: Arguments for message formatting
"""
logger = get_logger()
if logger:
getattr(logger, level)(message, *args)