All 12 validation agents complete: - Agent 1: E2E auth testing (11/11 tests pass, 8-layer validation) - Agent 2: Load testing framework ready (4 scenarios documented) - Agent 3: Docker deployment (6/6 infra services healthy) - Agent 4: Database integration (4 migrations, 6 NOTIFY channels, RBAC) - Agent 5: TLI client integration (JWT auth, OS keyring, API Gateway) - Agent 6: Performance profiling (978ns pipeline, 3 optimization recommendations) - Agent 7: Security penetration testing (OWASP Top 10, 3 critical findings) - Agent 8: gRPC proxy testing (3 proxies, 100% test pass, 5-8μs overhead) - Agent 9: Monitoring validation (Prometheus + Grafana, 5 issues identified) - Agent 10: Rate limiting stress test (8/8 tests pass, 99% attack mitigation) - Agent 11: Production readiness (7/9 criteria, 2 P0 blockers identified) - Agent 12: Documentation audit (92% complete, A- grade, production ready) Deliverables: - 30+ validation reports created (150+ KB documentation) - All 5 Dockerfiles updated with complete workspace - Redis/PostgreSQL integration tests operational - Comprehensive performance profiling completed - Security vulnerabilities documented with remediation 🔴 CRITICAL P0 BLOCKERS IDENTIFIED: 1. Audit trail persistence (trading_engine/src/compliance/audit_trails.rs:857) - Impact: SOX/MiFID II compliance violation - Status: Events not saved to database (only printed) 2. Test suite validation timeout - Historical: 1,919/1,919 tests passing (100%) - Current: Timeout after 2 minutes - Impact: Cannot certify regression-free state ⚠️ CRITICAL SECURITY VULNERABILITIES: 1. Authentication DISABLED (services/trading_service/src/main.rs:298-302) 2. Execution engine PANICS (execution_engine.rs:661,667,674) 3. Audit trail persistence (covered above) Production Decision: CONDITIONAL GO - Must fix 2 P0 blockers before production deployment - 7/9 production criteria met (78%) - SOX: 87.5% compliant, MiFID II: 87.5% compliant - Documentation: 92% complete (4,329 production lines) Next Wave: Address P0 blockers + performance optimization
170 lines
5.1 KiB
Rust
170 lines
5.1 KiB
Rust
//! Common test utilities for API Gateway integration tests
|
|
|
|
use anyhow::Result;
|
|
use jsonwebtoken::{encode, EncodingKey, Header};
|
|
use std::time::{SystemTime, UNIX_EPOCH};
|
|
use uuid::Uuid;
|
|
|
|
pub use api_gateway::auth::{Jti, JwtClaims};
|
|
|
|
/// Test JWT configuration
|
|
pub struct TestJwtConfig {
|
|
pub secret: String,
|
|
pub issuer: String,
|
|
pub audience: String,
|
|
}
|
|
|
|
impl Default for TestJwtConfig {
|
|
fn default() -> Self {
|
|
Self {
|
|
secret: "test-secret-must-be-at-least-64-characters-long-for-security-validation-ok-1234567890".to_string(),
|
|
issuer: "foxhunt-api-gateway".to_string(),
|
|
audience: "foxhunt-services".to_string(),
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Generate a valid JWT token for testing
|
|
pub fn generate_test_token(
|
|
user_id: &str,
|
|
roles: Vec<String>,
|
|
permissions: Vec<String>,
|
|
ttl_seconds: u64,
|
|
) -> Result<(String, String)> {
|
|
let config = TestJwtConfig::default();
|
|
let jti = Jti::new();
|
|
|
|
let now = SystemTime::now()
|
|
.duration_since(UNIX_EPOCH)?
|
|
.as_secs();
|
|
|
|
let claims = JwtClaims {
|
|
jti: jti.0.clone(),
|
|
sub: user_id.to_string(),
|
|
iat: now,
|
|
exp: now + ttl_seconds,
|
|
nbf: now, // Not before: valid from now
|
|
iss: config.issuer,
|
|
aud: config.audience,
|
|
roles,
|
|
permissions,
|
|
token_type: "access".to_string(),
|
|
session_id: Some(Uuid::new_v4().to_string()),
|
|
};
|
|
|
|
let token = encode(
|
|
&Header::default(),
|
|
&claims,
|
|
&EncodingKey::from_secret(config.secret.as_bytes()),
|
|
)?;
|
|
|
|
Ok((token, jti.0))
|
|
}
|
|
|
|
/// Generate an expired JWT token for testing
|
|
pub fn generate_expired_token(user_id: &str) -> Result<String> {
|
|
let config = TestJwtConfig::default();
|
|
|
|
let now = SystemTime::now()
|
|
.duration_since(UNIX_EPOCH)?
|
|
.as_secs();
|
|
|
|
let claims = JwtClaims {
|
|
jti: Jti::new().0,
|
|
sub: user_id.to_string(),
|
|
iat: now - 7200,
|
|
exp: now - 3600, // Expired 1 hour ago
|
|
nbf: now - 7200, // Not before: from 2 hours ago
|
|
iss: config.issuer,
|
|
aud: config.audience,
|
|
roles: vec!["trader".to_string()],
|
|
permissions: vec!["api.access".to_string()],
|
|
token_type: "access".to_string(),
|
|
session_id: Some(Uuid::new_v4().to_string()),
|
|
};
|
|
|
|
let token = encode(
|
|
&Header::default(),
|
|
&claims,
|
|
&EncodingKey::from_secret(config.secret.as_bytes()),
|
|
)?;
|
|
|
|
Ok(token)
|
|
}
|
|
|
|
/// Generate a token with invalid signature
|
|
pub fn generate_invalid_signature_token(user_id: &str) -> Result<String> {
|
|
let now = SystemTime::now()
|
|
.duration_since(UNIX_EPOCH)?
|
|
.as_secs();
|
|
|
|
let claims = JwtClaims {
|
|
jti: Jti::new().0,
|
|
sub: user_id.to_string(),
|
|
iat: now,
|
|
exp: now + 3600,
|
|
nbf: now, // Not before: valid from now
|
|
iss: "foxhunt-api-gateway".to_string(),
|
|
aud: "foxhunt-services".to_string(),
|
|
roles: vec!["trader".to_string()],
|
|
permissions: vec!["api.access".to_string()],
|
|
token_type: "access".to_string(),
|
|
session_id: Some(Uuid::new_v4().to_string()),
|
|
};
|
|
|
|
// Use wrong secret to create invalid signature
|
|
let token = encode(
|
|
&Header::default(),
|
|
&claims,
|
|
&EncodingKey::from_secret(b"wrong-secret-key-that-will-fail-validation-checks-completely"),
|
|
)?;
|
|
|
|
Ok(token)
|
|
}
|
|
|
|
/// Wait for Redis to be ready
|
|
pub async fn wait_for_redis(redis_url: &str, max_attempts: usize) -> Result<()> {
|
|
for attempt in 1..=max_attempts {
|
|
match redis::Client::open(redis_url) {
|
|
Ok(client) => {
|
|
match client.get_multiplexed_async_connection().await {
|
|
Ok(mut conn) => {
|
|
// Use redis::cmd to send PING command
|
|
if let Ok(_) = redis::cmd("PING").query_async::<String>(&mut conn).await {
|
|
println!("✓ Redis ready after {} attempts", attempt);
|
|
return Ok(());
|
|
}
|
|
}
|
|
Err(e) => {
|
|
if attempt == max_attempts {
|
|
return Err(anyhow::anyhow!("Redis not ready: {}", e));
|
|
}
|
|
tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
|
|
}
|
|
}
|
|
}
|
|
Err(e) => {
|
|
if attempt == max_attempts {
|
|
return Err(anyhow::anyhow!("Failed to create Redis client: {}", e));
|
|
}
|
|
tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
|
|
}
|
|
}
|
|
}
|
|
|
|
Err(anyhow::anyhow!("Redis not ready after {} attempts", max_attempts))
|
|
}
|
|
|
|
/// Clean up Redis test data
|
|
pub async fn cleanup_redis(redis_url: &str) -> Result<()> {
|
|
use redis::AsyncCommands;
|
|
|
|
let client = redis::Client::open(redis_url)?;
|
|
let mut conn = client.get_multiplexed_async_connection().await?;
|
|
|
|
// Delete all keys matching test patterns
|
|
let _: () = redis::cmd("FLUSHDB").query_async(&mut conn).await?;
|
|
|
|
Ok(())
|
|
}
|