Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
5 tasks done
lotabout opened this issue Nov 25, 2024 · 1 comment · Fixed by #11066
Closed
5 tasks done
Labels
🐞 bug Something isn't working

Comments

@lotabout
Copy link
Contributor

Self Checks

  • This is only for bug report, if you would like to ask a question, please head to Discussions.
  • I have searched for existing issues search for existing issues, including closed ones.
  • I confirm that I am using English to submit this report (我已阅读并同意 Language Policy).
  • [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:)
  • Please do not modify this template :) and fill in all the required fields.

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>
  1. create an app in web pages
  2. 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

Copy link

dosubot bot commented Nov 25, 2024

@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.

@dosubot dosubot bot added the 🐞 bug Something isn't working label Nov 25, 2024
lotabout added a commit to lotabout/dify that referenced this issue Nov 25, 2024
`load_user_from_request` is not called when logging in via ADMIN_KEY, which causes `contexts.tenant_id` to remain unset.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐞 bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant