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¶
- Configuration Manager
- Central hub for all configuration operations
- Manages configuration state and history
- Provides validation and event notification
-
Thread-safe with RwLock protection
-
Configuration Sources
- File-based (YAML, JSON, TOML)
- Environment variables
- Command-line arguments
- User input (CLI/UI)
- Programmatic API
-
Default values
-
Configuration Validation
- Type validation
- Range validation (min/max)
- Pattern matching (regex)
- Enumeration validation
-
Custom validation rules
-
Configuration Consumers
- System components
- User applications
- External services
Key Features¶
- 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.
- 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,
}
- 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>),
}
- 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>,
}
- Event-Based Notification
Components can subscribe to configuration changes:
rust
pub type ConfigChangeListener = Arc<dyn Fn(&ConfigChangeEvent) -> () + Send + Sync>;
- 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 managementConfigValue
: Type-safe configuration valuesConfigSource
: Configuration sourcesValidationRule
: Configuration validation rulesConfigChangeEvent
: Configuration change eventsCONFIG_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¶
- Sensitive Data Protection
Sensitive configuration values are: - Never logged - Excluded from file serialization - Masked in debug output
- Access Control
Configuration access is controlled through: - Read-only configuration options - Source-based precedence rules - Validation constraints
- Audit Trail
All configuration changes are tracked with: - Timestamp - Previous value - New value - Change source
Performance Considerations¶
-
Efficient Access
-
Configuration values are cached in memory
- RwLock is used for thread-safe access
-
Read operations are optimized
-
Serialization Optimization
-
Serialization is only performed when needed
- Format-specific optimizations are applied
Future Enhancements¶
-
Remote Configuration
-
Support for loading configuration from remote sources
-
Dynamic configuration updates
-
Schema-Based Validation
-
JSON Schema support for configuration validation
-
Schema extraction from code
-
Configuration UI
-
Web-based configuration management
-
Mobile configuration interface
-
Configuration Versioning
-
Full versioning of configuration state
- 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).