repotrial / nedrexapi

Code relating to the NeDRex API.
GNU General Public License v3.0
0 stars 1 forks source link

Implement rate limiting on routes #9

Open james-skelton opened 2 years ago

james-skelton commented 2 years ago

SlowAPI is probably the solution (rate limiting for FastAPI), but need to check whether there are any considerations we need to have (e.g., behind an Nginx or Apache reverse proxy).

james-skelton commented 2 years ago

I tested out using an Nginx proxy to a FastAPI server using SlowAPI. With the default settings, the IP address passed on to the FastAPI server is 127.0.0.1. Consequently, under default settings, the rate limiting applies collectively to all users (undesirable behaviour).

I'm figuring out how to modify this for an Nginx server, but we'll need to figure out how to address this for Apache2.

james-skelton commented 2 years ago

An alternative to having the rate limiting in the app is to set it in the Nginx config -- this is more of a "blunt instrument" than we could perhaps achieve with SlowAPI.

EDIT: Adding the following code to the server block in Nginx seemed to make the original IP address available to SlowAPI via Nginx:

proxy_set_header    X-Real-IP        $remote_addr;
proxy_set_header    X-Forwarded-For  $proxy_add_x_forwarded_for;
james-skelton commented 2 years ago

Next question is to figure out the rate limits to set. At first, I think a blanket 10 requests/second is sufficient, but we may wish to have a different rate for analysis tasks further down the line. These limits should be set in the configuration file.

EDIT:

Tested out how SlowAPI limits work with one another.

james-skelton commented 2 years ago

To-do:

james-skelton commented 2 years ago

Added first pass at limiting -- this can be switched on in the config file using the api.rate_limiting_enabled variable (boolean). The rate limit can be set using the api.rate_limit variable (string). This string should be a value compatible with slowapi (e.g., "5/minute").

At present, there is no implementation for rate limiting specific routes.