Skip to content

Aik 3156 #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# EditorConfig helps developers define and maintain consistent coding styles between different editors and IDEs
# For more information about the EditorConfig project, please visit http://editorconfig.org

# Top-most EditorConfig file
root = true

# Ensure there is a final newline at the end of the file
[*]
insert_final_newline = true
38 changes: 38 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Linting
on: [pull_request]
jobs:
lint:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.x'

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install poetry
poetry install

- name: Run Pylint
run: |
poetry run pylint --fail-under=9 --rcfile=.pylintrc aikido_firewall/
pylint_exit_code=$?
if [ $pylint_exit_code -ne 0 ]; then
echo "Pylint check failed. Please fix the issues."
exit 1
fi

- name: Run Black Check
run: |
poetry run black --check --diff aikido_firewall/
black_exit_code=$?
if [ $black_exit_code -ne 0 ]; then
echo "Black check failed. Please run 'black .' to format the code."
exit 1
fi
7 changes: 7 additions & 0 deletions .pylintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[MASTER]
# Disable the unused-argument warning
disable = unused-argument

[MESSAGES CONTROL]
# Disable the no-member warning [Currently enabled]
#disable = no-member
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,9 @@ build:

.PHONY: clean
clean:
poetry env remove python
poetry env remove python

.PHONY: lint
lint:
poetry run black aikido_firewall/
poetry run pylint aikido_firewall/
6 changes: 5 additions & 1 deletion aikido_firewall/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
"""
Aggregates from the different modules
"""

# Import sources
import aikido_firewall.sources.django
import aikido_firewall.sources.flask

# Import middleware
import aikido_firewall.middleware.django
import aikido_firewall.middleware.django
19 changes: 18 additions & 1 deletion aikido_firewall/middleware/django.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
"""
Django WSGI Aikido Middleware
uses headers, body, etc. as sources
"""

import logging


class AikidoMiddleware:
"""
Same as docstring above
"""

def __init__(self, get_response):
self.get_response = get_response

Expand All @@ -8,7 +19,13 @@ def __call__(self, request, *args, **kwargs):
return self.get_response(request)

def process_exception(self, request, exception):
"""
Do something when an exception occurs whilst django is processing a request
"""
logging.critical("[AIK] Aikido middleware : exception")

def process_request(self, request):
"""
executed during the request phase of the Django request-response cycle.
"""
logging.critical("[AIK] Aikido middleware : request")
19 changes: 13 additions & 6 deletions aikido_firewall/sources/django.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
"""
Django source module, intercepts django import and adds Aikido middleware
"""

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')

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

# pylint: disable=no-member
setattr(modified_django.settings, "MIDDLEWARE", new_middleware_array)
print("[AIK] Modified Django")
return modified_django
return modified_django
28 changes: 21 additions & 7 deletions aikido_firewall/sources/flask.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import importhook
"""
Flask source module, intercepts flask import and adds Aikido middleware
"""

import copy
from importlib.metadata import version
import logging
import importhook


class AikidoMiddleware: # pylint: disable=too-few-public-methods
"""
Aikido WSGI Middleware | uses headers, body, etc. as sources
"""

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

Expand All @@ -13,19 +22,24 @@ def __call__(self, 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')
@importhook.on_import("flask.app")
def on_flask_import(flask):
"""
Hook 'n wrap on `flask.app`
Our goal is to wrap the __init__ function of the "Flask" class, so we can insert our middleware
Returns : Modified flask.app object
"""
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)


# pylint: disable=no-member
setattr(modified_flask.Flask, "__init__", aikido_flask_init)
print("[AIK] Modified flask")
return modified_flask
return modified_flask
29 changes: 23 additions & 6 deletions docs/contributing/python.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
# Python configuration
# Poetry

To install and keep track of packages we use "pipenv", so installing packages goes as follows :
To run scripts, run pylint, etc. enter the virtualenv using :
```bash
pipenv shell
poetry shell
```
And from now on you can install all packages with :

When adding new packages, add them using :
```bash
poetry add <pacakge_name>
```

# Building
If you want to build this python package you can execute :
```bash
make build
```
When you're done or when you want to clean up use :
```bash
make clean
```

# Linting
We use `black` and `pylint`. To run these tools use :
```bash
make lint
```
pipenv install <your_package_name>
```
Loading
Loading