Skip to content

Move gunicorn code to a seperate file so our gunicorn config file is smaller #26

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

Closed
wants to merge 1 commit into from
Closed
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
67 changes: 67 additions & 0 deletions aikido_firewall/middleware/django_gunicorn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
"""
Includes all the wrappers for gunicorn config file
"""

from gunicorn.http.body import Body
from io import BytesIO
import aikido_firewall
from aikido_firewall.context import Context


def when_ready(prev_func):
"""
Aikido decorator for gunicorn config
Function: pre_request(worker, req)
"""

def aik_when_ready(server):
aikido_firewall.protect("server-only")
prev_func(server)

return aik_when_ready


def pre_request(prev_func):
"""
Aikido decorator for gunicorn config
Function: pre_request(worker, req)
"""

def aik_pre_request(worker, req):
req.body, req.body_copy = clone_body(req.body)

django_context = Context(req, "django-gunicorn")
django_context.set_as_current_context()
prev_func(worker, req)

return aik_pre_request


def post_fork(prev_func):
"""
Aikido decorator for gunicorn config
Function: post_fork(server, worker)
"""

def aik_post_fork(server, worker):
aikido_firewall.protect("django-gunicorn", False)
prev_func(server, worker)

return aik_post_fork


def clone_body(body):
"""
Clones the body by creating a new stream
"""
body_read = body.read()

# Read the body content into a buffer
body_buffer = BytesIO()
body_buffer.write(body_read)
body_buffer.seek(0)

# Create a new Body object with the same content
cloned_body = Body(body_buffer)

return (cloned_body, body_read)
46 changes: 7 additions & 39 deletions sample-apps/django-mysql-gunicorn/gunicorn_config.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,7 @@
import aikido_firewall
import json
from urllib.parse import parse_qs
from io import BytesIO
from aikido_firewall.context import Context
from gunicorn.http.body import Body

def when_ready(server):
aikido_firewall.protect("server-only")

def pre_fork(server, worker):
pass

def post_fork(server, worker):
print("----------------------> POST FORK")
import aikido_firewall
aikido_firewall.protect("django-gunicorn", False)

def pre_request(worker, req):
req.body, req.body_copy = clone_body(req.body)

django_context = Context(req, "django-gunicorn")
django_context.set_as_current_context()

worker.log.debug("%s %s", req.method, req.path)


def clone_body(body):
body_read = body.read()

# Read the body content into a buffer
body_buffer = BytesIO()
body_buffer.write(body_read)
body_buffer.seek(0)

# Create a new Body object with the same content
cloned_body = Body(body_buffer)

return (cloned_body, body_read)
import aikido_firewall.middleware.django_gunicorn as aik
@aik.when_ready
def when_ready(server): pass
@aik.post_fork
def post_fork(server, worker): pass
@aik.pre_request
def pre_request(worker, req): pass
Loading