-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathutils.rs
More file actions
277 lines (234 loc) · 10.1 KB
/
Copy pathutils.rs
File metadata and controls
277 lines (234 loc) · 10.1 KB
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
use std::{
collections::HashMap,
path::{Path, PathBuf},
};
use eyre::{Context, Result, bail};
use serde::de::DeserializeOwned;
use crate::{
config::{ADMIN_JWT_ENV, JWTS_ENV},
types::{BlsPublicKey, ModuleId},
};
pub fn load_env_var(env: &str) -> Result<String> {
std::env::var(env).wrap_err(format!("{env} is not set"))
}
pub fn load_optional_env_var(env: &str) -> Option<String> {
std::env::var(env).ok()
}
pub fn load_from_file<P: AsRef<Path> + std::fmt::Debug, T: DeserializeOwned>(
path: P,
) -> Result<(T, PathBuf)> {
let config_file = std::fs::read_to_string(path.as_ref())
.wrap_err(format!("Unable to find config file: {path:?}"))?;
match toml::from_str(&config_file).wrap_err("could not deserialize toml from string") {
Ok(config) => Ok((config, path.as_ref().to_path_buf())),
Err(e) => Err(e),
}
}
pub fn load_file_from_env<T: DeserializeOwned>(env: &str) -> Result<(T, PathBuf)> {
let path = std::env::var(env).wrap_err(format!("{env} is not set"))?;
load_from_file(&path)
}
/// Loads a map of module id -> jwt secret from a json env
pub fn load_jwt_secrets() -> Result<(String, HashMap<ModuleId, String>)> {
let admin_jwt = std::env::var(ADMIN_JWT_ENV).wrap_err(format!("{ADMIN_JWT_ENV} is not set"))?;
let jwt_secrets = std::env::var(JWTS_ENV).wrap_err(format!("{JWTS_ENV} is not set"))?;
decode_string_to_map(&jwt_secrets).map(|secrets| (admin_jwt, secrets))
}
/// Removes duplicate entries from a vector of BlsPublicKey
pub fn remove_duplicate_keys(keys: Vec<BlsPublicKey>) -> Vec<BlsPublicKey> {
let mut unique_keys = Vec::new();
let mut key_set = std::collections::HashSet::new();
for key in keys {
if key_set.insert(key.clone()) {
unique_keys.push(key);
}
}
unique_keys
}
pub fn decode_string_to_map(raw: &str) -> Result<HashMap<ModuleId, String>> {
// trim the string and split for comma
raw.trim()
.split(',')
.map(|pair| {
let mut parts = pair.trim().split('=');
match (parts.next(), parts.next()) {
(Some(key), Some(value)) => Ok((ModuleId(key.into()), value.into())),
_ => bail!("Invalid key-value pair: {pair}"),
}
})
.collect()
}
#[cfg(test)]
mod tests {
use std::sync::Mutex;
use super::*;
use crate::utils::TestRandomSeed;
// Serializes all tests that read/write environment variables.
// std::env::set_var is unsafe (Rust 1.81+) because mutating `environ`
// while another thread reads it is UB at the OS level. Holding this
// lock ensures our Rust threads don't race each other.
static ENV_LOCK: Mutex<()> = Mutex::new(());
/// Sets or removes env vars for the duration of `f`, then restores the
/// original values. Pass `Some("val")` to set, `None` to ensure absent.
fn with_env<R>(vars: &[(&str, Option<&str>)], f: impl FnOnce() -> R) -> R {
let _guard = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let saved: Vec<(&str, Option<String>)> =
vars.iter().map(|(k, _)| (*k, std::env::var(k).ok())).collect();
for (k, v) in vars {
match v {
Some(val) => unsafe { std::env::set_var(k, val) },
None => unsafe { std::env::remove_var(k) },
}
}
let result = f();
for (k, old) in &saved {
match old {
Some(v) => unsafe { std::env::set_var(k, v) },
None => unsafe { std::env::remove_var(k) },
}
}
result
}
// Minimal TOML-deserializable type used by load_from_file / load_file_from_env
// tests.
#[derive(serde::Deserialize, Debug, PartialEq)]
struct TestConfig {
value: String,
}
// ── decode_string_to_map ─────────────────────────────────────────────────
#[test]
fn test_decode_string_to_map_single_pair() {
let map = decode_string_to_map("ONLY=ONE").unwrap();
assert_eq!(map.len(), 1);
assert_eq!(map.get(&ModuleId("ONLY".into())), Some(&"ONE".to_string()));
}
#[test]
fn test_decode_string_to_map_empty_string() {
// An empty string yields one token with no `=`, which is invalid.
assert!(decode_string_to_map("").is_err());
}
#[test]
fn test_decode_string_to_map_malformed_no_equals() {
assert!(decode_string_to_map("KEYONLY").is_err());
}
// ── remove_duplicate_keys ────────────────────────────────────────────────
#[test]
fn test_remove_duplicate_keys() {
let key1 = BlsPublicKey::test_random();
let key2 = BlsPublicKey::test_random();
let keys = vec![key1.clone(), key2.clone(), key1.clone()];
let unique_keys = remove_duplicate_keys(keys);
assert_eq!(unique_keys.len(), 2);
assert!(unique_keys.contains(&key1));
assert!(unique_keys.contains(&key2));
}
// ── load_env_var ─────────────────────────────────────────────────────────
#[test]
fn test_load_env_var_present() {
with_env(&[("CB_TEST_LOAD_ENV_VAR", Some("hello"))], || {
assert_eq!(load_env_var("CB_TEST_LOAD_ENV_VAR").unwrap(), "hello");
});
}
#[test]
fn test_load_env_var_absent() {
with_env(&[("CB_TEST_LOAD_ENV_VAR_ABSENT", None)], || {
let err = load_env_var("CB_TEST_LOAD_ENV_VAR_ABSENT").unwrap_err();
assert!(err.to_string().contains("CB_TEST_LOAD_ENV_VAR_ABSENT"));
});
}
// ── load_optional_env_var ────────────────────────────────────────────────
#[test]
fn test_load_optional_env_var_present() {
with_env(&[("CB_TEST_OPT_VAR", Some("world"))], || {
assert_eq!(load_optional_env_var("CB_TEST_OPT_VAR"), Some("world".to_string()));
});
}
#[test]
fn test_load_optional_env_var_absent() {
with_env(&[("CB_TEST_OPT_VAR_ABSENT", None)], || {
assert_eq!(load_optional_env_var("CB_TEST_OPT_VAR_ABSENT"), None);
});
}
// ── load_from_file ───────────────────────────────────────────────────────
#[test]
fn test_load_from_file_valid() {
use std::io::Write as _;
let mut file = tempfile::NamedTempFile::new().unwrap();
file.write_all(b"value = \"hello\"").unwrap();
let path = file.path().to_path_buf();
let (config, returned_path): (TestConfig, _) = load_from_file(&path).unwrap();
assert_eq!(config.value, "hello");
assert_eq!(returned_path, path);
}
#[test]
fn test_load_from_file_missing() {
let result: eyre::Result<(TestConfig, _)> =
load_from_file("/nonexistent/cb_test_path/file.toml");
assert!(result.is_err());
}
#[test]
fn test_load_from_file_invalid_toml() {
use std::io::Write as _;
let mut file = tempfile::NamedTempFile::new().unwrap();
file.write_all(b"not valid toml !!!{{").unwrap();
let result: eyre::Result<(TestConfig, _)> = load_from_file(file.path());
assert!(result.is_err());
}
// ── load_file_from_env ───────────────────────────────────────────────────
#[test]
fn test_load_file_from_env_ok() {
use std::io::Write as _;
let mut file = tempfile::NamedTempFile::new().unwrap();
file.write_all(b"value = \"from_env\"").unwrap();
let path = file.path().to_str().unwrap().to_owned();
with_env(&[("CB_TEST_FILE_ENV", Some(&path))], || {
let (config, _): (TestConfig, _) = load_file_from_env("CB_TEST_FILE_ENV").unwrap();
assert_eq!(config.value, "from_env");
});
}
#[test]
fn test_load_file_from_env_var_not_set() {
with_env(&[("CB_TEST_FILE_ENV_ABSENT", None)], || {
let result: eyre::Result<(TestConfig, _)> =
load_file_from_env("CB_TEST_FILE_ENV_ABSENT");
assert!(result.is_err());
assert!(result.unwrap_err().to_string().contains("CB_TEST_FILE_ENV_ABSENT"));
});
}
// ── load_jwt_secrets ─────────────────────────────────────────────────────
#[test]
fn test_load_jwt_secrets_ok() {
with_env(
&[
(ADMIN_JWT_ENV, Some("admin_secret")),
(JWTS_ENV, Some("MODULE1=secret1,MODULE2=secret2")),
],
|| {
let (admin_jwt, secrets) = load_jwt_secrets().unwrap();
assert_eq!(admin_jwt, "admin_secret");
assert_eq!(secrets.get(&ModuleId("MODULE1".into())), Some(&"secret1".to_string()));
assert_eq!(secrets.get(&ModuleId("MODULE2".into())), Some(&"secret2".to_string()));
},
);
}
#[test]
fn test_load_jwt_secrets_missing_admin_jwt() {
with_env(&[(ADMIN_JWT_ENV, None), (JWTS_ENV, Some("MODULE1=secret1"))], || {
let err = load_jwt_secrets().unwrap_err();
assert!(err.to_string().contains(ADMIN_JWT_ENV));
});
}
#[test]
fn test_load_jwt_secrets_missing_jwts() {
with_env(&[(ADMIN_JWT_ENV, Some("admin_secret")), (JWTS_ENV, None)], || {
let err = load_jwt_secrets().unwrap_err();
assert!(err.to_string().contains(JWTS_ENV));
});
}
#[test]
fn test_load_jwt_secrets_malformed_jwts() {
with_env(&[(ADMIN_JWT_ENV, Some("admin_secret")), (JWTS_ENV, Some("MALFORMED"))], || {
assert!(load_jwt_secrets().is_err());
});
}
}