Skip to content

Commit b59f7b1

Browse files
authored
Db refactoring (#2151)
Refactor database access to prepare migration away from SQLx This refactoring introduces a DAO-based database abstraction and moves SQL-specific logic out of scanner components into dedicated database modules. The goal is to reduce coupling between business logic and the current SQLx-based implementation and to make the database backend easier to replace. The change is motivated by existing workarounds around SQLx transaction behavior and by the planned migration from sqlite to turso. This PR does not perform the backend switch itself. Instead, it restructures queries, stream handling, scheduling and related container-image-scanner/openvasd database paths so the later migration can happen on a cleaner and more isolated abstraction layer. The refactoring also reduces the DAO surface to a smaller set of core interfaces and aligns affected code paths with the new structure without intending functional changes.
1 parent d783978 commit b59f7b1

File tree

43 files changed

+2491
-1910
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+2491
-1910
lines changed

rust/crates/greenbone-scanner-framework/src/entry/response/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ macro_rules! internal_server_error {
4242
}};
4343
}
4444

45-
pub type StreamResult<'a, T, E> = Box<dyn Stream<Item = Result<T, E>> + Unpin + Send + 'a>;
45+
pub type StreamResult<T, E> = Pin<Box<dyn Stream<Item = Result<T, E>> + Send>>;
4646

4747
pub trait BodyKindError {
4848
fn into_body_kind(self) -> BodyKind;
@@ -100,7 +100,7 @@ impl BodyKind {
100100
/// If it is an empty stgream it will return an BodyKind::Stream with an empty stream
101101
pub async fn from_result_stream<T, E>(
102102
status_code: StatusCode,
103-
mut input: StreamResult<'static, T, E>,
103+
mut input: StreamResult<T, E>,
104104
) -> Self
105105
where
106106
T: Default + serde::Serialize + Send + 'static,

rust/crates/greenbone-scanner-framework/src/get_scans.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::{
1212
};
1313

1414
pub trait GetScans: Send + Sync {
15-
fn get_scans(&self, client_id: String) -> StreamResult<'static, String, GetScansError>;
15+
fn get_scans(&self, client_id: String) -> StreamResult<String, GetScansError>;
1616
}
1717

1818
pub struct GetScansHandler<T> {
@@ -110,6 +110,7 @@ impl From<GetScansError> for BodyKind {
110110

111111
#[cfg(test)]
112112
mod tests {
113+
113114
use entry::test_utilities;
114115
use futures::stream;
115116
use http_body_util::{BodyExt, Empty};
@@ -127,15 +128,15 @@ mod tests {
127128
}
128129

129130
impl GetScans for Test {
130-
fn get_scans(&self, client_id: String) -> StreamResult<'static, String, GetScansError> {
131+
fn get_scans(&self, client_id: String) -> StreamResult<String, GetScansError> {
131132
let ise = ClientHash::from("internal_server_error").to_string();
132133
if ise == client_id {
133-
return Box::new(stream::iter(vec![Err(GetScansError::External(Box::new(
134+
return Box::pin(stream::iter(vec![Err(GetScansError::External(Box::new(
134135
io::Error::other("oh no"),
135136
)))]));
136137
}
137138

138-
Box::new(stream::iter(vec![Ok(String::default())]))
139+
Box::pin(stream::iter(vec![Ok(String::default())]))
139140
}
140141
}
141142

rust/crates/greenbone-scanner-framework/src/get_scans_id.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ use crate::{
1212
};
1313

1414
pub trait GetScansId: MapScanID {
15-
fn get_scans_id(
16-
&self,
15+
fn get_scans_id<'a>(
16+
&'a self,
1717
id: String,
18-
) -> Pin<Box<dyn Future<Output = Result<models::Scan, GetScansIDError>> + Send + '_>>;
18+
) -> Pin<Box<dyn Future<Output = Result<models::Scan, GetScansIDError>> + Send + 'a>>;
1919
}
2020

2121
pub struct GetScansIdHandler<T> {

rust/crates/greenbone-scanner-framework/src/get_scans_id_results.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub trait GetScansIdResults: MapScanID {
1717
id: String,
1818
from: Option<usize>,
1919
to: Option<usize>,
20-
) -> StreamResult<'static, models::Result, GetScansIDResultsError>;
20+
) -> StreamResult<models::Result, GetScansIDResultsError>;
2121
}
2222

2323
pub struct GetScansIdResultsHandler<T> {
@@ -163,10 +163,10 @@ mod tests {
163163
client_id: String,
164164
from: Option<usize>,
165165
to: Option<usize>,
166-
) -> StreamResult<'static, models::Result, GetScansIDResultsError> {
166+
) -> StreamResult<models::Result, GetScansIDResultsError> {
167167
let ise = ClientHash::from("internal_server_error").to_string();
168168
if ise == client_id {
169-
return Box::new(stream::iter(vec![Err(GetScansError::External(Box::new(
169+
return Box::pin(stream::iter(vec![Err(GetScansError::External(Box::new(
170170
io::Error::other("oh no"),
171171
)))]));
172172
}
@@ -186,7 +186,7 @@ mod tests {
186186
})
187187
.collect();
188188

189-
Box::new(stream::iter(result))
189+
Box::pin(stream::iter(result))
190190
}
191191
}
192192

rust/crates/greenbone-scanner-framework/src/get_vts.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ use crate::{
1010
};
1111

1212
pub trait GetVts: Send + Sync {
13-
fn get_oids(&self, client_id: String) -> StreamResult<'static, String, GetVTsError>;
13+
fn get_oids(&self, client_id: String) -> StreamResult<String, GetVTsError>;
1414

15-
fn get_vts(&self, client_id: String) -> StreamResult<'static, VTData, GetVTsError>;
15+
fn get_vts(&self, client_id: String) -> StreamResult<VTData, GetVTsError>;
1616
}
1717

1818
pub struct GetVTsHandler<T> {
@@ -127,7 +127,7 @@ mod tests {
127127
}
128128

129129
impl GetVts for Test {
130-
fn get_oids(&self, client_id: String) -> StreamResult<'static, String, GetVTsError> {
130+
fn get_oids(&self, client_id: String) -> StreamResult<String, GetVTsError> {
131131
let ok = ClientHash::from("ok").to_string();
132132
let not_found = ClientHash::from("not_found").to_string();
133133
let unknown = "unknown".to_string();
@@ -141,11 +141,11 @@ mod tests {
141141
))))]
142142
};
143143
//stream::iter(result)
144-
Box::new(stream::iter(result))
144+
Box::pin(stream::iter(result))
145145
}
146146

147-
fn get_vts(&self, _: String) -> StreamResult<'static, VTData, GetVTsError> {
148-
Box::new(stream::iter(vec![Ok(VTData::default())]))
147+
fn get_vts(&self, _: String) -> StreamResult<VTData, GetVTsError> {
148+
Box::pin(stream::iter(vec![Ok(VTData::default())]))
149149
}
150150
}
151151

rust/crates/greenbone-scanner-framework/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ pub mod prelude {
6868
GetScansIdResultsId, GetScansIdStatus, GetScansPreferences, MapScanID, PostScans,
6969
PostScansError, StreamResult,
7070
delete_scans_id::{DeleteScansIDError, DeleteScansId},
71+
entry::Prefixed,
7172
models,
7273
post_scans_id::{PostScansIDError, PostScansId},
7374
};

rust/examples/container_image_scanning/local-registry/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ certs/domain.crt:
7373
auth-server:
7474
$(CONTAINER) run -d \
7575
--name auth_server \
76-
-p 5001:5001 \
76+
-p 127.0.0.1:5001:5001 \
7777
-v "$(BASE_PATH)/config:/config" \
7878
-v "$(BASE_PATH)/auth:/config/auth" \
7979
-v "$(BASE_PATH)/certs:/certs" \
@@ -91,7 +91,7 @@ rm-auth-server: stop-auth-server
9191
docker-registry:
9292
$(CONTAINER) run -d \
9393
--name registry \
94-
-p 5000:5000 \
94+
-p 127.0.0.1:5000:5000 \
9595
-v "$(BASE_PATH)/certs:/certs" \
9696
-e REGISTRY_AUTH=token \
9797
-e REGISTRY_HTTP_TLS_CERTIFICATE=/certs/domain.crt \

rust/migrations/20250711070445_init.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ CREATE TABLE vt_parameters (
100100
FOREIGN KEY (id, vt) REFERENCES vts(id, vt) ON DELETE CASCADE
101101
);
102102

103+
103104
-- maybe just store the json as a blob as we just get the data for id and result_id but never actually work with the data
104105
CREATE TABLE results (
105106
id INTEGER,
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-- Formalizes the name of the fields across container_image_scanner and nasl
2+
ALTER TABLE results RENAME COLUMN id TO scan_id;
3+
ALTER TABLE results RENAME COLUMN result_id TO id;
4+

rust/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ use std::pin::Pin;
2828
use futures::Stream;
2929
pub use greenbone_scanner_framework::models;
3030

31-
pub type PinBoxFut<T> = Pin<Box<dyn Future<Output = T> + Send>>;
32-
pub type PinBoxFutRef<'a, T> = Pin<Box<dyn Future<Output = T> + Send + Sync + 'a>>;
33-
pub type Streamer<T> = Pin<Box<dyn Stream<Item = T> + Send + Sync>>;
31+
pub type Promise<T> = Pin<Box<dyn Future<Output = T> + Send>>;
32+
pub type PromiseRef<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
33+
pub type Streamer<T> = Pin<Box<dyn Stream<Item = T> + Send>>;
3434
pub type ExternalError = Box<dyn std::error::Error + Send + Sync + 'static>;
3535

3636
pub const SQLITE_LIMIT_VARIABLE_NUMBER: usize = 32766;

0 commit comments

Comments
 (0)