Skip to main content

vault_client_rs/api/auth/
cert.rs

1use reqwest::Method;
2
3use crate::VaultClient;
4use crate::api::traits::CertAuthOperations;
5use crate::client::{encode_path, to_body};
6use crate::types::auth::{CertRoleInfo, CertRoleRequest};
7use crate::types::error::VaultError;
8use crate::types::response::AuthInfo;
9
10#[derive(Debug)]
11pub struct CertAuthHandler<'a> {
12    pub(crate) client: &'a VaultClient,
13    pub(crate) mount: String,
14}
15
16impl CertAuthOperations for CertAuthHandler<'_> {
17    async fn login(&self, name: Option<&str>) -> Result<AuthInfo, VaultError> {
18        let body = name.map(|n| serde_json::json!({ "name": n }));
19        let resp = self
20            .client
21            .exec_with_auth::<serde_json::Value>(
22                Method::POST,
23                &format!("auth/{}/login", self.mount),
24                body.as_ref(),
25            )
26            .await?;
27        let auth = resp.auth.ok_or(VaultError::EmptyResponse)?;
28        self.client.update_token_from_auth(&auth)?;
29        Ok(auth)
30    }
31
32    async fn create_role(&self, name: &str, params: &CertRoleRequest) -> Result<(), VaultError> {
33        let body = to_body(params)?;
34        self.client
35            .exec_empty(
36                Method::POST,
37                &format!("auth/{}/certs/{}", self.mount, encode_path(name)),
38                Some(&body),
39            )
40            .await
41    }
42
43    async fn read_role(&self, name: &str) -> Result<CertRoleInfo, VaultError> {
44        self.client
45            .exec_with_data(
46                Method::GET,
47                &format!("auth/{}/certs/{}", self.mount, encode_path(name)),
48                None,
49            )
50            .await
51    }
52
53    async fn delete_role(&self, name: &str) -> Result<(), VaultError> {
54        self.client
55            .exec_empty(
56                Method::DELETE,
57                &format!("auth/{}/certs/{}", self.mount, encode_path(name)),
58                None,
59            )
60            .await
61    }
62
63    async fn list_roles(&self) -> Result<Vec<String>, VaultError> {
64        self.client
65            .exec_list(&format!("auth/{}/certs", self.mount))
66            .await
67    }
68}