Skip to content

Latest commit

 

History

History
224 lines (175 loc) · 5.92 KB

File metadata and controls

224 lines (175 loc) · 5.92 KB

Pastebox Engine - Production Features Implementation

✅ Completed Features

Phase 1: Core Functionality

  • Process Monitoring (internal/daemon/process.go)

    • Actual process lifecycle management
    • Wait for instance health
    • Monitor process exit status
  • Enhanced Health Checker (internal/daemon/health.go)

    • Real CPU/memory monitoring with gopsutil
    • HTTP health checks
    • Load balancer integration
    • 30-second check interval
  • Encryption Queue Worker (internal/encryption/worker.go)

    • Processes encryption jobs from Redis queue
    • Automatic file cleanup after encryption
    • Duration tracking
  • SFTP Subsystem (internal/gateway/sftp.go)

    • Full SFTP server implementation
    • File read/write operations
    • Directory listing
    • File commands (remove, rename, mkdir)

Phase 2: Security

  • JWT Authentication (pkg/auth/auth.go)

    • Token generation with 24h expiry
    • Token validation
    • Gin middleware for protected routes
    • Optional auth middleware
  • Rate Limiting (pkg/middleware/ratelimit.go)

    • Per-IP rate limiting
    • 10 requests/second with burst of 20
    • Automatic cleanup
  • API Authentication

    • /api/auth/token - Get JWT token
    • All /api/* routes require Bearer token
    • User ID extracted from token

Phase 3: Monitoring

  • Prometheus Metrics (pkg/metrics/metrics.go)

    • pastebox_boxes_created_total - Total boxes created
    • pastebox_boxes_active - Currently active boxes
    • pastebox_files_uploaded_total - Files uploaded per box
    • pastebox_encryption_duration_seconds - Encryption time
    • pastebox_http_request_duration_seconds - HTTP latencies
    • pastebox_http_requests_total - Total HTTP requests
  • Metrics Endpoint

    • /metrics - Prometheus scrape endpoint
    • No authentication required
    • Integrated into router

Phase 4: Testing

  • Integration Tests (tests/integration/)
    • Box creation and lifecycle
    • Concurrent box creation (10 boxes)
    • Status and listing operations

📊 API Endpoints

Public Endpoints

  • GET /health - Health check
  • GET /metrics - Prometheus metrics
  • POST /api/auth/token - Get JWT token

Protected Endpoints (Require Bearer Token)

  • POST /api/pastebox/create - Create new pastebox
  • POST /api/pastebox/kill/:boxId - Kill pastebox
  • GET /api/pastebox/:boxId/status - Get status
  • GET /api/pasteboxes - List all boxes

🔐 Authentication Flow

# 1. Get token
curl -X POST http://localhost:8080/api/auth/token \
  -H "Content-Type: application/json" \
  -d '{"user_id": "alice"}'

# Response: {"token": "eyJhbGc..."}

# 2. Use token for API calls
curl -X POST http://localhost:8080/api/pastebox/create \
  -H "Authorization: Bearer eyJhbGc..." \
  -H "Content-Type: application/json" \
  -d '{
    "encryption": true,
    "passphrase": "secret",
    "ttl": 3600
  }'

📈 Metrics Example

# HELP pastebox_boxes_active Number of currently active pasteboxes
# TYPE pastebox_boxes_active gauge
pastebox_boxes_active 5

# HELP pastebox_boxes_created_total Total number of pasteboxes created
# TYPE pastebox_boxes_created_total counter
pastebox_boxes_created_total 42

# HELP pastebox_http_request_duration_seconds HTTP request latencies
# TYPE pastebox_http_request_duration_seconds histogram
pastebox_http_request_duration_seconds_bucket{endpoint="/api/pastebox/create",method="POST",status="201",le="0.005"} 10

🚀 Running the System

# Build
make build

# Start router (with all features)
./bin/router --config config.yaml

# In another terminal - test health
curl http://localhost:8080/health

# Get metrics
curl http://localhost:8080/metrics

# Get auth token
curl -X POST http://localhost:8080/api/auth/token \
  -H "Content-Type: application/json" \
  -d '{"user_id": "test"}'

# Create box (with token)
curl -X POST http://localhost:8080/api/pastebox/create \
  -H "Authorization: Bearer <token>" \
  -H "Content-Type: application/json" \
  -d '{"encryption": true, "passphrase": "test", "ttl": 3600}'

🧪 Running Tests

# Unit tests
go test -v ./tests/...

# Integration tests
go test -v ./tests/integration/...

# All tests with coverage
go test -cover ./...

📦 New Dependencies

github.com/golang-jwt/jwt/v5        # JWT authentication
golang.org/x/time/rate              # Rate limiting
github.com/prometheus/client_golang # Prometheus metrics
github.com/shirou/gopsutil/v3       # Process monitoring
github.com/pkg/sftp                 # SFTP server

🔧 Configuration

Add to config.yaml:

server:
  port: 8080
  ssh_port: 2222
  auth_secret: "change-this-in-production"  # JWT secret

daemon:
  max_instances: 100
  health_check_interval: 30s
  instance_timeout: 10m

rate_limit:
  requests_per_second: 10
  burst: 20

⚠️ Security Notes

  1. Change JWT Secret: The auth secret is hardcoded - use environment variable in production
  2. HTTPS: Add TLS configuration for production
  3. SSH Keys: Implement proper SSH key management
  4. Input Validation: Add more comprehensive validation
  5. CORS: Configure CORS if needed for web clients

🎯 What's Working

  • ✅ Process spawning and monitoring
  • ✅ Health checks with resource usage
  • ✅ JWT authentication
  • ✅ Rate limiting
  • ✅ Prometheus metrics
  • ✅ SFTP file operations
  • ✅ Encryption queue worker
  • ✅ Integration tests

📝 Next Steps (Optional)

  • TLS/HTTPS support
  • Distributed tracing (Jaeger)
  • Load testing (100+ concurrent boxes)
  • Security testing
  • Admin dashboard
  • Docker containerization
  • Kubernetes manifests
  • CI/CD pipeline

🎉 Summary

The Pastebox Engine now has production-grade features:

  • Security: JWT auth, rate limiting
  • Monitoring: Prometheus metrics, health checks
  • Performance: Resource monitoring, load balancing
  • Reliability: Process monitoring, graceful shutdown
  • Testing: Unit + integration tests

Ready for staging deployment!