Securing Celery Flower with Django Authentication

By hientd, at: 2025年1月13日10:04

Estimated Reading Time: __READING_TIME__ minutes

Securing Celery Flower with Django Authentication
Securing Celery Flower with Django Authentication

Securing Celery Flower with Django Authentication


When managing Celery tasks with Flower, security is an important consideration. By default, Flower does not integrate with Django’s authentication system, leaving it open to unauthorized access.

In this guide, I will demonstrate how to secure Flower using Django authentication by leveraging the django-revproxy package.

 

Why Secure Flower?


Flower provides a powerful monitoring tool for Celery tasks, but leaving it exposed can lead to potential misuse or unauthorized data access. Securing Flower with Django authentication ensures that only authorized users can access sensitive task-related information.

 

Tools and Setup


To achieve this, we will use the following tools:
 

  1. django-revproxy: A Django package that allows reverse proxying requests to external services.
     

  2. Custom Django View: A view that checks user permissions and forwards requests to the Flower dashboard.

 

Installing django-revproxy

First, install the django-revproxy package:

pip install django-revproxy

 

Refer to the django-revproxy documentation for more details.

 

Implementation


Create the Proxy View


We will create a custom view class that:
 

  • Uses django-revproxy to proxy requests to the Flower dashboard.
     

  • Checks user permissions to ensure only superusers can access Flower.
     

Here’s the implementation:

from django.conf import settings
from django.contrib.auth.mixins import UserPassesTestMixin
from django.urls import re_path
from revproxy.views import ProxyView

class FlowerCustomProxyView(UserPassesTestMixin, ProxyView):
    upstream = settings.FLOWER_URL
    url_prefix = settings.FLOWER_URL_PREFIX
    rewrite = ((r"^/{}$".format(url_prefix), r"/{}/".format(url_prefix)),)

    def test_func(self):
        return self.request.user.is_superuser

    @classmethod
    def as_url(cls):
        return re_path(
            r"^(?P<path>{}.*)$".format(cls.url_prefix), cls.as_view()
        )</path>

 

Configure URLs

Next, add the FlowerProxyView to your app’s URL configuration:

from django.urls import include, path
from . import views


app_name = "misc"
urlpatterns = [
    ...
    views.FlowerCustomProxyView.as_url(),
]

 

Settings Configuration

Ensure the following settings are added to your Django project:

FLOWER_URL = "http://localhost:5555"
FLOWER_URL_PREFIX = "flower"

 

Testing


Start your Django development server:

python manage.py runserver


Start Flower:

celery -A your_project flower


Navigate to http://localhost:5555/flower in your browser. Ensure you are logged in as a superuser to access the dashboard.

 

Further Thoughts
 

  1. Restrict Access by IP: For additional security, consider restricting access to the Flower dashboard by IP address.
     

  2. SSL/TLS: Use HTTPS to encrypt data in transit when accessing the Flower dashboard.
     

  3. Rate Limiting: Implement rate limiting to prevent abuse.
     

  4. Audit Logs: Log access attempts to monitor usage.

 

Conclusion


Securing Flower with Django authentication is a straightforward yet effective way to protect your Celery task data. By integrating django-revproxy and a custom permission-based view, you can ensure that only authorized users have access to your Flower dashboard.

Have you implemented similar security measures? Share your thoughts in the comments below!

Tag list:
- Secure Celery Flower
- Celery Flower security
- Django permissions for Flower
- Django authentication for Flower
- django-revproxy example
- Django superuser Flower access
- Flower reverse proxy setup
- Celery task monitoring security
- How to secure Flower monitoring
- Protect Flower dashboard

Subscribe

Subscribe to our newsletter and never miss out lastest news.