scanbridge/quarantine/traits.rs
1//! Quarantine store trait definition.
2
3use crate::core::error::QuarantineError;
4use crate::core::FileInput;
5use crate::quarantine::record::{QuarantineFilter, QuarantineId, QuarantineRecord};
6
7use async_trait::async_trait;
8use std::fmt::Debug;
9
10/// Trait for quarantine storage implementations.
11///
12/// Implementations of this trait provide storage for quarantined files,
13/// allowing them to be safely stored, retrieved, and managed.
14///
15/// # Example Implementation
16///
17/// ```rust,ignore
18/// use scanbridge::quarantine::{QuarantineStore, QuarantineRecord, QuarantineId, QuarantineFilter};
19/// use scanbridge::core::{FileInput, QuarantineError};
20/// use async_trait::async_trait;
21///
22/// #[derive(Debug)]
23/// struct MyQuarantineStore {
24/// // Your storage implementation
25/// }
26///
27/// #[async_trait]
28/// impl QuarantineStore for MyQuarantineStore {
29/// async fn store(
30/// &self,
31/// input: &FileInput,
32/// record: QuarantineRecord,
33/// ) -> Result<QuarantineId, QuarantineError> {
34/// // Store the file and record
35/// todo!()
36/// }
37///
38/// async fn retrieve(
39/// &self,
40/// id: &QuarantineId,
41/// ) -> Result<(Vec<u8>, QuarantineRecord), QuarantineError> {
42/// // Retrieve the file and record
43/// todo!()
44/// }
45///
46/// async fn delete(&self, id: &QuarantineId) -> Result<(), QuarantineError> {
47/// // Delete the file and record
48/// todo!()
49/// }
50///
51/// async fn list(
52/// &self,
53/// filter: QuarantineFilter,
54/// ) -> Result<Vec<QuarantineRecord>, QuarantineError> {
55/// // List records matching the filter
56/// todo!()
57/// }
58/// }
59/// ```
60#[async_trait]
61pub trait QuarantineStore: Send + Sync + Debug {
62 /// Stores a file in quarantine.
63 ///
64 /// # Arguments
65 ///
66 /// * `input` - The file to quarantine
67 /// * `record` - Metadata about the quarantined file
68 ///
69 /// # Returns
70 ///
71 /// The unique ID assigned to this quarantine entry.
72 async fn store(
73 &self,
74 input: &FileInput,
75 record: QuarantineRecord,
76 ) -> Result<QuarantineId, QuarantineError>;
77
78 /// Retrieves a file from quarantine.
79 ///
80 /// # Arguments
81 ///
82 /// * `id` - The quarantine ID of the file to retrieve
83 ///
84 /// # Returns
85 ///
86 /// The file contents and metadata.
87 async fn retrieve(
88 &self,
89 id: &QuarantineId,
90 ) -> Result<(Vec<u8>, QuarantineRecord), QuarantineError>;
91
92 /// Deletes a file from quarantine.
93 ///
94 /// # Arguments
95 ///
96 /// * `id` - The quarantine ID of the file to delete
97 async fn delete(&self, id: &QuarantineId) -> Result<(), QuarantineError>;
98
99 /// Lists quarantine records matching the filter.
100 ///
101 /// # Arguments
102 ///
103 /// * `filter` - Criteria for filtering records
104 ///
105 /// # Returns
106 ///
107 /// Matching quarantine records (without file contents).
108 async fn list(
109 &self,
110 filter: QuarantineFilter,
111 ) -> Result<Vec<QuarantineRecord>, QuarantineError>;
112
113 /// Returns the number of quarantined files.
114 async fn count(&self) -> Result<usize, QuarantineError> {
115 let records = self.list(QuarantineFilter::new()).await?;
116 Ok(records.len())
117 }
118
119 /// Checks if a file with the given hash is already quarantined.
120 async fn contains_hash(&self, hash: &str) -> Result<bool, QuarantineError> {
121 let filter = QuarantineFilter::new().with_file_hash(hash);
122 let records = self.list(filter).await?;
123 Ok(!records.is_empty())
124 }
125
126 /// Gets a record by ID without retrieving the file contents.
127 async fn get_record(&self, id: &QuarantineId) -> Result<QuarantineRecord, QuarantineError> {
128 let (_, record) = self.retrieve(id).await?;
129 Ok(record)
130 }
131
132 /// Deletes all expired quarantine records.
133 async fn cleanup_expired(&self) -> Result<usize, QuarantineError> {
134 let filter = QuarantineFilter::new().with_include_expired(true);
135 let records = self.list(filter).await?;
136
137 let mut deleted = 0;
138 for record in records {
139 if record.is_expired() {
140 if self.delete(&record.id).await.is_ok() {
141 deleted += 1;
142 }
143 }
144 }
145
146 Ok(deleted)
147 }
148}
149
150/// A no-op quarantine store that doesn't actually store anything.
151///
152/// Useful for testing or when quarantine is disabled.
153#[derive(Debug, Default)]
154pub struct NoOpQuarantineStore;
155
156impl NoOpQuarantineStore {
157 /// Creates a new no-op store.
158 pub fn new() -> Self {
159 Self
160 }
161}
162
163#[async_trait]
164impl QuarantineStore for NoOpQuarantineStore {
165 async fn store(
166 &self,
167 _input: &FileInput,
168 record: QuarantineRecord,
169 ) -> Result<QuarantineId, QuarantineError> {
170 tracing::debug!(id = %record.id, "NoOp quarantine store: file not actually stored");
171 Ok(record.id)
172 }
173
174 async fn retrieve(
175 &self,
176 id: &QuarantineId,
177 ) -> Result<(Vec<u8>, QuarantineRecord), QuarantineError> {
178 Err(QuarantineError::NotFound { id: id.to_string() })
179 }
180
181 async fn delete(&self, _id: &QuarantineId) -> Result<(), QuarantineError> {
182 Ok(())
183 }
184
185 async fn list(
186 &self,
187 _filter: QuarantineFilter,
188 ) -> Result<Vec<QuarantineRecord>, QuarantineError> {
189 Ok(Vec::new())
190 }
191
192 async fn count(&self) -> Result<usize, QuarantineError> {
193 Ok(0)
194 }
195}
196
197#[cfg(test)]
198mod tests {
199 use super::*;
200
201 #[tokio::test]
202 async fn test_noop_store() {
203 let store = NoOpQuarantineStore::new();
204
205 // Count should always be 0
206 assert_eq!(store.count().await.unwrap(), 0);
207
208 // Retrieve should always fail
209 let result = store.retrieve(&QuarantineId::new()).await;
210 assert!(matches!(result, Err(QuarantineError::NotFound { .. })));
211
212 // List should always be empty
213 let records = store.list(QuarantineFilter::new()).await.unwrap();
214 assert!(records.is_empty());
215 }
216}