Skip to main content

vault_client_rs/api/auth/
aws.rs

1use reqwest::Method;
2
3use crate::VaultClient;
4use crate::api::traits::AwsAuthOperations;
5use crate::client::{encode_path, to_body};
6use crate::types::aws::*;
7use crate::types::error::VaultError;
8use crate::types::response::AuthInfo;
9
10#[derive(Debug)]
11pub struct AwsAuthHandler<'a> {
12    pub(crate) client: &'a VaultClient,
13    pub(crate) mount: String,
14}
15
16impl AwsAuthOperations for AwsAuthHandler<'_> {
17    async fn login(&self, params: &AwsAuthLoginRequest) -> Result<AuthInfo, VaultError> {
18        let body = to_body(params)?;
19        let resp = self
20            .client
21            .exec_with_auth::<serde_json::Value>(
22                Method::POST,
23                &format!("auth/{}/login", self.mount),
24                Some(&body),
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 configure(&self, config: &AwsAuthConfigRequest) -> Result<(), VaultError> {
33        let body = to_body(config)?;
34        self.client
35            .exec_empty(
36                Method::POST,
37                &format!("auth/{}/config/client", self.mount),
38                Some(&body),
39            )
40            .await
41    }
42
43    async fn read_config(&self) -> Result<AwsAuthConfig, VaultError> {
44        self.client
45            .exec_with_data(
46                Method::GET,
47                &format!("auth/{}/config/client", self.mount),
48                None,
49            )
50            .await
51    }
52
53    async fn create_role(&self, name: &str, params: &AwsAuthRoleRequest) -> Result<(), VaultError> {
54        let body = to_body(params)?;
55        self.client
56            .exec_empty(
57                Method::POST,
58                &format!("auth/{}/role/{}", self.mount, encode_path(name)),
59                Some(&body),
60            )
61            .await
62    }
63
64    async fn read_role(&self, name: &str) -> Result<AwsAuthRoleInfo, VaultError> {
65        self.client
66            .exec_with_data(
67                Method::GET,
68                &format!("auth/{}/role/{}", self.mount, encode_path(name)),
69                None,
70            )
71            .await
72    }
73
74    async fn delete_role(&self, name: &str) -> Result<(), VaultError> {
75        self.client
76            .exec_empty(
77                Method::DELETE,
78                &format!("auth/{}/role/{}", self.mount, encode_path(name)),
79                None,
80            )
81            .await
82    }
83
84    async fn list_roles(&self) -> Result<Vec<String>, VaultError> {
85        self.client
86            .exec_list(&format!("auth/{}/role", self.mount))
87            .await
88    }
89}