Skip to content

Commit

Permalink
Fixes build errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Hermann-Core committed Oct 21, 2024
1 parent 00cb5c1 commit 194710e
Show file tree
Hide file tree
Showing 26 changed files with 408 additions and 190 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ Cargo.lock

# Environment variables files
.env.example
.env.test
.env

# Reference crate
mediator-server

# Test directory
test/
9 changes: 8 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,23 @@ tower-http = { workspace = true, features = ["catch-panic", "trace"] }

# optional dependencies
chrono = { workspace = true, optional = true }
plugins = { workspace = true, optional = true }
did-endpoint = { workspace = true, optional = true }
oob-messages = { workspace = true, optional = true }


[features]
default = ["plugin-index", "plugin-did_endpoint", "plugin-oob_messages"]
default = [
"plugin-index",
"plugin-did_endpoint",
"plugin-oob_messages",
"plugin-plugins",
]

plugin-index = ["dep:chrono"]
plugin-did_endpoint = ["dep:did-endpoint"]
plugin-oob_messages = ["dep:oob-messages"]
plugin-plugins = ["dep:plugins"]


[dev-dependencies]
Expand Down
30 changes: 14 additions & 16 deletions crates/database/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use once_cell::sync::OnceCell;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use thiserror::Error;
use tokio::{runtime::Runtime, sync::Mutex};
use tokio::sync::Mutex;

/// A trait that ensures the entity has an `id` field.
pub trait Identifiable {
Expand All @@ -33,7 +33,7 @@ pub enum RepositoryError {
static MONGO_DB: OnceCell<Arc<Mutex<Database>>> = OnceCell::new();

/// Get a handle to a database.
///
///
/// Many threads may call this function concurrently with different initializing functions,
/// but it is guaranteed that only one function will be executed.
pub fn get_or_init_database() -> Arc<Mutex<Database>> {
Expand All @@ -42,17 +42,17 @@ pub fn get_or_init_database() -> Arc<Mutex<Database>> {
let mongo_uri = std::env::var("MONGO_URI").expect("MONGO_URI env variable required");
let mongo_dbn = std::env::var("MONGO_DBN").expect("MONGO_DBN env variable required");

// Create a runtime to run the async MongoDB initialization synchronously
let rt = Runtime::new().unwrap();

let db = rt.block_on(async {
let client_options = ClientOptions::parse(mongo_uri)
.await
.expect("Failed to parse Mongo URI");
let client =
Client::with_options(client_options).expect("Failed to create MongoDB client");

client.database(&mongo_dbn)
// Create a handle to a database.
let db = tokio::task::block_in_place(|| {
tokio::runtime::Handle::current().block_on(async move {
let client_options = ClientOptions::parse(mongo_uri)
.await
.expect("Failed to parse Mongo URI");
let client = Client::with_options(client_options)
.expect("Failed to create MongoDB client");

client.database(&mongo_dbn)
})
});

// Get a handle to a database.
Expand Down Expand Up @@ -141,9 +141,7 @@ where
let collection = collection.lock().await;

// Delete the entity from the database
collection
.delete_one(doc! {"_id": id}, None)
.await?;
collection.delete_one(doc! {"_id": id}, None).await?;

Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion crates/did-utils/src/didkit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl Document {
also_known_as: None,
controller: None,
authentication: Some(vec![]),
assertion_method: None,
assertion_method: Some(vec![]),
capability_delegation: None,
capability_invocation: None,
key_agreement: Some(vec![]),
Expand Down
25 changes: 13 additions & 12 deletions crates/keystore/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use mongodb::{bson::oid::ObjectId, Collection};
use once_cell::sync::OnceCell;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use tokio::{sync::Mutex, runtime::Runtime};
use tokio::sync::Mutex;

static SECRETS_COLLECTION: OnceCell<Collection<Secrets>> = OnceCell::new();

Expand Down Expand Up @@ -42,25 +42,26 @@ where

impl KeyStore<Secrets> {
/// Create a new keystore with default Secrets type.
///
///
/// Calling this method many times will return the same keystore instance.
pub fn new() -> KeyStore<Secrets> {
let collection = SECRETS_COLLECTION.get_or_init(|| {
// Initialize runtime to run the async MongoDB initialization
let rt = Runtime::new().expect("Failed to create tokio runtime");
let db = database::get_or_init_database();

rt.block_on(async {
let db_lock = db.lock().await;
db_lock.collection::<Secrets>("secrets").clone()
let collection = SECRETS_COLLECTION
.get_or_init(|| {
let db = database::get_or_init_database();
let task = async move {
let db_lock = db.lock().await;
db_lock.collection::<Secrets>("secrets").clone()
};
let collection = tokio::task::block_in_place(|| tokio::runtime::Handle::current().block_on(task));
collection
})
}).clone();
.clone();

KeyStore { collection }
}

/// Retrieve the keystore instance.
///
///
/// If there is no keystore instance, a new one will be created only once.
pub fn get() -> KeyStore<Secrets> {
Self::new()
Expand Down
20 changes: 20 additions & 0 deletions crates/plugins/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,29 @@ version = "0.1.0"
edition = "2021"

[dependencies]
database.workspace = true
did-endpoint.workspace = true
keystore.workspace = true
shared.workspace = true
plugin-api.workspace = true
filesystem.workspace = true
forward.workspace = true
pickup.workspace = true
mediator-coordination.workspace = true

mongodb.workspace = true
didcomm.workspace = true
tracing.workspace = true
serde_json.workspace = true
tokio = { workspace = true, features = ["full"] }
hyper = { workspace = true, features = ["full"] }
axum = { workspace = true, features = ["macros"] }

[dev-dependencies]
json-canon = "0.1.3"
shared = { workspace = true, features = ["test-utils"] }
tokio = { version = "1.27.0", default-features = false, features = [
"macros",
"rt",
] }
tower = { version = "0.4.13", features = ["util"] }
88 changes: 63 additions & 25 deletions crates/plugins/did-endpoint/src/didgen.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use database::Repository;
use did_utils::{
crypto::{Ed25519KeyPair, Generate, PublicKeyFormat, ToMultikey, X25519KeyPair},
didcore::{Authentication, Document, KeyAgreement, KeyFormat, Service},
didcore::{AssertionMethod, Authentication, Document, KeyAgreement, KeyFormat, Service},
jwk::Jwk,
methods::{DidPeer, Purpose, PurposedKey},
};
Expand Down Expand Up @@ -43,6 +43,10 @@ where
purpose: Purpose::Verification,
public_key_multibase: auth_keys.to_multikey(),
},
PurposedKey {
purpose: Purpose::Assertion,
public_key_multibase: auth_keys.to_multikey(),
},
];

// Build services
Expand Down Expand Up @@ -83,14 +87,16 @@ where
// Create a new KeyStore
let keystore = KeyStore::new();

// Store the agreement key in the screts store
tokio::runtime::Runtime::new().unwrap().block_on(async {
match keystore.store(agreem_keys_secret).await {
Ok(_) => {
tracing::info!("Successfully stored agreement key.")
// Store the agreement key in the secrets store
tokio::task::block_in_place(|| {
tokio::runtime::Handle::current().block_on(async {
match keystore.store(agreem_keys_secret).await {
Ok(_) => {
tracing::info!("Successfully stored agreement key.")
}
Err(error) => tracing::error!("Error storing agreement key: {:?}", error),
}
Err(error) => tracing::error!("Error storing agreement key: {:?}", error),
}
})
});

let auth_keys_jwk: Jwk = auth_keys.try_into().expect("MediateRequestError");
Expand All @@ -108,17 +114,47 @@ where
Authentication::Reference(kid) => kid,
_ => unreachable!(),
},
secret_material: auth_keys_jwk.clone(),
};

// Store the authentication key in the secrets store
tokio::task::block_in_place(|| {
tokio::runtime::Handle::current().block_on(async {
match keystore.store(auth_keys_secret).await {
Ok(_) => {
tracing::info!("Successfully stored authentication key.")
}
Err(error) => tracing::error!("Error storing authentication key: {:?}", error),
}
})
});

let assert_keys_secret = Secrets {
id: None,
kid: match diddoc
.assertion_method
.as_ref()
.unwrap()
.get(0)
.unwrap()
.clone()
{
AssertionMethod::Reference(kid) => kid,
_ => unreachable!(),
},
secret_material: auth_keys_jwk,
};

// Store the authentication key in the screts store
tokio::runtime::Runtime::new().unwrap().block_on(async {
match keystore.store(auth_keys_secret).await {
Ok(_) => {
tracing::info!("Successfully stored authentication key.")
// Store the assertion key in the secrets store
tokio::task::block_in_place(|| {
tokio::runtime::Handle::current().block_on(async {
match keystore.store(assert_keys_secret).await {
Ok(_) => {
tracing::info!("Successfully stored assertion key.")
}
Err(error) => tracing::error!("Error storing assertion key: {:?}", error),
}
Err(error) => tracing::error!("Error storing authentication key: {:?}", error),
}
})
});

// Serialize and persist to file
Expand Down Expand Up @@ -159,12 +195,12 @@ where
_ => return Err(String::from("Unsupported key format")),
};

// Create a new KeyStore
let keystore = KeyStore::get();

let secret = tokio::runtime::Runtime::new()
.unwrap()
.block_on(async { keystore.find_one_by(doc! { "kid": method.id }).await });
let secret = tokio::task::block_in_place(|| {
tokio::runtime::Handle::current()
.block_on(async { keystore.find_one_by(doc! { "kid": method.id }).await })
});

secret
.map_err(|_| String::from("Error fetching secret"))?
Expand Down Expand Up @@ -195,18 +231,20 @@ mod tests {

// Verifies that the didgen function returns a DID document.
// Does not validate the DID document.
#[test]
fn test_didgen() {
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_didgen() {
dotenv_flow::from_filename("../../../.env").ok();
let (storage_dirpath, server_public_domain) = setup();

let diddoc = didgen(&storage_dirpath, &server_public_domain).unwrap();
assert_eq!(diddoc.id, "did:web:example.com");
let diddoc = didgen(&storage_dirpath, &server_public_domain);
assert!(diddoc.is_ok());

cleanup(&storage_dirpath);
}

#[test]
fn test_validate_diddoc() {
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_validate_diddoc() {
dotenv_flow::from_filename("../../../.env").ok();
let (storage_dirpath, server_public_domain) = setup();

didgen(&storage_dirpath, &server_public_domain).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion crates/plugins/did-endpoint/src/util.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[cfg(test)]
pub(crate) fn dotenv_flow_read(key: &str) -> Option<String> {
dotenv_flow::from_filename_iter(".env.example")
dotenv_flow::from_filename_iter("../../../.env")
.unwrap()
.find_map(|item| {
let (k, v) = item.unwrap();
Expand Down
8 changes: 7 additions & 1 deletion crates/plugins/mediator-coordination/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ did-endpoint.workspace = true
plugin-api.workspace = true
database.workspace = true
filesystem.workspace = true
keystore.workspace = true

chrono.workspace = true
mongodb.workspace = true
Expand All @@ -31,10 +32,15 @@ lazy_static.workspace = true
dotenv-flow = "0.15.0"
hyper = "0.14.27"
json-canon = "0.1.3"
tokio = { version = "1.27.0", default-features = false, features = ["macros", "rt"] }
tokio = { version = "1.27.0", default-features = false, features = [
"macros",
"rt",
] }
tokio-test = "0.4.2"
tower = { version = "0.4.13", features = ["util"] }
shared = { workspace = true, features = ["test-utils"] }

[features]
default = ["stateful"]
stateful = []
stateless = []
17 changes: 5 additions & 12 deletions crates/plugins/mediator-coordination/src/client/dic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,24 +67,17 @@ mod tests {
use did_utils::crypto::ToPublic;
use multibase::Base::Base64Url;
use serde_json::Value;
use shared::utils;
use filesystem::MockFileSystem;

fn setup() -> Jwk {
let mut mock_fs = MockFileSystem;

let diddoc = utils::read_diddoc(&mock_fs, "").unwrap();
let (_, pubkey) = utils::extract_assertion_key(&diddoc).unwrap();

let secret: Jwk = serde_json::from_str(
serde_json::from_str(
r#"{
"kty": "OKP",
"crv": "X25519",
"x": "SHSUZ6V3x355FqCzIUfgoPzrZB0BQs0JKyag4UfMqHQ",
"d": "0A8SSFkGHg3N9gmVDRnl63ih5fcwtEvnQu9912SVplY"
"crv": "Ed25519",
"x": "Z0GqpN71rMcnAkky6_J6Bfknr8B-TBsekG3qdI0EQX4",
"d": "fI1u4riKKd99eox08GlThknq-vEJXcKBI28aiUqArLo"
}"#,
)
.unwrap();
.unwrap()
}

#[test]
Expand Down
Loading

0 comments on commit 194710e

Please sign in to comment.