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
- Event Reception: Receives report delivery events from Event Hub
- Configuration Lookup: Retrieves delivery configuration from Cosmos DB
- Blob Retrieval: Downloads generated report from Azure Blob Storage
- Encryption Processing: Applies PGP encryption if configured
- Delivery Method Selection: Routes to appropriate delivery handler
- Content Processing: Applies compression, token replacement, and formatting
- Delivery Execution: Executes delivery through selected method
- 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
- Cosmos DB Integration: Retrieves delivery configurations from database
- Configuration Validation: Validates delivery settings and requirements
- Dynamic Configuration: Supports runtime configuration updates
- Multi-Publisher Support: Handles configurations for multiple publishers
Security Features
- PGP Encryption: Optional PGP encryption for sensitive reports
- Key Management: Secure handling of encryption keys per publisher
- Secure Transmission: Encrypted delivery channels (SFTP, HTTPS)
- Authentication: Secure authentication for all delivery methods
Content Processing
- Token Replacement: Dynamic replacement of tokens in file names and content
- File Compression: Automatic compression for email attachments
- Format Conversion: Content formatting for different delivery methods
- 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 operationsnodemailer: SMTP email deliveryssh2: SFTP file transferaws-sdk: AWS S3 integrationopenpgp: PGP encryption functionalityarchiver: 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
- Configuration Missing: Report delivery configuration not found
- Blob Retrieval Failure: Unable to download report file
- Delivery Method Failure: Specific delivery method errors
- Encryption Errors: PGP encryption processing failures
- 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
applogEvent 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
Related Services
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
- Email Delivery Failures: Check SMTP configuration and recipient addresses
- SFTP Connection Issues: Verify SSH keys and server connectivity
- S3 Upload Failures: Check AWS credentials and bucket permissions
- Encryption Errors: Verify PGP key configuration and format
- Blob Retrieval Issues: Check Azure Storage connectivity and permissions
Debug Steps
- Check Application Insights for delivery attempt logs
- Verify delivery configuration in Cosmos DB
- Test connectivity to delivery endpoints
- Validate file existence in Azure Blob Storage
- Review encryption key configuration and format
Development
Local Development Setup
- Clone repository
- Install dependencies:
npm install - Configure Cosmos DB connection
- Set up Azure Blob Storage connection
- Configure SMTP, SFTP, and other delivery endpoints
- Set up PGP encryption keys
- Run tests:
npm test
Code Structure
src/Handler.js: Main event processing logicActions/: Delivery method implementationsActions/SmtpEmail.js: Email delivery handlerActions/Sftp.js: SFTP delivery handlerActions/S3.js: AWS S3 delivery handlerActions/Blob.js: Azure Blob delivery handlerActions/Api.js: API endpoint delivery handlerHelperFunctions/: Utility functions for token replacement and compressionconfig/: Environment-specific configurations