ironmonitor 7.1.0

IronMonitor: hardware monitoring for CPUs, GPUs, NPUs, memory, I/O and network silicon on Linux, Windows and macOS, which reports what it cannot read as well as what it can
Documentation
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
//! Permission System - MCP-like Capability-Based Access Control
//!
//! This module implements a permission system inspired by the Model Context Protocol (MCP)
//! for controlling access to system observability data.
//!
//! # Permission Model
//!
//! Permissions are structured hierarchically:
//! - **Capability**: Broad category (e.g., "gpu", "process", "network")
//! - **Scope**: Specific action within capability (e.g., "read", "control", "kill")
//! - **Resource**: Optional resource filter (e.g., "gpu:0", "process:1234")
//!
//! # Configuration
//!
//! Permissions are configured via TOML/JSON similar to MCP server configs:
//!
//! ```toml
//! [api]
//! enabled = true
//! port = 8080
//! require_auth = true
//!
//! [[api.keys]]
//! name = "ai-agent-1"
//! key = "sk-xxxx"
//! capabilities = ["gpu:read", "cpu:read", "memory:read"]
//! rate_limit = 100  # requests per minute
//!
//! [[api.keys]]
//! name = "admin"
//! key = "sk-admin"
//! capabilities = ["*"]  # Full access
//! ```

use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::time::{Duration, Instant};

/// Capability categories for system access
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Capability {
    // Hardware monitoring (read-only)
    /// GPU metrics and information
    Gpu,
    /// CPU metrics and information
    Cpu,
    /// Memory metrics and information
    Memory,
    /// Disk metrics and information
    Disk,
    /// Network metrics and information
    Network,
    /// Motherboard/hardware sensors
    Motherboard,
    /// Power supply information
    Power,
    /// Fan information
    Fan,

    // Process management
    /// Process listing and metrics
    Process,
    /// Connection/socket information
    Connections,
    /// Service status
    Services,

    // System information
    /// System identification info
    SystemInfo,
    /// Boot configuration
    BootConfig,
    /// Driver information
    Drivers,

    // Advanced features
    /// Historical data queries
    History,
    /// Real-time event streaming
    Events,
    /// System context materialization
    Context,

    // Control capabilities (dangerous)
    /// Fan control
    FanControl,
    /// Process control (kill, priority)
    ProcessControl,
    /// GPU control (clocks, power limits)
    GpuControl,
    /// System power control (shutdown, reboot)
    PowerControl,

    // Administrative
    /// API management
    Admin,
}

impl Capability {
    /// Check if this is a read-only capability
    pub fn is_read_only(&self) -> bool {
        matches!(
            self,
            Capability::Gpu
                | Capability::Cpu
                | Capability::Memory
                | Capability::Disk
                | Capability::Network
                | Capability::Motherboard
                | Capability::Power
                | Capability::Fan
                | Capability::Process
                | Capability::Connections
                | Capability::Services
                | Capability::SystemInfo
                | Capability::BootConfig
                | Capability::Drivers
                | Capability::History
                | Capability::Events
                | Capability::Context
        )
    }

    /// Check if this is a control capability (potentially dangerous)
    pub fn is_control(&self) -> bool {
        matches!(
            self,
            Capability::FanControl
                | Capability::ProcessControl
                | Capability::GpuControl
                | Capability::PowerControl
        )
    }

    /// Get all capabilities
    pub fn all() -> Vec<Capability> {
        vec![
            Capability::Gpu,
            Capability::Cpu,
            Capability::Memory,
            Capability::Disk,
            Capability::Network,
            Capability::Motherboard,
            Capability::Power,
            Capability::Fan,
            Capability::Process,
            Capability::Connections,
            Capability::Services,
            Capability::SystemInfo,
            Capability::BootConfig,
            Capability::Drivers,
            Capability::History,
            Capability::Events,
            Capability::Context,
            Capability::FanControl,
            Capability::ProcessControl,
            Capability::GpuControl,
            Capability::PowerControl,
            Capability::Admin,
        ]
    }

    /// Get read-only capabilities (safe for external AI)
    pub fn read_only_set() -> HashSet<Capability> {
        Capability::all()
            .into_iter()
            .filter(|c| c.is_read_only())
            .collect()
    }
}

impl std::fmt::Display for Capability {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let name = match self {
            Capability::Gpu => "gpu",
            Capability::Cpu => "cpu",
            Capability::Memory => "memory",
            Capability::Disk => "disk",
            Capability::Network => "network",
            Capability::Motherboard => "motherboard",
            Capability::Power => "power",
            Capability::Fan => "fan",
            Capability::Process => "process",
            Capability::Connections => "connections",
            Capability::Services => "services",
            Capability::SystemInfo => "system_info",
            Capability::BootConfig => "boot_config",
            Capability::Drivers => "drivers",
            Capability::History => "history",
            Capability::Events => "events",
            Capability::Context => "context",
            Capability::FanControl => "fan_control",
            Capability::ProcessControl => "process_control",
            Capability::GpuControl => "gpu_control",
            Capability::PowerControl => "power_control",
            Capability::Admin => "admin",
        };
        write!(f, "{}", name)
    }
}

impl std::str::FromStr for Capability {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "gpu" => Ok(Capability::Gpu),
            "cpu" => Ok(Capability::Cpu),
            "memory" => Ok(Capability::Memory),
            "disk" => Ok(Capability::Disk),
            "network" => Ok(Capability::Network),
            "motherboard" => Ok(Capability::Motherboard),
            "power" => Ok(Capability::Power),
            "fan" => Ok(Capability::Fan),
            "process" => Ok(Capability::Process),
            "connections" => Ok(Capability::Connections),
            "services" => Ok(Capability::Services),
            "system_info" | "systeminfo" => Ok(Capability::SystemInfo),
            "boot_config" | "bootconfig" => Ok(Capability::BootConfig),
            "drivers" => Ok(Capability::Drivers),
            "history" => Ok(Capability::History),
            "events" => Ok(Capability::Events),
            "context" => Ok(Capability::Context),
            "fan_control" | "fancontrol" => Ok(Capability::FanControl),
            "process_control" | "processcontrol" => Ok(Capability::ProcessControl),
            "gpu_control" | "gpucontrol" => Ok(Capability::GpuControl),
            "power_control" | "powercontrol" => Ok(Capability::PowerControl),
            "admin" => Ok(Capability::Admin),
            "*" | "all" => Ok(Capability::Admin), // Admin grants all
            _ => Err(format!("Unknown capability: {}", s)),
        }
    }
}

/// Permission scope within a capability
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[derive(Default)]
pub enum Scope {
    /// Read access only
    #[default]
    Read,
    /// Write/control access
    Write,
    /// Full access
    All,
    /// Specific resource (e.g., "gpu:0", "process:1234")
    Resource(String),
}

/// A single permission grant
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Permission {
    /// Capability being granted
    pub capability: Capability,
    /// Scope of the permission
    pub scope: Scope,
    /// Optional resource filter
    pub resource: Option<String>,
}

impl Permission {
    /// Create a read permission for a capability
    pub fn read(capability: Capability) -> Self {
        Self {
            capability,
            scope: Scope::Read,
            resource: None,
        }
    }

    /// Create a full permission for a capability
    pub fn full(capability: Capability) -> Self {
        Self {
            capability,
            scope: Scope::All,
            resource: None,
        }
    }

    /// Create a permission for a specific resource
    pub fn resource(capability: Capability, resource: impl Into<String>) -> Self {
        let resource_str = resource.into();
        Self {
            capability,
            scope: Scope::Resource(resource_str.clone()),
            resource: Some(resource_str),
        }
    }

    /// Parse from string format "capability:scope" or just "capability"
    pub fn parse(s: &str) -> Result<Self, String> {
        let parts: Vec<&str> = s.split(':').collect();
        match parts.len() {
            1 => {
                let cap = parts[0].parse()?;
                Ok(Permission::read(cap))
            }
            2 => {
                let cap = parts[0].parse()?;
                let scope = match parts[1].to_lowercase().as_str() {
                    "read" | "r" => Scope::Read,
                    "write" | "w" => Scope::Write,
                    "all" | "*" => Scope::All,
                    resource => Scope::Resource(resource.to_string()),
                };
                Ok(Permission {
                    capability: cap,
                    scope,
                    resource: None,
                })
            }
            _ => Err(format!("Invalid permission format: {}", s)),
        }
    }
}

impl std::fmt::Display for Permission {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match &self.scope {
            Scope::Read => write!(f, "{}:read", self.capability),
            Scope::Write => write!(f, "{}:write", self.capability),
            Scope::All => write!(f, "{}:*", self.capability),
            Scope::Resource(r) => write!(f, "{}:{}", self.capability, r),
        }
    }
}

/// Rate limit configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RateLimit {
    /// Maximum requests per period
    pub max_requests: u32,
    /// Period duration in seconds
    pub period_secs: u64,
}

impl RateLimit {
    /// Get period as Duration
    pub fn period(&self) -> Duration {
        Duration::from_secs(self.period_secs)
    }
}

impl Default for RateLimit {
    fn default() -> Self {
        Self {
            max_requests: 100,
            period_secs: 60, // 100 req/min
        }
    }
}

/// API key configuration
///
/// `Debug` is written by hand so that the secret never reaches a log line, a panic
/// message or a `{:?}` in someone else's code. See the `Debug` impl below.
#[derive(Clone, Serialize, Deserialize)]
pub struct ApiKey {
    /// Human-readable name
    pub name: String,
    /// The actual key (should be hashed in production)
    pub key: String,
    /// Granted permissions (parsed from strings like "gpu:read")
    #[serde(default)]
    pub permissions: Vec<Permission>,
    /// Legacy capability strings (for backward compatibility)
    #[serde(default)]
    pub capabilities: Vec<String>,
    /// Rate limit override
    pub rate_limit: Option<RateLimit>,
    /// Whether this key is enabled
    #[serde(default = "default_true")]
    pub enabled: bool,
    /// Expiration time (optional)
    pub expires_at: Option<String>,
    /// Description/notes
    pub description: Option<String>,
}

fn default_true() -> bool {
    true
}

impl std::fmt::Debug for ApiKey {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        // Every field except the secret itself. The key is replaced by its length, so a
        // log line still shows that a key was present without showing what it was.
        f.debug_struct("ApiKey")
            .field("name", &self.name)
            .field("key", &format_args!("<redacted {} bytes>", self.key.len()))
            .field("permissions", &self.permissions)
            .field("capabilities", &self.capabilities)
            .field("rate_limit", &self.rate_limit)
            .field("enabled", &self.enabled)
            .field("expires_at", &self.expires_at)
            .field("description", &self.description)
            .finish()
    }
}

impl ApiKey {
    /// Create a new API key with specified permissions
    pub fn new(
        name: impl Into<String>,
        key: impl Into<String>,
        permissions: Vec<Permission>,
    ) -> Self {
        Self {
            name: name.into(),
            key: key.into(),
            permissions,
            capabilities: Vec::new(),
            rate_limit: None,
            enabled: true,
            expires_at: None,
            description: None,
        }
    }

    /// Create a read-only key with all monitoring capabilities
    pub fn read_only(name: impl Into<String>, key: impl Into<String>) -> Self {
        let permissions: Vec<Permission> = Capability::read_only_set()
            .into_iter()
            .map(Permission::read)
            .collect();
        Self::new(name, key, permissions)
    }

    /// Create an admin key with full access
    pub fn admin(name: impl Into<String>, key: impl Into<String>) -> Self {
        let permissions = vec![Permission::full(Capability::Admin)];
        Self::new(name, key, permissions)
    }

    /// Check if this key has a specific permission
    pub fn has_permission(&self, capability: Capability, required_scope: &Scope) -> bool {
        // Admin capability grants everything
        if self
            .permissions
            .iter()
            .any(|p| p.capability == Capability::Admin)
        {
            return true;
        }

        self.permissions.iter().any(|p| {
            p.capability == capability
                && match (&p.scope, required_scope) {
                    (Scope::All, _) => true,
                    (Scope::Write, Scope::Read) => true,
                    (Scope::Write, Scope::Write) => true,
                    (Scope::Read, Scope::Read) => true,
                    (Scope::Resource(granted), Scope::Resource(required)) => granted == required,
                    _ => false,
                }
        })
    }

    /// Get all granted capabilities
    pub fn granted_capabilities(&self) -> HashSet<Capability> {
        if self
            .permissions
            .iter()
            .any(|p| p.capability == Capability::Admin)
        {
            return Capability::all().into_iter().collect();
        }
        self.permissions.iter().map(|p| p.capability).collect()
    }
}

/// Rate limiter state
#[derive(Debug)]
pub struct RateLimiter {
    /// Request timestamps within the current window
    requests: Vec<Instant>,
    /// Rate limit configuration
    limit: RateLimit,
}

impl RateLimiter {
    /// Create a new rate limiter
    pub fn new(limit: RateLimit) -> Self {
        Self {
            requests: Vec::new(),
            limit,
        }
    }

    /// Check if a request is allowed
    pub fn check(&mut self) -> bool {
        let now = Instant::now();
        let window_start = now - self.limit.period();

        // Remove old requests outside the window
        self.requests.retain(|&t| t > window_start);

        if self.requests.len() < self.limit.max_requests as usize {
            self.requests.push(now);
            true
        } else {
            false
        }
    }

    /// Get remaining requests in current window
    pub fn remaining(&self) -> u32 {
        let now = Instant::now();
        let window_start = now - self.limit.period();
        let active_requests = self.requests.iter().filter(|&&t| t > window_start).count();
        self.limit
            .max_requests
            .saturating_sub(active_requests as u32)
    }

    /// Get time until next request is allowed
    pub fn retry_after(&self) -> Option<Duration> {
        if self.requests.len() < self.limit.max_requests as usize {
            return None;
        }

        let oldest = self.requests.first()?;
        let elapsed = oldest.elapsed();
        if elapsed < self.limit.period() {
            Some(self.limit.period() - elapsed)
        } else {
            None
        }
    }
}

/// Permission checker - validates requests against granted permissions
#[derive(Debug)]
pub struct PermissionChecker {
    /// API keys by key value
    keys: HashMap<String, ApiKey>,
    /// Rate limiters by key
    rate_limiters: HashMap<String, RateLimiter>,
    /// Default rate limit
    default_rate_limit: RateLimit,
}

impl PermissionChecker {
    /// Create a new permission checker with API keys
    pub fn new(keys: Vec<ApiKey>) -> Self {
        let key_map: HashMap<String, ApiKey> =
            keys.into_iter().map(|k| (k.key.clone(), k)).collect();
        Self {
            keys: key_map,
            rate_limiters: HashMap::new(),
            default_rate_limit: RateLimit::default(),
        }
    }

    /// Create a checker that allows everything (for local use)
    pub fn allow_all() -> Self {
        Self {
            keys: HashMap::new(),
            rate_limiters: HashMap::new(),
            default_rate_limit: RateLimit {
                max_requests: u32::MAX,
                period_secs: 1,
            },
        }
    }

    /// Get an API key by its value
    pub fn get_key(&self, key: &str) -> Option<&ApiKey> {
        self.keys.get(key)
    }

    /// Validate an API key and check permissions
    pub fn check(
        &mut self,
        api_key: &str,
        capability: Capability,
        scope: &Scope,
    ) -> Result<(), PermissionError> {
        // Get the key
        let key = self.keys.get(api_key).ok_or(PermissionError::InvalidKey)?;

        // Check if enabled
        if !key.enabled {
            return Err(PermissionError::KeyDisabled);
        }

        // Check rate limit
        let rate_limit = key
            .rate_limit
            .clone()
            .unwrap_or_else(|| self.default_rate_limit.clone());
        let limiter = self
            .rate_limiters
            .entry(api_key.to_string())
            .or_insert_with(|| RateLimiter::new(rate_limit));

        if !limiter.check() {
            return Err(PermissionError::RateLimited {
                retry_after: limiter.retry_after(),
            });
        }

        // Check permission
        if !key.has_permission(capability, scope) {
            return Err(PermissionError::AccessDenied {
                capability,
                scope: scope.clone(),
            });
        }

        Ok(())
    }

    /// Get granted capabilities for a key
    pub fn capabilities_for_key(&self, api_key: &str) -> HashSet<Capability> {
        self.keys
            .get(api_key)
            .map(|k| k.granted_capabilities())
            .unwrap_or_default()
    }

    /// Add or update an API key
    pub fn add_key(&mut self, key: ApiKey) {
        self.keys.insert(key.key.clone(), key);
    }

    /// Remove an API key
    pub fn remove_key(&mut self, api_key: &str) {
        self.keys.remove(api_key);
        self.rate_limiters.remove(api_key);
    }

    /// Get current rate limit status for an API key
    ///
    /// Returns (remaining_requests, total_limit, reset_timestamp) if the key exists.
    pub fn rate_limit_status(&self, api_key: &str) -> Option<(u32, u32, u64)> {
        let key = self.keys.get(api_key)?;
        let rate_limit = key
            .rate_limit
            .clone()
            .unwrap_or_else(|| self.default_rate_limit.clone());

        let remaining = self
            .rate_limiters
            .get(api_key)
            .map(|limiter| limiter.remaining())
            .unwrap_or(rate_limit.max_requests);

        // Reset time is current time + period
        let reset_at = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_secs() + rate_limit.period_secs)
            .unwrap_or(0);

        Some((remaining, rate_limit.max_requests, reset_at))
    }
}

/// Permission check error
#[derive(Debug, Clone)]
pub enum PermissionError {
    /// Invalid or unknown API key
    InvalidKey,
    /// API key is disabled
    KeyDisabled,
    /// Rate limit exceeded
    RateLimited { retry_after: Option<Duration> },
    /// Access denied for capability
    AccessDenied {
        capability: Capability,
        scope: Scope,
    },
}

impl std::fmt::Display for PermissionError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            PermissionError::InvalidKey => write!(f, "Invalid API key"),
            PermissionError::KeyDisabled => write!(f, "API key is disabled"),
            PermissionError::RateLimited { retry_after } => {
                if let Some(d) = retry_after {
                    write!(f, "Rate limited. Retry after {:?}", d)
                } else {
                    write!(f, "Rate limited")
                }
            }
            PermissionError::AccessDenied { capability, scope } => {
                write!(f, "Access denied: {}:{:?}", capability, scope)
            }
        }
    }
}

impl std::error::Error for PermissionError {}

/// API configuration loaded from file
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ApiConfig {
    /// Whether the API is enabled
    #[serde(default = "default_true")]
    pub enabled: bool,
    /// Bind address
    #[serde(default = "default_bind")]
    pub bind: String,
    /// Port number
    #[serde(default = "default_port")]
    pub port: u16,
    /// Require authentication
    #[serde(default = "default_true")]
    /// **Not consulted.** `ObservabilityApi::new` keeps only `keys`, and
    /// every API method looks its caller up in that map, so setting this to
    /// `false` does not disable authentication -- a keyless request is still
    /// refused with `InvalidKey`.
    ///
    /// Anonymous access is decided one layer out, at the request gate, by
    /// `ServerConfig::allow_anonymous`. That is the right place for it: the
    /// gate is what sees a request with no key at all. This field is kept
    /// because it is public API and because `http_server` sets it as
    /// documentation of intent, but it decides nothing.
    pub require_auth: bool,
    /// Allow unauthenticated read-only access
    #[serde(default)]
    /// **Not consulted.** See [`Self::require_auth`].
    pub allow_anonymous_read: bool,
    /// API keys
    #[serde(default)]
    pub keys: Vec<ApiKey>,
    /// Default rate limit for all keys
    #[serde(default)]
    pub default_rate_limit: Option<RateLimit>,
    /// Enable WebSocket streaming
    #[serde(default = "default_true")]
    pub enable_websocket: bool,
    /// Enable CORS
    #[serde(default)]
    pub enable_cors: bool,
    /// Allowed origins for CORS
    #[serde(default)]
    pub allowed_origins: Vec<String>,
}

fn default_bind() -> String {
    "127.0.0.1".to_string()
}

fn default_port() -> u16 {
    8787
}

impl Default for ApiConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            bind: default_bind(),
            port: default_port(),
            require_auth: true,
            allow_anonymous_read: false,
            keys: Vec::new(),
            default_rate_limit: None,
            enable_websocket: true,
            enable_cors: false,
            allowed_origins: Vec::new(),
        }
    }
}

impl ApiConfig {
    /// Load configuration from TOML file
    pub fn load_from_file(path: &std::path::Path) -> Result<Self, Box<dyn std::error::Error>> {
        let contents = std::fs::read_to_string(path)?;
        let config: Self = toml::from_str(&contents)?;
        Ok(config)
    }

    /// Save configuration to TOML file
    pub fn save_to_file(&self, path: &std::path::Path) -> Result<(), Box<dyn std::error::Error>> {
        let contents = toml::to_string_pretty(self)?;
        std::fs::write(path, contents)?;
        Ok(())
    }

    /// Generate a new random API key
    pub fn generate_key() -> String {
        use std::time::{SystemTime, UNIX_EPOCH};
        let timestamp = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_nanos();
        format!("sk-ironmon-{:x}", timestamp)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_permission_parse() {
        let p = Permission::parse("gpu:read").unwrap();
        assert_eq!(p.capability, Capability::Gpu);
        assert_eq!(p.scope, Scope::Read);

        let p = Permission::parse("process").unwrap();
        assert_eq!(p.capability, Capability::Process);
        assert_eq!(p.scope, Scope::Read);

        let p = Permission::parse("admin:*").unwrap();
        assert_eq!(p.capability, Capability::Admin);
        assert_eq!(p.scope, Scope::All);
    }

    #[test]
    fn api_key_debug_never_prints_the_secret() {
        let key = ApiKey::read_only("ci", "sk-very-secret-value");
        let shown = format!("{key:?}");
        assert!(!shown.contains("sk-very-secret-value"), "{shown}");
        assert!(shown.contains("<redacted 20 bytes>"), "{shown}");
        assert!(shown.contains("ci"), "name stays visible: {shown}");
        // The wrapping collection must not leak it either.
        let list = format!("{:?}", vec![key]);
        assert!(!list.contains("sk-very-secret-value"), "{list}");
    }

    #[test]
    fn test_api_key_permissions() {
        let key = ApiKey::read_only("test", "sk-test");
        assert!(key.has_permission(Capability::Gpu, &Scope::Read));
        assert!(!key.has_permission(Capability::GpuControl, &Scope::Write));

        let admin = ApiKey::admin("admin", "sk-admin");
        assert!(admin.has_permission(Capability::GpuControl, &Scope::Write));
        assert!(admin.has_permission(Capability::Admin, &Scope::All));
    }

    #[test]
    fn test_rate_limiter() {
        let mut limiter = RateLimiter::new(RateLimit {
            max_requests: 3,
            period_secs: 60,
        });

        assert!(limiter.check());
        assert!(limiter.check());
        assert!(limiter.check());
        assert!(!limiter.check()); // Should be rate limited
        assert_eq!(limiter.remaining(), 0);
    }

    #[test]
    fn test_rate_limit_status_unknown_key() {
        let checker = PermissionChecker::new(vec![]);
        assert!(checker.rate_limit_status("nonexistent").is_none());
    }

    #[test]
    fn test_rate_limit_status_known_key() {
        let key = ApiKey::read_only("test", "sk-test");
        let checker = PermissionChecker::new(vec![key]);
        let status = checker.rate_limit_status("sk-test");
        assert!(status.is_some());
        let (remaining, limit, reset_at) = status.unwrap();
        // Default rate limit: 100 requests per 60 seconds
        assert_eq!(limit, 100);
        assert_eq!(remaining, 100); // No requests made yet
        assert!(reset_at > 0);
    }

    #[test]
    fn test_rate_limit_status_after_requests() {
        let key = ApiKey::new("test", "sk-test", vec![Permission::read(Capability::Gpu)]);
        let mut checker = PermissionChecker::new(vec![key]);

        // Make some requests
        let _ = checker.check("sk-test", Capability::Gpu, &Scope::Read);
        let _ = checker.check("sk-test", Capability::Gpu, &Scope::Read);

        let (remaining, limit, _) = checker.rate_limit_status("sk-test").unwrap();
        assert_eq!(limit, 100);
        assert_eq!(remaining, 98); // 2 requests consumed
    }

    #[test]
    fn test_rate_limit_status_with_custom_limit() {
        let mut key = ApiKey::read_only("test", "sk-test");
        key.rate_limit = Some(RateLimit {
            max_requests: 10,
            period_secs: 30,
        });
        let checker = PermissionChecker::new(vec![key]);
        let (remaining, limit, _) = checker.rate_limit_status("sk-test").unwrap();
        assert_eq!(limit, 10);
        assert_eq!(remaining, 10);
    }
}