Skip to main content

hc_vault/kv2/
configure.rs

1use crate::Auth;
2use crate::Client;
3use crate::Error;
4
5use serde::{Deserialize, Serialize};
6
7/// Configuration describes the configuration for a single kv2-mount
8#[derive(Serialize, Deserialize, Debug)]
9pub struct Configuration {
10    /// Whether or not all keys are required to have the 'cas' option
11    /// set when updating/writing to them
12    #[serde(skip_serializing_if = "Option::is_none")]
13    pub cas_required: Option<bool>,
14
15    /// If set, this specifies the duration for which a version is held,
16    /// older versions than described will be dropped
17    #[serde(skip_serializing_if = "Option::is_none")]
18    pub delete_version_after: Option<String>,
19
20    /// The Number of Versions that should be kept at any given time
21    /// if this number is exceeded, the oldest versions are dropped
22    #[serde(skip_serializing_if = "Option::is_none")]
23    pub max_versions: Option<u32>,
24}
25
26impl PartialEq for Configuration {
27    fn eq(&self, other: &Self) -> bool {
28        self.cas_required == other.cas_required
29            && self.delete_version_after == other.delete_version_after
30            && self.max_versions == other.max_versions
31    }
32}
33
34#[derive(Deserialize)]
35struct ConfigurationResponse {
36    data: Configuration,
37}
38
39/// This function is used to configure the given kv2-mount with the provided
40/// configuration options
41///
42/// [Vault-Documentation](https://www.vaultproject.io/api-docs/secret/kv/kv-v2#configure-the-kv-engine)
43pub async fn configure(
44    client: &Client<impl Auth>,
45    mount: &str,
46    config: &Configuration,
47) -> Result<(), Error> {
48    let path = format!("{}/config", mount);
49
50    match client
51        .vault_request::<Configuration>(reqwest::Method::POST, &path, Some(config))
52        .await
53    {
54        Err(e) => Err(e),
55        Ok(_) => Ok(()),
56    }
57}
58
59/// Is used to load the current configuration of the kv2-backend mounted
60/// at the given mount point
61///
62/// [Vault-Documentation](https://www.vaultproject.io/api-docs/secret/kv/kv-v2#read-kv-engine-configuration)
63pub async fn get_configuration(
64    client: &Client<impl Auth>,
65    mount: &str,
66) -> Result<Configuration, Error> {
67    let path = format!("{}/config", mount);
68
69    let resp = match client
70        .vault_request::<String>(reqwest::Method::GET, &path, None)
71        .await
72    {
73        Err(e) => return Err(e),
74        Ok(r) => r,
75    };
76
77    let resp_body = match resp.json::<ConfigurationResponse>().await {
78        Err(e) => return Err(Error::from(e)),
79        Ok(res) => res,
80    };
81
82    Ok(resp_body.data)
83}