-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconfig.rs
More file actions
401 lines (349 loc) · 12 KB
/
config.rs
File metadata and controls
401 lines (349 loc) · 12 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
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
//! Configuration management for Arctic Wolf NFS Server
//!
//! Loads configuration from:
//! 1. CLI argument `--config <path>` (if provided)
//! 2. Default path `/etc/arcticwolf/config.toml` (falls back to defaults if not found)
use clap::Parser;
use serde::Deserialize;
use std::collections::HashMap;
use std::path::PathBuf;
const DEFAULT_CONFIG_PATH: &str = "/etc/arcticwolf/config.toml";
const DEFAULT_QUOTA_DB_PATH: &str = "/var/lib/arcticwolf/quota.db";
#[derive(Parser, Debug)]
#[command(name = "arcticwolf")]
#[command(about = "Arctic Wolf NFS Server", long_about = None)]
pub struct Cli {
/// Path to configuration file
#[arg(short, long)]
pub config: Option<PathBuf>,
}
#[derive(Debug, Clone, Deserialize, Default)]
#[serde(default)]
pub struct Config {
pub server: ServerConfig,
pub fsal: FsalConfig,
pub logging: LoggingConfig,
pub quota: QuotaConfig,
}
#[derive(Debug, Clone, Deserialize)]
#[serde(default)]
pub struct ServerConfig {
pub bind_address: String,
pub nfs_port: u16,
pub mount_port: u16,
}
#[derive(Debug, Clone, Deserialize)]
#[serde(default)]
pub struct FsalConfig {
pub backend: String,
pub export_path: PathBuf,
}
#[derive(Debug, Clone, Deserialize, Default)]
pub struct LoggingConfig {
/// Log level. If not set, falls back to RUST_LOG env var, then "info"
pub level: Option<String>,
}
#[derive(Debug, Clone, Deserialize)]
#[serde(default)]
pub struct QuotaConfig {
/// Enable quota enforcement
pub enabled: bool,
/// Path to the redb database file storing quota limits and usage
pub db_path: PathBuf,
/// Declarative quota bootstrap: map of first-level subdirectory name
/// to a size string (e.g. "10GB"). Each entry is applied at startup
/// only when the directory has no existing quota entry, so the
/// bootstrap is idempotent and safe across restarts.
pub bootstrap: HashMap<String, String>,
}
impl Default for QuotaConfig {
fn default() -> Self {
Self {
enabled: false,
db_path: PathBuf::from(DEFAULT_QUOTA_DB_PATH),
bootstrap: HashMap::new(),
}
}
}
/// Parse a human-readable size string into bytes
///
/// Supported suffixes (case-insensitive): B, KB, MB, GB, TB
/// Uses 1024-based units (KiB/MiB/GiB/TiB semantics). Plain numbers are treated as bytes.
///
/// # Examples
/// - "1024" -> 1024
/// - "10KB" -> 10240
/// - "5MB" -> 5242880
/// - "2GB" -> 2147483648
#[allow(dead_code)]
pub fn parse_size(s: &str) -> anyhow::Result<u64> {
let trimmed = s.trim();
if trimmed.is_empty() {
anyhow::bail!("Empty size string");
}
let upper = trimmed.to_uppercase();
let (num_part, multiplier): (&str, u64) = if let Some(prefix) = upper.strip_suffix("TB") {
(prefix, 1024u64.pow(4))
} else if let Some(prefix) = upper.strip_suffix("GB") {
(prefix, 1024u64.pow(3))
} else if let Some(prefix) = upper.strip_suffix("MB") {
(prefix, 1024u64.pow(2))
} else if let Some(prefix) = upper.strip_suffix("KB") {
(prefix, 1024)
} else if let Some(prefix) = upper.strip_suffix('B') {
(prefix, 1)
} else {
(upper.as_str(), 1)
};
let num_str = num_part.trim();
let num: u64 = num_str
.parse()
.map_err(|e| anyhow::anyhow!("Invalid size '{}': {}", s, e))?;
num.checked_mul(multiplier)
.ok_or_else(|| anyhow::anyhow!("Size overflow: {}", s))
}
impl Default for ServerConfig {
fn default() -> Self {
Self {
bind_address: "0.0.0.0".to_string(),
nfs_port: 2049,
mount_port: 0, // 0 = dynamic (OS-assigned)
}
}
}
impl Default for FsalConfig {
fn default() -> Self {
Self {
backend: "local".to_string(),
export_path: PathBuf::from("/tmp/nfs_exports"),
}
}
}
impl LoggingConfig {
/// Get log level with fallback: config -> RUST_LOG -> "info"
pub fn effective_level(&self) -> String {
match self.level.as_deref() {
Some(level) => level.to_string(),
None => std::env::var("RUST_LOG").unwrap_or_else(|_| "info".to_string()),
}
}
}
impl Config {
/// Load configuration from file or use defaults
pub fn load() -> anyhow::Result<Self> {
let cli = Cli::parse();
let (config_path, user_specified) = match cli.config {
Some(path) => (path, true),
None => (PathBuf::from(DEFAULT_CONFIG_PATH), false),
};
if config_path.exists() {
let content = std::fs::read_to_string(&config_path)?;
let config: Config = toml::from_str(&content)?;
println!(" Config: {}", config_path.display());
Ok(config)
} else if user_specified {
// User specified --config but file doesn't exist
anyhow::bail!("Configuration file not found: {}", config_path.display());
} else {
// Default path doesn't exist, use defaults
println!(" Config: using defaults");
Ok(Config::default())
}
}
/// Get the bind address for a specific port
pub fn bind_addr_for(&self, port: u16) -> String {
format!("{}:{}", self.server.bind_address, port)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_server_config_default() {
let config = ServerConfig::default();
assert_eq!(config.bind_address, "0.0.0.0");
assert_eq!(config.nfs_port, 2049);
assert_eq!(config.mount_port, 0);
}
#[test]
fn test_fsal_config_default() {
let config = FsalConfig::default();
assert_eq!(config.backend, "local");
assert_eq!(config.export_path, PathBuf::from("/tmp/nfs_exports"));
}
#[test]
fn test_logging_config_default() {
let config = LoggingConfig::default();
assert!(config.level.is_none());
}
#[test]
fn test_config_default() {
let config = Config::default();
assert_eq!(config.server.bind_address, "0.0.0.0");
assert_eq!(config.server.nfs_port, 2049);
assert_eq!(config.server.mount_port, 0);
assert_eq!(config.fsal.backend, "local");
assert_eq!(config.fsal.export_path, PathBuf::from("/tmp/nfs_exports"));
assert!(config.logging.level.is_none());
}
#[test]
fn test_bind_addr_for() {
let config = Config::default();
assert_eq!(config.bind_addr_for(2049), "0.0.0.0:2049");
assert_eq!(config.bind_addr_for(111), "0.0.0.0:111");
let mut custom = Config::default();
custom.server.bind_address = "127.0.0.1".to_string();
assert_eq!(custom.bind_addr_for(2049), "127.0.0.1:2049");
}
#[test]
fn test_effective_level_with_config() {
let config = LoggingConfig {
level: Some("debug".to_string()),
};
assert_eq!(config.effective_level(), "debug");
}
#[test]
fn test_effective_level_fallback() {
// Determine expected level based on current environment without mutating it
let expected = std::env::var("RUST_LOG").unwrap_or_else(|_| "info".to_string());
let config = LoggingConfig { level: None };
assert_eq!(config.effective_level(), expected);
}
#[test]
fn test_parse_full_toml() {
let toml = r#"
[server]
bind_address = "192.168.1.100"
nfs_port = 2049
mount_port = 20048
[fsal]
backend = "local"
export_path = "/data/exports"
[logging]
level = "trace"
"#;
let config: Config = toml::from_str(toml).expect("Failed to parse TOML");
assert_eq!(config.server.bind_address, "192.168.1.100");
assert_eq!(config.server.nfs_port, 2049);
assert_eq!(config.server.mount_port, 20048);
assert_eq!(config.fsal.backend, "local");
assert_eq!(config.fsal.export_path, PathBuf::from("/data/exports"));
assert_eq!(config.logging.level, Some("trace".to_string()));
}
#[test]
fn test_parse_partial_toml() {
// Only specify server section, others should use defaults
let toml = r#"
[server]
nfs_port = 8000
"#;
let config: Config = toml::from_str(toml).expect("Failed to parse TOML");
assert_eq!(config.server.bind_address, "0.0.0.0"); // default
assert_eq!(config.server.nfs_port, 8000); // custom
assert_eq!(config.server.mount_port, 0); // default
assert_eq!(config.fsal.backend, "local"); // default
assert_eq!(config.fsal.export_path, PathBuf::from("/tmp/nfs_exports")); // default
assert!(config.logging.level.is_none()); // default
}
#[test]
fn test_parse_empty_toml() {
let config: Config = toml::from_str("").expect("Failed to parse empty TOML");
assert_eq!(config.server.bind_address, "0.0.0.0");
assert_eq!(config.server.nfs_port, 2049);
assert_eq!(config.server.mount_port, 0);
assert_eq!(config.fsal.backend, "local");
}
#[test]
fn test_parse_invalid_toml() {
let result: Result<Config, _> = toml::from_str("this is not valid toml [[[");
assert!(result.is_err());
}
#[test]
fn test_quota_config_default() {
let config = QuotaConfig::default();
assert!(!config.enabled);
assert_eq!(config.db_path, PathBuf::from(DEFAULT_QUOTA_DB_PATH));
}
#[test]
fn test_config_default_includes_quota() {
let config = Config::default();
assert!(!config.quota.enabled);
}
#[test]
fn test_parse_quota_toml() {
let toml = r#"
[quota]
enabled = true
db_path = "/tmp/quota.db"
"#;
let config: Config = toml::from_str(toml).expect("Failed to parse TOML");
assert!(config.quota.enabled);
assert_eq!(config.quota.db_path, PathBuf::from("/tmp/quota.db"));
assert!(config.quota.bootstrap.is_empty());
}
#[test]
fn test_parse_quota_bootstrap_toml() {
let toml = r#"
[quota]
enabled = true
db_path = "/tmp/quota.db"
[quota.bootstrap]
"pvc-a" = "10GB"
"pvc-b" = "1MB"
"#;
let config: Config = toml::from_str(toml).expect("Failed to parse TOML");
assert_eq!(config.quota.bootstrap.len(), 2);
assert_eq!(
config.quota.bootstrap.get("pvc-a"),
Some(&"10GB".to_string())
);
assert_eq!(
config.quota.bootstrap.get("pvc-b"),
Some(&"1MB".to_string())
);
}
#[test]
fn test_parse_size_bytes() {
assert_eq!(parse_size("0").unwrap(), 0);
assert_eq!(parse_size("1").unwrap(), 1);
assert_eq!(parse_size("1024").unwrap(), 1024);
assert_eq!(parse_size("512B").unwrap(), 512);
}
#[test]
fn test_parse_size_kb() {
assert_eq!(parse_size("1KB").unwrap(), 1024);
assert_eq!(parse_size("10KB").unwrap(), 10 * 1024);
assert_eq!(parse_size("1kb").unwrap(), 1024); // case-insensitive
}
#[test]
fn test_parse_size_mb() {
assert_eq!(parse_size("1MB").unwrap(), 1024 * 1024);
assert_eq!(parse_size("5MB").unwrap(), 5 * 1024 * 1024);
}
#[test]
fn test_parse_size_gb() {
assert_eq!(parse_size("1GB").unwrap(), 1024u64.pow(3));
assert_eq!(parse_size("10GB").unwrap(), 10 * 1024u64.pow(3));
}
#[test]
fn test_parse_size_tb() {
assert_eq!(parse_size("1TB").unwrap(), 1024u64.pow(4));
assert_eq!(parse_size("2TB").unwrap(), 2 * 1024u64.pow(4));
}
#[test]
fn test_parse_size_whitespace() {
assert_eq!(parse_size(" 10MB ").unwrap(), 10 * 1024 * 1024);
}
#[test]
fn test_parse_size_invalid() {
assert!(parse_size("").is_err());
assert!(parse_size("abc").is_err());
assert!(parse_size("10XB").is_err());
assert!(parse_size("-5MB").is_err());
}
#[test]
fn test_parse_size_overflow() {
// u64::MAX / 1024^4 is ~16 million TB
assert!(parse_size("99999999999999999TB").is_err());
}
}