Coder Social home page Coder Social logo

accesshandler's Introduction

accesshandler

Resource access limit handler

What does it do?

The Access Handler is a web service that stores the Rules user creates. The Rules contains:

  • pattern: It can be either an exact explicit url or a regex
  • limit: Shows how many times an specific IP can request for an url matched the pattern

Sample rule be like:

{
    "limit":"100/sec",
    "isExactUrl":false,
    "pattern":"/foo/.*",
    "id":1
}

Also this web service recieves logs by the format of:

{
  "url": "example.com/foo",
  "IP": "1.1.1.1"
}

showing the which IP requested for url.

The goal is to check if an specific IP requested more than limit time interval for specific url matched the patterns and inform by 429 Too Many Requests HTTP status.

In action

Let's test how it works step by step:

NOTE: the $URL is url of web service.

  1. Create a rule by CREATE rule API and set the limit to a value like 2/min, like:
curl -X CREATE -H "Content-Type: application/json" --data '{"pattern": "example.com/foo/.*", "limit": "2/min"}' -i -- "$URL/apiv1/rules"
  1. Try to post log by POST log API for more than 2 times in less 1 minute. The web service must respond you with 200 OK status for 2 first times, and 429 Too Many Requests for more than 2 times of request. Your request should be like below:
curl -X POST -H "Content-Type: application/json" --data '{"url": "example.com/foo/bar", "IP": "1.1.1.1"}' -i -- "$URL/apiv1/logs?"

What we did was creating a rule that shows an specific IP: 1.1.1.1 can view the url: example.com/foo/bar at most for 2 times per minute because this url matched with created rule pattern: /foo/.*.

Installing dependencies

sudo apt-get install libass-dev libpq-dev postgresql \
    build-essential redis-server redis-tools

Installing project by pip

NOTE: Highly recommended to use virtual environment. There are some pip packages for this purpose. But I offer you using virtualenvwrapper package.

You can install by 'pip install' and use https by the following way:

pip install git+https://github.com/shayan-7/accesshandler.git

Or you can use SSH:

pip install [email protected]:shayan-7/accesshandler.git

Installing project (edit mode)

So, your changes will affect instantly on the installed version

accesshandler

cd /path/to/workspace
git clone [email protected]:shayan-7/accesshandler.git
cd accesshandler
pip install -e .

Setup database

Configuration

Accesshandler is zero configuration application and there is no extra configuration file needed, but if you want to have your own configuration file, you can make a accesshandler.yml in the following path: ~/.config/accesshandler.yml such as following format:

db:
  url: postgresql://postgres:postgres@localhost/accesshandler_dev
  test_url: postgresql://postgres:postgres@localhost/accesshandler_test
  administrative_url: postgresql://postgres:postgres@localhost/postgres

Remove old abd create a new database TAKE CARE ABOUT USING THAT

accesshandler db create --drop --mockup

Drop old database: TAKE CARE ABOUT USING THAT

accesshandler [-c path/to/config.yml] db drop

Create database

accesshandler [-c path/to/config.yml] db create

Or, you can add --drop to drop the previously created database: TAKE CARE ABOUT USING THAT

accesshandler [-c path/to/config.yml] db create --drop

Create schema

accesshandler [-c path/to/config.yml] db schema

Running tests

To check all tests passing and 100% coverage run the following command:

pip install -r requirements-ci.txt
pytest --cov=accesshandler

Then all the tested APIs generated in <path/to/accesshandler>/data/markdown by Markdown format.

Serving

  • Gunicorn
$ ./gunicorn

APIs list

Check out API.md for list of APIs and sample responses.

accesshandler's People

Contributors

shayaniox avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.