Skip to content

Commit 24a7805

Browse files
authored
ci(iast): fix postgres connexion in integrations_packages (#13297)
In this previous pull request, we reduced the timeout for the suite specs because some tests were migrated to integrations_packages. https://github.com/DataDog/dd-trace-py/pull/13261/files#diff-f2d9ffc2ed86aa35a3c226322703b2f640a1806b97dfade074d3b6bed795061aR119 For some reason, the PostgreSQL database tests are having trouble connecting to the database, which causes timeouts and makes those tests flaky ## Checklist - [x] PR author has checked that all the criteria below are met - The PR description includes an overview of the change - The PR description articulates the motivation for the change - The change includes tests OR the PR description describes a testing strategy - The PR description notes risks associated with the change, if any - Newly-added code is easy to change - The change follows the [library release note guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html) - The change includes or references documentation updates if necessary - Backport labels are set (if [applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)) ## Reviewer Checklist - [x] Reviewer has checked that all the criteria below are met - Title is accurate - All changes are related to the pull request's stated goal - Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes - Testing strategy adequately addresses listed risks - Newly-added code is easy to change - Release note makes sense to a user of the library - If necessary, author has acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment - Backport labels are set in a manner that is consistent with the [release branch maintenance policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)
1 parent 40fe58d commit 24a7805

File tree

5 files changed

+24
-13
lines changed

5 files changed

+24
-13
lines changed

hatch.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ DD_IAST_DEDUPLICATION_ENABLED = "false"
454454
test = [
455455
"uname -a",
456456
"pip freeze",
457-
"python -m pytest --no-ddtrace {args:tests/appsec/integrations/packages_tests/}",
457+
"python -m pytest -vvv -s --no-cov --no-ddtrace {args:tests/appsec/integrations/packages_tests/}",
458458
]
459459

460460
[[envs.appsec_integrations_packages.matrix]]

tests/appsec/integrations/fixtures/sql_injection_mysqldb.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
import os
2-
31
import MySQLdb
42
from MySQLdb import OperationalError
53

64
from ddtrace.appsec._iast._taint_tracking._taint_objects import get_tainted_ranges
75
from ddtrace.appsec._iast._taint_tracking._taint_objects import is_pyobject_tainted
8-
9-
10-
MYSQL_HOST = os.getenv("TEST_MYSQL_HOST", "127.0.0.1")
6+
from tests.appsec.integrations.packages_tests.db_utils import MYSQL_HOST
117

128

139
def get_connection():
Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
from psycopg2.errors import DuplicateTable
2+
from psycopg2.errors import InFailedSqlTransaction
3+
from psycopg2.errors import QueryCanceled
24

35
from ddtrace.appsec._iast._taint_tracking._taint_objects import get_tainted_ranges
46
from ddtrace.appsec._iast._taint_tracking._taint_objects import is_pyobject_tainted
@@ -10,9 +12,15 @@ def sqli_simple(table):
1012
cur = connection.cursor()
1113
try:
1214
cur.execute("CREATE TABLE students (name TEXT, addr TEXT, city TEXT, pin TEXT)")
13-
except DuplicateTable:
14-
connection.rollback()
15-
# label test_sql_injection
16-
cur.execute("SELECT 1 FROM " + table)
17-
rows = cur.fetchone()
15+
except (DuplicateTable, QueryCanceled):
16+
pass
17+
18+
rows = []
19+
try:
20+
# label test_sql_injection
21+
cur.execute("SELECT 1 FROM " + table)
22+
rows = cur.fetchone()
23+
except (QueryCanceled, InFailedSqlTransaction):
24+
pass
25+
1826
return {"result": rows, "tainted": is_pyobject_tainted(table), "ranges": str(get_tainted_ranges(table))}

tests/appsec/integrations/fixtures/sql_injection_sqlalchemy.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44

55
from ddtrace.appsec._iast._taint_tracking._taint_objects import get_tainted_ranges
66
from ddtrace.appsec._iast._taint_tracking._taint_objects import is_pyobject_tainted
7+
from tests.appsec.integrations.packages_tests.db_utils import POSTGRES_HOST
78

89

910
def sqli_simple(table):
10-
engine = create_engine("postgresql://postgres:postgres@127.0.0.1/postgres")
11+
engine = create_engine(f"postgresql://postgres:postgres@{POSTGRES_HOST}/postgres")
1112
with engine.connect() as connection:
13+
connection.execute(text("SET statement_timeout = 1000"))
1214
try:
1315
connection.execute(text("CREATE TABLE students (name TEXT, addr TEXT, city TEXT, pin TEXT)"))
1416
except ProgrammingError:

tests/appsec/integrations/packages_tests/db_utils.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@
1010

1111
def get_psycopg2_connection():
1212
connection = psycopg2.connect(
13-
user="postgres", password="postgres", host=POSTGRES_HOST, port="5432", database="postgres"
13+
user="postgres",
14+
password="postgres",
15+
host=POSTGRES_HOST,
16+
port=5432,
17+
database="postgres",
18+
options="-c statement_timeout=1000",
1419
)
1520
return connection
1621

0 commit comments

Comments
 (0)