mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-10 22:41:41 +00:00
fix(key_management_endpoints.py): add new param new_key for setting the regenerated key value
user request
This commit is contained in:
parent
ad31da9685
commit
eec8e1a746
2 changed files with 17 additions and 3 deletions
|
|
@ -351,13 +351,11 @@ class LiteLLMRoutes(enum.Enum):
|
|||
"/v1beta/models/{model_name}:countTokens",
|
||||
"/v1beta/models/{model_name}:generateContent",
|
||||
"/v1beta/models/{model_name}:streamGenerateContent",
|
||||
|
||||
"/models/{model_name}:countTokens",
|
||||
"/models/{model_name}:generateContent",
|
||||
"/models/{model_name}:streamGenerateContent",
|
||||
]
|
||||
|
||||
|
||||
apply_guardrail_routes = [
|
||||
"/guardrails/apply_guardrail",
|
||||
]
|
||||
|
|
@ -789,6 +787,7 @@ class UpdateKeyRequest(KeyRequestBase):
|
|||
class RegenerateKeyRequest(GenerateKeyRequest):
|
||||
# This needs to be different from UpdateKeyRequest, because "key" is optional for this
|
||||
key: Optional[str] = None
|
||||
new_key: Optional[str] = None
|
||||
duration: Optional[str] = None
|
||||
spend: Optional[float] = None
|
||||
metadata: Optional[dict] = None
|
||||
|
|
|
|||
|
|
@ -696,6 +696,7 @@ async def prepare_key_update_data(
|
|||
):
|
||||
data_json: dict = data.model_dump(exclude_unset=True)
|
||||
data_json.pop("key", None)
|
||||
data_json.pop("new_key", None)
|
||||
non_default_values = {}
|
||||
for k, v in data_json.items():
|
||||
if k in LiteLLM_ManagementEndpoint_MetadataFields:
|
||||
|
|
@ -1860,6 +1861,9 @@ async def regenerate_key_fn(
|
|||
Parameters:
|
||||
- key: str (path parameter) - The key to regenerate
|
||||
- data: Optional[RegenerateKeyRequest] - Request body containing optional parameters to update
|
||||
- key: Optional[str] - The key to regenerate.
|
||||
- new_master_key: Optional[str] - The new master key to use, if key is the master key.
|
||||
- new_key: Optional[str] - The new key to use, if key is not the master key. If both set, new_master_key will be used.
|
||||
- key_alias: Optional[str] - User-friendly key alias
|
||||
- user_id: Optional[str] - User ID associated with key
|
||||
- team_id: Optional[str] - Team ID associated with key
|
||||
|
|
@ -1983,7 +1987,18 @@ async def regenerate_key_fn(
|
|||
|
||||
verbose_proxy_logger.debug("key_in_db: %s", _key_in_db)
|
||||
|
||||
new_token = f"sk-{secrets.token_urlsafe(LENGTH_OF_LITELLM_GENERATED_KEY)}"
|
||||
if data and data.new_key is not None:
|
||||
new_token = data.new_key
|
||||
if not data.new_key.startswith("sk-"):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail={
|
||||
"error": "New key must start with 'sk-'. This is to distinguish a key hash (used by litellm for logging / internal logic) from the actual key."
|
||||
},
|
||||
)
|
||||
else:
|
||||
new_token = f"sk-{secrets.token_urlsafe(LENGTH_OF_LITELLM_GENERATED_KEY)}"
|
||||
|
||||
new_token_hash = hash_token(new_token)
|
||||
new_token_key_name = f"sk-...{new_token[-4:]}"
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue