forked from jmgilman/vaultrs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
userpass.rs
125 lines (106 loc) · 3.42 KB
/
userpass.rs
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
#[macro_use]
extern crate tracing;
mod common;
use common::{VaultServer, VaultServerHelper};
use test_log::test;
use vaultrs::auth::userpass;
use vaultrs::client::Client;
use vaultrs::error::ClientError;
#[test]
fn test() {
let test = common::new_test();
test.run(|instance| async move {
let server: VaultServer = instance.server();
let client = server.client();
let endpoint = setup(&server, &client).await.unwrap();
// Test user
user::test_set(&client, &endpoint).await;
user::test_read(&client, &endpoint).await;
user::test_list(&client, &endpoint).await;
user::test_update_policies(&client, &endpoint).await;
// Test login
test_login(&client, &endpoint).await;
// Test update password and delete
user::test_update_password(&client, &endpoint).await;
user::test_delete(&client, &endpoint).await;
});
}
pub async fn test_login(client: &impl Client, endpoint: &UserPassEndpoint) {
let res = userpass::login(
client,
endpoint.path.as_str(),
endpoint.username.as_str(),
endpoint.password.as_str(),
)
.await;
assert!(res.is_ok());
}
pub mod user {
use super::{Client, UserPassEndpoint};
use vaultrs::auth::userpass::user;
pub async fn test_delete(client: &impl Client, endpoint: &UserPassEndpoint) {
let res = user::delete(client, endpoint.path.as_str(), endpoint.username.as_str()).await;
assert!(res.is_ok());
}
pub async fn test_list(client: &impl Client, endpoint: &UserPassEndpoint) {
let res = user::list(client, endpoint.path.as_str()).await;
assert!(res.is_ok());
}
pub async fn test_read(client: &impl Client, endpoint: &UserPassEndpoint) {
let res = user::read(client, endpoint.path.as_str(), endpoint.username.as_str()).await;
assert!(res.is_ok());
}
pub async fn test_set(client: &impl Client, endpoint: &UserPassEndpoint) {
let res = user::set(
client,
endpoint.path.as_str(),
endpoint.username.as_str(),
endpoint.password.as_str(),
None,
)
.await;
assert!(res.is_ok());
}
pub async fn test_update_password(client: &impl Client, endpoint: &UserPassEndpoint) {
let res = user::update_password(
client,
endpoint.path.as_str(),
endpoint.username.as_str(),
"This1sAT3st!!",
)
.await;
assert!(res.is_ok());
}
pub async fn test_update_policies(client: &impl Client, endpoint: &UserPassEndpoint) {
let res = user::update_policies(
client,
endpoint.path.as_str(),
endpoint.username.as_str(),
"default",
)
.await;
assert!(res.is_ok());
}
}
#[derive(Debug)]
pub struct UserPassEndpoint {
pub path: String,
pub username: String,
pub password: String,
}
async fn setup(
server: &VaultServer,
client: &impl Client,
) -> Result<UserPassEndpoint, ClientError> {
debug!("setting up UserPass auth engine");
let path = "userpass_test";
let username = "test";
let password = "This1sAT3st!";
// Mount the UserPass auth engine
server.mount_auth(client, path, "userpass").await?;
Ok(UserPassEndpoint {
path: path.to_string(),
username: username.to_string(),
password: password.to_string(),
})
}