Skip to content

Conversation

PeterFarber
Copy link
Collaborator

@PeterFarber PeterFarber commented Sep 18, 2025

Description:

This PR introduces a new dev_ssl_cert device for automated SSL certificate management using Let's Encrypt ACME v2 with DNS-01 challenges. It enables HyperBEAM nodes to request, finalize, renew, and delete certificates, with support for both staging and production environments. Additionally, it adds secure certificate sharing capabilities between green zone nodes and significantly refactors encryption handling.

Key Changes

New Module:

  • Added src/dev_ssl_cert.erl implementing full certificate lifecycle management (request, finalize, renew, delete).
  • Supports automatic HTTPS server startup and HTTP→HTTPS redirects.

New SSL Certificate Sharing Features:

  • Added get_cert/3 endpoint to encrypt and share certificates with other green zone nodes
  • Added request_cert/3 endpoint to securely retrieve and use certificates from peer nodes
  • Implements AES-256-GCM encryption using shared green zone keys for secure certificate transfer

Encryption Helper Refactoring:

  • Extracted reusable encryption/decryption functions (encrypt_data/2, decrypt_data/3) to dev_green_zone
  • Refactored existing green zone wallet sharing to use centralized crypto helpers
  • Eliminated code duplication and improved maintainability across encryption operations

Server Enhancements:

  • Extended hb_http_server with HTTPS startup, TLS listener setup, and redirect handling.
  • Added new helpers for protocol options and startup hooks.
  • Fixed HTTP server protocol configuration (httpshttp2) for proper HTTP version semantics
  • Enhanced HTTPS port configuration support with configurable ports
  • Improved certificate file handling with automatic directory creation (certs/ directory)

Client Improvements:

  • Enhanced hb_http_client with redirect handling and transport updates.

Configuration Updates:

  • .gitignore: Added test config files.
  • erlang_ls.config: Simplified include_dirs.
  • hb_opts: Added new defaults for SSL cert device and HTTP client redirect handling.

Breaking Changes

  • start_https_node/4start_https_node/5 (added HttpsPort parameter)
  • redirect_to_https/2redirect_to_https/3 (added HttpsPort parameter)
  • Certificate files now stored in configurable certs/ directory instead of current working directory

Impact

  • Enables HyperBEAM nodes to natively manage and serve TLS certificates.
  • Enables secure certificate sharing between trusted HyperBEAM nodes in green zones.
  • Provides centralized, reusable encryption utilities for future features.
  • Removes reliance on external web servers like Nginx for HTTPS termination.
  • Supports flexible HTTPS port configuration for various deployment scenarios.
  • Significantly improves code maintainability through helper function extraction.
  • Provides a standardized API for certificate management through the device system.

PeterFarber and others added 29 commits September 8, 2025 12:57
Add complete SSL certificate management system for HyperBEAM:

* dev_ssl_cert device - HTTP API for certificate lifecycle management
* hb_acme_client - ACME v2 protocol implementation with Let's Encrypt
* hb_ssl_cert_tests - 24 comprehensive tests with structured logging
* DNS-01 challenge support for manual TXT record setup
* Enhanced error reporting with detailed ACME diagnostics
* Works with any DNS provider, staging/production environments
- Replace hb_ao parameter extraction with hb_opts configuration
- Update all API endpoints to use ssl_cert_request_id config
- Add enhanced error reporting and timeout configuration
- Update tests to match new configuration-driven approach
Major refactor improving code organization and maintainability:

SSL Certificate Device:
- Extract monolithic functions into focused helpers
- Leverage ssl_cert library functions for validation/operations
- Add comprehensive documentation and fix pattern matching warnings
- Organize with public API at top, internal helpers at bottom

HTTP Server:
- Reorganize functions by functionality with clear sections
- Add module constants for hardcoded values (ports, timeouts, paths)
- Eliminate duplicate code with shared utility functions
- Add type specifications and comprehensive documentation
- Standardize error handling and improve function naming

Key benefits:
- Better maintainability through focused, single-purpose functions
- Increased code reuse by leveraging existing libraries
- Production-ready code following Erlang best practices
- Remove complex redirect handling logic that was causing failures
- Simplify gun_req function to match old working version
- Remove MaxRedirects and redirects_left tracking
- Add parse_peer function for simpler peer URL parsing
- Use port-based transport detection instead of scheme-based
- Remove handle_redirect function and complex redirect following

This fixes scheduler test failures where redirects were not being
handled correctly.
@PeterFarber PeterFarber changed the title Feat: Add SSL Certificate Device and HTTPS Integration feat: Add SSL Certificate Device and HTTPS Integration Sep 18, 2025
- Add get_cert/3 and request_cert/3 endpoints to dev_ssl_cert for secure
  certificate sharing between green zone nodes using AES-256-GCM encryption
- Extract encryption/decryption logic into reusable helper functions in
  dev_green_zone (encrypt_data/2, decrypt_data/3)
- Refactor existing green zone code to use centralized crypto helpers
- Update hb_http_server to support configurable HTTPS ports and fix
  protocol field (https -> http2) for proper HTTP version semantics
- Improve certificate file handling with automatic directory creation
- Use modern Erlang 'maybe' expressions for cleaner error handling
- Add comprehensive API documentation and usage examples

Breaking changes:
- start_https_node/4 -> start_https_node/5 (added HttpsPort parameter)
- redirect_to_https/2 -> redirect_to_https/3 (added HttpsPort parameter)
- Certificate files now stored in configurable 'certs' directory
…ption

- Remove redundant src_dirs configuration (defaults to [src])
- Remove unused gun_max_redirects option from hb_opts default_message/0
@noahlevenson noahlevenson mentioned this pull request Sep 18, 2025
…d modular helpers

- Add comprehensive type specifications organized by function groups at top of file
- Refactor all main API functions (init/3, join/3, key/3, become/3) to use
  modern Erlang 'maybe' expressions for cleaner error handling
- Extract 15+ helper functions for better modularity and testability:
  * init/3 helpers: setup_green_zone_config/1, ensure_wallet/1, ensure_aes_key/1
  * join/3 helpers: extract_peer_info/1, should_join_peer/3
  * join_peer/5 helpers: prepare_join_request/1, verify_peer_response/3, etc.
  * validate_join/3 helpers: extract_join_request_data/2, process_successful_join/4
  * become/3 helpers: validate_become_params/1, request_and_verify_peer_key/3
  * key/3 helpers: get_appropriate_wallet/1, build_key_response/2
- Organize internal helper functions by main API function that uses them
- Update all function documentation to reflect refactored implementations
- Ensure all comment lines are ≤80 characters with proper line wrapping
- Improve code readability by eliminating deeply nested case statements
- Add comprehensive documentation for all helper functions
- Maintain backward compatibility while significantly improving code structure

Breaking changes: None (internal refactoring only)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants