-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
fix(logs): redact sensitive headers #1850
Merged
Merged
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d05f449
redact API key from debug logs
kristapratico 7f60f7e
add tests
kristapratico 670f3f4
fix
kristapratico 69f18c2
remove unused import
kristapratico 4289f8c
make case insensitive
kristapratico f3d9c66
review feedback
kristapratico ab11edd
Merge branch 'main' into redact-api-key
kristapratico a2984af
fix lint
kristapratico a137655
mypy ignore _logs.py
kristapratico 6d5a266
apply test feedback + lint
kristapratico File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
kristapratico marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import logging | ||
from typing import Any, Dict, cast | ||
|
||
import pytest | ||
|
||
from openai._utils import SensitiveHeadersFilter | ||
|
||
|
||
@pytest.fixture | ||
def logger_with_filter() -> logging.Logger: | ||
logger = logging.getLogger("test_logger") | ||
logger.setLevel(logging.DEBUG) | ||
logger.addFilter(SensitiveHeadersFilter()) | ||
return logger | ||
|
||
|
||
def test_keys_redacted(logger_with_filter: logging.Logger, caplog: pytest.LogCaptureFixture) -> None: | ||
with caplog.at_level(logging.DEBUG): | ||
logger_with_filter.debug( | ||
"Request options: %s", | ||
{ | ||
"method": "post", | ||
"url": "chat/completions", | ||
"headers": {"api-key": "12345", "Authorization": "Bearer token"}, | ||
}, | ||
) | ||
|
||
log_record = cast(Dict[str, Any], caplog.records[0].args) | ||
assert log_record["method"] == "post" | ||
assert log_record["url"] == "chat/completions" | ||
assert log_record["headers"]["api-key"] == "<redacted>" | ||
assert log_record["headers"]["Authorization"] == "<redacted>" | ||
kristapratico marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assert ( | ||
caplog.messages[0] | ||
== "Request options: {'method': 'post', 'url': 'chat/completions', 'headers': {'api-key': '<redacted>', 'Authorization': '<redacted>'}}" | ||
) | ||
|
||
|
||
def test_keys_redacted_case_insensitive(logger_with_filter: logging.Logger, caplog: pytest.LogCaptureFixture) -> None: | ||
with caplog.at_level(logging.DEBUG): | ||
logger_with_filter.debug( | ||
"Request options: %s", | ||
{ | ||
"method": "post", | ||
"url": "chat/completions", | ||
"headers": {"Api-key": "12345", "authorization": "Bearer token"}, | ||
}, | ||
) | ||
|
||
log_record = cast(Dict[str, Any], caplog.records[0].args) | ||
assert log_record["method"] == "post" | ||
assert log_record["url"] == "chat/completions" | ||
assert log_record["headers"]["Api-key"] == "<redacted>" | ||
assert log_record["headers"]["authorization"] == "<redacted>" | ||
assert ( | ||
caplog.messages[0] | ||
== "Request options: {'method': 'post', 'url': 'chat/completions', 'headers': {'Api-key': '<redacted>', 'authorization': '<redacted>'}}" | ||
) | ||
|
||
|
||
def test_no_headers(logger_with_filter: logging.Logger, caplog: pytest.LogCaptureFixture) -> None: | ||
with caplog.at_level(logging.DEBUG): | ||
logger_with_filter.debug( | ||
"Request options: %s", | ||
{"method": "post", "url": "chat/completions"}, | ||
) | ||
|
||
log_record = cast(Dict[str, Any], caplog.records[0].args) | ||
assert log_record["method"] == "post" | ||
assert log_record["url"] == "chat/completions" | ||
assert "api-key" not in log_record | ||
assert "Authorization" not in log_record | ||
assert caplog.messages[0] == "Request options: {'method': 'post', 'url': 'chat/completions'}" | ||
|
||
|
||
def test_headers_without_sensitive_info(logger_with_filter: logging.Logger, caplog: pytest.LogCaptureFixture) -> None: | ||
with caplog.at_level(logging.DEBUG): | ||
logger_with_filter.debug( | ||
"Request options: %s", | ||
{ | ||
"method": "post", | ||
"url": "chat/completions", | ||
"headers": {"custom": "value"}, | ||
}, | ||
) | ||
|
||
log_record = cast(Dict[str, Any], caplog.records[0].args) | ||
assert log_record["method"] == "post" | ||
assert log_record["url"] == "chat/completions" | ||
assert log_record["headers"] == {"custom": "value"} | ||
assert ( | ||
caplog.messages[0] | ||
== "Request options: {'method': 'post', 'url': 'chat/completions', 'headers': {'custom': 'value'}}" | ||
) | ||
|
||
|
||
def test_standard_debug_msg(logger_with_filter: logging.Logger, caplog: pytest.LogCaptureFixture) -> None: | ||
with caplog.at_level(logging.DEBUG): | ||
logger_with_filter.debug("Sending HTTP Request: %s %s", "POST", "chat/completions") | ||
assert caplog.messages[0] == "Sending HTTP Request: POST chat/completions" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note: I think you could've avoided the
_logger_with_filter
stuff by putting all these tests in a class and then doingbut totally not worth blocking on that change
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gotcha. Moved the tests in a class, however ruff still complains about the unused logger function, so kept it with underscore prefix.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah sorry tbc I meant that the class would just be for the logging tests as I assumed we wouldn't want the fixture to be setup for the other tests but if that would be fine then you don't need a class at all and it can just be at the module level
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh! I completely misunderstood, my bad 😆. Updating...