litellm icon indicating copy to clipboard operation
litellm copied to clipboard

[Feature]: Authentication error for master key should show IP to allow banning via log

Open thiswillbeyourgithub opened this issue 1 year ago • 1 comments

The Feature

As reported in #4880, using the wrong master key in a curl request does not raise an authentication error but a "No connected db" error. I understand the rationale with that but I think it would be good if this exception could show the IP address that tried to connect in the error message.

Currently, here's the log I have when attempting to do a chat completion with the wrong key for example:

DATE HOSTNAME anid: 09:16:52 - LiteLLM Proxy:ERROR: user_api_key_auth.py:1087 - litellm.proxy.proxy_server.user_api_key_auth(): Exception occured - No connected db.
DATE HOSTNAME anid: Traceback (most recent call last):
DATE HOSTNAME anid:   File "/usr/local/lib/python3.11/site-packages/litellm/proxy/auth/user_api_key_auth.py", line 547, in user_api_key_auth
DATE HOSTNAME anid:     raise Exception("No connected db.")
DATE HOSTNAME anid: Exception: No connected db.

Motivation, pitch

Hence, the offending IP would appear in the log, enabling things like fail2ban for banning IPs

The way fail2ban works is by applying specific regex on the logs, this way by setting my docker containers to log to journald I'm used to using fail2ban for my containers. It also allows me to get notifications that someone tried to log etc.

I'm sure there are other software than fail2ban that could make use of this.

Twitter / LinkedIn details

No response

thiswillbeyourgithub avatar Aug 15 '24 09:08 thiswillbeyourgithub

Makes sense. happy to enable this.

Would you be open to contributing a tutorial on using fail2ban for banning IPs w/ litellm proxy?

I've seen a few users have similar issues, and this would be very useful! @thiswillbeyourgithub

Here's the right place to add it - https://github.com/BerriAI/litellm/tree/main/docs/my-website/docs/tutorials

krrishdholakia avatar Aug 17 '24 00:08 krrishdholakia

Done

krrishdholakia avatar Aug 19 '24 18:08 krrishdholakia