Skip to content

Commit 5b2b5ff

Browse files
authored
Set Authenticated User and Role Fields from Client (#490)
Addresses #489
1 parent cf48898 commit 5b2b5ff

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

dbos/_client.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import asyncio
2+
import json
23
import time
34
import uuid
45
from typing import (
@@ -63,6 +64,8 @@ class EnqueueOptions(_EnqueueOptionsRequired, total=False):
6364
priority: int
6465
max_recovery_attempts: int
6566
queue_partition_key: str
67+
authenticated_user: str
68+
authenticated_roles: list[str]
6669

6770

6871
def validate_enqueue_options(options: EnqueueOptions) -> None:
@@ -189,6 +192,13 @@ def _enqueue(self, options: EnqueueOptions, *args: Any, **kwargs: Any) -> str:
189192
"queue_partition_key": options.get("queue_partition_key"),
190193
}
191194

195+
authenticated_user = options.get("authenticated_user")
196+
authenticated_roles = (
197+
json.dumps(options.get("authenticated_roles"))
198+
if options.get("authenticated_roles")
199+
else None
200+
)
201+
192202
inputs: WorkflowInputs = {
193203
"args": args,
194204
"kwargs": kwargs,
@@ -202,9 +212,9 @@ def _enqueue(self, options: EnqueueOptions, *args: Any, **kwargs: Any) -> str:
202212
"queue_name": queue_name,
203213
"app_version": enqueue_options_internal["app_version"],
204214
"config_name": None,
205-
"authenticated_user": None,
215+
"authenticated_user": authenticated_user,
206216
"assumed_role": None,
207-
"authenticated_roles": None,
217+
"authenticated_roles": authenticated_roles,
208218
"output": None,
209219
"error": None,
210220
"created_at": None,

tests/test_client.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,3 +593,35 @@ def test_enqueue_with_priority(dbos: DBOS, client: DBOSClient) -> None:
593593
def test_client_bad_url() -> None:
594594
with pytest.raises(DBAPIError) as exc_info:
595595
DBOSClient("postgresql://postgres:fakepassword@localhost:5433/fake_database")
596+
597+
598+
def test_client_auth(dbos: DBOS, client: DBOSClient) -> None:
599+
run_client_collateral()
600+
601+
johnDoe: Person = {"first": "John", "last": "Doe", "age": 30}
602+
wfid = str(uuid.uuid4())
603+
604+
user = "testuser"
605+
roles = ["role1", "role2"]
606+
607+
options: EnqueueOptions = {
608+
"queue_name": "test_queue",
609+
"workflow_name": "enqueue_test",
610+
"workflow_id": wfid,
611+
"authenticated_user": user,
612+
"authenticated_roles": roles,
613+
}
614+
615+
handle: WorkflowHandle[str] = client.enqueue(options, 42, "test", johnDoe)
616+
result = handle.get_result()
617+
assert result == '42-test-{"first": "John", "last": "Doe", "age": 30}'
618+
619+
list_results = client.list_workflows()
620+
assert len(list_results) == 1
621+
assert list_results[0].workflow_id == wfid
622+
assert list_results[0].status == "SUCCESS"
623+
assert list_results[0].output == result
624+
assert list_results[0].input is not None
625+
assert list_results[0].authenticated_user == user
626+
assert list_results[0].authenticated_roles == roles
627+
assert list_results[0].assumed_role is None

0 commit comments

Comments
 (0)