Skip to content

mattleads/CompressStamp

Repository files navigation

Symfony Messenger Secure & Compress Stamps

A lightweight, drop-in solution for independently compressing and encrypting Symfony Messenger payloads before sending them to message brokers (like Redis, RabbitMQ, or Doctrine).

Why?

  1. Compression: Shrink massive payloads (e.g., bulk exports, huge data syncs) to fit within message broker limits.
  2. Encryption: Protect sensitive data (PII) so it's never stored in plain text in your infrastructure (Redis, RabbitMQ, DB).

Features

  • Granular Control: Use CompressStamp only, SecureStamp only, or both together.
  • Transparent Consumption: The serializer automatically detects headers (X-Compressed, X-Secured) and restores the payload before it hits your handler.
  • AES-256-CBC Encryption: Industry-standard security for data at rest in your queue.
  • gzdeflate Compression: Highly efficient storage reduction.
  • Decorator Pattern: Wraps the native Symfony Messenger serializer for full compatibility.

Prerequisites

  • PHP 8.2+
  • Composer
  • Symfony CLI (optional, but recommended)

Setup

  1. Clone the repository:
    git clone https://github.com/mattleads/CompressStamp.git
    cd CompressStamp
  2. Install dependencies:
    composer install

Configuration

1. Set the Encryption Key

Add a secure string to your .env file:

MESSENGER_ENCRYPTION_KEY="ChangeMeToASecureRandomKey123!"

2. Register the Serializer Service

Decorate the default messenger serializer in your config/services.yaml:

services:
    App\Messenger\Serialization\CompressSerializer:
        arguments:
            $innerSerializer: '@messenger.default_serializer'
            $encryptionKey: '%env(MESSENGER_ENCRYPTION_KEY)%'

3. Configure the Transport

Update config/packages/messenger.yaml:

framework:
    messenger:
        transports:
            async:
                dsn: '%env(MESSENGER_TRANSPORT_DSN)%'
                serializer: App\Messenger\Serialization\CompressSerializer

Usage

Using Both (Highly Recommended for Large PII)

use App\Messenger\Stamp\CompressStamp;
use App\Messenger\Stamp\SecureStamp;

$bus->dispatch(new MyMessage($data), [
    new CompressStamp(),
    new SecureStamp()
]);

Using Encryption Only

$bus->dispatch(new MyMessage($data), [
    new SecureStamp()
]);

Using Compression Only

$bus->dispatch(new MyMessage($data), [
    new CompressStamp()
]);

Running the Demo

  1. Install: composer install
  2. Consume: php bin/console messenger:consume async -vv
  3. Trigger: php bin/console app:trigger-bulk-invoice

Under the Hood

Encoding Flow

  1. Compression: If CompressStamp is present, the body is compressed and X-Compressed: true is added.
  2. Encryption: If SecureStamp is present, the (potentially compressed) body is encrypted using AES-256-CBC and X-Secured: true is added.

Decoding Flow

  1. Decryption: If X-Secured is found, the body is decrypted using your key.
  2. Decompression: If X-Compressed is found, the body is then decompressed.
  3. Hydration: The resulting clean payload is passed to the inner serializer to become a PHP object. ject.

About

A lightweight Symfony Messenger extension to transparently compress (gzdeflate) and encrypt (AES-256) large message payloads

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages