Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redact tokens/passwords from URLs in ConsDbClient HTTPError reports #120

Merged
merged 1 commit into from
Sep 6, 2024
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
22 changes: 21 additions & 1 deletion python/lsst/summit/utils/consdbClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import os
from dataclasses import dataclass
from typing import Any
from urllib.parse import quote
from urllib.parse import quote, urlparse

import requests
from astropy.table import Table
Expand Down Expand Up @@ -77,6 +77,24 @@ def _check_status(r: requests.Response) -> None:
raise e


def clean_url(resp: requests.Response, *args, **kwargs) -> requests.Response:
"""Parse url from response and remove netloc portion.

Set new url in response and return response

Parameters
----------
resp : `requests.Response`
The response that could contain a URL with tokens
"""
url = urlparse(resp.url)
short_user = f"{url.username[:2]}***" if url.username is not None else ""
short_pass = f":{url.password[:2]}***" if url.password is not None else ""
netloc = f"{short_user}{short_pass}@{url.hostname}"
resp.url = url._replace(netloc=netloc).geturl()
return resp


@dataclass
class FlexibleMetadataInfo:
"""Description of a flexible metadata value.
Expand Down Expand Up @@ -127,6 +145,8 @@ class ConsDbClient:

def __init__(self, url: str | None = None):
self.session = requests.Session()
self.session.hooks["response"].append(clean_url)

if url is None:
self.url = os.environ["LSST_CONSDB_PQ_URL"]
else:
Expand Down
41 changes: 41 additions & 0 deletions tests/test_consdbClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@

@pytest.fixture
def client():
"""Initialize client with a fake url
Requires mocking connection with @responses.activate decorator
"""
return ConsDbClient("http://example.com/consdb")


Expand Down Expand Up @@ -161,6 +164,44 @@ def test_schema(client):
assert client.schema(instrument, table) == description


@responses.activate
@pytest.mark.parametrize(
"secret, redacted",
[
("usdf:v987wefVMPz", "us***:v9***"),
("u:v", "u***:v***"),
("ulysses", "ul***"),
(":alberta94", "***:al***"),
],
)
def test_clean_token_url_response(secret, redacted):
"""Test tokens URL is cleaned when an error is thrown from requests
Use with pytest raises assert an error'
assert that url does not contain tokens
"""
domain = "@usdf-fake.slackers.stanford.edu/consdb"
complex_client = ConsDbClient(f"https://{secret}{domain}")

obs_type = "exposure"
responses.post(
f"https://{secret}{domain}/flex/bad_instrument/exposure/addkey",
status=404,
)
with pytest.raises(HTTPError, match="404") as error:
complex_client.add_flexible_metadata_key(
"bad_instrument", obs_type, "error", "int", "instrument error"
)

url = error.value.args[0].split()[-1]
sanitized = f"https://{redacted}{domain}/flex/bad_instrument/exposure/addkey"
assert url == sanitized

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you really want coverage, you could also test the username/no password case, a password/no username case (although I don't know if that is actually legal), and a single-character username case (if someone uses 1 or 2 character passwords, that's their own problem...).


def test_client(client):
"""Test ConsDbClient is initialized properly"""
assert "clean_url" in str(client.session.hooks["response"])


# TODO: more POST tests
# client.insert(instrument, table, obs_id, values, allow_update)
# client.insert_multiple(instrument, table, obs_dict, allow_update)
Expand Down
Loading