Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -407,8 +407,14 @@ jobs:
pip install weaviate_client-*.whl
- run: rm -r weaviate
- name: start weaviate
env:
# ci/docker-compose-rbac.yml passes it to Weaviate as LICENSE_KEY.
WEAVIATE_LICENSE_KEY: ${{ secrets.WEAVIATE_LICENSE_KEY }}
run: /bin/bash ci/start_weaviate.sh ${{ matrix.version }}
- name: Run integration tests without auth secrets (auth tests ran in previous job)
env:
# The DB user expiration tests skip where it is empty, as on forks.
WEAVIATE_LICENSE_KEY: ${{ secrets.WEAVIATE_LICENSE_KEY }}
run: pytest -v -n auto --dist loadgroup integration

build-and-publish:
Expand Down
1 change: 1 addition & 0 deletions ci/docker-compose-rbac.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ services:
RAFT_BOOTSTRAP_EXPECT: "1"
AUTHORIZATION_ENABLE_RBAC: "true"
AUTHENTICATION_DB_USERS_ENABLED: "true"
LICENSE_KEY: ${WEAVIATE_LICENSE_KEY:-}
AUTHENTICATION_OIDC_ENABLED: 'true'
AUTHENTICATION_OIDC_CLIENT_ID: 'Peuc12y02UA0eAED1dqSjE5HtGUrpBsx'
AUTHENTICATION_OIDC_ISSUER: 'https://auth.weaviate.cloud/Peuc12y02UA0eAED1dqSjE5HtGUrpBsx'
Expand Down
152 changes: 152 additions & 0 deletions integration/test_users.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import os
from datetime import datetime, timedelta, timezone
from typing import Callable, Union

import pytest
from _pytest.fixtures import SubRequest

import weaviate
from integration.conftest import ClientFactory, _sanitize_collection_name
from weaviate.auth import Auth
from weaviate.rbac.models import Role, RoleBase, UserTypes
from weaviate.users.sync import _UsersDB

RBAC_PORTS = (8092, 50063)
RBAC_AUTH_CREDS = Auth.api_key("admin-key")
Expand Down Expand Up @@ -265,3 +270,150 @@ def test_list_all_include_last_used_time(
assert target.last_used_time is not None
finally:
client.users.db.delete(user_id=user_id)


def _skip_unless_user_expiration(
client: Union[weaviate.WeaviateClient, weaviate.WeaviateAsyncClient],
) -> None:
if client._connection._weaviate_version.is_lower_than(1, 41, 0):
pytest.skip("This test requires Weaviate 1.41.0 or higher")
if not os.environ.get("WEAVIATE_LICENSE_KEY"):
pytest.skip("This test requires WEAVIATE_LICENSE_KEY, the server's license key")


def _in_days(days: int) -> datetime:
# _in_days drops the microseconds the server does not keep, and uses a non-UTC
# zone so a round trip shows that the client sends the offset.
return (datetime.now(timezone(timedelta(hours=2))) + timedelta(days=days)).replace(
microsecond=0
)


def test_user_expiration(client_factory: ClientFactory, request: SubRequest) -> None:
with client_factory(ports=RBAC_PORTS, auth_credentials=RBAC_AUTH_CREDS) as client:
_skip_unless_user_expiration(client)
# admin-user is a static user from the server's env config, whose key never expires.
assert client.users.get_my_user().expires_at is None

user_id = _unique_user_id(request.node.name)
in_one_day = _in_days(1)
api_key = client.users.db.create(user_id=user_id, expires_at=in_one_day)
try:
user = client.users.db.get(user_id=user_id)
assert user is not None
assert user.expires_at == in_one_day
listed = next(u for u in client.users.db.list_all() if u.user_id == user_id)
assert listed.expires_at == in_one_day
with weaviate.connect_to_local(
port=RBAC_PORTS[0], grpc_port=RBAC_PORTS[1], auth_credentials=Auth.api_key(api_key)
) as client2:
assert client2.users.get_my_user().expires_at == in_one_day

in_two_days = _in_days(2)
client.users.db.update_expiration(user_id=user_id, expires_at=in_two_days)
user = client.users.db.get(user_id=user_id)
assert user is not None
assert user.expires_at == in_two_days

client.users.db.update_expiration(user_id=user_id, expires_at=None)
user = client.users.db.get(user_id=user_id)
assert user is not None
assert user.expires_at is None
listed = next(u for u in client.users.db.list_all() if u.user_id == user_id)
assert listed.expires_at is None
finally:
client.users.db.delete(user_id=user_id)


@pytest.mark.asyncio
async def test_user_expiration_async(request: SubRequest) -> None:
async with weaviate.use_async_with_local(
port=RBAC_PORTS[0], grpc_port=RBAC_PORTS[1], auth_credentials=RBAC_AUTH_CREDS
) as client:
_skip_unless_user_expiration(client)

user_id = _unique_user_id(request.node.name)
in_one_day = _in_days(1)
await client.users.db.create(user_id=user_id, expires_at=in_one_day)
try:
user = await client.users.db.get(user_id=user_id)
assert user is not None
assert user.expires_at == in_one_day

in_two_days = _in_days(2)
await client.users.db.update_expiration(user_id=user_id, expires_at=in_two_days)
user = await client.users.db.get(user_id=user_id)
assert user is not None
assert user.expires_at == in_two_days

await client.users.db.update_expiration(user_id=user_id, expires_at=None)
user = await client.users.db.get(user_id=user_id)
assert user is not None
assert user.expires_at is None
finally:
await client.users.db.delete(user_id=user_id)


def test_update_expiration_on_never_expiring_user(
client_factory: ClientFactory, request: SubRequest
) -> None:
with client_factory(ports=RBAC_PORTS, auth_credentials=RBAC_AUTH_CREDS) as client:
_skip_unless_user_expiration(client)

user_id = _unique_user_id(request.node.name)
api_key = client.users.db.create(user_id=user_id)
try:
user = client.users.db.get(user_id=user_id)
assert user is not None
assert user.expires_at is None
with weaviate.connect_to_local(
port=RBAC_PORTS[0], grpc_port=RBAC_PORTS[1], auth_credentials=Auth.api_key(api_key)
) as client2:
assert client2.users.get_my_user().expires_at is None

in_one_day = _in_days(1)
client.users.db.update_expiration(user_id=user_id, expires_at=in_one_day)
user = client.users.db.get(user_id=user_id)
assert user is not None
assert user.expires_at == in_one_day
with weaviate.connect_to_local(
port=RBAC_PORTS[0], grpc_port=RBAC_PORTS[1], auth_credentials=Auth.api_key(api_key)
) as client2:
assert client2.users.get_my_user().expires_at == in_one_day

new_api_key = client.users.db.rotate_key(user_id=user_id)
with weaviate.connect_to_local(
port=RBAC_PORTS[0],
grpc_port=RBAC_PORTS[1],
auth_credentials=Auth.api_key(new_api_key),
) as client2:
assert client2.users.get_my_user().expires_at == in_one_day
finally:
client.users.db.delete(user_id=user_id)


def test_update_expiration_of_missing_user(client_factory: ClientFactory) -> None:
with client_factory(ports=RBAC_PORTS, auth_credentials=RBAC_AUTH_CREDS) as client:
_skip_unless_user_expiration(client)

with pytest.raises(weaviate.exceptions.UnexpectedStatusCodeError):
client.users.db.update_expiration(user_id="I-do-not-exist", expires_at=_in_days(1))


@pytest.mark.parametrize(
"call",
[
lambda db: db.create(user_id="expiry-unsupported", expires_at=_in_days(1)),
lambda db: db.update_expiration(user_id="expiry-unsupported", expires_at=_in_days(1)),
],
ids=["create", "update_expiration"],
)
def test_user_expiration_unsupported_version(
client_factory: ClientFactory, call: Callable[[_UsersDB], object]
) -> None:
with client_factory(ports=RBAC_PORTS, auth_credentials=RBAC_AUTH_CREDS) as client:
if not client._connection._weaviate_version.is_lower_than(1, 41, 0):
pytest.skip("This test requires a Weaviate version below 1.41.0")

with pytest.raises(weaviate.exceptions.WeaviateUnsupportedFeatureError):
call(client.users.db)
1 change: 1 addition & 0 deletions weaviate/rbac/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ class WeaviateDBUserRoleNames(TypedDict):
createdAt: NotRequired[str]
lastUsedAt: NotRequired[str]
apiKeyFirstLetters: NotRequired[str]
expiresAt: NotRequired[Optional[str]]


class _Action:
Expand Down
4 changes: 3 additions & 1 deletion weaviate/users/async_.pyi
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import datetime
from typing import Dict, List, Literal, Optional, Union, overload

from typing_extensions import deprecated
Expand Down Expand Up @@ -52,7 +53,8 @@ class _UsersDBAsync(_UsersDBExecutor[ConnectionAsync]):
) -> Union[Dict[str, Role], Dict[str, RoleBase]]: ...
async def assign_roles(self, *, user_id: str, role_names: Union[str, List[str]]) -> None: ...
async def revoke_roles(self, *, user_id: str, role_names: Union[str, List[str]]) -> None: ...
async def create(self, *, user_id: str) -> str: ...
async def create(self, *, user_id: str, expires_at: Optional[datetime] = None) -> str: ...
async def update_expiration(self, *, user_id: str, expires_at: Optional[datetime]) -> None: ...
async def delete(self, *, user_id: str) -> bool: ...
async def rotate_key(self, *, user_id: str) -> str: ...
async def activate(self, *, user_id: str) -> bool: ...
Expand Down
Loading
Loading