(fix) adding public routes when using custom header (#7045)

* get_api_key_from_custom_header

* add test_get_api_key_from_custom_header

* fix testing use 1 file for test user api key auth

* fix test user api key auth

* test_custom_api_key_header_name
This commit is contained in:
Ishaan Jaff 2024-12-06 14:17:10 -08:00 committed by GitHub
parent 7e0680cf94
commit 14b7f25d7d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 97 additions and 440 deletions

View file

@ -1373,15 +1373,27 @@ def _get_user_role(
def get_api_key_from_custom_header(
request: Request, custom_litellm_key_header_name: str
):
) -> str:
"""
Get API key from custom header
Args:
request (Request): Request object
custom_litellm_key_header_name (str): Custom header name
Returns:
Optional[str]: API key
"""
api_key: str = ""
# use this as the virtual key passed to litellm proxy
custom_litellm_key_header_name = custom_litellm_key_header_name.lower()
_headers = {k.lower(): v for k, v in request.headers.items()}
verbose_proxy_logger.debug(
"searching for custom_litellm_key_header_name= %s, in headers=%s",
custom_litellm_key_header_name,
request.headers,
_headers,
)
custom_api_key = request.headers.get(custom_litellm_key_header_name)
custom_api_key = _headers.get(custom_litellm_key_header_name)
if custom_api_key:
api_key = _get_bearer_token(api_key=custom_api_key)
verbose_proxy_logger.debug(
@ -1390,7 +1402,7 @@ def get_api_key_from_custom_header(
)
)
else:
raise ValueError(
verbose_proxy_logger.exception(
f"No LiteLLM Virtual Key pass. Please set header={custom_litellm_key_header_name}: Bearer <api_key>"
)
return api_key