-
Notifications
You must be signed in to change notification settings - Fork 85
/
proxy_conf.rs
305 lines (276 loc) · 8.58 KB
/
proxy_conf.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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// TODO these constant destinations are not final.
use std::env;
use std::path::{Path, PathBuf};
// Where the cache and socket are stored by default
pub const NETAVARK_PROXY_RUN_DIR: &str = "/run/podman";
pub const NETAVARK_PROXY_RUN_DIR_ENV: &str = "NETAVARK_PROXY_RUN_DIR_ENV";
// Default UDS path for gRPC to communicate on.
pub const DEFAULT_UDS_PATH: &str = "/run/podman/nv-proxy.sock";
// Default configuration directory.
pub const DEFAULT_CONFIG_DIR: &str = "";
// Default Network configuration path
pub const DEFAULT_NETWORK_CONFIG: &str = "/dev/stdin";
// Default epoll wait time before dhcp socket times out
pub const DEFAULT_TIMEOUT: u32 = 8;
// Proxy server gRPC socket file name
pub const PROXY_SOCK_NAME: &str = "nv-proxy.sock";
// Where leases are stored on the filesystem
pub const CACHE_FILE_NAME: &str = "nv-proxy.lease";
// Seconds until the service should exit
pub const DEFAULT_INACTIVITY_TIMEOUT: u64 = 300;
/// Get the RUN_DIR where the proxy cache and socket
/// are stored
///
///
/// # Arguments
///
/// * `run_cli`:
///
/// returns: String
///
/// # Examples
///
/// ```
///
/// ```
pub fn get_run_dir(run_cli: Option<&str>) -> String {
// if environment, return it
// if opt, return it
// return default
match env::var(NETAVARK_PROXY_RUN_DIR_ENV) {
// env::var returns an error if the key doesnt exist
Ok(v) => return v,
Err(_) => {
if let Some(val) = run_cli {
return val.to_string();
}
}
}
NETAVARK_PROXY_RUN_DIR.to_string()
}
/// Returns the fully qualified path of the proxy socket file including
/// the socket file name
///
/// # Arguments
///
/// * `run_dir_opt`:
///
/// returns: PathBuf
///
/// # Examples
///
/// ```
///
/// ```
pub fn get_proxy_sock_fqname(run_dir_opt: Option<&str>) -> PathBuf {
let run_dir = get_run_dir(run_dir_opt);
Path::new(&run_dir).join(PROXY_SOCK_NAME)
}
/// Returns the fully qualified path of the cache file including the cache
/// file name
///
///
/// # Arguments
///
/// * `run_dir`:
///
/// returns: PathBuf
///
/// # Examples
///
/// ```
///
/// ```
pub fn get_cache_fqname(run_dir: Option<&str>) -> PathBuf {
let run_dir = get_run_dir(run_dir);
Path::new(&run_dir).join(CACHE_FILE_NAME)
}
#[cfg(test)]
mod conf_tests {
use crate::dhcp_proxy::proxy_conf::{
get_cache_fqname, get_proxy_sock_fqname, get_run_dir, CACHE_FILE_NAME,
NETAVARK_PROXY_RUN_DIR, NETAVARK_PROXY_RUN_DIR_ENV, PROXY_SOCK_NAME,
};
use std::path::Path;
use std::collections::HashMap;
use std::env;
use std::ffi::OsStr;
use std::hash::Hash;
use std::panic::{self, RefUnwindSafe, UnwindSafe};
use std::sync::Mutex;
use once_cell::sync::Lazy;
use rand::distributions::Alphanumeric;
use rand::{thread_rng, Rng};
/// Make sure that the environment isn't modified concurrently.
static SERIAL_TEST: Lazy<Mutex<()>> = Lazy::new(Default::default);
///
/// The following stanzas of code should be attributed to https://github.com/vmx/temp-env
///
/// The previous value is restored when the closure completes or panics, before unwinding the
/// panic.
///
/// If `value` is set to `None`, then the environment variable is unset.
pub fn with_var<K, V, F, R>(key: K, value: Option<V>, closure: F) -> R
where
K: AsRef<OsStr> + Clone + Eq + Hash,
V: AsRef<OsStr> + Clone,
F: Fn() -> R + UnwindSafe + RefUnwindSafe,
{
with_vars(vec![(key, value)], closure)
}
/// Unsets a single environment variable for the duration of the closure.
///
/// The previous value is restored when the closure completes or panics, before unwinding the
/// panic.
///
/// This is a shorthand and identical to the following:
/// ```rust
/// temp_env::with_var("MY_ENV_VAR", None::<&str>, || {
/// // Run some code where `MY_ENV_VAR` is unset.
/// });
/// ```
pub fn with_var_unset<K, F, R>(key: K, closure: F) -> R
where
K: AsRef<OsStr> + Clone + Eq + Hash,
F: Fn() -> R + UnwindSafe + RefUnwindSafe,
{
with_var(key, None::<&str>, closure)
}
/// Sets environment variables for the duration of the closure.
///
/// The previous values are restored when the closure completes or panics, before unwinding the
/// panic.
///
/// If a `value` is set to `None`, then the environment variable is unset.
///
/// If the variable with the same name is set multiple times, the last one wins.
pub fn with_vars<K, V, F, R>(kvs: Vec<(K, Option<V>)>, closure: F) -> R
where
K: AsRef<OsStr> + Clone + Eq + Hash,
V: AsRef<OsStr> + Clone,
F: Fn() -> R + UnwindSafe + RefUnwindSafe,
{
let guard = SERIAL_TEST.lock().unwrap();
let mut old_kvs: HashMap<K, Option<String>> = HashMap::new();
for (key, value) in kvs {
// If the same key is given several times, the original/old value is only correct before
// the environment was updated.
if !old_kvs.contains_key(&key) {
let old_value = env::var(&key).ok();
old_kvs.insert(key.clone(), old_value);
}
update_env(&key, value);
}
match panic::catch_unwind(closure) {
Ok(result) => {
for (key, value) in old_kvs {
update_env(key, value);
}
result
}
Err(err) => {
for (key, value) in old_kvs {
update_env(key, value);
}
drop(guard);
panic::resume_unwind(err);
}
}
}
fn update_env<K, V>(key: K, value: Option<V>)
where
K: AsRef<OsStr>,
V: AsRef<OsStr>,
{
match value {
Some(v) => env::set_var(key, v),
None => env::remove_var(key),
}
}
fn random_string(len: usize) -> String {
let rand_string: String = thread_rng()
.sample_iter(&Alphanumeric)
.take(len)
.map(char::from)
.collect();
format!("/{rand_string}")
}
// The following tests seem to be susceptible to the environment variables poisoning
// each other when run in parallel (default rust behavior). If we set --test-threads=1,
// this will not happen. For now, I wrap the tests in `with_var_unset`.
#[test]
fn test_run_dir_env() {
let r = random_string(25);
with_var(NETAVARK_PROXY_RUN_DIR_ENV, Some(&r), || {
assert_eq!(get_run_dir(None), r)
});
}
#[test]
fn test_run_dir_with_opt() {
let r = random_string(25);
with_var_unset(NETAVARK_PROXY_RUN_DIR_ENV, || {
assert_eq!(get_run_dir(Some(&r)), r)
});
}
#[test]
fn test_run_dir_as_none() {
with_var_unset(NETAVARK_PROXY_RUN_DIR_ENV, || {
assert_eq!(get_run_dir(None), NETAVARK_PROXY_RUN_DIR)
});
}
#[test]
fn test_get_cache_env() {
let r = random_string(25);
with_var(NETAVARK_PROXY_RUN_DIR_ENV, Some(&r), || {
assert_eq!(get_cache_fqname(None), Path::new(&r).join(CACHE_FILE_NAME));
});
}
#[test]
fn test_get_cache_with_opt() {
let r = random_string(25);
with_var_unset(NETAVARK_PROXY_RUN_DIR_ENV, || {
assert_eq!(
get_cache_fqname(Some(&r)),
Path::new(&r).join(CACHE_FILE_NAME)
)
})
}
#[test]
fn test_get_cache_as_none() {
with_var_unset(NETAVARK_PROXY_RUN_DIR_ENV, || {
assert_eq!(
get_cache_fqname(None),
Path::new(NETAVARK_PROXY_RUN_DIR).join(CACHE_FILE_NAME)
)
});
}
#[test]
fn test_get_proxy_sock_env() {
let r = random_string(25);
with_var(NETAVARK_PROXY_RUN_DIR_ENV, Some(&r), || {
assert_eq!(
get_proxy_sock_fqname(None),
Path::new(&r).join(PROXY_SOCK_NAME)
);
});
}
#[test]
fn test_get_proxy_sock_with_opt() {
let r = random_string(25);
with_var_unset(NETAVARK_PROXY_RUN_DIR_ENV, || {
assert_eq!(
get_proxy_sock_fqname(Some(&r)),
Path::new(&r).join(PROXY_SOCK_NAME)
)
})
}
#[test]
fn test_get_proxy_sock_as_none() {
with_var_unset(NETAVARK_PROXY_RUN_DIR_ENV, || {
assert_eq!(
get_proxy_sock_fqname(None),
Path::new(NETAVARK_PROXY_RUN_DIR).join(PROXY_SOCK_NAME)
)
});
}
}