Skip to main content

vac_sidecar/
error.rs

1use axum::{http::StatusCode, response::IntoResponse};
2use thiserror::Error;
3
4/// V-A-C Sidecar error types with explicit fail-closed enforcement
5#[derive(Debug, Error)]
6pub enum VacError {
7    #[error("Missing authorization token")]
8    MissingToken,
9    
10    #[error("Invalid token format")]
11    InvalidTokenFormat,
12    
13    #[error("Invalid biscuit signature")]
14    InvalidSignature,
15    
16    #[error("Receipt expired")]
17    ReceiptExpired,
18    
19    #[error("Correlation ID mismatch")]
20    CorrelationIdMismatch,
21    
22    #[error("Policy violation: {0}")]
23    PolicyViolation(String),
24    
25    #[error("Request denied by fail-closed policy")]
26    Deny,
27    
28    #[error("Configuration error: {0}")]
29    ConfigError(String),
30    
31    #[error("Internal server error: {0}")]
32    InternalError(String),
33    
34    #[error("Proxy error: {0}")]
35    ProxyError(String),
36    
37    #[error("Receipt verification failed: {0}")]
38    ReceiptError(String),
39}
40
41impl From<VacError> for StatusCode {
42    fn from(err: VacError) -> Self {
43        From::from(&err)
44    }
45}
46
47impl IntoResponse for VacError {
48    fn into_response(self) -> axum::response::Response {
49        let status: StatusCode = From::from(&self);
50        let body = format!("{}", self);
51        (status, body).into_response()
52    }
53}
54
55impl From<&VacError> for StatusCode {
56    fn from(err: &VacError) -> Self {
57        match err {
58            VacError::MissingToken => StatusCode::UNAUTHORIZED,
59            VacError::InvalidTokenFormat => StatusCode::BAD_REQUEST,
60            VacError::InvalidSignature => StatusCode::FORBIDDEN,
61            VacError::ReceiptExpired => StatusCode::FORBIDDEN,
62            VacError::CorrelationIdMismatch => StatusCode::CONFLICT,
63            VacError::PolicyViolation(_) => StatusCode::FORBIDDEN,
64            VacError::Deny => StatusCode::FORBIDDEN,
65            VacError::ConfigError(_) => StatusCode::INTERNAL_SERVER_ERROR,
66            VacError::InternalError(_) => StatusCode::INTERNAL_SERVER_ERROR,
67            VacError::ProxyError(_) => StatusCode::BAD_GATEWAY,
68            VacError::ReceiptError(_) => StatusCode::FORBIDDEN,
69        }
70    }
71}
72
73// Note: biscuit-auth errors are handled inline in biscuit.rs and receipt.rs
74// We don't provide a generic conversion here since error types vary
75
76impl From<hex::FromHexError> for VacError {
77    fn from(_: hex::FromHexError) -> Self {
78        VacError::ConfigError("Invalid hex encoding for public key".to_string())
79    }
80}
81
82impl From<reqwest::Error> for VacError {
83    fn from(err: reqwest::Error) -> Self {
84        VacError::ProxyError(format!("HTTP request failed: {}", err))
85    }
86}