Configuration Management Architecture

Table of Contents

Last Updated: 2025-03-06

Overview

The Configuration Management system [AIR-3] provides a centralized approach to managing all system configurations, supporting multiple configuration sources, validation, and both automated and user input options.

Architecture

The configuration management system follows a hexagonal architecture pattern with clear separation of concerns and a focus on flexibility and extensibility.

                      +------------------+
                      |                  |
                      |  Configuration   |
                      |     Manager      |
                      |                  |
                      +--------+---------+
                               |
                               |
         +-------------------+-+-------------------+
         |                   |                     |
+--------v---------+ +-------v--------+  +--------v---------+
|                  | |                |  |                  |
|   Configuration  | | Configuration  |  |  Configuration   |
|     Sources      | |   Validation   |  |    Consumers     |
|                  | |                |  |                  |
+------------------+ +----------------+  +------------------+
         |                   |                     |
         |                   |                     |
+--------v---------+ +-------v--------+  +--------v---------+
|  File  |  Env    | | Type | Format  |  | System | User    |
| Source | Source  | |      |         |  |        |         |
+--------+---------+ +-------+--------+  +--------+---------+

Core Components

  1. Configuration Manager
  2. Central hub for all configuration operations
  3. Manages configuration state and history
  4. Provides validation and event notification
  5. Thread-safe with RwLock protection

  6. Configuration Sources

  7. File-based (YAML, JSON, TOML)
  8. Environment variables
  9. Command-line arguments
  10. User input (CLI/UI)
  11. Programmatic API
  12. Default values

  13. Configuration Validation

  14. Type validation
  15. Range validation (min/max)
  16. Pattern matching (regex)
  17. Enumeration validation
  18. Custom validation rules

  19. Configuration Consumers

  20. System components
  21. User applications
  22. External services

Key Features

  1. Multi-Source Configuration

The system can load configuration from multiple sources with a defined precedence order:

User Input > Command Line > Environment Variables > Config Files > Defaults

This allows for flexible configuration management with appropriate overrides.

  1. Type-Safe Configuration

All configuration values are strongly typed with validation:

rust pub enum ConfigValue { String(String), Integer(i64), Float(f64), Boolean(bool), Array(Vec<ConfigValue>), Map(HashMap<String, ConfigValue>), Null, }

  1. Configuration Validation

Configurable validation rules for each configuration key:

rust pub enum ValidationRule { Required, MinValue(f64), MaxValue(f64), MinLength(usize), MaxLength(usize), Pattern(String), Enum(Vec<ConfigValue>), Custom(Arc<dyn Fn(&ConfigValue) -> Result<(), ValidationError> + Send + Sync>), }

  1. Change Tracking and History

All configuration changes are tracked with history:

rust pub struct ConfigChangeEvent { pub key: String, pub old_value: Option<ConfigValue>, pub new_value: ConfigValue, pub source: ConfigSource, pub timestamp: chrono::DateTime<chrono::Utc>, }

  1. Event-Based Notification

Components can subscribe to configuration changes:

rust pub type ConfigChangeListener = Arc<dyn Fn(&ConfigChangeEvent) -> () + Send + Sync>;

  1. Sensitive Configuration

Configuration values can be marked as sensitive for security:

rust config_manager.mark_as_sensitive("security.api_key", true);

Implementation

The configuration management system is implemented in the src/core/config_management.rs file with the following key components:

  • ConfigManager: Central configuration management
  • ConfigValue: Type-safe configuration values
  • ConfigSource: Configuration sources
  • ValidationRule: Configuration validation rules
  • ConfigChangeEvent: Configuration change events
  • CONFIG_MANAGER: Global configuration manager instance

Usage Examples

Basic Configuration Access

// Access the global configuration manager
let config = &crate::core::CONFIG_MANAGER;

// Set a configuration value
config.set_value(
    "system.auto_save_frequency",
    ConfigValue::Integer(20),
    ConfigSource::Default
).unwrap();

// Get a configuration value
let auto_save_frequency = config.get_integer("system.auto_save_frequency").unwrap();

Loading Configuration from File

// Load configuration from a JSON file
config.load_from_file(&PathBuf::from("config.json")).unwrap();

// Load configuration from a YAML file
config.load_from_file(&PathBuf::from("config.yaml")).unwrap();

// Load configuration from a TOML file
config.load_from_file(&PathBuf::from("config.toml")).unwrap();

Loading Configuration from Environment

// Load configuration from environment variables with the ANYA_ prefix
config.load_from_env("ANYA_").unwrap();

Adding Validation Rules

// Add validation rules
config.add_validation_rule("system.auto_save_frequency", ValidationRule::MinValue(1.0));
config.add_validation_rule("system.auto_save_frequency", ValidationRule::MaxValue(100.0));

// Add a custom validation rule
config.add_validation_rule("custom.value", ValidationRule::Custom(Arc::new(|value| {
    // Custom validation logic
    if let ConfigValue::String(s) = value {
        if s.contains("forbidden") {
            return Err(ValidationError::Custom("Contains forbidden word".to_string()));
        }
    }
    Ok(())
})));

Listening for Configuration Changes

// Add a configuration change listener
config.add_listener(Arc::new(|event| {
    println!("Configuration changed: {} = {:?}", event.key, event.new_value);
}));

Saving Configuration to File

// Save configuration to a file
config.save_to_file(&PathBuf::from("config.json")).unwrap();

Integration with Core Systems

The configuration management system is integrated with the CoreSystem in src/core/mod.rs:

pub struct CoreSystem {
    // ...
    config_manager: &'static ConfigManager,
}

impl CoreSystem {
    // ...

    /// Get access to the configuration manager
    pub fn config_manager(&self) -> &ConfigManager {
        self.config_manager
    }

    /// Initialize configuration with default values
    pub fn initialize_default_config(&self) -> Result<(), String> {
        // ...
    }
}

Configuration Components

Component Configuration Type Auto/User Input Status
Core System System parameters Both 100%
Security Security policies Auto with override 100%
Performance Resource allocation Auto with override 100%
Layer 2 Protocol parameters Both 100%
Web5 Connection parameters Both 100%
ML System Model parameters Auto with override 100%
Monitoring Alert thresholds Both 100%
Testing Test parameters Auto 100%

Security Considerations

  1. Sensitive Data Protection

Sensitive configuration values are: - Never logged - Excluded from file serialization - Masked in debug output

  1. Access Control

Configuration access is controlled through: - Read-only configuration options - Source-based precedence rules - Validation constraints

  1. Audit Trail

All configuration changes are tracked with: - Timestamp - Previous value - New value - Change source

Performance Considerations

  1. Efficient Access

  2. Configuration values are cached in memory

  3. RwLock is used for thread-safe access
  4. Read operations are optimized

  5. Serialization Optimization

  6. Serialization is only performed when needed

  7. Format-specific optimizations are applied

Future Enhancements

  1. Remote Configuration

  2. Support for loading configuration from remote sources

  3. Dynamic configuration updates

  4. Schema-Based Validation

  5. JSON Schema support for configuration validation

  6. Schema extraction from code

  7. Configuration UI

  8. Web-based configuration management

  9. Mobile configuration interface

  10. Configuration Versioning

  11. Full versioning of configuration state

  12. Rollback capability

[AIR-3][AIS-3][BPC-3][RES-3]

This document follows the AI Labeling System standards based on official Bitcoin Improvement Proposals (BIPs).

See Also