merka-vault 0.3.2

Vault provisioning and management crate integrating with merka-core
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
//! Vault initialization and unsealing operations.
//!
//! This module provides functions to initialize a new Vault server and
//! unseal it for use.

use crate::vault::status;
use crate::vault::VaultError;
#[cfg(any(test, feature = "full-api"))]
use anyhow::Context;
use anyhow::{anyhow, Result as AnyhowResult};
use reqwest::Client;
use serde::{Deserialize, Serialize};
use serde_json::json;
use tracing::{debug, error, info, warn};

/// Options for initializing Vault infrastructure.
#[derive(Debug, Serialize)]
pub struct InitOptions {
    pub secret_shares: u8,
    pub secret_threshold: u8,
}

/// Request structure for Vault initialization.
#[derive(Serialize)]
struct InitRequest {
    #[serde(skip_serializing_if = "Option::is_none")]
    secret_shares: Option<u8>,
    #[serde(skip_serializing_if = "Option::is_none")]
    secret_threshold: Option<u8>,
    #[serde(skip_serializing_if = "Option::is_none")]
    recovery_shares: Option<u8>,
    #[serde(skip_serializing_if = "Option::is_none")]
    recovery_threshold: Option<u8>,
}

/// Response structure for Vault initialization.
#[derive(Deserialize)]
struct InitResponse {
    #[serde(default)]
    keys: Vec<String>,
    #[serde(default)]
    keys_base64: Vec<String>,
    root_token: String,
    #[serde(default)]
    recovery_keys: Option<Vec<String>>,
    #[serde(default)]
    recovery_keys_base64: Option<Vec<String>>,
}

/// Response returned from Vault initialization.
#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct InitResult {
    /// Unseal keys generated during initialization.
    pub keys: Vec<String>,

    /// Unseal keys in Base64 format.
    pub keys_base64: Option<Vec<String>>,

    /// Recovery keys, used in auto-unseal mode.
    pub recovery_keys: Option<Vec<String>>,

    /// Recovery keys in Base64 format.
    pub recovery_keys_base64: Option<Vec<String>>,

    /// Root token for the initialized Vault.
    pub root_token: String,
}

/// Structure representing the result of an unseal operation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UnsealResult {
    pub sealed: bool,
    #[serde(rename = "t", default)]
    pub threshold: u8,
    #[serde(rename = "n", default)]
    pub shares: u8,
    #[serde(default)]
    pub progress: u8,
    #[serde(default)]
    pub success: bool,
}

// Initializes a new Vault server.
///
/// # Arguments
///
/// * `addr` - The Vault server's URL.
/// * `secret_shares` - Number of key shares to split the master key into.
/// * `secret_threshold` - Number of key shares required to reconstruct the master key.
/// * `recovery_shares` - Optional number of recovery key shares for auto-unseal setups.
/// * `recovery_threshold` - Optional number of recovery key threshold for auto-unseal setups.
///
/// # Returns
///
/// A `Result` containing the root token and unseal keys on success, or a `VaultError` on failure.
pub async fn init_vault(
    addr: &str,
    secret_shares: u8,
    secret_threshold: u8,
    recovery_shares: Option<u8>,
    recovery_threshold: Option<u8>,
) -> Result<InitResult, VaultError> {
    let client = Client::new();
    let req = InitRequest {
        secret_shares: if recovery_shares.is_some() {
            None
        } else {
            Some(secret_shares)
        },
        secret_threshold: if recovery_threshold.is_some() {
            None
        } else {
            Some(secret_threshold)
        },
        recovery_shares,
        recovery_threshold,
    };
    let req_url = format!("{}/v1/sys/init", addr);

    let resp = client.put(&req_url).json(&req).send().await?;

    if resp.status().is_success() {
        let init_resp: InitResponse = resp.json().await?;
        Ok(InitResult {
            root_token: init_resp.root_token,
            keys: init_resp.keys,
            keys_base64: Some(init_resp.keys_base64),
            recovery_keys: init_resp.recovery_keys,
            recovery_keys_base64: init_resp.recovery_keys_base64,
        })
    } else {
        Err(VaultError::ApiError(format!(
            "Failed to initialize Vault: {}",
            resp.text().await?
        )))
    }
}

/// Initializes Vault by calling the native sys/init endpoint.
///
/// # Arguments
///
/// * `addr` - The Vault server's URL
/// * `options` - Initialization options including shares and threshold
///
/// # Returns
///
/// A `Result` containing the initialization response or an error
#[cfg(any(test, feature = "full-api"))]
pub async fn initialize_vault_infrastructure(
    addr: &str,
    options: InitOptions,
) -> AnyhowResult<InitResult> {
    let client = Client::new();
    let url = format!("{}/v1/sys/init", addr);
    let payload = serde_json::json!({
        "secret_shares": options.secret_shares,
        "secret_threshold": options.secret_threshold
    });
    let response = client
        .put(&url)
        .json(&payload)
        .send()
        .await
        .with_context(|| "Failed to send init request")?;
    let result = response
        .json::<InitResult>()
        .await
        .with_context(|| "Failed to parse init response")?;
    Ok(result)
}

/// Unseals a Vault server.
///
/// # Arguments
///
/// * `addr` - The Vault server's URL.
/// * `keys` - The unseal keys to use.
///
/// # Returns
///
/// A `Result` indicating success or containing a `VaultError` on failure.
#[cfg(any(test, feature = "full-api"))]
pub async fn unseal_vault(addr: &str, keys: &[String]) -> Result<(), VaultError> {
    let client = Client::new();
    let req_url = format!("{}/v1/sys/unseal", addr);

    for key in keys {
        let resp = client
            .put(&req_url)
            .json(&json!({ "key": key }))
            .send()
            .await?;

        if !resp.status().is_success() {
            return Err(VaultError::ApiError(format!(
                "Failed to unseal Vault: {}",
                resp.text().await?
            )));
        }
    }

    Ok(())
}

/// Unseals Vault by sending each provided unseal key in sequence.
///
/// # Arguments
///
/// * `addr` - The Vault server's URL
/// * `keys` - Vector of unseal keys to use
///
/// # Returns
///
/// A `Result` containing the unseal status or an error
pub async fn unseal_root_vault(addr: &str, keys: Vec<String>) -> AnyhowResult<UnsealResult> {
    info!("Unsealing vault at {} with {} keys", addr, keys.len());
    let client = Client::new();
    let unseal_url = format!("{}/v1/sys/unseal", addr);

    // Check current seal status first
    match status::get_vault_status(addr).await {
        Ok(status) => {
            if !status.sealed {
                info!("Vault at {} is already unsealed", addr);
                return Ok(UnsealResult {
                    sealed: false,
                    threshold: 0,
                    shares: 0,
                    progress: 0,
                    success: true,
                });
            }
            info!("Vault is sealed, proceeding with unsealing...");
        }
        Err(e) => warn!("Could not check seal status: {}", e),
    }

    let mut last_result: Option<UnsealResult> = None;

    // Only use the threshold number of keys (normally 3)
    let threshold_keys = keys.iter().take(3);

    for key in threshold_keys {
        debug!("Sending unseal request with key to {}", unseal_url);
        let unseal_response = client
            .put(&unseal_url)
            .json(&json!({ "key": key }))
            .send()
            .await?;

        if !unseal_response.status().is_success() {
            let status = unseal_response.status();
            let text = unseal_response.text().await?;
            error!(
                "Unseal operation failed. Status: {}, Body: {}",
                status, text
            );
            return Err(anyhow!(
                "Failed to unseal Vault: HTTP {} - {}",
                status,
                text
            ));
        }

        let unseal_result: UnsealResult = unseal_response.json().await?;
        last_result = Some(unseal_result.clone());

        info!(
            "Unseal progress: sealed={}, threshold={}, shares={}, progress={}",
            unseal_result.sealed,
            unseal_result.threshold,
            unseal_result.shares,
            unseal_result.progress
        );

        if !unseal_result.sealed {
            info!("Vault unsealed successfully!");
            break;
        }
    }

    match last_result {
        Some(result) if !result.sealed => {
            info!("Vault unsealing completed successfully");
            Ok(result)
        }
        Some(result) => {
            warn!(
                "Vault still sealed after applying threshold keys. Progress: {}/{}",
                result.progress, result.threshold
            );
            Ok(result)
        }
        None => {
            error!("No unseal operations were performed");
            Err(anyhow!("No unseal operations were performed"))
        }
    }
}

#[cfg(any(test, feature = "full-api"))]
pub async fn initialize_vault(addr: &str) -> AnyhowResult<InitResult> {
    info!("Initializing vault at {}", addr);
    let client = Client::new();
    let init_url = format!("{}/v1/sys/init", addr);

    // Check if already initialized
    match status::get_vault_status(addr).await {
        Ok(status) if status.initialized => {
            info!("Vault at {} is already initialized", addr);
            return Err(anyhow!("Vault is already initialized"));
        }
        Ok(_) => info!("Vault not yet initialized, proceeding..."),
        Err(e) => warn!("Could not check initialization status: {}", e),
    }

    debug!("Sending initialization request to {}", init_url);
    let init_response = client
        .put(&init_url)
        .json(&json!({
            "secret_shares": 5,
            "secret_threshold": 3
        }))
        .send()
        .await?;

    if !init_response.status().is_success() {
        let status = init_response.status();
        let text = init_response.text().await?;
        error!(
            "Vault initialization failed. Status: {}, Body: {}",
            status, text
        );
        return Err(anyhow!(
            "Failed to initialize Vault: HTTP {} - {}",
            status,
            text
        ));
    }

    let init_result: InitResult = init_response.json().await?;
    info!(
        "Vault initialized successfully with {} keys",
        init_result.keys.len()
    );

    Ok(init_result)
}

/// Seals a Vault server.
///
/// # Arguments
///
/// * `addr` - The Vault server's URL.
/// * `token` - The authentication token.
///
/// # Returns
///
/// A `Result` indicating success or containing a `VaultError` on failure.
#[cfg(any(test, feature = "full-api"))]
pub async fn seal_vault(addr: &str, token: &str) -> Result<(), VaultError> {
    let client = Client::new();
    let req_url = format!("{}/v1/sys/seal", addr);

    let resp = client.post(&req_url).bearer_auth(token).send().await?;

    if !resp.status().is_success() {
        return Err(VaultError::ApiError(format!(
            "Failed to seal Vault: {}",
            resp.text().await?
        )));
    }

    Ok(())
}

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

    use crate::vault::test_utils::{setup_vault_container, wait_for_vault_ready, VaultMode};
    use tracing::info;

    #[tokio::test]
    async fn test_init_with_invalid_params() -> Result<(), Box<dyn std::error::Error>> {
        // Test with invalid parameters
        let vault_addr = "http://127.0.0.1:8200";

        // Try to initialize with invalid parameters (threshold > shares)
        let init_result = init_vault(
            vault_addr, 2, // secret_shares
            3, // secret_threshold (invalid: threshold > shares)
            None, None,
        )
        .await;

        // This should fail
        assert!(
            init_result.is_err(),
            "Should fail with invalid threshold > shares"
        );

        // Check for connection errors
        let init_result = init_vault(
            "http://127.0.0.1:9999", // Invalid address
            1,
            1,
            None,
            None,
        )
        .await;

        assert!(init_result.is_err(), "Should fail with connection error");

        Ok(())
    }

    /// Tests that initialization fails on a development mode Vault instance.
    /// Dev mode Vaults are pre-initialized and unsealed, so this test confirms that
    /// our init function properly detects this condition and returns an error.
    #[tokio::test]
    async fn test_vault_init_and_unseal_dev_mode() -> Result<(), Box<dyn std::error::Error>> {
        init_logging();
        let vault_container = setup_vault_container(VaultMode::Dev).await;
        let host = vault_container.get_host().await.unwrap();
        let host_port = vault_container.get_host_port_ipv4(8200).await.unwrap();
        let vault_url = format!("http://{}:{}", host, host_port);

        // Give the container more time to fully start up and be ready
        info!("Waiting for Vault dev container to start up...");
        tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;

        // Increase retries and timeout for container readiness
        wait_for_vault_ready(&vault_url, 30, 1000)
            .await
            .map_err(|e| e.to_string())?;

        // Dev mode vaults are already initialized, so this should fail
        assert!(init_vault(&vault_url, 1, 1, None, None).await.is_err());

        Ok(())
    }
}