-
Notifications
You must be signed in to change notification settings - Fork 0
feat(db): rough database implementation #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| # For documentation on how to configure this file, | ||
| # see https://diesel.rs/guides/configuring-diesel-cli | ||
|
|
||
| [print_schema] | ||
| file = "src/db/schema.rs" | ||
| custom_type_derives = ["diesel::query_builder::QueryId", "Clone"] | ||
|
|
||
| [migrations_directory] | ||
| dir = "/Users/caden/Developer/dtim/migrations" | ||
|
Comment on lines
+8
to
+9
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Avoid absolute paths for migrations directory. The [migrations_directory]
-dir = "/Users/caden/Developer/dtim/migrations"
+dir = "migrations"🤖 Prompt for AI Agents |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| -- This file was automatically created by Diesel to setup helper functions | ||
| -- and other internal bookkeeping. This file is safe to edit, any future | ||
| -- changes will be added to existing projects as new migrations. | ||
|
|
||
| DROP FUNCTION IF EXISTS diesel_manage_updated_at(_tbl regclass); | ||
| DROP FUNCTION IF EXISTS diesel_set_updated_at(); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| -- This file was automatically created by Diesel to setup helper functions | ||
| -- and other internal bookkeeping. This file is safe to edit, any future | ||
| -- changes will be added to existing projects as new migrations. | ||
|
|
||
|
|
||
|
|
||
|
|
||
| -- Sets up a trigger for the given table to automatically set a column called | ||
| -- `updated_at` whenever the row is modified (unless `updated_at` was included | ||
| -- in the modified columns) | ||
| -- | ||
| -- # Example | ||
| -- | ||
| -- ```sql | ||
| -- CREATE TABLE users (id SERIAL PRIMARY KEY, updated_at TIMESTAMP NOT NULL DEFAULT NOW()); | ||
| -- | ||
| -- SELECT diesel_manage_updated_at('users'); | ||
| -- ``` | ||
| CREATE OR REPLACE FUNCTION diesel_manage_updated_at(_tbl regclass) RETURNS VOID AS $$ | ||
| BEGIN | ||
| EXECUTE format('CREATE TRIGGER set_updated_at BEFORE UPDATE ON %s | ||
| FOR EACH ROW EXECUTE PROCEDURE diesel_set_updated_at()', _tbl); | ||
| END; | ||
| $$ LANGUAGE plpgsql; | ||
|
|
||
| CREATE OR REPLACE FUNCTION diesel_set_updated_at() RETURNS trigger AS $$ | ||
| BEGIN | ||
| IF ( | ||
| NEW IS DISTINCT FROM OLD AND | ||
| NEW.updated_at IS NOT DISTINCT FROM OLD.updated_at | ||
| ) THEN | ||
| NEW.updated_at := current_timestamp; | ||
| END IF; | ||
| RETURN NEW; | ||
| END; | ||
| $$ LANGUAGE plpgsql; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| DROP TABLE encrypted_indicators; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Ensure safe, idempotent rollback of the table. Using a plain -DROP TABLE encrypted_indicators;
+DROP TABLE IF EXISTS encrypted_indicators CASCADE;🤖 Prompt for AI Agents |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| CREATE TABLE encrypted_indicators ( | ||
| id CHAR(64) PRIMARY KEY, | ||
| ciphertext BYTEA NOT NULL, | ||
| nonce BYTEA NOT NULL, | ||
| mac BYTEA NOT NULL, | ||
| tlp_level TEXT NOT NULL | ||
| ); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,19 +13,17 @@ use ed25519_dalek::VerifyingKey; | |
| use http_body_util::BodyExt as _; | ||
| use rustls::ServerConfig; | ||
| use serde::Serialize; | ||
| use std::{str::FromStr as _, sync::Arc}; | ||
| use std::sync::Arc; | ||
| use tokio::sync::Mutex; | ||
|
|
||
| use crate::models::ThreatIndicator; | ||
| use crate::{ | ||
| crypto::MeshIdentity, | ||
| db::models::EncryptedIndicator, | ||
| error::{ApiError, ApiErrorResponse}, | ||
| node::{Node, NodePeer}, | ||
| }; | ||
| use crate::{crypto::SymmetricKeyManager, models::StixBundle}; | ||
| use crate::{ | ||
| models::{EncryptedThreatIndicator, ThreatIndicator}, | ||
| uuid::Uuid, | ||
| }; | ||
|
|
||
| #[derive(Clone)] | ||
| pub struct AppState { | ||
|
|
@@ -200,13 +198,14 @@ struct GossipIndicatorsResponse { | |
|
|
||
| async fn gossip_indicators_handler( | ||
| State(state): State<Arc<AppState>>, | ||
| Json(indicators): Json<Vec<EncryptedThreatIndicator>>, | ||
| Json(indicators): Json<Vec<EncryptedIndicator>>, | ||
| ) -> ApiResponse<GossipIndicatorsResponse> { | ||
| let mut node = state.node.lock().await; | ||
| let mut count = 0; | ||
| for encrypted in indicators { | ||
| if let Ok(indicator) = ThreatIndicator::decrypt(&encrypted, &state.key_mgr) { | ||
| node.add_or_increment_indicator(indicator); | ||
| node.add_or_increment_indicator(indicator) | ||
| .map_err(|_| ApiError::INTERNAL_SERVER_ERROR)?; | ||
| count += 1; | ||
| } | ||
| } | ||
|
Comment on lines
199
to
211
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Minimise lock contention by decrypting before acquiring the
- let mut node = state.node.lock().await;
- let mut count = 0;
- for encrypted in indicators {
- if let Ok(indicator) = ThreatIndicator::decrypt(&encrypted, &state.key_mgr) {
- node.add_or_increment_indicator(indicator)
- .map_err(|_| ApiError::INTERNAL_SERVER_ERROR)?;
- count += 1;
- }
- }
+ // 1. Decrypt outside the critical section
+ let mut decrypted: Vec<ThreatIndicator> = indicators
+ .into_iter()
+ .filter_map(|enc| ThreatIndicator::decrypt(&enc, &state.key_mgr).ok())
+ .collect();
+
+ // 2. Acquire the lock *once* for all DB mutations
+ let mut node = state.node.lock().await;
+ let mut count = 0;
+ for indicator in decrypted.drain(..) {
+ node.add_or_increment_indicator(indicator)
+ .map_err(|_| ApiError::INTERNAL_SERVER_ERROR)?;
+ count += 1;
+ }Benefits 🤖 Prompt for AI Agents |
||
|
|
@@ -229,7 +228,9 @@ async fn get_public_indicators_handler( | |
| State(state): State<Arc<AppState>>, | ||
| ) -> ApiResponse<GetIndicatorsResponse> { | ||
| let node = state.node.lock().await; | ||
| let indicators = node.list_indicators_by_tlp(crate::models::TlpLevel::White); | ||
| let indicators = node | ||
| .list_indicators_by_tlp(crate::models::TlpLevel::White) | ||
| .map_err(|_| ApiError::INTERNAL_SERVER_ERROR)?; | ||
| // Return anonymized (open) view | ||
| Ok(( | ||
| StatusCode::OK, | ||
|
|
@@ -244,7 +245,9 @@ async fn get_private_indicators_handler( | |
| State(state): State<Arc<AppState>>, | ||
| ) -> ApiResponse<GetIndicatorsResponse> { | ||
| let node = state.node.lock().await; | ||
| let indicators = node.list_indicators_by_tlp(crate::models::TlpLevel::Red); | ||
| let indicators = node | ||
| .list_indicators_by_tlp(crate::models::TlpLevel::Red) | ||
| .map_err(|_| ApiError::INTERNAL_SERVER_ERROR)?; | ||
| // TODO: Ensure the node is an authenticated recipient for private indicators | ||
| // Return anonymized (moderate) view | ||
| Ok(( | ||
|
|
@@ -261,8 +264,9 @@ async fn get_indicator_by_id_handler( | |
| Path(id): Path<String>, | ||
| ) -> ApiResponse<serde_json::Value> { | ||
| let node = state.node.lock().await; | ||
| let id = Uuid::from_str(&id).map_err(|_| ApiError::INVALID_INDICATOR_ID)?; | ||
| let indicator = node.get_indicator_by_id(&id).ok_or(ApiError::NOT_FOUND)?; | ||
| let indicator = node | ||
| .get_indicator_by_id(&id) | ||
| .map_err(|_| ApiError::NOT_FOUND)?; | ||
| Ok((StatusCode::OK, Json(indicator.to_json(node.get_level())))) | ||
| } | ||
|
|
||
|
|
@@ -319,7 +323,12 @@ async fn taxii_get_objects_handler( | |
| Path(_collection_id): Path<String>, | ||
| ) -> ApiResponse<serde_json::Value> { | ||
| let node = state.node.lock().await; | ||
| let stix_objects = node.list_objects_by_tlp(crate::models::TlpLevel::White); | ||
| let stix_objects = node | ||
| .list_objects_by_tlp(crate::models::TlpLevel::White) | ||
| .map_err(|error| { | ||
| println!("Error: {:?}", error); | ||
| ApiError::INTERNAL_SERVER_ERROR | ||
| })?; | ||
| let bundle = StixBundle::new(stix_objects); | ||
| Ok((StatusCode::OK, Json(bundle.to_stix()))) | ||
| } | ||
|
|
@@ -331,6 +340,7 @@ async fn taxii_post_objects_handler( | |
| ) -> ApiResponse<serde_json::Value> { | ||
| let indicator = ThreatIndicator::from_stix(stix).map_err(|_| ApiError::INVALID_STIX_OBJECT)?; | ||
| let mut node = state.node.lock().await; | ||
| node.add_indicator(indicator); | ||
| node.add_indicator(indicator) | ||
| .map_err(|_| ApiError::INTERNAL_SERVER_ERROR)?; | ||
| Ok((StatusCode::OK, Json(serde_json::json!({ "status": "ok" })))) | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Review database connection string for security
The PostgreSQL connection string uses default credentials without a password. While acceptable for a local development example, this poses security risks:
Add clarifying comments to guide users on securing their database:
📝 Committable suggestion
🤖 Prompt for AI Agents