From 5e4a2c1df2aceef1ad736f6ddd9d3170711a8510 Mon Sep 17 00:00:00 2001 From: Katharina Fey Date: Tue, 12 Jun 2018 01:24:27 +0200 Subject: =?UTF-8?q?Adding=20/api=20and=20/vault=20endpoints=20as=20first?= =?UTF-8?q?=20working=20routes=20=E2=9C=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.lock | 24 ++++++++++++------------ lockchain-core/src/lib.rs | 2 +- lockchain-core/src/meta.rs | 7 +++++++ lockchain-core/src/traits.rs | 4 +++- lockchain-files/src/lib.rs | 14 +++++++++++--- lockchain-http/src/handlers.rs | 27 +++++++++++++++++++++++++-- lockchain-http/src/lib.rs | 6 +++++- lockchain-http/src/model.rs | 20 ++++++++++++++++++-- lockchain-server/src/main.rs | 19 +++++++++---------- 9 files changed, 91 insertions(+), 32 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b67d7a1..af85363 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -298,7 +298,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "chrono" -version = "0.4.2" +version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "num-integer 0.1.38 (registry+https://github.com/rust-lang/crates.io-index)", @@ -801,17 +801,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index" name = "lockchain-client" version = "0.0.0" dependencies = [ - "lockchain-core 0.8.0", + "lockchain-core 0.8.1", ] [[package]] name = "lockchain-core" -version = "0.8.0" +version = "0.8.1" dependencies = [ "base64 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "bcrypt 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "blake2 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", - "chrono 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", + "chrono 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", @@ -822,7 +822,7 @@ dependencies = [ name = "lockchain-crypto" version = "0.7.0" dependencies = [ - "lockchain-core 0.8.0", + "lockchain-core 0.8.1", "miscreant 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", @@ -832,7 +832,7 @@ dependencies = [ name = "lockchain-files" version = "0.7.1" dependencies = [ - "lockchain-core 0.8.0", + "lockchain-core 0.8.1", ] [[package]] @@ -843,7 +843,7 @@ dependencies = [ "actix-web 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)", - "lockchain-core 0.8.0", + "lockchain-core 0.8.1", "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -854,7 +854,7 @@ version = "0.1.0" dependencies = [ "clap 2.31.2 (registry+https://github.com/rust-lang/crates.io-index)", "insult 2.0.3 (registry+https://github.com/rust-lang/crates.io-index)", - "lockchain-core 0.8.0", + "lockchain-core 0.8.1", "lockchain-files 0.7.1", "lockchain-http 0.2.1", ] @@ -1023,7 +1023,7 @@ name = "owning_ref" version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "stable_deref_trait 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "stable_deref_trait 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1375,7 +1375,7 @@ dependencies = [ [[package]] name = "stable_deref_trait" -version = "1.0.0" +version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -1873,7 +1873,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum cargo_metadata 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)" = "682476b87b3e22cd3820d86b26cd8603cd84ab76dce7547b2631858347aa8967" "checksum cc 1.0.17 (registry+https://github.com/rust-lang/crates.io-index)" = "49ec142f5768efb5b7622aebc3fdbdbb8950a4b9ba996393cb76ef7466e8747d" "checksum cfg-if 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "405216fd8fe65f718daa7102ea808a946b6ce40c742998fbfd3463645552de18" -"checksum chrono 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1cce36c92cb605414e9b824f866f5babe0a0368e39ea07393b9b63cf3844c0e6" +"checksum chrono 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a81892f0d5a53f46fc05ef0b917305a81c13f1f13bb59ac91ff595817f0764b1" "checksum clap 2.31.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f0f16b89cbb9ee36d87483dc939fe9f1e13c05898d56d7b230a0d4dff033a536" "checksum clear_on_drop 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "97276801e127ffb46b66ce23f35cc96bd454fa311294bced4bbace7baa8b1d17" "checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" @@ -1996,7 +1996,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum slab 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fdeff4cd9ecff59ec7e3744cbca73dfe5ac35c2aedb2cfba8a1c715a18912e9d" "checksum smallvec 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "03dab98ab5ded3a8b43b2c80751194608d0b2aa0f1d46cf95d1c35e192844aa7" "checksum socket2 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "06dc9f86ee48652b7c80f3d254e3b9accb67a928c562c64d10d7b016d3d98dab" -"checksum stable_deref_trait 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "15132e0e364248108c5e2c02e3ab539be8d6f5d52a01ca9bbf27ed657316f02b" +"checksum stable_deref_trait 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ffbc596e092fe5f598b12ef46cc03754085ac2f4d8c739ad61c4ae266cc3b3fa" "checksum string 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "31f98b200e7caca9efca50fc0aa69cd58a5ec81d5f6e75b2f3ecaad2e998972a" "checksum strsim 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bb4f380125926a99e52bc279241539c018323fab05ad6368b56f93d9369ff550" "checksum subtle 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c7a6bab57c3efd01ebd3d750f4244ae0af4cdd1fc505a7904a41603192b803c5" diff --git a/lockchain-core/src/lib.rs b/lockchain-core/src/lib.rs index abf72c3..d9b90ef 100644 --- a/lockchain-core/src/lib.rs +++ b/lockchain-core/src/lib.rs @@ -21,4 +21,4 @@ mod record; pub use self::crypto::PackedData; pub use self::record::{Header, Payload, Record, EncryptedBody}; -pub use self::meta::MetaDomain; \ No newline at end of file +pub use self::meta::{MetaDomain, VaultMetadata}; \ No newline at end of file diff --git a/lockchain-core/src/meta.rs b/lockchain-core/src/meta.rs index 8973b7d..503c7ff 100644 --- a/lockchain-core/src/meta.rs +++ b/lockchain-core/src/meta.rs @@ -8,6 +8,13 @@ use record::Payload; use std::collections::HashMap; use traits::{AutoEncoder, Body}; +/// A simple representation of metadata for a vault or vault section +pub struct VaultMetadata { + pub name: String, + pub location: String, + pub size: usize, +} + /// A metadomain is a simplified version of a cleartext record. /// /// It is not encoded in special ways, it is not used in any way diff --git a/lockchain-core/src/traits.rs b/lockchain-core/src/traits.rs index 963d2c6..9ef2903 100644 --- a/lockchain-core/src/traits.rs +++ b/lockchain-core/src/traits.rs @@ -11,7 +11,7 @@ //! compilation work without external crates but not calling //! functions at runtime. -use meta::MetaDomain; +use meta::{MetaDomain, VaultMetadata}; use record::{EncryptedBody, Header, Payload, Record}; use serde::{de::DeserializeOwned, Serialize}; @@ -88,6 +88,8 @@ where { /// A shared constructor for all vault implementations fn new(name: &str, location: &str) -> Self; + /// Get basic vault metadata + fn metadata(&self) -> VaultMetadata; /// Fetch metadata headers for all records fn fetch(&mut self); /// Pull a specific record from the backend diff --git a/lockchain-files/src/lib.rs b/lockchain-files/src/lib.rs index b1974f1..d8ac864 100644 --- a/lockchain-files/src/lib.rs +++ b/lockchain-files/src/lib.rs @@ -6,9 +6,8 @@ extern crate lockchain_core as lcc; use lcc::traits::{Body, Vault}; -use lcc::{MetaDomain, Payload, Record}; +use lcc::{MetaDomain, Payload, Record, VaultMetadata}; use std::collections::HashMap; -use std::fmt::Debug; mod fs; use fs::{FileType, Filesystem}; @@ -16,6 +15,7 @@ use fs::{FileType, Filesystem}; /// Represents a vault on disk #[derive(Debug)] pub struct DataVault { + meta_info: (String, String), records: HashMap>, metadata: HashMap, fs: Filesystem, @@ -27,18 +27,26 @@ impl DataVault { self.fs.scaffold(); self } - } impl Vault for DataVault { fn new(name: &str, location: &str) -> DataVault { Self { + meta_info: (name.into(), location.into()), records: HashMap::new(), metadata: HashMap::new(), fs: Filesystem::create(location, name), }.initialize() } + fn metadata(&self) -> VaultMetadata { + VaultMetadata { + name: self.meta_info.0.clone(), + location: self.meta_info.1.clone(), + size: self.records.len(), + } + } + /// Caches all files from disk to memory fn fetch(&mut self) { self.records.clear(); diff --git a/lockchain-http/src/handlers.rs b/lockchain-http/src/handlers.rs index 77bc823..4fc3c27 100644 --- a/lockchain-http/src/handlers.rs +++ b/lockchain-http/src/handlers.rs @@ -5,14 +5,25 @@ use lockchain::{ traits::{Body, Vault}, Record, }; -use model::CarrierMessage; +use model::*; use std::sync::{Arc, Mutex}; type HttpRequestState = HttpRequest>>; +/// GET /vault +/// +/// Check the documentation for more information about how to provide payloads +pub fn get_vaults(req: HttpRequestState>) -> impl Responder { + let meta = req.state().lock().unwrap().metadata(); + Json(VaultList { + vaults: vec![meta.name], + count: meta.size, + }) +} + /// PUT /vault -/// +/// /// Check the documentation for more information about how to provide payloads pub fn create_vault(_req: HttpRequestState>) -> impl Responder { format!("Unimplemented!") @@ -64,3 +75,15 @@ pub fn authenticate(_req: HttpRequestState>) -> impl Resp pub fn deauthenticate(_req: HttpRequestState>) -> impl Responder { format!("Unimplemented!") } + +/// GET /api +/// +/// Check the documentation for more information about how to provide payloads +pub fn api_data(_: HttpRequestState>) -> impl Responder { + Json(ApiInformation { + version: "1.0".into(), + providers: vec!["FileVault".into(), "EncryptedBody".into()], + hostname: None, + supported: "1.0".into(), + }) +} diff --git a/lockchain-http/src/lib.rs b/lockchain-http/src/lib.rs index a3872e1..6468967 100644 --- a/lockchain-http/src/lib.rs +++ b/lockchain-http/src/lib.rs @@ -61,6 +61,9 @@ pub fn create_server( server::new(move || { vec![ App::with_state(Arc::clone(&state)) + .resource("/vault", |r| { + r.method(http::Method::GET).with(handlers::get_vaults) + }) .resource("/vault", |r| { r.method(http::Method::PUT).with(handlers::create_vault) }) @@ -77,7 +80,8 @@ pub fn create_server( r.f(handlers::delete_record) }) .resource("/authenticate", |r| r.f(handlers::authenticate)) - .resource("/deauthenticate", |r| r.f(handlers::deauthenticate)), + .resource("/deauthenticate", |r| r.f(handlers::deauthenticate)) + .resource("/api", |r| r.f(handlers::api_data)), ] }).bind(format!("{}:{}", bind, port)) .expect("Oh no!") diff --git a/lockchain-http/src/model.rs b/lockchain-http/src/model.rs index f9e2665..ce92e1a 100644 --- a/lockchain-http/src/model.rs +++ b/lockchain-http/src/model.rs @@ -15,9 +15,25 @@ pub struct CarrierMessage { pub data: Option, } +/// **Returns** Api information +#[derive(Serialize, Deserialize)] +pub struct ApiInformation { + pub version: String, + pub providers: Vec, + pub hostname: Option, + pub supported: String, +} + +/// **Returns** List existing vaults +#[derive(Serialize, Deserialize)] +pub struct VaultList { + pub vaults: Vec, + pub count: usize, +} + /// Fields provided when creating a new vault #[derive(Serialize, Deserialize)] pub struct VaultCreate { - name: String, - location: String, + pub name: String, + pub location: String, } diff --git a/lockchain-server/src/main.rs b/lockchain-server/src/main.rs index 8ffbaa1..6e22b53 100644 --- a/lockchain-server/src/main.rs +++ b/lockchain-server/src/main.rs @@ -6,18 +6,17 @@ extern crate lockchain_core as core; extern crate lockchain_files as files; extern crate lockchain_http as http; -// use core::{traits::*, EncryptedBody}; -// use files::DataVault; -// use http::create_server; +use core::{traits::*, EncryptedBody}; +use files::DataVault; +use http::create_server; fn main() { - - // let server = create_server( - // "localhost", - // "8080", - // DataVault::::new("name", "location"), - // ); - // server.run(); + let server = create_server( + "localhost", + "8080", + DataVault::::new("name", "location"), + ); + server.run(); // println!("After the server died!"); } -- cgit v1.2.3