mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-17 09:34:48 +00:00
fix(proxy): let the master key create and accept invitation links on a proxy with no default_user_id row
This commit is contained in:
parent
4212f1af27
commit
ce9b56c49a
7 changed files with 84 additions and 19 deletions
|
|
@ -0,0 +1,15 @@
|
|||
-- DropForeignKey
|
||||
DO $$
|
||||
BEGIN
|
||||
IF EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'LiteLLM_InvitationLink_created_by_fkey' AND conrelid = '"LiteLLM_InvitationLink"'::regclass) THEN
|
||||
ALTER TABLE "LiteLLM_InvitationLink" DROP CONSTRAINT "LiteLLM_InvitationLink_created_by_fkey";
|
||||
END IF;
|
||||
END $$;
|
||||
|
||||
-- DropForeignKey
|
||||
DO $$
|
||||
BEGIN
|
||||
IF EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'LiteLLM_InvitationLink_updated_by_fkey' AND conrelid = '"LiteLLM_InvitationLink"'::regclass) THEN
|
||||
ALTER TABLE "LiteLLM_InvitationLink" DROP CONSTRAINT "LiteLLM_InvitationLink_updated_by_fkey";
|
||||
END IF;
|
||||
END $$;
|
||||
|
|
@ -329,8 +329,6 @@ model LiteLLM_UserTable {
|
|||
// relations
|
||||
litellm_organization_table LiteLLM_OrganizationTable? @relation(fields: [organization_id], references: [organization_id])
|
||||
organization_memberships LiteLLM_OrganizationMembership[]
|
||||
invitations_created LiteLLM_InvitationLink[] @relation("CreatedBy")
|
||||
invitations_updated LiteLLM_InvitationLink[] @relation("UpdatedBy")
|
||||
invitations_user LiteLLM_InvitationLink[] @relation("UserId")
|
||||
object_permission LiteLLM_ObjectPermissionTable? @relation(fields: [object_permission_id], references: [object_permission_id])
|
||||
}
|
||||
|
|
@ -844,8 +842,6 @@ model LiteLLM_InvitationLink {
|
|||
|
||||
// Relations
|
||||
liteLLM_user_table_user LiteLLM_UserTable @relation("UserId", fields: [user_id], references: [user_id])
|
||||
liteLLM_user_table_created LiteLLM_UserTable @relation("CreatedBy", fields: [created_by], references: [user_id])
|
||||
liteLLM_user_table_updated LiteLLM_UserTable @relation("UpdatedBy", fields: [updated_by], references: [user_id])
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -329,8 +329,6 @@ model LiteLLM_UserTable {
|
|||
// relations
|
||||
litellm_organization_table LiteLLM_OrganizationTable? @relation(fields: [organization_id], references: [organization_id])
|
||||
organization_memberships LiteLLM_OrganizationMembership[]
|
||||
invitations_created LiteLLM_InvitationLink[] @relation("CreatedBy")
|
||||
invitations_updated LiteLLM_InvitationLink[] @relation("UpdatedBy")
|
||||
invitations_user LiteLLM_InvitationLink[] @relation("UserId")
|
||||
object_permission LiteLLM_ObjectPermissionTable? @relation(fields: [object_permission_id], references: [object_permission_id])
|
||||
}
|
||||
|
|
@ -844,8 +842,6 @@ model LiteLLM_InvitationLink {
|
|||
|
||||
// Relations
|
||||
liteLLM_user_table_user LiteLLM_UserTable @relation("UserId", fields: [user_id], references: [user_id])
|
||||
liteLLM_user_table_created LiteLLM_UserTable @relation("CreatedBy", fields: [created_by], references: [user_id])
|
||||
liteLLM_user_table_updated LiteLLM_UserTable @relation("UpdatedBy", fields: [updated_by], references: [user_id])
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -329,8 +329,6 @@ model LiteLLM_UserTable {
|
|||
// relations
|
||||
litellm_organization_table LiteLLM_OrganizationTable? @relation(fields: [organization_id], references: [organization_id])
|
||||
organization_memberships LiteLLM_OrganizationMembership[]
|
||||
invitations_created LiteLLM_InvitationLink[] @relation("CreatedBy")
|
||||
invitations_updated LiteLLM_InvitationLink[] @relation("UpdatedBy")
|
||||
invitations_user LiteLLM_InvitationLink[] @relation("UserId")
|
||||
object_permission LiteLLM_ObjectPermissionTable? @relation(fields: [object_permission_id], references: [object_permission_id])
|
||||
}
|
||||
|
|
@ -844,8 +842,6 @@ model LiteLLM_InvitationLink {
|
|||
|
||||
// Relations
|
||||
liteLLM_user_table_user LiteLLM_UserTable @relation("UserId", fields: [user_id], references: [user_id])
|
||||
liteLLM_user_table_created LiteLLM_UserTable @relation("CreatedBy", fields: [created_by], references: [user_id])
|
||||
liteLLM_user_table_updated LiteLLM_UserTable @relation("UpdatedBy", fields: [updated_by], references: [user_id])
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -7,11 +7,11 @@ import sys
|
|||
from collections.abc import Iterator, Mapping
|
||||
from pathlib import Path
|
||||
from types import MappingProxyType
|
||||
from typing import Final
|
||||
from typing import Final, TypeAlias
|
||||
|
||||
from pydantic import TypeAdapter
|
||||
|
||||
Schema = Mapping[str, Mapping[str, str]]
|
||||
Schema: TypeAlias = Mapping[str, Mapping[str, str]]
|
||||
MODELS: Final = re.compile(r"^[ \t]*model\s+(\w+)\s*\{(.*?)^[ \t]*\}", re.MULTILINE | re.DOTALL)
|
||||
|
||||
|
||||
|
|
@ -36,18 +36,23 @@ def _field_changes(model: str, field: str, old_type: str, new_fields: Mapping[st
|
|||
yield f"Breaking: Field {model}.{field} changed type from {old_type} to {new_type}"
|
||||
|
||||
|
||||
def _model_changes(model: str, old_fields: Mapping[str, str], new_schema: Schema) -> Iterator[str]:
|
||||
def _is_relation(field_type: str, old_schema: Schema) -> bool:
|
||||
return re.split(r"[\[?\s]", field_type, maxsplit=1)[0] in old_schema
|
||||
|
||||
|
||||
def _model_changes(model: str, old_schema: Schema, new_schema: Schema) -> Iterator[str]:
|
||||
if model not in new_schema:
|
||||
yield f"Breaking: Model {model} was removed"
|
||||
return
|
||||
for field, old_type in old_fields.items():
|
||||
yield from _field_changes(model, field, old_type, new_schema[model])
|
||||
for field, old_type in old_schema[model].items():
|
||||
if not _is_relation(old_type, old_schema):
|
||||
yield from _field_changes(model, field, old_type, new_schema[model])
|
||||
|
||||
|
||||
def check_breaking_changes(old_schema: Schema, new_schema: Schema) -> tuple[str, ...]:
|
||||
def changes() -> Iterator[str]:
|
||||
for model, fields in old_schema.items():
|
||||
yield from _model_changes(model, fields, new_schema)
|
||||
for model in old_schema:
|
||||
yield from _model_changes(model, old_schema, new_schema)
|
||||
|
||||
return tuple(changes())
|
||||
|
||||
|
|
|
|||
37
tests/integration/management/test_invitation_master_key.py
Normal file
37
tests/integration/management/test_invitation_master_key.py
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
import uuid
|
||||
from pathlib import Path
|
||||
from typing import Final
|
||||
|
||||
from integration._support.client import Gateway, object_value
|
||||
from integration._support.database import scratch_database
|
||||
from integration._support.process import owned_proxy
|
||||
|
||||
MASTER_KEY_USER: Final = "default_user_id"
|
||||
|
||||
|
||||
def test_master_key_invites_on_a_proxy_where_nobody_signed_in(gateway: Gateway, tmp_path: Path) -> None:
|
||||
with (
|
||||
scratch_database() as database_url,
|
||||
owned_proxy(
|
||||
gateway, tmp_path, {"DATABASE_URL": database_url}, remove_environment=("DATABASE_URL_READ_REPLICA",)
|
||||
) as proxy,
|
||||
proxy.scenario() as scenario,
|
||||
):
|
||||
invitee: Final = scenario.user(user_role="proxy_admin")
|
||||
created: Final = proxy.request("POST", "/invitation/new", {"user_id": invitee})
|
||||
assert created.status_code == 200, created.text
|
||||
invitation: Final = object_value(created.json())
|
||||
assert (invitation["user_id"], invitation["created_by"], invitation["updated_by"]) == (
|
||||
invitee,
|
||||
MASTER_KEY_USER,
|
||||
MASTER_KEY_USER,
|
||||
)
|
||||
accepted: Final = proxy.request(
|
||||
"POST", "/invitation/update", {"invitation_id": invitation["id"], "is_accepted": True}
|
||||
)
|
||||
assert accepted.status_code == 200, accepted.text
|
||||
assert object_value(accepted.json())["is_accepted"] is True
|
||||
assert proxy.rows('SELECT user_id FROM "LiteLLM_UserTable" WHERE user_id = %s', (MASTER_KEY_USER,)) == []
|
||||
missing: Final = proxy.request("POST", "/invitation/new", {"user_id": f"integration-{uuid.uuid4().hex}"})
|
||||
assert missing.status_code == 400, missing.text
|
||||
assert "User id does not exist in 'LiteLLM_UserTable'" in missing.text
|
||||
|
|
@ -40,3 +40,23 @@ def test_schema_compatibility_rejects_removed_models_fields_and_narrowed_types(
|
|||
schema: str, expected: tuple[str, ...]
|
||||
) -> None:
|
||||
assert check_breaking_changes(parse_model_fields(ORIGINAL), parse_model_fields(schema)) == expected
|
||||
|
||||
|
||||
RELATED: Final = (
|
||||
"model Owner {\n id String @id\n items Item[] @relation(\"Owned\")\n created Item[] @relation(\"Created\")\n}\n"
|
||||
"model Item {\n id String @id\n owner_id String?\n created_by String\n"
|
||||
' owner Owner? @relation("Owned", fields: [owner_id], references: [id])\n'
|
||||
' creator Owner @relation("Created", fields: [created_by], references: [id])\n}\n'
|
||||
)
|
||||
|
||||
|
||||
def test_schema_compatibility_allows_dropping_relations_but_not_their_columns() -> None:
|
||||
relations_dropped: Final = (
|
||||
"model Owner {\n id String @id\n}\nmodel Item {\n id String @id\n owner_id String?\n created_by String\n}\n"
|
||||
)
|
||||
columns_dropped: Final = "model Owner {\n id String @id\n}\nmodel Item {\n id String @id\n}\n"
|
||||
assert check_breaking_changes(parse_model_fields(RELATED), parse_model_fields(relations_dropped)) == ()
|
||||
assert check_breaking_changes(parse_model_fields(RELATED), parse_model_fields(columns_dropped)) == (
|
||||
"Breaking: Field Item.owner_id was removed",
|
||||
"Breaking: Field Item.created_by was removed",
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue