hyperi-rustlib 2.5.1

Opinionated Rust framework for high-throughput data pipelines at PB scale. Auto-wiring config, logging, metrics, tracing, health, and graceful shutdown — built from many years of production infrastructure experience.
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
// Project:   hyperi-rustlib
// File:      src/secrets/vault.rs
// Purpose:   OpenBao/Vault secret provider
// Language:  Rust
//
// License:   FSL-1.1-ALv2
// Copyright: (c) 2026 HYPERI PTY LIMITED

//! OpenBao/Vault secret provider using vaultrs.
//!
//! Supports multiple authentication methods:
//! - Token authentication
//! - AppRole authentication
//! - Kubernetes authentication

use serde::{Deserialize, Serialize};
use tracing::debug;
use vaultrs::client::{Client, VaultClient, VaultClientSettingsBuilder};
use vaultrs::kv2;

use super::error::{SecretsError, SecretsResult};
use super::provider::SecretProvider;
use super::types::{SecretMetadata, SecretValue};

/// OpenBao/Vault connection configuration.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OpenBaoConfig {
    /// Vault address (e.g., "https://vault.example.com:8200").
    pub address: String,

    /// Authentication method.
    pub auth: OpenBaoAuth,

    /// Namespace (for Vault Enterprise).
    #[serde(default)]
    pub namespace: Option<String>,

    /// TLS CA certificate path for Vault server.
    #[serde(default)]
    pub ca_cert: Option<String>,

    /// Skip TLS verification (not recommended for production).
    #[serde(default)]
    pub skip_verify: bool,
}

/// OpenBao/Vault authentication method.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "method", rename_all = "snake_case")]
pub enum OpenBaoAuth {
    /// Token authentication.
    Token {
        /// Vault token.
        token: String,
    },

    /// AppRole authentication.
    AppRole {
        /// Role ID.
        role_id: String,
        /// Secret ID.
        secret_id: String,
        /// Mount path (default: "approle").
        #[serde(default = "default_approle_mount")]
        mount: String,
    },

    /// Kubernetes authentication.
    Kubernetes {
        /// Role name.
        role: String,
        /// Path to service account token.
        #[serde(default = "default_k8s_token_path")]
        token_path: String,
        /// Mount path (default: "kubernetes").
        #[serde(default = "default_k8s_mount")]
        mount: String,
    },
}

fn default_approle_mount() -> String {
    "approle".to_string()
}

fn default_k8s_token_path() -> String {
    "/var/run/secrets/kubernetes.io/serviceaccount/token".to_string()
}

fn default_k8s_mount() -> String {
    "kubernetes".to_string()
}

impl OpenBaoConfig {
    /// Load configuration from environment variables.
    ///
    /// Uses standard `VAULT_*` environment variables with `OPENBAO_*` and `BAO_*`
    /// as legacy fallbacks (with deprecation warnings).
    ///
    /// ## Environment Variables
    ///
    /// - `VAULT_ADDR` - Vault/OpenBao server address
    /// - `VAULT_TOKEN` - Authentication token (for token auth)
    /// - `VAULT_ROLE_ID` + `VAULT_SECRET_ID` - AppRole authentication
    /// - `VAULT_K8S_ROLE` - Kubernetes authentication role
    /// - `VAULT_NAMESPACE` - Vault namespace (Enterprise)
    /// - `VAULT_CACERT` - Path to CA certificate
    /// - `VAULT_SKIP_VERIFY` - Skip TLS verification
    ///
    /// ## Authentication Priority
    ///
    /// 1. If `VAULT_TOKEN` is set, uses token authentication
    /// 2. If `VAULT_ROLE_ID` and `VAULT_SECRET_ID` are set, uses AppRole
    /// 3. If `VAULT_K8S_ROLE` is set, uses Kubernetes authentication
    /// 4. Otherwise, returns an error
    ///
    /// # Errors
    ///
    /// Returns `None` if `VAULT_ADDR` is not set or no authentication method
    /// can be determined.
    #[must_use]
    pub fn from_env() -> Option<Self> {
        use crate::config::env_compat::vault;

        let address = vault::addr().get()?;

        // Determine authentication method
        let auth = if let Some(token) = vault::token().get() {
            OpenBaoAuth::Token { token }
        } else if let (Some(role_id), Some(secret_id)) = (
            vault::approle_role_id().get(),
            vault::approle_secret_id().get(),
        ) {
            OpenBaoAuth::AppRole {
                role_id,
                secret_id,
                mount: default_approle_mount(),
            }
        } else if let Some(role) = vault::k8s_role().get() {
            OpenBaoAuth::Kubernetes {
                role,
                token_path: default_k8s_token_path(),
                mount: default_k8s_mount(),
            }
        } else {
            // No authentication method configured
            return None;
        };

        Some(Self {
            address,
            auth,
            namespace: vault::namespace().get(),
            ca_cert: vault::ca_cert().get(),
            skip_verify: vault::skip_verify().get_bool().unwrap_or(false),
        })
    }

    /// Create a configuration for token authentication.
    #[must_use]
    pub fn with_token(address: &str, token: &str) -> Self {
        Self {
            address: address.to_string(),
            auth: OpenBaoAuth::Token {
                token: token.to_string(),
            },
            namespace: None,
            ca_cert: None,
            skip_verify: false,
        }
    }

    /// Create a configuration for AppRole authentication.
    #[must_use]
    pub fn with_approle(address: &str, role_id: &str, secret_id: &str) -> Self {
        Self {
            address: address.to_string(),
            auth: OpenBaoAuth::AppRole {
                role_id: role_id.to_string(),
                secret_id: secret_id.to_string(),
                mount: default_approle_mount(),
            },
            namespace: None,
            ca_cert: None,
            skip_verify: false,
        }
    }

    /// Set the namespace (for Vault Enterprise).
    #[must_use]
    pub fn with_namespace(mut self, namespace: &str) -> Self {
        self.namespace = Some(namespace.to_string());
        self
    }

    /// Set the CA certificate path.
    #[must_use]
    pub fn with_ca_cert(mut self, path: &str) -> Self {
        self.ca_cert = Some(path.to_string());
        self
    }

    /// Enable TLS skip verification (not recommended for production).
    #[must_use]
    pub fn with_skip_verify(mut self) -> Self {
        self.skip_verify = true;
        self
    }
}

/// OpenBao/Vault secret provider.
pub struct OpenBaoProvider {
    config: OpenBaoConfig,
}

impl OpenBaoProvider {
    /// Create a new OpenBao provider.
    ///
    /// # Errors
    ///
    /// Returns an error if client initialization fails.
    pub fn new(config: &OpenBaoConfig) -> SecretsResult<Self> {
        Ok(Self {
            config: config.clone(),
        })
    }

    /// Get an authenticated Vault client.
    ///
    /// Creates a new client for each request since `VaultClient` is not Clone.
    /// The underlying HTTP client uses connection pooling so this is efficient.
    async fn get_client(&self) -> SecretsResult<VaultClient> {
        self.create_client().await
    }

    /// Create and authenticate a new Vault client.
    async fn create_client(&self) -> SecretsResult<VaultClient> {
        let mut settings = VaultClientSettingsBuilder::default();
        settings.address(&self.config.address);

        if let Some(ref ns) = self.config.namespace {
            settings.namespace(Some(ns.clone()));
        }

        // Note: vaultrs handles TLS configuration via the address URL scheme
        // For custom CA certs, users should configure system trust store or use VAULT_CACERT env var

        let settings = settings.build().map_err(|e| {
            SecretsError::ConfigError(format!("failed to build Vault client settings: {e}"))
        })?;

        let mut client = VaultClient::new(settings).map_err(|e| {
            SecretsError::ProviderError(format!("failed to create Vault client: {e}"))
        })?;

        // Authenticate based on method
        match &self.config.auth {
            OpenBaoAuth::Token { token } => {
                client.set_token(token);
            }
            OpenBaoAuth::AppRole {
                role_id,
                secret_id,
                mount,
            } => {
                self.auth_approle(&mut client, role_id, secret_id, mount)
                    .await?;
            }
            OpenBaoAuth::Kubernetes {
                role,
                token_path,
                mount,
            } => {
                self.auth_kubernetes(&mut client, role, token_path, mount)
                    .await?;
            }
        }

        Ok(client)
    }

    /// Authenticate using AppRole.
    async fn auth_approle(
        &self,
        client: &mut VaultClient,
        role_id: &str,
        secret_id: &str,
        mount: &str,
    ) -> SecretsResult<()> {
        let auth_info = vaultrs::auth::approle::login(client, mount, role_id, secret_id)
            .await
            .map_err(|e| SecretsError::AuthError(format!("AppRole login failed: {e}")))?;

        client.set_token(&auth_info.client_token);
        debug!("AppRole authentication successful");
        Ok(())
    }

    /// Authenticate using Kubernetes service account.
    async fn auth_kubernetes(
        &self,
        client: &mut VaultClient,
        role: &str,
        token_path: &str,
        mount: &str,
    ) -> SecretsResult<()> {
        let jwt = tokio::fs::read_to_string(token_path).await.map_err(|e| {
            SecretsError::AuthError(format!(
                "failed to read K8s service account token from {token_path}: {e}"
            ))
        })?;

        let auth_info = vaultrs::auth::kubernetes::login(client, mount, role, jwt.trim())
            .await
            .map_err(|e| SecretsError::AuthError(format!("Kubernetes login failed: {e}")))?;

        client.set_token(&auth_info.client_token);
        debug!("Kubernetes authentication successful");
        Ok(())
    }

    /// Get a secret from Vault KV v2.
    ///
    /// # Errors
    ///
    /// Returns an error if the secret cannot be fetched.
    pub async fn get(&self, path: &str, key: &str) -> SecretsResult<SecretValue> {
        let client = self.get_client().await?;

        // Parse path to extract mount and secret path
        // Expected format: "secret/data/myapp/tls" or "myapp/tls" (assumes "secret" mount)
        let (mount, secret_path) = Self::parse_path(path);

        // Read the secret
        let secret: std::collections::HashMap<String, String> =
            kv2::read(&client, &mount, &secret_path)
                .await
                .map_err(|e| {
                    // Check for auth errors (token expired)
                    if e.to_string().contains("403") || e.to_string().contains("permission denied")
                    {
                        SecretsError::AuthError("Vault token expired or invalid".into())
                    } else {
                        SecretsError::ProviderError(format!("failed to read secret {path}: {e}"))
                    }
                })?;

        // Extract the requested key
        let value = secret.get(key).ok_or_else(|| {
            SecretsError::NotFound(format!("key '{key}' not found in secret '{path}'"))
        })?;

        let metadata = SecretMetadata {
            version: None, // KV v2 version would require additional API call
            source_path: Some(path.to_string()),
            provider: Some("openbao".into()),
        };

        Ok(SecretValue::with_metadata(
            value.as_bytes().to_vec(),
            metadata,
        ))
    }

    /// Parse a Vault path into mount and secret path.
    ///
    /// Handles formats:
    /// - "secret/data/myapp/tls" -> ("secret", "myapp/tls")
    /// - "myapp/tls" -> ("secret", "myapp/tls") (default mount)
    fn parse_path(path: &str) -> (String, String) {
        // Check for KV v2 "data" in path
        if let Some(rest) = path.strip_prefix("secret/data/") {
            return ("secret".into(), rest.into());
        }

        // Check for custom mount with "data" segment
        let parts: Vec<&str> = path.splitn(3, '/').collect();
        if parts.len() >= 3 && parts[1] == "data" {
            return (parts[0].into(), parts[2..].join("/"));
        }

        // Default to "secret" mount
        ("secret".into(), path.into())
    }
}

impl SecretProvider for OpenBaoProvider {
    async fn get(&self, path: &str, key: Option<&str>) -> SecretsResult<SecretValue> {
        let key = key.ok_or_else(|| {
            SecretsError::ConfigError("key is required for OpenBao secrets".into())
        })?;
        self.get(path, key).await
    }

    async fn health_check(&self) -> SecretsResult<()> {
        let client = self.get_client().await?;

        // Check sys/health endpoint
        vaultrs::sys::health(&client)
            .await
            .map_err(|e| SecretsError::ProviderError(format!("Vault health check failed: {e}")))?;

        Ok(())
    }

    fn name(&self) -> &'static str {
        "openbao"
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_path_with_mount() {
        let (mount, path) = OpenBaoProvider::parse_path("secret/data/myapp/tls");
        assert_eq!(mount, "secret");
        assert_eq!(path, "myapp/tls");
    }

    #[test]
    fn test_parse_path_custom_mount() {
        let (mount, path) = OpenBaoProvider::parse_path("kv/data/myapp/creds");
        assert_eq!(mount, "kv");
        assert_eq!(path, "myapp/creds");
    }

    #[test]
    fn test_parse_path_default_mount() {
        let (mount, path) = OpenBaoProvider::parse_path("myapp/tls");
        assert_eq!(mount, "secret");
        assert_eq!(path, "myapp/tls");
    }

    #[test]
    fn test_openbao_auth_token_serialization() {
        let auth = OpenBaoAuth::Token {
            token: "test-token".into(),
        };
        let json = serde_json::to_string(&auth).unwrap();
        assert!(json.contains("\"method\":\"token\""));
    }

    #[test]
    fn test_openbao_auth_approle_serialization() {
        let auth = OpenBaoAuth::AppRole {
            role_id: "role123".into(),
            secret_id: "secret456".into(),
            mount: "approle".into(),
        };
        let json = serde_json::to_string(&auth).unwrap();
        assert!(json.contains("\"method\":\"app_role\""));
        assert!(json.contains("role_id"));
    }

    #[test]
    fn test_openbao_config_serialization() {
        let config = OpenBaoConfig {
            address: "https://vault.example.com:8200".into(),
            auth: OpenBaoAuth::Token {
                token: "test".into(),
            },
            namespace: Some("hypersec".into()),
            ca_cert: None,
            skip_verify: false,
        };
        let json = serde_json::to_string(&config).unwrap();
        assert!(json.contains("vault.example.com"));
    }
}