Skip to content
Merged
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
6 changes: 3 additions & 3 deletions .github/workflows/pre-commit.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ jobs:
name: "pre-commit"
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
- uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0
- uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0
with:
python-version: '3.x'
- name: pre-commit (cache)
uses: actions/cache@v4
uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0
with:
path: ~/.cache/pre-commit
key: pre-commit-${{ hashFiles('.pre-commit-config.yaml') }}
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/pypi-publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ jobs:
permissions:
id-token: write
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
- uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0
- uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0
with:
python-version: "3.12"
- name: Install uv
uses: astral-sh/setup-uv@v5
uses: astral-sh/setup-uv@d4b2f3b6ecc6e67c4457f6d3e41ec42d3d0fcb86 # v5.4.2
with:
version: "0.13.7"
- name: Compare tag and project version
Expand All @@ -29,4 +29,4 @@ jobs:
run: |
uv build
- name: Publish package distributions to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
uses: pypa/gh-action-pypi-publish@7245906fb61fa33a13554f9d5fb856fe61758086 # v1.14.3
6 changes: 3 additions & 3 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ jobs:
python-version: ['3.10', '3.11', '3.12', '3.13', '3.14']

steps:
- uses: actions/checkout@v4
- uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0
with:
python-version: ${{ matrix.python-version }}
cache: 'pip'

- name: Install uv
uses: astral-sh/setup-uv@v5
uses: astral-sh/setup-uv@d4b2f3b6ecc6e67c4457f6d3e41ec42d3d0fcb86 # v5.4.2
with:
version: "0.13.7"

Expand Down
71 changes: 71 additions & 0 deletions tests/test_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import pytest
import requests
from websockets.client import ClientProtocol
from websockets.uri import parse_uri

from wherobots.db.constants import DEFAULT_CLOSE_TIMEOUT_SECONDS
from wherobots.db.driver import (
Expand Down Expand Up @@ -581,6 +583,75 @@ def test_close_handshake_is_bounded(self, mock_ws):
assert kwargs["close_timeout"] == DEFAULT_CLOSE_TIMEOUT_SECONDS


def _send_handshake_request(*, uri, additional_headers, logger, **_):
"""Stand-in for websockets' connect that sends its real handshake request.

``ClientProtocol.send_request`` is where websockets traces each request
header at DEBUG, so this exercises the same logging the real connect does.
"""
protocol = ClientProtocol(parse_uri(uri), logger=logger)
request = protocol.connect()
request.headers.update(additional_headers)
protocol.send_request(request)
return MagicMock()


SECRET = "s3cr3t-credential-value"


class TestCredentialsStayOutOfDebugLogs:
def test_unfiltered_handshake_trace_exposes_headers(self, caplog):
caplog.set_level(logging.DEBUG)
_send_handshake_request(
uri="wss://compute.example.com/sql/org/session-id",
additional_headers={"X-API-Key": SECRET},
logger=None,
)
assert SECRET in caplog.text

@pytest.mark.parametrize(
"headers",
[{"X-API-Key": SECRET}, {"Authorization": f"Bearer {SECRET}"}],
ids=["api key", "token"],
)
@pytest.mark.parametrize("require_execution_ownership", [False, True])
def test_connect_direct(self, caplog, headers, require_execution_ownership):
caplog.set_level(logging.DEBUG)
with (
patch(
"wherobots.db.driver.websockets.sync.client.connect",
side_effect=_send_handshake_request,
),
patch("wherobots.db.driver.Connection"),
):
connect_direct(
uri="wss://compute.example.com/sql/org/session-id",
headers=headers,
require_execution_ownership=require_execution_ownership,
)
assert SECRET not in caplog.text

@pytest.mark.parametrize(
"credential",
[{"api_key": SECRET}, {"token": SECRET}],
ids=["api key", "token"],
)
@patch("wherobots.db.driver.requests.get")
@patch("wherobots.db.driver.requests.post")
def test_connect(self, mock_post, mock_get, caplog, credential):
caplog.set_level(logging.DEBUG)
_mock_ready_session(mock_post, mock_get)
with (
patch(
"wherobots.db.driver.websockets.sync.client.connect",
side_effect=_send_handshake_request,
),
patch("wherobots.db.driver.Connection"),
):
connect(**credential)
assert SECRET not in caplog.text


class TestWherobotsClientHeader:
"""connect() emits/appends the shared X-Wherobots-Client hop.

Expand Down
11 changes: 4 additions & 7 deletions wherobots/db/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,10 @@
DEFAULT_HTTP_TIMEOUT = 30


class _OwnershipTransportLogger(logging.LoggerAdapter):
class _TransportLogger(logging.LoggerAdapter):
def isEnabledFor(self, level: int) -> bool:
# WebSocket frame tracing includes credentials in the message body.
# websockets' DEBUG tracing writes handshake headers (API key, bearer
# token) and frame payloads (execution claims) in clear text.
return level >= logging.INFO and super().isEnabledFor(level)


Expand Down Expand Up @@ -444,11 +445,7 @@ def open_ws(
if require_execution_ownership
else None
),
logger=(
_OwnershipTransportLogger(logging.getLogger("websockets.client"))
if require_execution_ownership
else None
),
logger=_TransportLogger(logging.getLogger("websockets.client")),
)

@tenacity.retry(
Expand Down
Loading