confers 0.2.2

A modern, type-safe configuration management library with validation, diff, and hot-reload support
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
// Copyright (c) 2025 Kirky.X
//
// Licensed under the MIT License
// See LICENSE file in the project root for full license information.

use crate::error::ConfigError;
use crate::providers::provider::{ConfigProvider, ProviderMetadata, ProviderType};
#[cfg(feature = "encryption")]
use crate::security::{SecureString, SensitivityLevel};
use crate::utils::ssrf::validate_remote_url;
use crate::watcher::TlsConfig;
use base64::{engine::general_purpose::STANDARD as BASE64, Engine as _};
use failsafe::{
    backoff, failure_policy, CircuitBreaker, Config as CircuitBreakerConfig, Error as FailsafeError,
};
use figment::{
    providers::Serialized,
    value::{Dict, Map},
    Figment, Profile,
};
use std::path::PathBuf;
use std::sync::Arc;
use std::time::Duration;
use url::Url;

/// ACL token type for Consul (requires encryption feature)
#[cfg(feature = "encryption")]
#[derive(Clone, Debug)]
pub enum ConsulAclToken {
    /// Standard ACL token
    Token(Arc<SecureString>),
    /// Bearer token format (common in Consul Enterprise)
    Bearer(Arc<SecureString>),
    /// Identity token for OIDC/JWT authentication
    Identity(Arc<SecureString>),
    /// Agent token (for agent-specific operations)
    Agent(Arc<SecureString>),
}

#[cfg(feature = "encryption")]
impl ConsulAclToken {
    /// Create a standard ACL token
    pub fn token(token: impl Into<String>) -> Self {
        ConsulAclToken::Token(Arc::new(SecureString::new(
            token.into(),
            SensitivityLevel::High,
        )))
    }

    /// Create a bearer token (for Consul Enterprise)
    pub fn bearer(token: impl Into<String>) -> Self {
        ConsulAclToken::Bearer(Arc::new(SecureString::new(
            token.into(),
            SensitivityLevel::High,
        )))
    }

    /// Create an identity token for OIDC/JWT
    pub fn identity(token: impl Into<String>) -> Self {
        ConsulAclToken::Identity(Arc::new(SecureString::new(
            token.into(),
            SensitivityLevel::High,
        )))
    }

    /// Create an agent token
    pub fn agent(token: impl Into<String>) -> Self {
        ConsulAclToken::Agent(Arc::new(SecureString::new(
            token.into(),
            SensitivityLevel::High,
        )))
    }

    fn as_str(&self) -> &str {
        match self {
            ConsulAclToken::Token(t)
            | ConsulAclToken::Bearer(t)
            | ConsulAclToken::Identity(t)
            | ConsulAclToken::Agent(t) => t.as_str(),
        }
    }

    fn header_name(&self) -> &'static str {
        match self {
            ConsulAclToken::Token(_) => "X-Consul-Token",
            ConsulAclToken::Bearer(_) => "Authorization",
            ConsulAclToken::Identity(_) => "X-Consul-Identity",
            ConsulAclToken::Agent(_) => "X-Consul-Agent-Token",
        }
    }
}

/// Simple ACL token type (without encryption feature)
#[cfg(not(feature = "encryption"))]
#[derive(Clone, Debug)]
pub enum ConsulAclToken {
    Token(String),
    Bearer(String),
    Identity(String),
    Agent(String),
}

#[cfg(not(feature = "encryption"))]
impl ConsulAclToken {
    pub fn token(token: impl Into<String>) -> Self {
        ConsulAclToken::Token(token.into())
    }

    pub fn bearer(token: impl Into<String>) -> Self {
        ConsulAclToken::Bearer(token.into())
    }

    pub fn identity(token: impl Into<String>) -> Self {
        ConsulAclToken::Identity(token.into())
    }

    pub fn agent(token: impl Into<String>) -> Self {
        ConsulAclToken::Agent(token.into())
    }

    fn as_str(&self) -> &str {
        match self {
            ConsulAclToken::Token(t)
            | ConsulAclToken::Bearer(t)
            | ConsulAclToken::Identity(t)
            | ConsulAclToken::Agent(t) => t.as_str(),
        }
    }

    fn header_name(&self) -> &'static str {
        match self {
            ConsulAclToken::Token(_) => "X-Consul-Token",
            ConsulAclToken::Bearer(_) => "Authorization",
            ConsulAclToken::Identity(_) => "X-Consul-Identity",
            ConsulAclToken::Agent(_) => "X-Consul-Agent-Token",
        }
    }
}

/// Consul ACL policy configuration
#[derive(Clone, Default)]
pub struct ConsulAclPolicy {
    /// Policy ID or name
    pub policy_id: Option<String>,
    /// Role ID or name (Consul Enterprise)
    pub role_id: Option<String>,
    /// Namespace (Consul Enterprise)
    pub namespace: Option<String>,
    /// Partition (Consul Enterprise)
    pub partition: Option<String>,
    /// Datacenter
    pub datacenter: Option<String>,
}

impl ConsulAclPolicy {
    /// Create a new ACL policy
    pub fn new() -> Self {
        Self::default()
    }

    /// Set policy ID
    pub fn with_policy_id(mut self, policy_id: impl Into<String>) -> Self {
        self.policy_id = Some(policy_id.into());
        self
    }

    /// Set role ID (Consul Enterprise)
    pub fn with_role_id(mut self, role_id: impl Into<String>) -> Self {
        self.role_id = Some(role_id.into());
        self
    }

    /// Set namespace (Consul Enterprise)
    pub fn with_namespace(mut self, namespace: impl Into<String>) -> Self {
        self.namespace = Some(namespace.into());
        self
    }

    /// Set partition (Consul Enterprise)
    pub fn with_partition(mut self, partition: impl Into<String>) -> Self {
        self.partition = Some(partition.into());
        self
    }

    /// Set datacenter
    pub fn with_datacenter(mut self, datacenter: impl Into<String>) -> Self {
        self.datacenter = Some(datacenter.into());
        self
    }
}

#[derive(Clone)]
pub struct ConsulConfigProvider {
    address: String,
    key: String,
    token: Option<ConsulAclToken>,
    acl_policy: Option<ConsulAclPolicy>,
    tls_config: Option<TlsConfig>,
    priority: u8,
}

#[derive(serde::Deserialize)]
#[allow(non_snake_case)]
struct ConsulKvPair {
    Value: String,
}

impl ConsulConfigProvider {
    pub fn new(address: impl Into<String>, key: impl Into<String>) -> Self {
        Self {
            address: address.into(),
            key: key.into(),
            token: None,
            acl_policy: None,
            tls_config: None,
            priority: 30,
        }
    }

    pub fn from_address(address: impl Into<String>, key: impl Into<String>) -> Self {
        Self::new(address, key)
    }

    /// Set ACL token (standard Consul token)
    pub fn with_token(mut self, token: impl Into<String>) -> Self {
        self.token = Some(ConsulAclToken::token(token));
        self
    }

    /// Set ACL token securely (encryption feature only)
    #[cfg(feature = "encryption")]
    pub fn with_token_secure(mut self, token: Arc<SecureString>) -> Self {
        self.token = Some(ConsulAclToken::Token(token));
        self
    }

    /// Set bearer token (for Consul Enterprise)
    pub fn with_bearer_token(mut self, token: impl Into<String>) -> Self {
        self.token = Some(ConsulAclToken::bearer(token));
        self
    }

    /// Set identity token (for OIDC/JWT authentication)
    pub fn with_identity_token(mut self, token: impl Into<String>) -> Self {
        self.token = Some(ConsulAclToken::identity(token));
        self
    }

    /// Set agent token
    pub fn with_agent_token(mut self, token: impl Into<String>) -> Self {
        self.token = Some(ConsulAclToken::agent(token));
        self
    }

    /// Set ACL policy configuration
    pub fn with_acl_policy(mut self, policy: ConsulAclPolicy) -> Self {
        self.acl_policy = Some(policy);
        self
    }

    /// Set TLS configuration
    pub fn with_tls(
        mut self,
        ca_cert: impl Into<PathBuf>,
        client_cert: Option<impl Into<PathBuf>>,
        client_key: Option<impl Into<PathBuf>>,
    ) -> Self {
        let ca_cert: PathBuf = ca_cert.into();
        let mut tls = TlsConfig::new().with_ca_cert(&ca_cert);
        if let Some(cert) = client_cert {
            tls = tls.with_client_cert(cert);
        }
        if let Some(key) = client_key {
            tls = tls.with_client_key(key);
        }
        self.tls_config = Some(tls);
        self
    }

    /// Set priority (lower values are loaded first)
    pub fn with_priority(mut self, priority: u8) -> Self {
        self.priority = priority;
        self
    }

    /// Backward compatibility: with_auth does nothing (deprecated)
    pub fn with_auth(self, _username: impl Into<String>, _password: impl Into<String>) -> Self {
        self
    }

    fn build_client(&self) -> Result<reqwest::blocking::Client, ConfigError> {
        let mut client_builder = reqwest::blocking::Client::builder();

        if let Some(tls) = &self.tls_config {
            if let Some(ca_path) = tls.ca_cert_path() {
                let cert_data = std::fs::read(ca_path).map_err(|e| {
                    ConfigError::RemoteError(format!("Failed to read CA cert: {}", e))
                })?;
                let cert = reqwest::Certificate::from_pem(&cert_data).map_err(|e| {
                    ConfigError::RemoteError(format!("Failed to parse CA cert: {}", e))
                })?;
                client_builder = client_builder.add_root_certificate(cert);
            }

            if let (Some(cert_path), Some(key_path)) =
                (tls.client_cert_path(), tls.client_key_path())
            {
                let cert_data = std::fs::read(cert_path).map_err(|e| {
                    ConfigError::RemoteError(format!("Failed to read client cert: {}", e))
                })?;
                let key_data = std::fs::read(key_path).map_err(|e| {
                    ConfigError::RemoteError(format!("Failed to read client key: {}", e))
                })?;
                // Combine cert and key for identity
                let mut combined = cert_data;
                combined.extend_from_slice(b"\n");
                combined.extend_from_slice(&key_data);
                let identity = reqwest::Identity::from_pem(&combined).map_err(|e| {
                    ConfigError::RemoteError(format!("Failed to parse client identity: {}", e))
                })?;
                client_builder = client_builder.identity(identity);
            }
        }

        client_builder
            .build()
            .map_err(|e| ConfigError::RemoteError(format!("Failed to build client: {}", e)))
    }

    fn build_url(&self) -> Result<Url, ConfigError> {
        let mut url = Url::parse(&self.address)
            .map_err(|e| ConfigError::RemoteError(format!("Invalid Consul URL: {}", e)))?;

        let path = url.path();
        let key = &self.key;

        // Build base path with key
        let base_path = if path == "/" || path.is_empty() {
            format!("/v1/kv/{}", key)
        } else if path.ends_with("/v1/kv/") {
            format!("{}{}", path, key)
        } else if path.contains("/v1/kv") {
            format!("{}/{}", path.trim_end_matches('/'), key)
        } else {
            format!("{}/v1/kv/{}", path.trim_end_matches('/'), key)
        };

        url.set_path(&base_path);

        // Add query parameters for ACL policy
        let mut query_pairs: Vec<(String, String)> = Vec::new();

        if let Some(policy) = &self.acl_policy {
            if let Some(ns) = &policy.namespace {
                query_pairs.push(("ns".to_string(), ns.clone()));
            }
            if let Some(partition) = &policy.partition {
                query_pairs.push(("partition".to_string(), partition.clone()));
            }
            if let Some(dc) = &policy.datacenter {
                query_pairs.push(("dc".to_string(), dc.clone()));
            }
            if let Some(pid) = &policy.policy_id {
                query_pairs.push(("policy".to_string(), pid.clone()));
            }
            if let Some(rid) = &policy.role_id {
                query_pairs.push(("role".to_string(), rid.clone()));
            }
        }

        // Only set query if we have parameters
        if !query_pairs.is_empty() {
            let query: String = query_pairs
                .iter()
                .map(|(k, v)| format!("{}={}", k, v))
                .collect::<Vec<_>>()
                .join("&");
            url.set_query(Some(&query));
        }

        Ok(url)
    }

    fn fetch_data(&self) -> Result<Map<Profile, Dict>, ConfigError> {
        let url = self.build_url()?;
        let client = self.build_client()?;

        let mut req = client.get(url);

        // Add ACL headers based on token type
        if let Some(token) = &self.token {
            req = req.header(token.header_name(), token.as_str());
        }

        let resp = req
            .send()
            .map_err(|e| ConfigError::RemoteError(format!("Failed to connect to Consul: {}", e)))?;

        if resp.status().is_success() {
            let kvs: Vec<ConsulKvPair> = resp.json().map_err(|e| {
                ConfigError::RemoteError(format!("Failed to parse Consul response: {}", e))
            })?;

            if let Some(kv) = kvs.first() {
                let val_str = &kv.Value;
                let decoded = BASE64.decode(val_str).map_err(|e| {
                    ConfigError::RemoteError(format!("Base64 decode failed: {}", e))
                })?;

                let json_str = String::from_utf8(decoded)
                    .map_err(|e| ConfigError::RemoteError(format!("UTF-8 error: {}", e)))?;

                let map: Dict = serde_json::from_str(&json_str).map_err(|e| {
                    ConfigError::RemoteError(format!("Failed to parse JSON: {}", e))
                })?;

                let mut profiles = Map::new();
                profiles.insert(Profile::Default, map);
                Ok(profiles)
            } else {
                Err(ConfigError::RemoteError(format!(
                    "Key {} not found in Consul (empty response)",
                    self.key
                )))
            }
        } else if resp.status() == reqwest::StatusCode::NOT_FOUND {
            Err(ConfigError::RemoteError(format!(
                "Key {} not found in Consul",
                self.key
            )))
        } else if resp.status() == reqwest::StatusCode::FORBIDDEN {
            Err(ConfigError::RemoteError(
                "Access denied: ACL token insufficient permissions or invalid".to_string(),
            ))
        } else if resp.status() == reqwest::StatusCode::UNAUTHORIZED {
            Err(ConfigError::RemoteError(
                "Authentication failed: Invalid or expired ACL token".to_string(),
            ))
        } else {
            Err(ConfigError::RemoteError(format!(
                "Consul returned error: {}",
                resp.status()
            )))
        }
    }
}

impl ConfigProvider for ConsulConfigProvider {
    fn load(&self) -> Result<Figment, ConfigError> {
        // Validate URL to prevent SSRF attacks
        validate_remote_url(&self.address)?;

        let circuit_breaker = CircuitBreakerConfig::new()
            .failure_policy(failure_policy::consecutive_failures(
                3,
                backoff::constant(Duration::from_secs(10)),
            ))
            .build();

        let result = circuit_breaker.call(|| self.fetch_data());

        match result {
            Ok(data) => {
                let figment = Figment::new().merge(Serialized::from(data, Profile::Default));
                Ok(figment)
            }
            Err(FailsafeError::Inner(e)) => Err(e),
            Err(FailsafeError::Rejected) => Err(ConfigError::RemoteError(
                "Circuit breaker open: Consul requests rejected".to_string(),
            )),
        }
    }

    fn name(&self) -> &str {
        "consul"
    }

    fn is_available(&self) -> bool {
        !self.address.is_empty() && self.address.starts_with("http")
    }

    fn priority(&self) -> u8 {
        self.priority
    }

    fn metadata(&self) -> ProviderMetadata {
        ProviderMetadata {
            name: self.name().to_string(),
            description: format!("Consul provider for key: {}", self.key),
            source_type: ProviderType::Remote,
            requires_network: true,
            supports_watch: false,
            priority: self.priority,
        }
    }

    fn as_any(&self) -> &dyn std::any::Any {
        self
    }
}

#[deprecated(since = "0.4.0", note = "Use ConsulConfigProvider instead")]
pub type ConsulProvider = ConsulConfigProvider;