kindly_guard_server/
lib.rs

1// Copyright 2025 Kindly Software Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//! `KindlyGuard` Server Library
15//!
16//! Exposes the scanner functionality for use by the CLI and other tools
17
18pub mod audit;
19pub mod auth;
20pub mod cli;
21pub mod component_selector;
22pub mod config;
23pub mod daemon;
24#[cfg(feature = "enhanced")]
25pub(crate) mod enhanced_impl;
26pub mod error;
27#[cfg(feature = "enhanced")]
28pub mod event_processor;
29pub mod logging;
30pub mod metrics;
31pub mod neutralizer;
32pub mod permissions;
33pub mod plugins;
34pub mod protocol;
35pub mod rate_limit;
36pub mod resilience;
37pub mod scanner;
38pub mod security;
39pub mod server;
40pub mod setup;
41pub mod shield;
42pub mod signing;
43pub mod standard_impl;
44pub mod storage;
45pub mod telemetry;
46pub mod traits;
47pub mod transport;
48pub mod versioning;
49pub mod web;
50
51use std::sync::Arc;
52
53pub use auth::{AuthContext, AuthManager};
54pub use component_selector::{ComponentManager, ComponentSelector};
55pub use config::{Config, ScannerConfig};
56pub use error::{KindlyError, KindlyResult, ResultExt};
57pub use metrics::MetricsRegistry;
58pub use neutralizer::{
59    create_neutralizer, create_neutralizer_with_telemetry, NeutralizationConfig,
60    NeutralizationMode, NeutralizeResult, ThreatNeutralizer,
61};
62pub use scanner::{Location, SecurityScanner, Severity, Threat, ThreatType};
63pub use server::McpServer;
64pub use shield::Shield;
65pub use traits::{CorrelationEngine, EnhancedScanner, RateLimiter, SecurityEventProcessor};
66
67/// Create an event buffer based on configuration
68#[cfg(feature = "enhanced")]
69pub fn create_event_buffer(
70    config: &event_processor::EventProcessorConfig,
71) -> anyhow::Result<Option<Box<dyn traits::EventBufferTrait>>> {
72    if !config.enabled {
73        return Ok(None);
74    }
75
76    #[cfg(feature = "enhanced")]
77    {
78        // Check if enhanced mode is requested
79        if config.enhanced_mode.unwrap_or(false) {
80            tracing::info!(
81                target: "security.config",
82                buffer_size_mb = config.buffer_size_mb,
83                max_endpoints = config.max_endpoints,
84                "Initializing enhanced atomic bit-packed event buffer"
85            );
86            // Use the enhanced implementation
87            // Configuration for enhanced buffer
88            let buffer_size_mb = config.buffer_size_mb;
89            let max_endpoints = config.max_endpoints;
90
91            // Create enhanced buffer through factory
92            return Ok(Some(Box::new(enhanced_impl::create_enhanced_event_buffer(
93                buffer_size_mb,
94                max_endpoints,
95            )?)));
96        }
97    }
98
99    // Default to simple implementation
100    tracing::info!(
101        target: "security.config",
102        "Using standard event buffer implementation"
103    );
104    Ok(Some(Box::new(event_processor::SimpleEventBuffer::new())))
105}
106
107/// Create a security scanner instance based on configuration
108pub fn create_scanner(config: &Config) -> Arc<scanner::SecurityScanner> {
109    Arc::new(
110        scanner::SecurityScanner::new(config.scanner.clone())
111            .expect("Failed to create security scanner"),
112    )
113}
114
115/// Create a storage provider based on configuration
116pub fn create_storage(config: &Config) -> Arc<dyn storage::StorageProvider> {
117    storage::create_storage_provider(config)
118}
119
120/// Create a rate limiter based on configuration
121pub fn create_rate_limiter(config: &Config) -> Arc<rate_limit::RateLimiter> {
122    Arc::new(rate_limit::RateLimiter::new(config.rate_limit.clone()))
123}
124
125/// Create a transport based on configuration
126pub fn create_transport(config: &Config) -> Arc<dyn transport::Transport> {
127    transport::create_transport(config)
128}
129
130/// Create a telemetry provider based on configuration
131pub fn create_telemetry(config: &Config) -> Arc<dyn telemetry::TelemetryProvider> {
132    telemetry::create_telemetry_provider(config)
133}
134
135/// Create an audit logger based on configuration
136pub fn create_audit_logger(config: &Config) -> Arc<dyn audit::AuditLogger> {
137    audit::create_audit_logger(config)
138}
139
140/// Mock types for testing
141#[cfg(any(test, feature = "test-utils"))]
142pub mod mocks {
143    // NOTE: Trait mocks disabled due to mockall compatibility issues with async_trait
144    // Manual test doubles should be created when needed
145    pub use crate::permissions::MockToolPermissionManager;
146    pub use crate::traits::MockEnhancedScanner; // This one doesn't use async_trait
147}