From 428762542c372b4b910f6130b695d8f47ca0bfe9 Mon Sep 17 00:00:00 2001 From: Ishaan Jaff Date: Tue, 10 Sep 2024 08:04:18 -0700 Subject: [PATCH 1/2] fix regen keys when no duration is passed --- .../management_endpoints/key_management_endpoints.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/litellm/proxy/management_endpoints/key_management_endpoints.py b/litellm/proxy/management_endpoints/key_management_endpoints.py index ec17358e1..3173e3886 100644 --- a/litellm/proxy/management_endpoints/key_management_endpoints.py +++ b/litellm/proxy/management_endpoints/key_management_endpoints.py @@ -293,12 +293,12 @@ async def prepare_key_update_data( continue if v is not None and v not in ([], {}, 0): non_default_values[k] = v - if "duration" in non_default_values: duration = non_default_values.pop("duration") - duration_s = _duration_in_seconds(duration=duration) - expires = datetime.now(timezone.utc) + timedelta(seconds=duration_s) - non_default_values["expires"] = expires + if duration and (isinstance(duration, str)) and len(duration) > 0: + duration_s = _duration_in_seconds(duration=duration) + expires = datetime.now(timezone.utc) + timedelta(seconds=duration_s) + non_default_values["expires"] = expires if "budget_duration" in non_default_values: duration_s = _duration_in_seconds( From 39a8bb2bc4a5d4409b80ce43659d19efd0fdb7e4 Mon Sep 17 00:00:00 2001 From: Ishaan Jaff Date: Tue, 10 Sep 2024 09:12:03 -0700 Subject: [PATCH 2/2] add test test_regenerate_key_ui --- .../test_key_management.py | 53 +++++++++++++++++++ .../test_usage_endpoints.py | 1 + 2 files changed, 54 insertions(+) diff --git a/tests/proxy_admin_ui_tests/test_key_management.py b/tests/proxy_admin_ui_tests/test_key_management.py index ddc3adcc8..3d754ac63 100644 --- a/tests/proxy_admin_ui_tests/test_key_management.py +++ b/tests/proxy_admin_ui_tests/test_key_management.py @@ -269,3 +269,56 @@ async def test_regenerate_api_key_with_new_alias_and_expiration(prisma_client): now = datetime.now(dt.timezone.utc) assert new_key.expires > now + dt.timedelta(days=29) assert new_key.expires < now + dt.timedelta(days=31) + + +@pytest.mark.asyncio() +async def test_regenerate_key_ui(prisma_client): + litellm.set_verbose = True + setattr(litellm.proxy.proxy_server, "prisma_client", prisma_client) + setattr(litellm.proxy.proxy_server, "master_key", "sk-1234") + await litellm.proxy.proxy_server.prisma_client.connect() + import uuid + + # generate new key + key_alias = f"test_alias_regenerate_key-{uuid.uuid4()}" + spend = 100 + max_budget = 400 + models = ["fake-openai-endpoint"] + new_key = await generate_key_fn( + data=GenerateKeyRequest( + key_alias=key_alias, spend=spend, max_budget=max_budget, models=models + ), + user_api_key_dict=UserAPIKeyAuth( + user_role=LitellmUserRoles.PROXY_ADMIN, + api_key="sk-1234", + user_id="1234", + ), + ) + + generated_key = new_key.key + print(generated_key) + + # assert the new key works as expected + request = Request(scope={"type": "http"}) + request._url = URL(url="/chat/completions") + + async def return_body(): + return_string = f'{{"model": "fake-openai-endpoint"}}' + # return string as bytes + return return_string.encode() + + request.body = return_body + result = await user_api_key_auth(request=request, api_key=f"Bearer {generated_key}") + print(result) + + # regenerate the key + new_key = await regenerate_key_fn( + key=generated_key, + data=RegenerateKeyRequest(duration=""), + user_api_key_dict=UserAPIKeyAuth( + user_role=LitellmUserRoles.PROXY_ADMIN, + api_key="sk-1234", + user_id="1234", + ), + ) + print("response from regenerate_key_fn", new_key) diff --git a/tests/proxy_admin_ui_tests/test_usage_endpoints.py b/tests/proxy_admin_ui_tests/test_usage_endpoints.py index 9918015a1..68cf3b6f2 100644 --- a/tests/proxy_admin_ui_tests/test_usage_endpoints.py +++ b/tests/proxy_admin_ui_tests/test_usage_endpoints.py @@ -89,6 +89,7 @@ from litellm.caching import DualCache from litellm.proxy._types import ( DynamoDBArgs, GenerateKeyRequest, + RegenerateKeyRequest, KeyRequest, LiteLLM_UpperboundKeyGenerateParams, NewCustomerRequest,