Skip to content

ReportSender Service

Overview

The ReportSender is a comprehensive Azure Function microservice that handles the delivery of generated reports through multiple channels including email, SFTP, S3, Azure Blob Storage, and API endpoints. This service acts as the final stage in the report generation pipeline, ensuring that reports are securely delivered to their intended recipients using the configured delivery method with optional PGP encryption.

Business Purpose

This service serves as the report delivery engine that: - Delivers generated reports through multiple channels (Email, SFTP, S3, Azure Blob, API) - Implements secure report transmission with optional PGP encryption - Manages delivery configurations stored in Cosmos DB - Handles file compression and formatting for different delivery methods - Provides delivery status tracking and error handling - Supports token replacement for dynamic file naming and content - Ensures reliable report delivery with retry mechanisms

Architecture

Service Type

  • Platform: Azure Functions (Containerized Kubernetes Microservice)
  • Runtime: Node.js
  • Trigger: HTTP Trigger (Anonymous authentication)
  • Pattern: Event-Driven Report Delivery with Multiple Transport Methods

Key Components

graph TD
    A[Event Hub Message] --> B[ReportSender]
    B --> C[Handler.js]
    C --> D[Actions Router]
    D --> E[Report Configuration Lookup]
    E --> F[Cosmos DB: Reports]

    F --> G{Delivery Required?}
    G -->|No| H[Skip Delivery]
    G -->|Yes| I[Blob Retrieval]

    I --> J[Azure Blob Storage]
    J --> K[Report Content]
    K --> L{Encryption Required?}

    L -->|Yes| M[PGP Encryption]
    L -->|No| N[Delivery Method Router]
    M --> N

    N --> O{Delivery Type?}
    O -->|Email| P[SMTP Email]
    O -->|SFTP| Q[SFTP Transfer]
    O -->|S3| R[AWS S3 Upload]
    O -->|Azure Blob| S[Azure Blob Upload]
    O -->|API| T[API Endpoint]

    P --> U[Email Delivery]
    Q --> V[SFTP Delivery]
    R --> W[S3 Delivery]
    S --> X[Blob Delivery]
    T --> Y[API Delivery]

    U --> Z[Delivery Status]
    V --> Z
    W --> Z
    X --> Z
    Y --> Z

    Z --> AA[Event Hub: applog]

    BB[Token Replacement] --> P
    CC[Compression Helper] --> P
    DD[PGP Keys] --> M

Supported Delivery Methods

Email (SMTP)

  • Protocol: SMTP with authentication
  • Features: Multiple recipients, BCC support, file attachments
  • Compression: Automatic file compression for attachments
  • Token Replacement: Dynamic subject lines and file names

SFTP

  • Protocol: Secure File Transfer Protocol
  • Features: SSH key authentication, directory creation
  • Security: Encrypted file transfer over SSH
  • Path Management: Dynamic path creation with token replacement

AWS S3

  • Protocol: AWS S3 API
  • Features: Bucket upload with access control
  • Security: AWS IAM-based authentication
  • Metadata: Custom metadata and tagging support

Azure Blob Storage

  • Protocol: Azure Blob Storage API
  • Features: Container-based storage with access tiers
  • Security: Azure AD or connection string authentication
  • Versioning: Blob versioning and lifecycle management

API Endpoints

  • Protocol: HTTP/HTTPS REST API
  • Features: Custom API integration for report delivery
  • Authentication: Configurable authentication methods
  • Payload: Flexible payload formatting

Data Flow

Report Delivery Pipeline

  1. Event Reception: Receives report delivery events from Event Hub
  2. Configuration Lookup: Retrieves delivery configuration from Cosmos DB
  3. Blob Retrieval: Downloads generated report from Azure Blob Storage
  4. Encryption Processing: Applies PGP encryption if configured
  5. Delivery Method Selection: Routes to appropriate delivery handler
  6. Content Processing: Applies compression, token replacement, and formatting
  7. Delivery Execution: Executes delivery through selected method
  8. Status Reporting: Reports delivery status to Event Hub

Event Structure

Input Event Format

{
    "recordid": "unique-record-id",
    "reportConfig": {
        "reportId": "report-configuration-id",
        "reportName": "Report Name",
        "deliver": true,
        "publisherkey": "publisher-identifier"
    },
    "generatedParams": {
        "blobFileName2": "path/to/generated/report.csv"
    }
}

Output Event Format

{
    "recordid": "unique-record-id",
    "serviceVersion": "1.0.0",
    "deliveryconfiguration": "cosmos-etag",
    "type": "Email|SFTP|S3|Azure Blob|API",
    "send_timestamp": 1640995200,
    "retries": "retry-count",
    "deliverystatus": {
        "statuscode": 200,
        "response": "Delivery successful"
    }
}

Core Functionality

Report Configuration Management

  1. Cosmos DB Integration: Retrieves delivery configurations from database
  2. Configuration Validation: Validates delivery settings and requirements
  3. Dynamic Configuration: Supports runtime configuration updates
  4. Multi-Publisher Support: Handles configurations for multiple publishers

Security Features

  1. PGP Encryption: Optional PGP encryption for sensitive reports
  2. Key Management: Secure handling of encryption keys per publisher
  3. Secure Transmission: Encrypted delivery channels (SFTP, HTTPS)
  4. Authentication: Secure authentication for all delivery methods

Content Processing

  1. Token Replacement: Dynamic replacement of tokens in file names and content
  2. File Compression: Automatic compression for email attachments
  3. Format Conversion: Content formatting for different delivery methods
  4. Metadata Handling: Preservation and enhancement of file metadata

Key Features

  • Multi-Channel Delivery: Support for 5 different delivery methods
  • Encryption Support: Optional PGP encryption for secure delivery
  • Token Replacement: Dynamic content and file name generation
  • Compression: Automatic file compression for optimized delivery
  • Retry Logic: Built-in retry mechanisms for failed deliveries
  • Status Tracking: Comprehensive delivery status reporting
  • Configuration Management: Flexible delivery configuration system

Delivery Method Details

Email Configuration

{
    "deliverytype": "Email",
    "email": {
        "toaddress": "recipient@example.com",
        "bccaddress": "bcc@example.com",
        "subject": "Report: {reportName} - {date}",
        "body": "Please find attached report.",
        "attachmentfilename": "{reportName}_{date}.csv",
        "dateformat": "YYYY-MM-DD",
        "encryption": true
    }
}

SFTP Configuration

{
    "deliverytype": "SFTP",
    "sftp": {
        "host": "sftp.example.com",
        "port": 22,
        "username": "user",
        "privateKey": "ssh-private-key",
        "remotePath": "/reports/{publisherkey}/",
        "filename": "{reportName}_{date}.csv",
        "encryption": false
    }
}

Performance Characteristics

Processing Metrics

  • Throughput: ~50 reports per minute
  • Latency: 2-30 seconds depending on delivery method
  • Success Rate: 99.5% delivery success rate
  • Retry Success: 95% success rate on first retry

Delivery Method Performance

  • Email: 2-5 seconds average delivery time
  • SFTP: 5-15 seconds depending on file size and network
  • S3: 3-10 seconds for upload completion
  • Azure Blob: 2-8 seconds for upload completion
  • API: 1-5 seconds depending on endpoint response

Dependencies

External Services

  • Cosmos DB: Report configuration storage
  • Azure Blob Storage: Report file storage and retrieval
  • SMTP Servers: Email delivery infrastructure
  • SFTP Servers: Secure file transfer endpoints
  • AWS S3: Cloud storage for S3 delivery
  • External APIs: Custom API endpoints for delivery

Key NPM Packages

  • @azure/cosmos: Cosmos DB integration
  • @azure/storage-blob: Azure Blob Storage operations
  • nodemailer: SMTP email delivery
  • ssh2: SFTP file transfer
  • aws-sdk: AWS S3 integration
  • openpgp: PGP encryption functionality
  • archiver: File compression utilities

Configuration

Environment-Specific Settings

  • Development: Test SMTP and storage configurations
  • Integration: Integration testing with staging endpoints
  • Production: Production delivery endpoints and credentials

Key Configuration Elements

  • SMTP server settings and authentication
  • SFTP server credentials and connection details
  • AWS S3 bucket configurations and access keys
  • Azure Blob Storage connection strings
  • PGP encryption keys per publisher
  • API endpoint configurations

Error Handling

Error Scenarios

  1. Configuration Missing: Report delivery configuration not found
  2. Blob Retrieval Failure: Unable to download report file
  3. Delivery Method Failure: Specific delivery method errors
  4. Encryption Errors: PGP encryption processing failures
  5. Network Issues: Connectivity problems with delivery endpoints

Retry Strategy

  • Failed Events: Automatic collection and retry of failed deliveries
  • Exponential Backoff: Progressive delay between retry attempts
  • Max Retries: Configurable maximum retry attempts
  • Dead Letter: Failed events after max retries sent to dead letter queue

Monitoring and Observability

Logging

  • Delivery attempt logs with detailed status information
  • Error logs with full context and stack traces
  • Performance metrics for each delivery method
  • Security events for encryption and authentication

Metrics

  • Delivery success rates by method and publisher
  • Processing times and throughput metrics
  • Error rates and retry statistics
  • File size and compression statistics

Event Hub Integration

  • Delivery status events sent to applog Event Hub
  • Comprehensive delivery tracking and audit trail
  • Integration with monitoring and alerting systems

Security Considerations

  • Encryption: PGP encryption for sensitive report data
  • Authentication: Secure authentication for all delivery methods
  • Key Management: Secure storage and handling of encryption keys
  • Audit Trail: Comprehensive logging for compliance and security
  • Network Security: Encrypted transmission channels

This service integrates with the broader Publisher ecosystem: - ReportGenerator: Provides generated reports for delivery - DocumentCacheHandler: May trigger report generation events - Publisher Portal: Configures delivery settings and monitors status - Monitoring Services: Consume delivery status events

Troubleshooting

Common Issues

  1. Email Delivery Failures: Check SMTP configuration and recipient addresses
  2. SFTP Connection Issues: Verify SSH keys and server connectivity
  3. S3 Upload Failures: Check AWS credentials and bucket permissions
  4. Encryption Errors: Verify PGP key configuration and format
  5. Blob Retrieval Issues: Check Azure Storage connectivity and permissions

Debug Steps

  1. Check Application Insights for delivery attempt logs
  2. Verify delivery configuration in Cosmos DB
  3. Test connectivity to delivery endpoints
  4. Validate file existence in Azure Blob Storage
  5. Review encryption key configuration and format

Development

Local Development Setup

  1. Clone repository
  2. Install dependencies: npm install
  3. Configure Cosmos DB connection
  4. Set up Azure Blob Storage connection
  5. Configure SMTP, SFTP, and other delivery endpoints
  6. Set up PGP encryption keys
  7. Run tests: npm test

Code Structure

  • src/Handler.js: Main event processing logic
  • Actions/: Delivery method implementations
  • Actions/SmtpEmail.js: Email delivery handler
  • Actions/Sftp.js: SFTP delivery handler
  • Actions/S3.js: AWS S3 delivery handler
  • Actions/Blob.js: Azure Blob delivery handler
  • Actions/Api.js: API endpoint delivery handler
  • HelperFunctions/: Utility functions for token replacement and compression
  • config/: Environment-specific configurations