Skip to content

Commit

Permalink
Merge pull request #5 from AikidoSec/AIK-3154
Browse files Browse the repository at this point in the history
AIK-3154
  • Loading branch information
willem-delbare authored Jul 16, 2024
2 parents 24e2df9 + bd1d08b commit cbc4843
Show file tree
Hide file tree
Showing 24 changed files with 155 additions and 483 deletions.
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.PHONY: build
build:
poetry build

.PHONY: clean
clean:
poetry env remove python
6 changes: 6 additions & 0 deletions aikido_firewall/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Import sources
import aikido_firewall.sources.django
import aikido_firewall.sources.flask

# Import middleware
import aikido_firewall.middleware.django
Empty file.
14 changes: 14 additions & 0 deletions aikido_firewall/middleware/django.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import logging
class AikidoMiddleware:
def __init__(self, get_response):
self.get_response = get_response

def __call__(self, request, *args, **kwargs):
logging.critical("[AIK] Aikido middleware : call")
return self.get_response(request)

def process_exception(self, request, exception):
logging.critical("[AIK] Aikido middleware : exception")

def process_request(self, request):
logging.critical("[AIK] Aikido middleware : request")
Empty file.
16 changes: 16 additions & 0 deletions aikido_firewall/sources/django.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import importhook
import copy
from importlib.metadata import version

AIKIDO_MIDDLEWARE_ADDR = "aikido_firewall.middleware.django.AikidoMiddleware"

# Hook 'n wrap on `django.conf`
# Our goal here is to wrap the settings object and add our middleware into the list
@importhook.on_import('django.conf')
def on_django_import(django):
modified_django = importhook.copy_module(django)
new_middleware_array = [AIKIDO_MIDDLEWARE_ADDR] + django.settings.MIDDLEWARE

setattr(modified_django.settings, "MIDDLEWARE", new_middleware_array)
print("[AIK] Modified Django")
return modified_django
31 changes: 31 additions & 0 deletions aikido_firewall/sources/flask.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import importhook
import copy
from importlib.metadata import version
import logging

class AikidoMiddleware(object):
def __init__(self, app):
self.app = app

def __call__(self, environ, start_response):
logging.critical("[AIK] Aikido middleware is working")
response = self.app(environ, start_response)
return response


# Hook 'n wrap on `flask.app`
# Our goal is to wrap the __init__ function of the "Flask" class, so we can insert our middleware
@importhook.on_import('flask.app')
def on_flask_import(flask):
modified_flask = importhook.copy_module(flask)

prev_flask_init = copy.deepcopy(flask.Flask.__init__)
def aikido_flask_init(_self, *args, **kwargs):
prev_flask_init(_self, *args, **kwargs)
print("[AIK] Flask version : ", version("flask"))
_self.wsgi_app = AikidoMiddleware(_self.wsgi_app)
print(_self)

setattr(modified_flask.Flask, "__init__", aikido_flask_init)
print("[AIK] Modified flask")
return modified_flask
10 changes: 10 additions & 0 deletions docs/contributing/python.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Python configuration

To install and keep track of packages we use "pipenv", so installing packages goes as follows :
```bash
pipenv shell
```
And from now on you can install all packages with :
```
pipenv install <your_package_name>
```
17 changes: 17 additions & 0 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 14 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
[tool.poetry]
name = "aikido_firewall"
version = "0.1.0"
description = "Aikido RASP for Python"
authors = ["Aikido"]
readme = "README.md"

[tool.poetry.dependencies]
python = "^3.12"
importhook = "^1.0.9"


[build-system]
requires = ['setuptools>=42']
build-backend = 'setuptools.build_meta'
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
23 changes: 12 additions & 11 deletions sample-apps/django-mysql/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
# Use an official Python runtime as a parent image
FROM python:3

ENV VIRTUAL_ENV=/opt/venv
RUN python3 -m venv $VIRTUAL_ENV
ENV PATH="$VIRTUAL_ENV/bin:$PATH"

# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
#Copy code base
COPY ./ /tmp

# Set the working directory
WORKDIR /app

# Install dependencies
COPY Pipfile ./
RUN pip install --no-cache-dir pipenv && pipenv install
RUN mv /tmp/sample-apps/django-mysql/requirements.txt ./
RUN pip install -r requirements.txt

# Build and install aikido_firewall from source
WORKDIR /tmp
RUN pip install poetry
RUN make build
RUN pip install ./dist/aikido_firewall-0.1.0.tar.gz
RUN pip list

# Copy the project code into the container
COPY . /app/
WORKDIR /app
15 changes: 0 additions & 15 deletions sample-apps/django-mysql/Pipfile

This file was deleted.

168 changes: 0 additions & 168 deletions sample-apps/django-mysql/Pipfile.lock

This file was deleted.

4 changes: 2 additions & 2 deletions sample-apps/django-mysql/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ services:

backend:
build:
context: .
dockerfile: Dockerfile
context: ./../../
dockerfile: ./sample-apps/django-mysql/Dockerfile
container_name: project_name_backend
command: sh -c "python3 manage.py migrate --noinput && python manage.py runserver 0.0.0.0:8000"
restart: always
Expand Down
1 change: 1 addition & 0 deletions sample-apps/django-mysql/manage.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import aikido_firewall # Aikido module
import os
import sys

Expand Down
4 changes: 4 additions & 0 deletions sample-apps/django-mysql/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
django
pymysql
python-decouple
cryptography
Loading

0 comments on commit cbc4843

Please sign in to comment.