-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2a0885d
commit dbe803e
Showing
9 changed files
with
312 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,5 @@ members = [ | |
"graphql", | ||
"honey-badger", | ||
"mole", | ||
"squirrel", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
[package] | ||
name = "squirrel" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
hex = "0.4.3" | ||
log = "0.4.17" | ||
|
||
graphql = { path = "../graphql" } | ||
honey-badger = { path = "../honey-badger" } | ||
|
||
[dev-dependencies] | ||
bitcoin = { version = "0.29.2" } | ||
ctor = "0.2.0" | ||
rand = "0.8.5" | ||
simplelog = { version ="0.12.0", features = ["test"] } | ||
serial_test = "2.0.0" | ||
tokio = "1.32.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use graphql::errors::*; | ||
use graphql::perro::{runtime_error, MapToError}; | ||
use graphql::schema::*; | ||
use graphql::{build_async_client, post}; | ||
use honey_badger::Auth; | ||
use std::sync::Arc; | ||
|
||
#[derive(Debug, PartialEq)] | ||
pub struct Backup { | ||
pub encrypted_backup: Vec<u8>, | ||
pub schema_name: String, | ||
pub schema_version: String, | ||
} | ||
|
||
pub struct RemoteBackupClient { | ||
backend_url: String, | ||
auth: Arc<Auth>, | ||
} | ||
|
||
impl RemoteBackupClient { | ||
pub fn new(backend_url: String, auth: Arc<Auth>) -> Self { | ||
Self { backend_url, auth } | ||
} | ||
|
||
pub async fn create_backup(&self, backup: &Backup) -> Result<()> { | ||
let token = self.auth.query_token()?; | ||
let client = build_async_client(Some(&token))?; | ||
let variables = create_backup::Variables { | ||
encrypted_backup: graphql_hex_encode(&backup.encrypted_backup), | ||
schema_name: backup.schema_name.clone(), | ||
schema_version: backup.schema_version.clone(), | ||
}; | ||
post::<CreateBackup>(&client, &self.backend_url, variables).await?; | ||
|
||
Ok(()) | ||
} | ||
|
||
pub async fn recover_backup(&self, schema_name: &str) -> Result<Backup> { | ||
let token = self.auth.query_token()?; | ||
let client = build_async_client(Some(&token))?; | ||
let variables = recover_backup::Variables { | ||
schema_name: schema_name.to_string(), | ||
}; | ||
let data = post::<RecoverBackup>(&client, &self.backend_url, variables).await?; | ||
|
||
match data.recover_backup { | ||
None => Err(runtime_error( | ||
GraphQlRuntimeErrorCode::ObjectNotFound, | ||
"No backup found", | ||
)), | ||
Some(d) => Ok(Backup { | ||
encrypted_backup: graphql_hex_decode(&d.encrypted_backup.unwrap()).unwrap(), | ||
schema_name: schema_name.to_string(), | ||
schema_version: d.schema_version.unwrap(), | ||
}), | ||
} | ||
} | ||
} | ||
|
||
fn graphql_hex_encode(data: &Vec<u8>) -> String { | ||
format!("\\x{}", hex::encode(data)) | ||
} | ||
|
||
fn graphql_hex_decode(data: &str) -> Result<Vec<u8>> { | ||
hex::decode(data.replacen("\\x", "", 1)).map_to_runtime_error( | ||
GraphQlRuntimeErrorCode::CorruptData, | ||
"Could not decode hex encoded binary", | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
use bitcoin::Network; | ||
use graphql::perro::Error::RuntimeError; | ||
use graphql::GraphQlRuntimeErrorCode; | ||
use honey_badger::secrets::{derive_keys, generate_keypair, generate_mnemonic}; | ||
use honey_badger::{Auth, AuthLevel}; | ||
use rand::random; | ||
use simplelog::TestLogger; | ||
use squirrel::{Backup, RemoteBackupClient}; | ||
use std::env; | ||
use std::sync::{Arc, Once}; | ||
|
||
static INIT_LOGGER_ONCE: Once = Once::new(); | ||
|
||
#[cfg(test)] | ||
#[ctor::ctor] | ||
fn init() { | ||
INIT_LOGGER_ONCE.call_once(|| { | ||
TestLogger::init(simplelog::LevelFilter::Info, simplelog::Config::default()).unwrap(); | ||
}); | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_recovering_backup_when_there_is_none() { | ||
let client = build_backup_client(); | ||
|
||
let result = client.recover_backup("a").await; | ||
|
||
assert!(result.is_err()); | ||
assert!(matches!( | ||
result, | ||
Err(RuntimeError { | ||
code: GraphQlRuntimeErrorCode::ObjectNotFound, | ||
.. | ||
}) | ||
)); | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_backup_persistence() { | ||
let client = build_backup_client(); | ||
let dummy_backup_schema_a_version_1 = Backup { | ||
encrypted_backup: random::<[u8; 32]>().to_vec(), | ||
schema_name: "a".to_string(), | ||
schema_version: "1".to_string(), | ||
}; | ||
let dummy_backup_schema_a_version_2 = Backup { | ||
encrypted_backup: random::<[u8; 32]>().to_vec(), | ||
schema_name: "a".to_string(), | ||
schema_version: "2".to_string(), | ||
}; | ||
let dummy_backup_schema_b_version_1 = Backup { | ||
encrypted_backup: random::<[u8; 32]>().to_vec(), | ||
schema_name: "b".to_string(), | ||
schema_version: "1".to_string(), | ||
}; | ||
let dummy_backup_schema_b_version_2 = Backup { | ||
encrypted_backup: random::<[u8; 32]>().to_vec(), | ||
schema_name: "b".to_string(), | ||
schema_version: "2".to_string(), | ||
}; | ||
|
||
client | ||
.create_backup(&dummy_backup_schema_a_version_1) | ||
.await | ||
.unwrap(); | ||
client | ||
.create_backup(&dummy_backup_schema_b_version_1) | ||
.await | ||
.unwrap(); | ||
|
||
assert_eq!( | ||
client.recover_backup("a").await.unwrap(), | ||
dummy_backup_schema_a_version_1 | ||
); | ||
assert_eq!( | ||
client.recover_backup("b").await.unwrap(), | ||
dummy_backup_schema_b_version_1 | ||
); | ||
|
||
client | ||
.create_backup(&dummy_backup_schema_a_version_2) | ||
.await | ||
.unwrap(); | ||
client | ||
.create_backup(&dummy_backup_schema_b_version_2) | ||
.await | ||
.unwrap(); | ||
|
||
assert_eq!( | ||
client.recover_backup("a").await.unwrap(), | ||
dummy_backup_schema_a_version_2 | ||
); | ||
assert_eq!( | ||
client.recover_backup("b").await.unwrap(), | ||
dummy_backup_schema_b_version_2 | ||
); | ||
|
||
// non-existing schema name | ||
let result = client.recover_backup("c").await; | ||
|
||
assert!(result.is_err()); | ||
assert!(matches!( | ||
result, | ||
Err(RuntimeError { | ||
code: GraphQlRuntimeErrorCode::ObjectNotFound, | ||
.. | ||
}) | ||
)); | ||
} | ||
|
||
fn build_backup_client() -> RemoteBackupClient { | ||
println!("Generating keys ..."); | ||
let mnemonic = generate_mnemonic(); | ||
println!("mnemonic: {mnemonic:?}"); | ||
let wallet_keys = derive_keys(Network::Testnet, mnemonic).wallet_keypair; | ||
let auth_keys = generate_keypair(); | ||
|
||
let auth = Auth::new( | ||
get_backend_url(), | ||
AuthLevel::Pseudonymous, | ||
wallet_keys, | ||
auth_keys, | ||
) | ||
.unwrap(); | ||
|
||
RemoteBackupClient::new(get_backend_url(), Arc::new(auth)) | ||
} | ||
|
||
fn get_backend_url() -> String { | ||
env::var("GRAPHQL_API_URL").expect("GRAPHQL_API_URL environment variable is not set") | ||
} |