-
-
Notifications
You must be signed in to change notification settings - Fork 11.2k
fix(proxy): preserve master_key set by initialize() during lifespan startup #22483
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
Closed
giulio-leone
wants to merge
4
commits into
BerriAI:main
from
giulio-leone:fix/issue-22330-mcp-auth-context
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8138429
fix(proxy): preserve master_key set by initialize() during lifespan s…
cc9bd50
test(proxy): add regression test for master_key preservation (#22330)
e0ef8e8
fix: correct asynccontextmanager usage in test + is-not-None guard
giulio-leone cd6241c
chore: trigger re-review
giulio-leone 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 hidden or 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 hidden or 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,49 @@ | ||
| """ | ||
| Regression test for #22330: | ||
| A master_key set by initialize() (via config file) must be preserved when | ||
| LITELLM_MASTER_KEY is not set as an environment variable. | ||
| """ | ||
|
|
||
| from unittest.mock import AsyncMock, patch | ||
|
|
||
| import pytest | ||
|
|
||
| from litellm.proxy import proxy_server | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_master_key_preserved_when_env_var_absent(): | ||
| """proxy_startup_event must NOT overwrite a config-provided master_key.""" | ||
| config_key = "sk-from-config-file-1234" | ||
|
|
||
| # Simulate initialize() having set master_key from a config file | ||
| original_master_key = proxy_server.master_key | ||
| proxy_server.master_key = config_key | ||
|
|
||
| try: | ||
| with patch( | ||
| "litellm.proxy.proxy_server.get_secret_str", return_value=None | ||
| ), patch( | ||
| "litellm.proxy.proxy_server.get_secret", return_value=None | ||
| ), patch( | ||
| "litellm.proxy.proxy_server.init_verbose_loggers" | ||
| ), patch( | ||
| "litellm.proxy.proxy_server._license_check" | ||
| ): | ||
| # proxy_startup_event is an @asynccontextmanager, so we must | ||
| # enter it with `async with` — a bare `await` would only create | ||
| # the generator object without executing the function body. | ||
| try: | ||
| async with proxy_server.proxy_startup_event(app=AsyncMock()): | ||
| pass | ||
| except Exception: | ||
| # Startup will fail on DB/router setup — we only care about | ||
| # the master_key guard at the top of the function. | ||
| pass | ||
|
giulio-leone marked this conversation as resolved.
|
||
|
|
||
| assert proxy_server.master_key == config_key, ( | ||
| f"master_key was overwritten: expected {config_key!r}, " | ||
| f"got {proxy_server.master_key!r}" | ||
| ) | ||
| finally: | ||
| proxy_server.master_key = original_master_key | ||
|
giulio-leone marked this conversation as resolved.
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.