Skip to content

Commit

Permalink
Move to tokio 0.2 and futures 3 and make groups api async (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
coltfred authored Nov 27, 2019
1 parent 7a5d44c commit 0e340cd
Show file tree
Hide file tree
Showing 14 changed files with 786 additions and 911 deletions.
14 changes: 6 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,19 @@ base64-serde = "~0.3.1"
bytes = "~0.4.12"
serde = "~1.0"
serde_json = "~1.0"
publicsuffix = "=1.5.2" # exact version needed to match url used by reqwest 0.9
publicsuffix = "^1.5.4"
serde_derive = "~1.0"
rand = "~0.6"
rand_chacha = "~0.1"
regex = "~1.1"
regex = "^1.0"
ring = { version= "~0.16", features = ["std"] }
recrypt = "~0.9"
reqwest = "~0.9"
reqwest10 = {package = "reqwest", version = "0.10.0-alpha.2"}
tokio = "~0.1"
futures3 = {package = "futures", version = "0.3.1", features = ["compat"]}
futures-util = "0.3.1" # shouldn't need to bring this in explicitly. Perhaps after futures v1 is completely gone we can just import this out of the (new) futures crate?
url= "^2.1.0"
reqwest = {version="0.10.0-alpha.2", features = ["json"]}
tokio = "0.2.0-alpha.6"
hex = "~0.3"
itertools = "~0.8"
futures = "~0.1.25"
futures = "~0.3.1"
quick-error = "~1.2"
lazy_static = "~1.4"
chrono = { version = "0.4", features = ["serde"] }
Expand Down
47 changes: 19 additions & 28 deletions src/document/advanced.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use crate::{
document::{partition_user_or_group, DocumentEncryptOpts},
internal, Result,
};
use futures3::future::{FutureExt, TryFutureExt};
use itertools::EitherOrBoth;
use tokio::runtime::current_thread::Runtime;

Expand Down Expand Up @@ -72,22 +71,18 @@ impl DocumentAdvancedOps for crate::IronOxide {
}
};

rt.block_on(
internal::document_api::encrypted_document_unmanaged(
self.device.auth(),
&self.recrypt,
&self.user_master_pub_key,
&self.rng,
data,
encrypt_opts.id.clone(),
grant_to_author,
&explicit_users,
&explicit_groups,
policy_grants,
)
.boxed_local() // required because something is not Send
.compat(),
)
rt.block_on(internal::document_api::encrypted_document_unmanaged(
self.device.auth(),
&self.recrypt,
&self.user_master_pub_key,
&self.rng,
data,
encrypt_opts.id.clone(),
grant_to_author,
&explicit_users,
&explicit_groups,
policy_grants,
))
}

fn document_decrypt_unmanaged(
Expand All @@ -97,16 +92,12 @@ impl DocumentAdvancedOps for crate::IronOxide {
) -> Result<DocumentDecryptUnmanagedResult> {
let mut rt = Runtime::new().unwrap();

rt.block_on(
internal::document_api::decrypt_document_unmanaged(
self.device.auth(),
&self.recrypt,
self.device().device_private_key(),
encrypted_data,
encrypted_deks,
)
.boxed_local() // required because something is not Send
.compat(),
)
rt.block_on(internal::document_api::decrypt_document_unmanaged(
self.device.auth(),
&self.recrypt,
self.device().device_private_key(),
encrypted_data,
encrypted_deks,
))
}
}
123 changes: 49 additions & 74 deletions src/document/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use crate::{
policy::*,
Result,
};
use futures3::future::{FutureExt, TryFutureExt};
use itertools::{Either, EitherOrBoth, Itertools};
use tokio::runtime::current_thread::Runtime;

Expand Down Expand Up @@ -46,7 +45,7 @@ impl ExplicitGrant {
}
}

impl<'a> DocumentEncryptOpts {
impl DocumentEncryptOpts {
pub fn new(
id: Option<DocumentId>,
name: Option<DocumentName>,
Expand Down Expand Up @@ -200,20 +199,12 @@ pub trait DocumentOps {
impl DocumentOps for crate::IronOxide {
fn document_list(&self) -> Result<DocumentListResult> {
let mut rt = Runtime::new().unwrap();
rt.block_on(
document_api::document_list(self.device.auth())
.boxed()
.compat(),
)
rt.block_on(document_api::document_list(self.device.auth()))
}

fn document_get_metadata(&self, id: &DocumentId) -> Result<DocumentMetadataResult> {
let mut rt = Runtime::new().unwrap();
rt.block_on(
document_api::document_get_metadata(self.device.auth(), id)
.boxed_local()
.compat(),
)
rt.block_on(document_api::document_get_metadata(self.device.auth(), id))
}

fn document_get_id_from_bytes(&self, encrypted_document: &[u8]) -> Result<DocumentId> {
Expand Down Expand Up @@ -246,23 +237,19 @@ impl DocumentOps for crate::IronOxide {
}
};

rt.block_on(
document_api::encrypt_document(
self.device.auth(),
&self.recrypt,
&self.user_master_pub_key,
&self.rng,
document_data,
encrypt_opts.id,
encrypt_opts.name,
grant_to_author,
&explicit_users,
&explicit_groups,
policy_grants.as_ref(),
)
.boxed_local()
.compat(),
)
rt.block_on(document_api::encrypt_document(
self.device.auth(),
&self.recrypt,
&self.user_master_pub_key,
&self.rng,
document_data,
encrypt_opts.id,
encrypt_opts.name,
grant_to_author,
&explicit_users,
&explicit_groups,
policy_grants.as_ref(),
))
}

fn document_update_bytes(
Expand All @@ -272,33 +259,25 @@ impl DocumentOps for crate::IronOxide {
) -> Result<DocumentEncryptResult> {
let mut rt = Runtime::new().unwrap();

rt.block_on(
document_api::document_update_bytes(
self.device.auth(),
&self.recrypt,
self.device.device_private_key(),
&self.rng,
id,
&new_document_data,
)
.boxed_local() // required because something is not Send
.compat(),
)
rt.block_on(document_api::document_update_bytes(
self.device.auth(),
&self.recrypt,
self.device.device_private_key(),
&self.rng,
id,
&new_document_data,
))
}

fn document_decrypt(&self, encrypted_document: &[u8]) -> Result<DocumentDecryptResult> {
let mut rt = Runtime::new().unwrap();

rt.block_on(
document_api::decrypt_document(
self.device.auth(),
&self.recrypt,
self.device.device_private_key(),
encrypted_document,
)
.boxed_local() // required because something is not Send
.compat(),
)
rt.block_on(document_api::decrypt_document(
self.device.auth(),
&self.recrypt,
self.device.device_private_key(),
encrypted_document,
))
}

fn document_update_name(
Expand All @@ -308,11 +287,11 @@ impl DocumentOps for crate::IronOxide {
) -> Result<DocumentMetadataResult> {
let mut rt = Runtime::new().unwrap();

rt.block_on(
document_api::update_document_name(self.device.auth(), id, name)
.boxed_local() // required because something is not Send
.compat(),
)
rt.block_on(document_api::update_document_name(
self.device.auth(),
id,
name,
))
}

fn document_grant_access(
Expand All @@ -324,19 +303,15 @@ impl DocumentOps for crate::IronOxide {

let (users, groups) = partition_user_or_group(grant_list);

rt.block_on(
document_api::document_grant_access(
self.device.auth(),
&self.recrypt,
id,
&self.user_master_pub_key,
&self.device.device_private_key(),
&users,
&groups,
)
.boxed_local() // required because something is not Send
.compat(),
)
rt.block_on(document_api::document_grant_access(
self.device.auth(),
&self.recrypt,
id,
&self.user_master_pub_key,
&self.device.device_private_key(),
&users,
&groups,
))
}

fn document_revoke_access(
Expand All @@ -346,11 +321,11 @@ impl DocumentOps for crate::IronOxide {
) -> Result<DocumentAccessResult> {
let mut rt = Runtime::new().unwrap();

rt.block_on(
document_api::document_revoke_access(self.device.auth(), id, revoke_list)
.boxed_local() // required because something is not Send
.compat(),
)
rt.block_on(document_api::document_revoke_access(
self.device.auth(),
id,
revoke_list,
))
}
}

Expand Down
32 changes: 15 additions & 17 deletions src/internal/document_api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::{
DeviceSigningKeyPair,
};
use chrono::{DateTime, Utc};
use futures_util::try_join;
use futures::try_join;
use hex::encode;
use itertools::{Either, Itertools};
use protobuf::{Message, RepeatedField};
Expand Down Expand Up @@ -590,9 +590,8 @@ async fn resolve_keys_for_grants(
policy_grant: Option<&PolicyGrant>,
maybe_user_master_pub_key: Option<&UserMasterPublicKey>,
) -> Result<(Vec<WithKey<UserOrGroup>>, Vec<DocAccessEditErr>), IronOxideErr> {
use futures3::compat::Future01CompatExt;
let get_user_keys_f = internal::user_api::get_user_keys(auth, user_grants);
let get_group_keys_f = internal::group_api::get_group_keys(auth, group_grants).compat();
let get_group_keys_f = internal::group_api::get_group_keys(auth, group_grants);

let maybe_policy_grants_f =
policy_grant.map(|p| requests::policy_get::policy_get_request(auth, p));
Expand Down Expand Up @@ -697,9 +696,9 @@ fn dedupe_grants(grants: &[WithKey<UserOrGroup>]) -> Vec<WithKey<UserOrGroup>> {
/// Encrypt the document using transform crypto (recrypt).
/// Can be called once you have public keys for users/groups that should have access as well as the
/// AES encrypted data.
fn recrypt_document<'a, CR: rand::CryptoRng + rand::RngCore>(
signing_keys: &'a DeviceSigningKeyPair,
recrypt: &'a Recrypt<Sha256, Ed25519, RandomBytes<CR>>,
fn recrypt_document<CR: rand::CryptoRng + rand::RngCore>(
signing_keys: &DeviceSigningKeyPair,
recrypt: &Recrypt<Sha256, Ed25519, RandomBytes<CR>>,
dek: Plaintext,
encrypted_doc: AesEncryptedValue,
doc_id: &DocumentId,
Expand Down Expand Up @@ -944,11 +943,11 @@ pub async fn document_update_bytes<

/// Decrypt the provided document with the provided device private key. Return metadata about the document
/// that was decrypted along with its decrypted bytes.
pub async fn decrypt_document<'a, CR: rand::CryptoRng + rand::RngCore>(
auth: &'a RequestAuth,
recrypt: &'a Recrypt<Sha256, Ed25519, RandomBytes<CR>>,
device_private_key: &'a PrivateKey,
encrypted_doc: &'a [u8],
pub async fn decrypt_document<CR: rand::CryptoRng + rand::RngCore>(
auth: &RequestAuth,
recrypt: &Recrypt<Sha256, Ed25519, RandomBytes<CR>>,
device_private_key: &PrivateKey,
encrypted_doc: &[u8],
) -> Result<DocumentDecryptResult, IronOxideErr> {
let (doc_header, mut enc_doc) = parse_document_parts(encrypted_doc)?;
let doc_meta = document_get_metadata(auth, &doc_header.document_id).await?;
Expand Down Expand Up @@ -1032,10 +1031,10 @@ fn edeks_and_header_match_or_err(

// Update a documents name. Value can be updated to either a new name with a Some or the name value can be cleared out
// by providing a None.
pub async fn update_document_name<'a>(
auth: &'a RequestAuth,
id: &'a DocumentId,
name: Option<&'a DocumentName>,
pub async fn update_document_name(
auth: &RequestAuth,
id: &DocumentId,
name: Option<&DocumentName>,
) -> Result<DocumentMetadataResult, IronOxideErr> {
requests::document_update::document_update_request(auth, id, name)
.await
Expand All @@ -1051,12 +1050,11 @@ pub async fn document_grant_access<CR: rand::CryptoRng + rand::RngCore>(
user_grants: &Vec<UserId>,
group_grants: &Vec<GroupId>,
) -> Result<DocumentAccessResult, IronOxideErr> {
use futures3::compat::Future01CompatExt;
let (doc_meta, users, groups) = try_join!(
document_get_metadata(auth, id),
// and the public keys for the users and groups
internal::user_api::get_user_keys(auth, user_grants),
internal::group_api::get_group_keys(auth, group_grants).compat(),
internal::group_api::get_group_keys(auth, group_grants),
)?;
let (grants, other_errs) = {
// decrypt the dek
Expand Down
Loading

0 comments on commit 0e340cd

Please sign in to comment.