diff --git a/Cargo.lock b/Cargo.lock index 9dd9d2f..a27ac7f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -699,6 +699,7 @@ version = "0.1.0" dependencies = [ "anyhow", "async-trait", + "bincode", "chrono", "deadpool", "futures-util", diff --git a/giterated-daemon/src/connection/wrapper.rs b/giterated-daemon/src/connection/wrapper.rs index c4b762a..2f9b8e4 100644 --- a/giterated-daemon/src/connection/wrapper.rs +++ b/giterated-daemon/src/connection/wrapper.rs @@ -23,7 +23,7 @@ use toml::Table; use crate::{ authentication::AuthenticationTokenGranter, backend::{MetadataBackend, RepositoryBackend, UserBackend}, - database_backend::Foobackend, + database_backend::DatabaseBackend, federation::connections::InstanceConnections, keys::PublicKeyCache, }; @@ -41,6 +41,7 @@ pub async fn connection_wrapper( instance: impl ToOwned, instance_connections: Arc>, config: Table, + backend: DatabaseBackend, ) { let connection_state = ConnectionState { socket: Arc::new(Mutex::new(socket)), @@ -82,8 +83,6 @@ pub async fn connection_wrapper( let message: GiteratedMessage = message.into_message(); - let backend = Foobackend {}; - backend .object_operation(message.object, message.payload) .await diff --git a/giterated-daemon/src/database_backend/mod.rs b/giterated-daemon/src/database_backend/mod.rs index ac4fa5c..b93adce 100644 --- a/giterated-daemon/src/database_backend/mod.rs +++ b/giterated-daemon/src/database_backend/mod.rs @@ -1,12 +1,15 @@ pub mod handler; +use std::any::type_name; use std::{str::FromStr, sync::Arc}; use giterated_models::error::OperationError; use giterated_models::instance::Instance; -use giterated_models::object::{GiteratedObject, Object, ObjectRequestError}; +use giterated_models::object::{ + GiteratedObject, Object, ObjectRequest, ObjectRequestError, ObjectResponse, +}; use giterated_models::object_backend::ObjectBackend; -use giterated_models::operation::GiteratedOperation; +use giterated_models::operation::{AnyOperation, GiteratedOperation}; use giterated_models::repository::Repository; use giterated_models::user::User; use std::fmt::Debug; @@ -49,6 +52,20 @@ pub struct DatabaseBackend { pub(self) repository_backend: Arc>, } +impl DatabaseBackend { + pub fn new( + instance: Instance, + user_backend: Arc>, + repository_backend: Arc>, + ) -> Self { + Self { + our_instance: instance, + user_backend, + repository_backend, + } + } +} + impl Debug for DatabaseBackend { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_struct("DatabaseBackend").finish() @@ -121,8 +138,29 @@ impl ObjectBackend for DatabaseBackend { )), }, } - } else if serde_json::from_str::(&object).is_ok() { - Err(OperationError::Unhandled) + } else if let Ok(instance) = Instance::from_str(&object) { + if instance != self.our_instance { + // We don't handle other objects currently + return Err(OperationError::Unhandled); + } + + if let Ok(object_request) = serde_json::from_value::(serialized.clone()) + { + // No-op + let response = serde_json::to_string(&ObjectResponse(object_request.0)).unwrap(); + + info!("Target: {}", type_name::()); + info!("Meow: {}", response); + info!( + "Im just a neko! {:?}", + serde_json::from_str::(&response) + ); + + Ok(serde_json::from_str(&response) + .map_err(|e| OperationError::Internal(e.to_string()))?) + } else { + Err(OperationError::Unhandled) + } } else { Err(OperationError::Unhandled) } @@ -168,7 +206,12 @@ impl ObjectBackend for DatabaseBackend { } else { return Err(OperationError::Unhandled); } - } else if Instance::from_str(object_str).is_ok() { + } else if let Ok(instance) = Instance::from_str(object_str) { + if instance != self.our_instance { + // We don't handle other objects currently + return Err(OperationError::Unhandled); + } + return Err(OperationError::Unhandled); } else { // Invalid object type diff --git a/giterated-daemon/src/main.rs b/giterated-daemon/src/main.rs index 797d5be..d2b8b75 100644 --- a/giterated-daemon/src/main.rs +++ b/giterated-daemon/src/main.rs @@ -6,6 +6,7 @@ use giterated_daemon::{ git::GitBackend, settings::DatabaseSettings, user::UserAuth, RepositoryBackend, UserBackend, }, connection::{self, wrapper::connection_wrapper}, + database_backend::DatabaseBackend, federation::connections::InstanceConnections, }; @@ -81,6 +82,12 @@ async fn main() -> Result<(), Error> { info!("Connected"); + let database_backend = DatabaseBackend::new( + Instance::from_str(config["giterated"]["instance"].as_str().unwrap()).unwrap(), + user_backend.clone(), + repository_backend.clone(), + ); + loop { let stream = accept_stream(&mut listener).await; info!("Connected"); @@ -122,6 +129,7 @@ async fn main() -> Result<(), Error> { Instance::from_str(config["giterated"]["instance"].as_str().unwrap()).unwrap(), instance_connections.clone(), config.clone(), + database_backend.clone(), )), }; diff --git a/giterated-models/src/authenticated.rs b/giterated-models/src/authenticated.rs index d5f1994..53895ad 100644 --- a/giterated-models/src/authenticated.rs +++ b/giterated-models/src/authenticated.rs @@ -42,10 +42,13 @@ pub struct AuthenticatedPayload { impl AuthenticatedPayload { pub fn into_message(self) -> GiteratedMessage { + info!("Into message: {:#?}", self); + let payload = serde_json::from_slice::(&self.payload).unwrap(); + info!("Message payload: {}", payload); GiteratedMessage { object: AnyObject(self.object), operation: self.operation, - payload: AnyOperation(serde_json::from_slice::(&self.payload).unwrap()), + payload: AnyOperation(payload), } } } @@ -103,7 +106,7 @@ impl> Authenticated { } pub fn into_payload(mut self) -> AuthenticatedPayload { - let payload = bincode::serialize(&self.message.payload).unwrap(); + let payload = serde_json::to_vec(&self.message.payload).unwrap(); AuthenticatedPayload { object: self.message.object.to_string(), diff --git a/giterated-models/src/object/operations.rs b/giterated-models/src/object/operations.rs index 0611ce3..5b5605a 100644 --- a/giterated-models/src/object/operations.rs +++ b/giterated-models/src/object/operations.rs @@ -10,7 +10,7 @@ use super::GiteratedObject; pub struct ObjectRequest(pub String); #[derive(Serialize, Deserialize)] -pub struct ObjectResponse(pub Vec); +pub struct ObjectResponse(pub String); impl GiteratedOperation for ObjectRequest { type Success = ObjectResponse; diff --git a/giterated-models/src/repository/mod.rs b/giterated-models/src/repository/mod.rs index ae04ba7..1864a32 100644 --- a/giterated-models/src/repository/mod.rs +++ b/giterated-models/src/repository/mod.rs @@ -78,11 +78,19 @@ impl FromStr for Repository { fn from_str(s: &str) -> Result { let mut by_ampersand = s.split('@'); - let mut path_split = by_ampersand.next().unwrap().split('/'); + let mut path_split = by_ampersand + .next() + .ok_or_else(|| RepositoryParseError)? + .split('/'); - let instance = Instance::from_str(by_ampersand.next().unwrap()).unwrap(); - let owner = User::from_str(path_split.next().unwrap()).unwrap(); - let name = path_split.next().unwrap().to_string(); + let instance = Instance::from_str(by_ampersand.next().ok_or_else(|| RepositoryParseError)?) + .map_err(|_| RepositoryParseError)?; + let owner = User::from_str(path_split.next().ok_or_else(|| RepositoryParseError)?) + .map_err(|_| RepositoryParseError)?; + let name = path_split + .next() + .ok_or_else(|| RepositoryParseError)? + .to_string(); Ok(Self { instance, @@ -93,7 +101,8 @@ impl FromStr for Repository { } #[derive(Debug, thiserror::Error)] -pub enum RepositoryParseError {} +#[error("no parse!")] +pub struct RepositoryParseError; /// Visibility of the repository to the general eye #[derive(PartialEq, Eq, Debug, Hash, Serialize, Deserialize, Clone, sqlx::Type)] diff --git a/giterated-models/src/user/mod.rs b/giterated-models/src/user/mod.rs index 63e1a3e..16a0ab3 100644 --- a/giterated-models/src/user/mod.rs +++ b/giterated-models/src/user/mod.rs @@ -72,8 +72,12 @@ impl FromStr for User { } let mut colon_split = s.split(':'); - let username = colon_split.next().unwrap().to_string(); - let instance = Instance::from_str(colon_split.next().unwrap()).unwrap(); + let username = colon_split + .next() + .ok_or_else(|| UserParseError)? + .to_string(); + let instance = Instance::from_str(colon_split.next().ok_or_else(|| UserParseError)?) + .map_err(|_| UserParseError)?; Ok(Self { username, instance }) }