langgenius / dify

Dify is an open-source LLM app development platform. Dify's intuitive interface combines AI workflow, RAG pipeline, agent capabilities, model management, observability features and more, letting you quickly go from prototype to production.
https://dify.ai
Other
53.24k stars 7.77k forks source link

LookupError: <ContextVar name='tenant_id' at 0x15b781080> when login via ADMIN_API_KEY #11065

Closed lotabout closed 2 days ago

lotabout commented 4 days ago

Self Checks

Dify version

0.10.2

Cloud or Self Hosted

Self Hosted (Source)

Steps to reproduce

  1. Setting the following environment:

    ADMIN_API_KEY_ENABLE=true
    ADMIN_API_KEY=<some key>
  2. create an app in web pages

  3. send the following request:

curl 'http://<host>:<port>/console/api/apps/c6ed881f-b20d-4148-b3c3-893329af6021/workflows/draft' \
 -H 'Authorization: Bearer <ADMIN_API_KEY>' \
 -H 'X-WORKSPACE-ID: <WORKSPACE_ID_OF_APP>'

and the error is reproduced.

✔️ Expected Behavior

The request returns normally just like the user's JWT tokens.

❌ Actual Behavior

No response

dosubot[bot] commented 4 days ago

@lotabout Hello there! I'm here to assist you with any bugs, questions, or contributions. Let's work together to resolve your issue.

The LookupError for the tenant_id ContextVar occurs when there is an attempt to access its value before it has been set in the current context. In the case of logging in via ADMIN_API_KEY, it seems that the tenant_id is not being set properly before it is accessed.

To resolve this issue, ensure that the tenant_id is correctly set in the context when handling requests authenticated with the ADMIN_API_KEY. This typically involves setting the tenant_id after successfully validating the ADMIN_API_KEY and retrieving the corresponding tenant information using the X-WORKSPACE-ID header. Make sure that the logic for setting the tenant_id is consistent with how it is set when using user JWT tokens, as the expected behavior is for both authentication methods to handle context variables similarly [1].

To continue talking to Dosu, mention @dosu.