forked from jmgilman/vaultrs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
approle.rs
234 lines (202 loc) · 7.16 KB
/
approle.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
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
#[macro_use]
extern crate tracing;
mod common;
use common::{VaultServer, VaultServerHelper};
use test_log::test;
use vaultrs::auth::approle;
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 roles
crate::role::test_set(&client, &endpoint).await;
crate::role::test_read(&client, &endpoint).await;
crate::role::test_list(&client, &endpoint).await;
crate::role::test_read_id(&client, &endpoint).await;
crate::role::test_update_id(&client, &endpoint).await;
// Test secret IDs
let (id, accessor) = crate::role::secret::test_generate(&client, &endpoint).await;
crate::role::secret::test_read(&client, &endpoint, id.as_str()).await;
crate::role::secret::test_read_accessor(&client, &endpoint, accessor.as_str()).await;
crate::role::secret::test_list(&client, &endpoint).await;
crate::role::secret::test_delete_accessor(&client, &endpoint, accessor.as_str()).await;
crate::role::secret::test_custom(&client, &endpoint).await;
crate::role::secret::test_delete(&client, &endpoint, "test").await;
// Test auth
test_login(&client, &endpoint).await;
crate::role::test_delete(&client, &endpoint).await;
})
}
pub async fn test_login(client: &impl Client, endpoint: &AppRoleEndpoint) {
use vaultrs::auth::approle::role;
let role_id_resp =
role::read_id(client, endpoint.path.as_str(), endpoint.role_name.as_str()).await;
assert!(role_id_resp.is_ok());
let role_id = role_id_resp.unwrap().role_id;
let secret_id_resp = role::secret::generate(
client,
endpoint.path.as_str(),
endpoint.role_name.as_str(),
None,
)
.await;
assert!(secret_id_resp.is_ok());
let secret_id = secret_id_resp.unwrap().secret_id;
let resp = approle::login(
client,
endpoint.path.as_str(),
role_id.as_str(),
secret_id.as_str(),
)
.await;
assert!(resp.is_ok());
}
mod role {
use super::{AppRoleEndpoint, Client};
use vaultrs::{api::auth::approle::requests::SetAppRoleRequest, auth::approle::role};
pub async fn test_delete(client: &impl Client, endpoint: &AppRoleEndpoint) {
let res = role::delete(client, endpoint.path.as_str(), endpoint.role_name.as_str()).await;
assert!(res.is_ok());
}
pub async fn test_list(client: &impl Client, endpoint: &AppRoleEndpoint) {
let res = role::list(client, endpoint.path.as_str()).await;
assert!(res.is_ok());
}
pub async fn test_read(client: &impl Client, endpoint: &AppRoleEndpoint) {
let res = role::read(client, endpoint.path.as_str(), endpoint.role_name.as_str()).await;
assert!(res.is_ok());
}
pub async fn test_set(client: &impl Client, endpoint: &AppRoleEndpoint) {
let res = role::set(
client,
endpoint.path.as_str(),
endpoint.role_name.as_str(),
Some(&mut SetAppRoleRequest::builder().token_ttl("10m")),
)
.await;
assert!(res.is_ok());
}
pub async fn test_read_id(client: &impl Client, endpoint: &AppRoleEndpoint) {
let res = role::read_id(client, endpoint.path.as_str(), endpoint.role_name.as_str()).await;
assert!(res.is_ok());
}
pub async fn test_update_id(client: &impl Client, endpoint: &AppRoleEndpoint) {
let res = role::update_id(
client,
endpoint.path.as_str(),
endpoint.role_name.as_str(),
"test",
)
.await;
assert!(res.is_ok());
}
pub mod secret {
use super::{AppRoleEndpoint, Client};
use vaultrs::{
api::auth::approle::requests::GenerateNewSecretIDRequest, auth::approle::role::secret,
};
pub async fn test_custom(client: &impl Client, endpoint: &AppRoleEndpoint) {
let res = secret::custom(
client,
endpoint.path.as_str(),
endpoint.role_name.as_str(),
"test",
None,
)
.await;
assert!(res.is_ok());
}
pub async fn test_delete(client: &impl Client, endpoint: &AppRoleEndpoint, id: &str) {
let res = secret::delete(
client,
endpoint.path.as_str(),
endpoint.role_name.as_str(),
id,
)
.await;
assert!(res.is_ok());
}
pub async fn test_delete_accessor(
client: &impl Client,
endpoint: &AppRoleEndpoint,
accessor: &str,
) {
let res = secret::delete_accessor(
client,
endpoint.path.as_str(),
endpoint.role_name.as_str(),
accessor,
)
.await;
assert!(res.is_ok());
}
pub async fn test_generate(
client: &impl Client,
endpoint: &AppRoleEndpoint,
) -> (String, String) {
let res = secret::generate(
client,
endpoint.path.as_str(),
endpoint.role_name.as_str(),
Some(
&mut GenerateNewSecretIDRequest::builder()
.metadata("{ \"tag1\": \"production\" }"),
),
)
.await;
assert!(res.is_ok());
let id = res.unwrap();
(id.secret_id, id.secret_id_accessor)
}
pub async fn test_list(client: &impl Client, endpoint: &AppRoleEndpoint) {
let res =
secret::list(client, endpoint.path.as_str(), endpoint.role_name.as_str()).await;
assert!(res.is_ok());
}
pub async fn test_read(client: &impl Client, endpoint: &AppRoleEndpoint, id: &str) {
let res = secret::read(
client,
endpoint.path.as_str(),
endpoint.role_name.as_str(),
id,
)
.await;
assert!(res.is_ok());
}
pub async fn test_read_accessor(
client: &impl Client,
endpoint: &AppRoleEndpoint,
accessor: &str,
) {
let res = secret::read_accessor(
client,
endpoint.path.as_str(),
endpoint.role_name.as_str(),
accessor,
)
.await;
assert!(res.is_ok());
}
}
}
#[derive(Debug)]
pub struct AppRoleEndpoint {
pub path: String,
pub role_name: String,
}
async fn setup(server: &VaultServer, client: &impl Client) -> Result<AppRoleEndpoint, ClientError> {
debug!("setting up AppRole auth engine");
let path = "approle_test";
let role_name = "test";
// Mount the AppRole auth engine
server.mount_auth(client, path, "approle").await?;
Ok(AppRoleEndpoint {
path: path.to_string(),
role_name: role_name.to_string(),
})
}