Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/changes/unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ no longer supports it (see exasol/project-keeper#808). This is a breaking change
* Removed the GitHub action `security-issues`
* Removed the CLI commands `tbx security`command group

## Bugfixes

* #1014: Fixed reporting fixed vulnerabilities in nox session `release:prepare`

## Refactoring

Expand Down
25 changes: 25 additions & 0 deletions doc/developer_guide/developer_guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,28 @@
../design
plugins
agent_skills

Deprecating Features
--------------------

When you plan to deprecate one of the PTB's features, you can give the users a
warning and period of time to prepare and adapt to the planned breaking
change.

For doing so:

* Add a comment and a constant to the file containing the nox session, e.g.:

.. code-block:: python

# The relevant nox session will be removed in:
# https://github.com/exasol/python-toolbox/issues/...
FEATURE_DEPRECATION_DATE = "2026-12-23"
* Add a warning to the resp. nox session, e.g.

.. code-block:: python

session.warn(
f'Warning: Nox session "{session.name}" is deprecated "
f"and will be removed on {FEATURE_DEPRECATION_DATE}."
)
8 changes: 8 additions & 0 deletions exasol/toolbox/nox/_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
)
from noxconfig import PROJECT_CONFIG

# The relevant nox session will be removed in:
# https://github.com/exasol/python-toolbox/issues/1013
DEPENDENCY_AUDIT_DEPRECATION_DATE = "2026-12-23"


def _format_update_vulnerabilities_message(was_updated: bool, report_json: str) -> str:
if not was_updated:
Expand Down Expand Up @@ -58,6 +62,10 @@ def audit(session: Session) -> None:

security_issue_dict = vulnerabilities.security_issue_dict
print(json.dumps(security_issue_dict, indent=2))
session.warn(
f'Warning: Nox session "{session.name}" is deprecated and will be removed on '
f"{DEPENDENCY_AUDIT_DEPRECATION_DATE}."
)


@nox.session(name="vulnerabilities:update", python=False)
Expand Down
32 changes: 22 additions & 10 deletions exasol/toolbox/util/dependencies/audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,23 +175,35 @@ def export_dependencies_to_file(output_file: Path, working_directory: Path) -> N
all dependencies defined in groups, like dev dependencies, and all optional
dependencies. We keep hashes so `pip-audit` can skip pip-based resolution.
"""
command = [

def run_str(command: str):
args = command.split()
run(*args)

def run(*command: str):
output = subprocess.run(
command,
capture_output=True,
text=True,
cwd=working_directory,
) # nosec: B603 - allow fixed poetry usage
if output.returncode != 0:
raise PoetryException.from_subprocess(
output,
list(command),
cwd=working_directory,
)

run_str("poetry self add poetry-plugin-export")
run(
"poetry",
"export",
"--format=requirements.txt",
"--all-groups",
"--all-extras",
"-o",
str(output_file),
]
output = subprocess.run(
command,
capture_output=True,
text=True,
cwd=working_directory,
) # nosec: B603 - allow fixed poetry usage
if output.returncode != 0:
raise PoetryException.from_subprocess(output, command, cwd=working_directory)
)


def audit_poetry_files(working_directory: Path) -> str:
Expand Down
89 changes: 46 additions & 43 deletions test/unit/util/dependencies/audit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from pathlib import Path
from subprocess import CompletedProcess
from unittest import mock
from unittest.mock import MagicMock
from unittest.mock import Mock

import pytest

Expand All @@ -22,13 +22,35 @@
from noxconfig import PROJECT_CONFIG


def completed_process(
returncode: int = 0,
stdout: str = "",
stderr: str = "",
) -> CompletedProcess:
return Mock(
spec=CompletedProcess,
returncode=returncode,
stdout=stdout,
stderr=stderr,
)


@pytest.fixture
def mock_poetry_export():
result = MagicMock(CompletedProcess)
result.returncode = 0
result.stdout = "dummy_requirements_content"
result.stderr = ""
return result
return completed_process(stdout="dummy_requirements_content")


def pip_audit_side_effect(stdout: str = "", stderr: str = ""):
pip_audit_result = completed_process(
returncode=(0 if stderr == "" else 1),
stdout=stdout,
stderr=stderr,
)
return [
completed_process(), # poetry self add plugin passes
completed_process(), # poetry export passes
pip_audit_result,
]


class TestVulnerability:
Expand Down Expand Up @@ -156,12 +178,11 @@ def extract_package_names(content) -> list[str]:
@mock.patch("subprocess.run")
def test_poetry_export_versions(mock_run, tmp_path):
requirements_txt = tmp_path / "requirements.txt"
mock_run.return_value = MagicMock(CompletedProcess)
mock_run.return_value.returncode = 0
mock_run.return_value.stdout = ""
mock_run.return_value.stderr = ""
mock_run.return_value = completed_process()

def write_hashes(command, **kwargs):
if " ".join(command) == "poetry self add poetry-plugin-export":
return mock_run.return_value
output_file = Path(command[command.index("-o") + 1])
output_file.write_text(
"astroid==4.0.4 --hash=sha256:deadbeef\n"
Expand Down Expand Up @@ -199,59 +220,48 @@ def write_hashes(command, **kwargs):
@mock.patch("subprocess.run")
def test_poetry_export_command_includes_disable_pip(mock_run, tmp_path):
requirements_txt = tmp_path / "requirements.txt"
mock_poetry_export = MagicMock(CompletedProcess)
mock_poetry_export.returncode = 0
mock_poetry_export.stdout = ""
mock_poetry_export.stderr = ""
mock_poetry_export = completed_process()
mock_run.side_effect = [mock_poetry_export, mock_poetry_export]

export_dependencies_to_file(
output_file=requirements_txt, working_directory=tmp_path
)

assert mock_run.call_args.args[0] == [
assert mock_run.call_args.args[0] == (
"poetry",
"export",
"--format=requirements.txt",
"--all-groups",
"--all-extras",
"-o",
str(requirements_txt),
]
)


class TestAuditPoetryFiles:
@staticmethod
@mock.patch("subprocess.run")
def test_poetry_export_fails(mock_run):
result = MagicMock(CompletedProcess)
result.returncode = 1
result.stdout = ""
result.stderr = "pyproject.toml changed significantly since poetry.lock was last generated. Run `poetry lock` to fix the lock file.\n"
mock_run.return_value = result
error_message = "Run poetry.lock to fix the lock file"
mock_run.return_value = completed_process(returncode=1, stderr=error_message)

with pytest.raises(PoetryException) as e:
audit_poetry_files(working_directory=Path())
assert e.value.stderr == result.stderr
assert e.value.stderr == error_message

@staticmethod
@mock.patch("subprocess.run")
def test_found_vulnerability_passes(
mock_run, mock_poetry_export, sample_vulnerability
):
mock_pip_audit = MagicMock(CompletedProcess)
mock_pip_audit.returncode = 1
mock_pip_audit.stdout = sample_vulnerability.pip_audit_json
mock_pip_audit.stderr = "Found 1 known vulnerability in 1 package"

mock_run.side_effect = [
mock_poetry_export, # poetry export passes
mock_pip_audit, # pip-audit finds vulnerability
]
mock_run.side_effect = pip_audit_side_effect(
stdout=sample_vulnerability.pip_audit_json,
stderr="Found 1 known vulnerability in 1 package",
)

result = audit_poetry_files(working_directory=Path())
assert result == mock_pip_audit.stdout
assert mock_run.call_args_list[1].args[0] == [
assert result == sample_vulnerability.pip_audit_json
assert mock_run.call_args_list[2].args[0] == [
"pip-audit",
"--disable-pip",
"-r",
Expand All @@ -263,23 +273,16 @@ def test_found_vulnerability_passes(
@staticmethod
@mock.patch("subprocess.run")
def test_pip_audit_failed_due_to_another_issue(mock_run, mock_poetry_export):
mock_pip_audit = MagicMock(CompletedProcess)
mock_pip_audit.returncode = 1
mock_pip_audit.stdout = ""
mock_pip_audit.stderr = (
error_message = (
"ERROR:pip_audit._cli:Failed to upgrade `pip`:"
" ['/tmp/tmpjcn9gz0d/bin/python', '-m', 'pip', 'install',"
" '--upgrade', 'pip', 'wheel', 'setuptools']"
)

mock_run.side_effect = [
mock_poetry_export, # poetry export passes
mock_pip_audit, # pip-audit failed
]
mock_run.side_effect = pip_audit_side_effect(stderr=error_message)

with pytest.raises(PipAuditException) as e:
audit_poetry_files(working_directory=Path())
assert e.value.stderr == mock_pip_audit.stderr
assert e.value.stderr == error_message


class TestVulnerabilities:
Expand Down
Loading