neumann_server 0.4.0

gRPC server exposing Neumann database via QueryRouter
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
// SPDX-License-Identifier: MIT OR Apache-2.0
//! Authentication middleware for API key validation.

use tonic::{Request, Status};

use crate::audit::{AuditEvent, AuditLogger};
use crate::config::AuthConfig;
use crate::rate_limit::{Operation, RateLimiter};

/// Extension type to carry authenticated identity through the request.
#[derive(Debug, Clone)]
pub struct AuthenticatedIdentity(pub Option<String>);

/// Validate an incoming request against the authentication configuration.
///
/// Returns the authenticated identity if valid, or an error status if not.
///
/// # Errors
///
/// Returns `Status::unauthenticated` if the API key is missing or invalid.
pub fn validate_request<T>(
    request: &Request<T>,
    config: &Option<AuthConfig>,
) -> Result<Option<String>, Status> {
    let Some(auth_config) = config else {
        // No auth configured - allow all requests
        return Ok(None);
    };

    // Try to extract API key from metadata
    let api_key = request
        .metadata()
        .get(&auth_config.api_key_header)
        .and_then(|v| v.to_str().ok());

    api_key.map_or_else(
        || {
            if auth_config.allow_anonymous {
                Ok(None)
            } else {
                Err(Status::unauthenticated("API key required"))
            }
        },
        |key| {
            auth_config.validate_key(key).map_or_else(
                || Err(Status::unauthenticated("invalid API key")),
                |identity| Ok(Some(identity.to_string())),
            )
        },
    )
}

/// Extract identity from request, falling back to query-level identity if available.
///
/// # Errors
///
/// Returns `Status::unauthenticated` if authentication fails.
pub fn extract_identity<T>(
    request: &Request<T>,
    query_identity: Option<&str>,
    config: &Option<AuthConfig>,
) -> Result<Option<String>, Status> {
    // First try request-level auth
    let request_identity = validate_request(request, config)?;

    // Query-level identity overrides if present
    Ok(query_identity.map(ToString::to_string).or(request_identity))
}

/// Validate request with rate limiting and audit logging.
///
/// This function combines authentication validation with rate limiting and audit logging.
/// It returns the authenticated identity if successful.
///
/// # Errors
///
/// Returns `Status::unauthenticated` if authentication fails, or
/// `Status::resource_exhausted` if the rate limit is exceeded.
pub fn validate_request_with_audit<T>(
    request: &Request<T>,
    auth_config: &Option<AuthConfig>,
    rate_limiter: Option<&RateLimiter>,
    audit_logger: Option<&AuditLogger>,
) -> Result<Option<String>, Status> {
    let remote_addr = request.remote_addr().map(|a| a.to_string());

    // Validate authentication
    let result = validate_request(request, auth_config);

    // Audit the result
    if let Some(logger) = audit_logger {
        match &result {
            Ok(Some(identity)) => {
                logger.record(
                    AuditEvent::AuthSuccess {
                        identity: identity.clone(),
                    },
                    remote_addr.as_deref(),
                );
            },
            Ok(None) => {
                // Anonymous access - no need to log
            },
            Err(status) => {
                logger.record(
                    AuditEvent::AuthFailure {
                        reason: status.message().to_string(),
                    },
                    remote_addr.as_deref(),
                );
            },
        }
    }

    // Check rate limit for all requests (authenticated and anonymous)
    if let Ok(ref identity_opt) = result {
        if let Some(limiter) = rate_limiter {
            // Use identity for authenticated requests, remote address for anonymous
            let rate_limit_key = identity_opt
                .as_deref()
                .unwrap_or_else(|| remote_addr.as_deref().unwrap_or("anonymous"));
            if let Err(msg) = limiter.check_and_record(rate_limit_key, Operation::Request) {
                if let Some(logger) = audit_logger {
                    logger.record(
                        AuditEvent::RateLimited {
                            identity: rate_limit_key.to_string(),
                            operation: "request".to_string(),
                        },
                        remote_addr.as_deref(),
                    );
                }
                return Err(Status::resource_exhausted(msg));
            }
        }
    }

    result
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::ApiKey;
    use tonic::metadata::MetadataValue;

    fn create_request_with_key(key: &str) -> Request<()> {
        let mut request = Request::new(());
        request
            .metadata_mut()
            .insert("x-api-key", MetadataValue::try_from(key).unwrap());
        request
    }

    fn create_request_without_key() -> Request<()> {
        Request::new(())
    }

    #[test]
    fn test_no_auth_config_allows_all() {
        let request = create_request_without_key();
        let result = validate_request(&request, &None);
        assert!(result.is_ok());
        assert!(result.unwrap().is_none());
    }

    #[test]
    fn test_valid_api_key() {
        let auth_config = AuthConfig::new()
            .with_api_key(ApiKey::new(
                "test-api-key-12345678".to_string(),
                "user:alice".to_string(),
            ))
            .with_anonymous(false);

        let request = create_request_with_key("test-api-key-12345678");
        let result = validate_request(&request, &Some(auth_config));
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), Some("user:alice".to_string()));
    }

    #[test]
    fn test_invalid_api_key() {
        let auth_config = AuthConfig::new()
            .with_api_key(ApiKey::new(
                "test-api-key-12345678".to_string(),
                "user:alice".to_string(),
            ))
            .with_anonymous(false);

        let request = create_request_with_key("wrong-key");
        let result = validate_request(&request, &Some(auth_config));
        assert!(result.is_err());
        assert_eq!(result.unwrap_err().code(), tonic::Code::Unauthenticated);
    }

    #[test]
    fn test_missing_key_with_anonymous_allowed() {
        let auth_config = AuthConfig::new().with_anonymous(true);

        let request = create_request_without_key();
        let result = validate_request(&request, &Some(auth_config));
        assert!(result.is_ok());
        assert!(result.unwrap().is_none());
    }

    #[test]
    fn test_missing_key_without_anonymous() {
        let auth_config = AuthConfig::new()
            .with_api_key(ApiKey::new(
                "test-api-key-12345678".to_string(),
                "user:alice".to_string(),
            ))
            .with_anonymous(false);

        let request = create_request_without_key();
        let result = validate_request(&request, &Some(auth_config));
        assert!(result.is_err());
        assert_eq!(result.unwrap_err().code(), tonic::Code::Unauthenticated);
    }

    #[test]
    fn test_extract_identity_query_override() {
        let auth_config = AuthConfig::new()
            .with_api_key(ApiKey::new(
                "test-api-key-12345678".to_string(),
                "user:alice".to_string(),
            ))
            .with_anonymous(false);

        let request = create_request_with_key("test-api-key-12345678");
        let result = extract_identity(&request, Some("user:bob"), &Some(auth_config));
        assert!(result.is_ok());
        // Query identity overrides request identity
        assert_eq!(result.unwrap(), Some("user:bob".to_string()));
    }

    #[test]
    fn test_extract_identity_fallback_to_request() {
        let auth_config = AuthConfig::new()
            .with_api_key(ApiKey::new(
                "test-api-key-12345678".to_string(),
                "user:alice".to_string(),
            ))
            .with_anonymous(false);

        let request = create_request_with_key("test-api-key-12345678");
        let result = extract_identity(&request, None, &Some(auth_config));
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), Some("user:alice".to_string()));
    }

    #[test]
    fn test_custom_header() {
        let auth_config = AuthConfig::new()
            .with_header("authorization".to_string())
            .with_api_key(ApiKey::new(
                "Bearer token12345678".to_string(),
                "user:alice".to_string(),
            ))
            .with_anonymous(false);

        let mut request = Request::new(());
        request.metadata_mut().insert(
            "authorization",
            MetadataValue::try_from("Bearer token12345678").unwrap(),
        );

        let result = validate_request(&request, &Some(auth_config));
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), Some("user:alice".to_string()));
    }

    #[test]
    fn test_validate_with_rate_limit_success() {
        use crate::rate_limit::RateLimitConfig;

        let auth_config = AuthConfig::new()
            .with_api_key(ApiKey::new(
                "test-api-key-12345678".to_string(),
                "user:alice".to_string(),
            ))
            .with_anonymous(false);

        let limiter = RateLimiter::new(RateLimitConfig::default());
        let request = create_request_with_key("test-api-key-12345678");

        let result =
            validate_request_with_audit(&request, &Some(auth_config), Some(&limiter), None);
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), Some("user:alice".to_string()));
    }

    #[test]
    fn test_validate_with_rate_limit_exceeded() {
        use crate::rate_limit::RateLimitConfig;
        use std::time::Duration;

        let auth_config = AuthConfig::new()
            .with_api_key(ApiKey::new(
                "test-api-key-12345678".to_string(),
                "user:alice".to_string(),
            ))
            .with_anonymous(false);

        let limiter = RateLimiter::new(
            RateLimitConfig::new()
                .with_max_requests(1)
                .with_window(Duration::from_secs(60)),
        );

        // First request succeeds
        let request = create_request_with_key("test-api-key-12345678");
        let result =
            validate_request_with_audit(&request, &Some(auth_config.clone()), Some(&limiter), None);
        assert!(result.is_ok());

        // Second request should fail
        let request = create_request_with_key("test-api-key-12345678");
        let result =
            validate_request_with_audit(&request, &Some(auth_config), Some(&limiter), None);
        assert!(result.is_err());
        assert_eq!(result.unwrap_err().code(), tonic::Code::ResourceExhausted);
    }

    #[test]
    fn test_validate_with_audit_logging() {
        use crate::audit::AuditConfig;

        let auth_config = AuthConfig::new()
            .with_api_key(ApiKey::new(
                "test-api-key-12345678".to_string(),
                "user:alice".to_string(),
            ))
            .with_anonymous(false);

        let logger = AuditLogger::new(AuditConfig::default());
        let request = create_request_with_key("test-api-key-12345678");

        let result = validate_request_with_audit(&request, &Some(auth_config), None, Some(&logger));
        assert!(result.is_ok());

        // Check audit log
        assert_eq!(logger.count(), 1);
        let entries = logger.by_identity("user:alice");
        assert_eq!(entries.len(), 1);
    }

    #[test]
    fn test_auth_failure_audited() {
        use crate::audit::AuditConfig;

        let auth_config = AuthConfig::new()
            .with_api_key(ApiKey::new(
                "test-api-key-12345678".to_string(),
                "user:alice".to_string(),
            ))
            .with_anonymous(false);

        let logger = AuditLogger::new(AuditConfig::default());
        let request = create_request_with_key("wrong-key");

        let result = validate_request_with_audit(&request, &Some(auth_config), None, Some(&logger));
        assert!(result.is_err());

        // Auth failure should be logged
        assert_eq!(logger.count(), 1);
    }

    #[test]
    fn test_rate_limit_audited() {
        use crate::audit::AuditConfig;
        use crate::rate_limit::RateLimitConfig;
        use std::time::Duration;

        let auth_config = AuthConfig::new()
            .with_api_key(ApiKey::new(
                "test-api-key-12345678".to_string(),
                "user:alice".to_string(),
            ))
            .with_anonymous(false);

        let limiter = RateLimiter::new(
            RateLimitConfig::new()
                .with_max_requests(1)
                .with_window(Duration::from_secs(60)),
        );
        let logger = AuditLogger::new(AuditConfig::default());

        // First request succeeds
        let request = create_request_with_key("test-api-key-12345678");
        let _ = validate_request_with_audit(
            &request,
            &Some(auth_config.clone()),
            Some(&limiter),
            Some(&logger),
        );

        // Second request should be rate limited
        let request = create_request_with_key("test-api-key-12345678");
        let _ = validate_request_with_audit(
            &request,
            &Some(auth_config),
            Some(&limiter),
            Some(&logger),
        );

        // Should have 2 auth success + 1 rate limited
        assert_eq!(logger.count(), 3);
    }
}