Skip to main content

Chunking Django file handlers for S3 and ClamAV service uploads

Project description

This library provides the following Django file upload handlers:

If used together, the results of the anti virus check are written to the object uploaded to S3.

Installation

pip install django-chunk-upload-handlers

Usage

Using in a Django logging configuration:

INSTALLED_APPS = [
    ...
    "django_chunk_upload_handlers",
]

FILE_UPLOAD_HANDLERS = (
    "django_chunk_upload_handlers.allow_list.AllowListFileUploadHandler",
    "django_chunk_upload_handlers.clam_av.ClamAVFileUploadHandler",
    "django_chunk_upload_handlers.s3.S3FileUploadHandler",
)  # Order is important

Dependencies

This project is a Django app and depends on the Django package.

The s3 file handler depends on boto3 and django-storages

settings.DEFAULT_FILE_STORAGE must be set to "storages.backends.s3boto3.S3Boto3Storage" or a class that derives from it.

Settings

Allow list

To allow file uploads, decorate the target views using the @allow_uploads decorator.

The fields list should contain field names of fields that you want to allow file uploads from.

Examples:

For function-based views, use:

@allow_uploads(fields=['field', 'names', 'here'])

For class-based views, use:

@method_decorator(allow_uploads(fields=['field', 'names', 'here']), name='dispatch')

For views imported from external packages, add a dictionary named UPLOAD_ALLOW_DICT to your Django settings with the path to the endpoint as the key and the value as an array of field names.

UPLOAD_ALLOW_DICT = {
    "/people/.+?/edit/role/": ["upload"], # Regex can be used for the path
    "/forecast/upload-budgets": ["file"],
}

S3

AWS_ACCESS_KEY_ID CHUNK_UPLOADER_AWS_ACCESS_KEY_ID

Provide either for the AWS access key optional. CHUNK_UPLOADER_AWS_ACCESS_KEY_ID is preferred if both are set.

AWS_SECRET_ACCESS_KEY CHUNK_UPLOADER_AWS_SECRET_ACCESS_KEY

Provide either for the AWS access secret key optional. CHUNK_UPLOADER_AWS_SECRET_ACCESS_KEY is preferred if both are set.

AWS_STORAGE_BUCKET_NAME CHUNK_UPLOADER_AWS_STORAGE_BUCKET_NAME

The S3 bucket to use for uploads. CHUNK_UPLOADER_AWS_STORAGE_BUCKET_NAME is preferred if both are set.

AWS_REGION CHUNK_UPLOADER_AWS_REGION

The AWS region to use. CHUNK_UPLOADER_AWS_REGION is preferred if both are set.

S3_ROOT_DIRECTORY CHUNK_UPLOADER_S3_ROOT_DIRECTORY

The directory path to use as root for uploads. CHUNK_UPLOADER_S3_ROOT_DIRECTORY is preferred if both are set.

CHUNK_UPLOADER_RAISE_EXCEPTION_ON_VIRUS_FOUND Defines whether or not to throw an exception if a virus is found. Defaults to False.

ClamAV

CLAM_AV_USERNAME The ClamAV service username.

CLAM_AV_PASSWORD The ClamAV service password.

CLAM_AV_DOMAIN The domain to use for the ClamAV service. Note, this is domain only so test.com rather than https://test.com

CLAM_PATH The path to the ClamAV service (used with the domain defined in the setting above). Defaults to /v2/scan-chunked

CLAM_AV_IGNORE_EXTENSIONS A list of file extensions to not process with ClamAV. Defaults to an empty list.

CLAM_USE_HTTP Use http rather than https. Should not be used in production environments. Defaults to False.

Usage with file fields

The package provides a validator for use with form and model fields.

The CHUNK_UPLOADER_RAISE_EXCEPTION_ON_VIRUS_FOUND should not be set to True when using this validator.

from django import forms
from django_chunk_upload_handlers.clam_av import validate_virus_check_result


class ExampleForm(forms.Form):
    example_form_field = forms.FileField(
        validators=[validate_virus_check_result, ]
    )

from django.db import models

class ExampleModel(models.Model):
    example_model_field = models.FileField(
        max_length=10,
        validators=[validate_virus_check_result, ],
    )

The validation message will display ‘A virus was found’ if a virus is detected. This message is a translation string.

Tests

$ pip install -r requirements.txt
$ tox

Publishing to PyPI

  1. Acquire API token from Passman - Request access from the SRE team.

  2. Add token to your keyring: keyring set https://upload.pypi.org/legacy/ __token__

  3. Ensure build and twine are installed: pip install --upgrade build twine

  4. Update the version field in setup.py

  5. Build the package: python -m build

  6. Upload the package: twine upload --repository pypi dist/*

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_chunk_upload_handlers-0.0.17.tar.gz (15.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_chunk_upload_handlers-0.0.17-py3-none-any.whl (18.0 kB view details)

Uploaded Python 3

File details

Details for the file django_chunk_upload_handlers-0.0.17.tar.gz.

File metadata

File hashes

Hashes for django_chunk_upload_handlers-0.0.17.tar.gz
Algorithm Hash digest
SHA256 9fe999a20cbeda1f49c5a678f41961cec1c75c06010c4aa49e8f2e6e8c3f0fd1
MD5 d3924250c09bf1ff5a2b64ff60cc6dbd
BLAKE2b-256 16497a257cb6427d41c60611b2f6768802481f9523e4cbf6178ccc931bfd773f

See more details on using hashes here.

File details

Details for the file django_chunk_upload_handlers-0.0.17-py3-none-any.whl.

File metadata

File hashes

Hashes for django_chunk_upload_handlers-0.0.17-py3-none-any.whl
Algorithm Hash digest
SHA256 189d2212bb551ae3983813e079526784de5432bbf27c8206bb39fa6bd12c938a
MD5 14808831bb3d41a2eacfff7fd99fabd4
BLAKE2b-256 2a8dd611a31c886b13bac34b65bba145cf2715b8d878935ea14868c6fb21d863

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page