forked from jmgilman/vaultrs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kv2.rs
228 lines (195 loc) · 6.54 KB
/
kv2.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
#[macro_use]
extern crate tracing;
mod common;
use common::{VaultServer, VaultServerHelper};
use serde::{Deserialize, Serialize};
use test_log::test;
use vaultrs::api::kv2::requests::{SetSecretMetadataRequest, SetSecretRequestOptions};
use vaultrs::client::Client;
use vaultrs::error::ClientError;
use vaultrs::kv2;
#[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 set / read
test_list(&client, &endpoint).await;
test_read(&client, &endpoint).await;
test_read_metadata(&client, &endpoint).await;
test_read_version(&client, &endpoint).await;
test_set(&client, &endpoint).await;
test_set_with_compare_and_swap(&client, &endpoint).await;
test_set_metadata(&client, &endpoint).await;
// Test delete
test_delete_latest(&client, &endpoint).await;
test_undelete_versions(&client, &endpoint).await;
test_delete_versions(&client, &endpoint).await;
create(&client, &endpoint).await.unwrap();
test_destroy_versions(&client, &endpoint).await;
create(&client, &endpoint).await.unwrap();
test_delete_metadata(&client, &endpoint).await;
create(&client, &endpoint).await.unwrap();
// Test config
crate::config::test_set(&client, &endpoint).await;
crate::config::test_read(&client, &endpoint).await;
});
}
async fn test_delete_latest(client: &impl Client, endpoint: &SecretEndpoint) {
let res = kv2::delete_latest(client, endpoint.path.as_str(), endpoint.name.as_str()).await;
assert!(res.is_ok());
}
async fn test_delete_metadata(client: &impl Client, endpoint: &SecretEndpoint) {
let res = kv2::delete_metadata(client, endpoint.path.as_str(), endpoint.name.as_str()).await;
assert!(res.is_ok());
}
async fn test_delete_versions(client: &impl Client, endpoint: &SecretEndpoint) {
let res = kv2::delete_versions(
client,
endpoint.path.as_str(),
endpoint.name.as_str(),
vec![1],
)
.await;
assert!(res.is_ok());
}
async fn test_destroy_versions(client: &impl Client, endpoint: &SecretEndpoint) {
let res = kv2::destroy_versions(
client,
endpoint.path.as_str(),
endpoint.name.as_str(),
vec![1],
)
.await;
assert!(res.is_ok());
}
async fn test_list(client: &impl Client, endpoint: &SecretEndpoint) {
let res = kv2::list(client, endpoint.path.as_str(), "").await;
assert!(res.is_ok());
assert!(!res.unwrap().is_empty());
}
async fn test_read(client: &impl Client, endpoint: &SecretEndpoint) {
let res: Result<TestSecret, _> = kv2::read(client, endpoint.path.as_str(), "test").await;
assert!(res.is_ok());
assert_eq!(res.unwrap().key, endpoint.secret.key);
}
async fn test_read_metadata(client: &impl Client, endpoint: &SecretEndpoint) {
let res = kv2::read_metadata(client, endpoint.path.as_str(), endpoint.name.as_str()).await;
assert!(res.is_ok());
assert!(!res.unwrap().versions.is_empty());
}
async fn test_read_version(client: &impl Client, endpoint: &SecretEndpoint) {
let res: Result<TestSecret, _> =
kv2::read_version(client, endpoint.path.as_str(), "test", 1).await;
assert!(res.is_ok());
assert_eq!(res.unwrap().key, endpoint.secret.key);
}
async fn test_set(client: &impl Client, endpoint: &SecretEndpoint) {
let res = kv2::set(client, endpoint.path.as_str(), "test", &endpoint.secret).await;
assert!(res.is_ok());
}
async fn test_set_with_compare_and_swap(client: &impl Client, endpoint: &SecretEndpoint) {
let res = kv2::set_with_options(
client,
endpoint.path.as_str(),
"test-compare-and-swap",
&endpoint.secret,
SetSecretRequestOptions { cas: 0 },
)
.await;
assert!(res.is_ok());
let res = kv2::set_with_options(
client,
endpoint.path.as_str(),
"test-compare-and-swap",
&endpoint.secret,
SetSecretRequestOptions { cas: 0 },
)
.await;
assert!(res.is_err());
}
async fn test_set_metadata(client: &impl Client, endpoint: &SecretEndpoint) {
let res = kv2::set_metadata(
client,
endpoint.path.as_str(),
endpoint.name.as_str(),
Some(SetSecretMetadataRequest::builder().delete_version_after("1h")),
)
.await;
assert!(res.is_ok());
}
async fn test_undelete_versions(client: &impl Client, endpoint: &SecretEndpoint) {
let res = kv2::undelete_versions(
client,
endpoint.path.as_str(),
endpoint.name.as_str(),
vec![1],
)
.await;
assert!(res.is_ok());
}
mod config {
use crate::{Client, SecretEndpoint};
use vaultrs::{api::kv2::requests::SetConfigurationRequest, kv2::config};
pub async fn test_read(client: &impl Client, endpoint: &SecretEndpoint) {
let resp = config::read(client, endpoint.path.as_str()).await;
assert!(resp.is_ok());
}
pub async fn test_set(client: &impl Client, endpoint: &SecretEndpoint) {
let versions: u64 = 100;
let resp = config::set(
client,
endpoint.path.as_str(),
Some(
SetConfigurationRequest::builder()
.max_versions(versions)
.delete_version_after("768h"),
),
)
.await;
assert!(resp.is_ok());
}
}
#[derive(Debug)]
pub struct SecretEndpoint {
pub path: String,
pub name: String,
pub secret: TestSecret,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct TestSecret {
key: String,
password: String,
}
async fn create(client: &impl Client, endpoint: &SecretEndpoint) -> Result<(), ClientError> {
trace!(?endpoint, "creating kv2 secret");
kv2::set(
client,
endpoint.path.as_str(),
endpoint.name.as_str(),
&endpoint.secret,
)
.await?;
Ok(())
}
async fn setup(server: &VaultServer, client: &impl Client) -> Result<SecretEndpoint, ClientError> {
debug!("setting up kv2 auth engine");
let path = "secret_test";
let name = "test";
let secret = TestSecret {
key: "mykey".to_string(),
password: "supersecret".to_string(),
};
let endpoint = SecretEndpoint {
path: path.to_string(),
name: name.to_string(),
secret,
};
// Mount the KV2 engine
server.mount_secret(client, path, "kv-v2").await?;
// Create a test secret
create(client, &endpoint).await?;
Ok(endpoint)
}