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
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@ def RestrictionDict(self) -> dict[str, tuple[str, str]]:
"""
return self._restrictions.copy()

def RedactedRestrictionDict(self) -> dict[str, tuple[str, str]]:
"""
Get a copy of the restrictions dictionary safe for logging.

Returns:
dict: Copy of the restrictions mapping API names to (header_key, redacted_value)
"""
return {api: (key, "***") for api, (key, _) in self._restrictions.items()}

def Insert(self, api: str, restriction: tuple[str, str]):
"""
Add a restriction for a specific API.
Expand Down
2 changes: 1 addition & 1 deletion python/openai/openai_frontend/frontend/fastapi_frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def _add_api_restriction_middleware(self, app: FastAPI):
APIRestrictionMiddleware, restricted_apis=self.restricted_apis
)
print(
f"[INFO] API restrictions enabled. Restricted API endpoints: {self.restricted_apis.RestrictionDict()}"
f"[INFO] API restrictions enabled. Restricted API endpoints: {self.restricted_apis.RedactedRestrictionDict()}"
)

def _add_request_size_limit_middleware(self, app: FastAPI):
Expand Down
22 changes: 22 additions & 0 deletions python/openai/tests/test_openai_restricted_apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import requests
from tests.utils import OpenAIServer

from frontend.fastapi.middleware.api_restriction import RestrictedFeatures


def assert_response_success(
response: requests.Response, expected_status: int = 200, description: str = ""
Expand Down Expand Up @@ -257,6 +259,26 @@ def test_duplicate_apis(self, malformed_arg):
expected_error_pattern="restricted api 'inference' can not be specified in multiple config groups",
)

def test_restriction_log_view_redacts_secret_values(self):
"""Restriction secrets are used for auth and must not be printed to logs."""
restrictions = RestrictedFeatures(
[
["inference", "api-key", "my-secret-key"],
["model-repository", "admin-key", "admin-secret"],
]
)

assert restrictions.RestrictionDict() == {
"inference": ("api-key", "my-secret-key"),
"model-repository": ("admin-key", "admin-secret"),
}
assert restrictions.RedactedRestrictionDict() == {
"inference": ("api-key", "***"),
"model-repository": ("admin-key", "***"),
}
assert "my-secret-key" not in repr(restrictions.RedactedRestrictionDict())
assert "admin-secret" not in repr(restrictions.RedactedRestrictionDict())

@pytest.mark.parametrize(
"malformed_arg",
[
Expand Down