Setup and Installation of Zen Firewall for Python

This guide will walk you through installing and setting up Zen Firewall by Aikido for your application. Follow the steps below to protect your application.

We have first class support for multiple frameworks and database drivers, for the full list check our README on GitHub.

1

Install Zen Firewall by Aikido

Zen Firewall for Python comes as a PIP package that needs to be installed together with your code so your application can be protected.

pip install aikido_zen

Choose your framework or runtime and follow the specific instructions.

Django

In manage.py add the following snippet to your application

import os
import sys
import aikido_zen

if __name__ == "__main__":
    aikido_zen.protect()
    # ...

For more advanced options check out our Github docs.

2

Create an app in the dashboard and generate a token

  1. Create your Aikido account if you haven't done so already

  2. Go to the Zen section in Aikido.

  3. Click on Add app.

  4. Choose a name for your app and click Generate token.

  5. Copy the generated token

Zen app setup form: enter app name, repository, and environment to enable security reporting.

3

Start Zen Firewall in dry / detection-only mode

  1. Set the token as an environment variable: AIKIDO_TOKEN=YOUR_SECRET_TOKEN

  2. Start your app in dry mode AIKIDO_BLOCK=false to ensure it works as expected without blocking any requests. We advise to run Aikido Zen in staging for two weeks to avoid any false positives.

You can use AIKIDO_DEBUG=true to enable debug mode for more detailed information about what the agent is doing. For more information about your environment variables: Configuration via environment variables

4

Test your app

Browse to your application and perform a couple of actions or open a couple of pages. Zen will automatically discover the routes in your application.

You can verify a working agent by looking at the following pages of your Zen application:

  • Events: Should show an "Application started" event.

  • Routes: After some time your application routes will start showing here with the method, route and requests.

  • Instances: Should show the number of active instances for your application where Zen is installed.

5

Enable Rate limiting and User blocking

Enable additional features like Rate limiting and User blocking from within your code by using the relevant framework snippets below.

Django

In settings.py add the following snippet to your application to enable request blocking

MIDDLEWARE = [
    # Authorization middleware here (Make sure aikido middleware runs after this)
    "aikido_zen.middleware.AikidoDjangoMiddleware",
    # ...
]

To more easily identify your users in the Zen UI, use the following helper function within your authentication middleware after you've authenticated your user.

from aikido_zen import set_user

# Set a user (presumably in middleware) :
set_user({"id": "123", "name": "John Doe"})
6

Setup rate limiting in the dashboard

After you've added the Aikido middleware, you can test it out by logging in to your Aikido account and navigating to the Zen dashboard.

Agent start event logged with info severity and timestamp shown.

To protect a route from brute force attacks, set up rate limiting in the Aikido Dashboard:

  1. Click on the created app.

  2. Go to the Routes tab.

  3. Find the route you would like to limit and click Setup rate limiting.

  4. Follow the instructions to configure the rate limit (e.g., 5 requests per minute).

API route management interface showing authentication routes with protection and rate limiting options.
Set rate limiting for POST /auth/login to 5 requests per minute.

Verify Rate Limiting

Start your app and try to access the route you've rate limited 5 times within a minute. After the fifth attempt, you should receive a rate limit error:

You are rate limited by Aikido firewall. (Your IP: 1.2.3.4)
7

Success & Next Steps

Congrats you've successfully installed Aikido Zen. If you encountered any problems, have concerns or have feature requests, don't hesitate to reach out to support.

You can now go and explore the many features that Zen provides:

Additional information:

Last updated

Was this helpful?