Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PM-8301] Create bitwarden-send #829

Merged
merged 7 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .github/workflows/publish-rust-crates.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ on:
required: true
default: true
type: boolean
publish_bitwarden-send:
description: "Publish bitwarden-send crate"
required: true
default: true
type: boolean
publish_bitwarden-vault:
description: "Publish bitwarden-valt crate"
required: true
Expand Down Expand Up @@ -96,6 +101,7 @@ jobs:
PUBLISH_BITWARDEN_CLI: ${{ github.event.inputs.publish_bitwarden-cli }}
PUBLISH_BITWARDEN_GENERATORS: ${{ github.event.inputs.publish_bitwarden-generators }}
PUBLISH_BITWARDEN_EXPORTERS: ${{ github.event.inputs.publish_bitwarden-exporters }}
PUBLISH_BITWARDEN_SEND: ${{ github.event.inputs.publish_bitwarden-send }}
PUBLISH_BITWARDEN_VAULT: ${{ github.event.inputs.publish_bitwarden-vault }}
run: |
if [[ "$PUBLISH_BITWARDEN" == "false" ]] && [[ "$PUBLISH_BITWARDEN_API_API" == "false" ]] && [[ "$PUBLISH_BITWARDEN_API_IDENTITY" == "false" ]]; then
Expand Down Expand Up @@ -148,6 +154,11 @@ jobs:
PACKAGES_LIST="$PACKAGES_LIST bitwarden-exporters"
fi

if [[ "$PUBLISH_BITWARDEN_SEND" == "true" ]]; then
PACKAGES_COMMAND="$PACKAGES_COMMAND -p bitwarden-send"
PACKAGES_LIST="$PACKAGES_LIST bitwarden-send"
fi

if [[ "$PUBLISH_BITWARDEN_VAULT" == "true" ]]; then
PACKAGES_COMMAND="$PACKAGES_COMMAND -p bitwarden-vault"
PACKAGES_LIST="$PACKAGES_LIST bitwarden-vault"
Expand Down
20 changes: 20 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ bitwarden-core = { path = "crates/bitwarden-core", version = "=0.5.0" }
bitwarden-crypto = { path = "crates/bitwarden-crypto", version = "=0.5.0" }
bitwarden-exporters = { path = "crates/bitwarden-exporters", version = "=0.5.0" }
bitwarden-generators = { path = "crates/bitwarden-generators", version = "=0.5.0" }
bitwarden-send = { path = "crates/bitwarden-send", version = "=0.5.0" }
bitwarden-vault = { path = "crates/bitwarden-vault", version = "=0.5.0" }

[workspace.lints.clippy]
Expand Down
38 changes: 38 additions & 0 deletions crates/bitwarden-send/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[package]
name = "bitwarden-send"
description = """
Internal crate for the bitwarden crate. Do not use.
"""

version.workspace = true
authors.workspace = true
edition.workspace = true
rust-version.workspace = true
homepage.workspace = true
repository.workspace = true
license-file.workspace = true
keywords.workspace = true

[features]
uniffi = [
"bitwarden-core/uniffi",
"bitwarden-crypto/uniffi",
"dep:uniffi",
] # Uniffi bindings

[dependencies]
base64 = ">=0.21.2, <0.23"
bitwarden-api-api = { workspace = true }
bitwarden-core = { workspace = true }
bitwarden-crypto = { workspace = true }
chrono = { version = ">=0.4.26, <0.5", default-features = false }
schemars = { version = ">=0.8.9, <0.9", features = ["uuid1", "chrono"] }
serde = { version = ">=1.0, <2.0", features = ["derive"] }
serde_repr = ">=0.1.12, <0.2"
thiserror = ">=1.0.40, <2.0"
uniffi = { version = "=0.27.2", optional = true }
uuid = { version = ">=1.3.3, <2.0", features = ["serde"] }
zeroize = { version = ">=1.7.0, <2.0" }

[lints]
workspace = true
11 changes: 11 additions & 0 deletions crates/bitwarden-send/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use thiserror::Error;

#[derive(Debug, Error)]

Check warning on line 3 in crates/bitwarden-send/src/error.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-send/src/error.rs#L3

Added line #L3 was not covered by tests
pub enum SendParseError {
#[error(transparent)]
Chrono(#[from] chrono::ParseError),
#[error(transparent)]
Crypto(#[from] bitwarden_crypto::CryptoError),
#[error(transparent)]
MissingFieldError(#[from] bitwarden_core::MissingFieldError),
}
9 changes: 9 additions & 0 deletions crates/bitwarden-send/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#[cfg(feature = "uniffi")]
uniffi::setup_scaffolding!();

Check warning on line 2 in crates/bitwarden-send/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-send/src/lib.rs#L2

Added line #L2 was not covered by tests
#[cfg(feature = "uniffi")]
mod uniffi_support;

mod error;
pub use error::SendParseError;
mod send;
pub use send::{Send, SendListView, SendView};
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use uuid::Uuid;
use zeroize::Zeroizing;

use crate::error::{Error, Result};
use crate::error::SendParseError;

const SEND_ITERATIONS: u32 = 100_000;

Expand Down Expand Up @@ -142,7 +142,7 @@
}

impl Send {
pub(crate) fn get_key(
pub fn get_key(
send_key: &EncString,
enc_key: &SymmetricCryptoKey,
) -> Result<SymmetricCryptoKey, CryptoError> {
Expand Down Expand Up @@ -302,9 +302,9 @@
}

impl TryFrom<SendResponseModel> for Send {
type Error = Error;
type Error = SendParseError;

fn try_from(send: SendResponseModel) -> Result<Self> {
fn try_from(send: SendResponseModel) -> Result<Self, Self::Error> {

Check warning on line 307 in crates/bitwarden-send/src/send.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-send/src/send.rs#L307

Added line #L307 was not covered by tests
Ok(Send {
id: send.id,
access_id: send.access_id,
Expand Down Expand Up @@ -336,9 +336,9 @@
}

impl TryFrom<SendFileModel> for SendFile {
type Error = Error;
type Error = SendParseError;

fn try_from(file: SendFileModel) -> Result<Self> {
fn try_from(file: SendFileModel) -> Result<Self, Self::Error> {

Check warning on line 341 in crates/bitwarden-send/src/send.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-send/src/send.rs#L341

Added line #L341 was not covered by tests
Ok(SendFile {
id: file.id,
file_name: require!(file.file_name).parse()?,
Expand All @@ -349,9 +349,9 @@
}

impl TryFrom<SendTextModel> for SendText {
type Error = Error;
type Error = SendParseError;

fn try_from(text: SendTextModel) -> Result<Self> {
fn try_from(text: SendTextModel) -> Result<Self, Self::Error> {

Check warning on line 354 in crates/bitwarden-send/src/send.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-send/src/send.rs#L354

Added line #L354 was not covered by tests
Ok(SendText {
text: EncString::try_from_optional(text.text)?,
hidden: text.hidden.unwrap_or(false),
Expand All @@ -361,13 +361,24 @@

#[cfg(test)]
mod tests {
use bitwarden_crypto::{KeyDecryptable, KeyEncryptable, MasterKey};
use std::collections::HashMap;

use super::{Send, SendText, SendTextView, SendType};
use crate::{
client::{encryption_settings::EncryptionSettings, Kdf},
tool::SendView,
};
use bitwarden_crypto::{Kdf, KeyContainer, KeyDecryptable, KeyEncryptable, MasterKey};

use super::*;

struct MockKeyContainer(HashMap<Option<Uuid>, SymmetricCryptoKey>);
impl MockKeyContainer {
fn new(master_key: MasterKey, user_key: EncString) -> Result<Self, CryptoError> {
let user_key = master_key.decrypt_user_key(user_key)?;
Ok(Self(HashMap::from([(None, user_key)])))
}
}
impl KeyContainer for MockKeyContainer {
fn get_key<'a>(&'a self, org_id: &Option<Uuid>) -> Option<&'a SymmetricCryptoKey> {
self.0.get(org_id)
}
}
Comment on lines +370 to +381
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think long term we should consider extracting this into a test crate or something.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think a separate test crate, or maybe even just a dedicated test module in bitwarden-core would be helpful in the future


#[test]
fn test_get_send_key() {
Expand All @@ -380,10 +391,9 @@
},
)
.unwrap();
let enc = EncryptionSettings::new(
let enc = MockKeyContainer::new(
master_key,
"2.majkL1/hNz9yptLqNAUSnw==|RiOzMTTJMG948qu8O3Zm1EQUO2E8BuTwFKnO9LWQjMzxMWJM5GbyOq2/A+tumPbTERt4JWur/FKfgHb+gXuYiEYlXPMuVBvT7nv4LPytJuM=|IVqMxHJeR1ZXY0sGngTC0x+WqbG8p6V+BTrdgBbQXjM=".parse().unwrap(),
"2.kmLY8NJVuiKBFJtNd/ZFpA==|qOodlRXER+9ogCe3yOibRHmUcSNvjSKhdDuztLlucs10jLiNoVVVAc+9KfNErLSpx5wmUF1hBOJM8zwVPjgQTrmnNf/wuDpwiaCxNYb/0v4FygPy7ccAHK94xP1lfqq7U9+tv+/yiZSwgcT+xF0wFpoxQeNdNRFzPTuD9o4134n8bzacD9DV/WjcrXfRjbBCzzuUGj1e78+A7BWN7/5IWLz87KWk8G7O/W4+8PtEzlwkru6Wd1xO19GYU18oArCWCNoegSmcGn7w7NDEXlwD403oY8Oa7ylnbqGE28PVJx+HLPNIdSC6YKXeIOMnVs7Mctd/wXC93zGxAWD6ooTCzHSPVV50zKJmWIG2cVVUS7j35H3rGDtUHLI+ASXMEux9REZB8CdVOZMzp2wYeiOpggebJy6MKOZqPT1R3X0fqF2dHtRFPXrNsVr1Qt6bS9qTyO4ag1/BCvXF3P1uJEsI812BFAne3cYHy5bIOxuozPfipJrTb5WH35bxhElqwT3y/o/6JWOGg3HLDun31YmiZ2HScAsUAcEkA4hhoTNnqy4O2s3yVbCcR7jF7NLsbQc0MDTbnjxTdI4VnqUIn8s2c9hIJy/j80pmO9Bjxp+LQ9a2hUkfHgFhgHxZUVaeGVth8zG2kkgGdrp5VHhxMVFfvB26Ka6q6qE/UcS2lONSv+4T8niVRJz57qwctj8MNOkA3PTEfe/DP/LKMefke31YfT0xogHsLhDkx+mS8FCc01HReTjKLktk/Jh9mXwC5oKwueWWwlxI935ecn+3I2kAuOfMsgPLkoEBlwgiREC1pM7VVX1x8WmzIQVQTHd4iwnX96QewYckGRfNYWz/zwvWnjWlfcg8kRSe+68EHOGeRtC5r27fWLqRc0HNcjwpgHkI/b6czerCe8+07TWql4keJxJxhBYj3iOH7r9ZS8ck51XnOb8tGL1isimAJXodYGzakwktqHAD7MZhS+P02O+6jrg7d+yPC2ZCuS/3TOplYOCHQIhnZtR87PXTUwr83zfOwAwCyv6KP84JUQ45+DItrXLap7nOVZKQ5QxYIlbThAO6eima6Zu5XHfqGPMNWv0bLf5+vAjIa5np5DJrSwz9no/hj6CUh0iyI+SJq4RGI60lKtypMvF6MR3nHLEHOycRUQbZIyTHWl4QQLdHzuwN9lv10ouTEvNr6sFflAX2yb6w3hlCo7oBytH3rJekjb3IIOzBpeTPIejxzVlh0N9OT5MZdh4sNKYHUoWJ8mnfjdM+L4j5Q2Kgk/XiGDgEebkUxiEOQUdVpePF5uSCE+TPav/9FIRGXGiFn6NJMaU7aBsDTFBLloffFLYDpd8/bTwoSvifkj7buwLYM+h/qcnfdy5FWau1cKav+Blq/ZC0qBpo658RTC8ZtseAFDgXoQZuksM10hpP9bzD04Bx30xTGX81QbaSTNwSEEVrOtIhbDrj9OI43KH4O6zLzK+t30QxAv5zjk10RZ4+5SAdYndIlld9Y62opCfPDzRy3ubdve4ZEchpIKWTQvIxq3T5ogOhGaWBVYnkMtM2GVqvWV//46gET5SH/MdcwhACUcZ9kCpMnWH9CyyUwYvTT3UlNyV+DlS27LMPvaw7tx7qa+GfNCoCBd8S4esZpQYK/WReiS8=|pc7qpD42wxyXemdNPuwxbh8iIaryrBPu8f/DGwYdHTw=".parse().unwrap(),
).unwrap();

let k = enc.get_key(&None).unwrap();
Expand All @@ -398,7 +408,7 @@
assert_eq!(send_key_b64, "IR9ImHGm6rRuIjiN7csj94bcZR5WYTJj5GtNfx33zm6tJCHUl+QZlpNPba8g2yn70KnOHsAODLcR0um6E3MAlg==");
}

fn build_encryption_settings() -> EncryptionSettings {
fn build_encryption_settings() -> MockKeyContainer {
let master_key = MasterKey::derive(
"asdfasdfasdf".as_bytes(),
"test@bitwarden.com".as_bytes(),
Expand All @@ -408,10 +418,9 @@
)
.unwrap();

EncryptionSettings::new(
MockKeyContainer::new(
master_key,
"2.Q/2PhzcC7GdeiMHhWguYAQ==|GpqzVdr0go0ug5cZh1n+uixeBC3oC90CIe0hd/HWA/pTRDZ8ane4fmsEIcuc8eMKUt55Y2q/fbNzsYu41YTZzzsJUSeqVjT8/iTQtgnNdpo=|dwI+uyvZ1h/iZ03VQ+/wrGEFYVewBUUl/syYgjsNMbE=".parse().unwrap(),
"2.yN7l00BOlUE0Sb0M//Q53w==|EwKG/BduQRQ33Izqc/ogoBROIoI5dmgrxSo82sgzgAMIBt3A2FZ9vPRMY+GWT85JiqytDitGR3TqwnFUBhKUpRRAq4x7rA6A1arHrFp5Tp1p21O3SfjtvB3quiOKbqWk6ZaU1Np9HwqwAecddFcB0YyBEiRX3VwF2pgpAdiPbSMuvo2qIgyob0CUoC/h4Bz1be7Qa7B0Xw9/fMKkB1LpOm925lzqosyMQM62YpMGkjMsbZz0uPopu32fxzDWSPr+kekNNyLt9InGhTpxLmq1go/pXR2uw5dfpXc5yuta7DB0EGBwnQ8Vl5HPdDooqOTD9I1jE0mRyuBpWTTI3FRnu3JUh3rIyGBJhUmHqGZvw2CKdqHCIrQeQkkEYqOeJRJVdBjhv5KGJifqT3BFRwX/YFJIChAQpebNQKXe/0kPivWokHWwXlDB7S7mBZzhaAPidZvnuIhalE2qmTypDwHy22FyqV58T8MGGMchcASDi/QXI6kcdpJzPXSeU9o+NC68QDlOIrMVxKFeE7w7PvVmAaxEo0YwmuAzzKy9QpdlK0aab/xEi8V4iXj4hGepqAvHkXIQd+r3FNeiLfllkb61p6WTjr5urcmDQMR94/wYoilpG5OlybHdbhsYHvIzYoLrC7fzl630gcO6t4nM24vdB6Ymg9BVpEgKRAxSbE62Tqacxqnz9AcmgItb48NiR/He3n3ydGjPYuKk/ihZMgEwAEZvSlNxYONSbYrIGDtOY+8Nbt6KiH3l06wjZW8tcmFeVlWv+tWotnTY9IqlAfvNVTjtsobqtQnvsiDjdEVtNy/s2ci5TH+NdZluca2OVEr91Wayxh70kpM6ib4UGbfdmGgCo74gtKvKSJU0rTHakQ5L9JlaSDD5FamBRyI0qfL43Ad9qOUZ8DaffDCyuaVyuqk7cz9HwmEmvWU3VQ+5t06n/5kRDXttcw8w+3qClEEdGo1KeENcnXCB32dQe3tDTFpuAIMLqwXs6FhpawfZ5kPYvLPczGWaqftIs/RXJ/EltGc0ugw2dmTLpoQhCqrcKEBDoYVk0LDZKsnzitOGdi9mOWse7Se8798ib1UsHFUjGzISEt6upestxOeupSTOh0v4+AjXbDzRUyogHww3V+Bqg71bkcMxtB+WM+pn1XNbVTyl9NR040nhP7KEf6e9ruXAtmrBC2ah5cFEpLIot77VFZ9ilLuitSz+7T8n1yAh1IEG6xxXxninAZIzi2qGbH69O5RSpOJuJTv17zTLJQIIc781JwQ2TTwTGnx5wZLbffhCasowJKd2EVcyMJyhz6ru0PvXWJ4hUdkARJs3Xu8dus9a86N8Xk6aAPzBDqzYb1vyFIfBxP0oO8xFHgd30Cgmz8UrSE3qeWRrF8ftrI6xQnFjHBGWD/JWSvd6YMcQED0aVuQkuNW9ST/DzQThPzRfPUoiL10yAmV7Ytu4fR3x2sF0Yfi87YhHFuCMpV/DsqxmUizyiJuD938eRcH8hzR/VO53Qo3UIsqOLcyXtTv6THjSlTopQ+JOLOnHm1w8dzYbLN44OG44rRsbihMUQp+wUZ6bsI8rrOnm9WErzkbQFbrfAINdoCiNa6cimYIjvvnMTaFWNymqY1vZxGztQiMiHiHYwTfwHTXrb9j0uPM=|09J28iXv9oWzYtzK2LBT6Yht4IT4MijEkk0fwFdrVQ4=".parse().unwrap(),
).unwrap()
}

Expand Down
8 changes: 8 additions & 0 deletions crates/bitwarden-send/src/uniffi_support.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use bitwarden_crypto::EncString;
use uuid::Uuid;

uniffi::ffi_converter_forward!(EncString, bitwarden_crypto::UniFfiTag, crate::UniFfiTag);

type DateTime = chrono::DateTime<chrono::Utc>;
uniffi::ffi_converter_forward!(DateTime, bitwarden_core::UniFfiTag, crate::UniFfiTag);
uniffi::ffi_converter_forward!(Uuid, bitwarden_core::UniFfiTag, crate::UniFfiTag);
9 changes: 9 additions & 0 deletions crates/bitwarden-send/uniffi.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[bindings.kotlin]
package_name = "com.bitwarden.send"
generate_immutable_records = true
android = true

[bindings.swift]
ffi_module_name = "BitwardenSendFFI"
module_name = "BitwardenSend"
generate_immutable_records = true
1 change: 1 addition & 0 deletions crates/bitwarden-uniffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ bitwarden = { workspace = true, features = ["internal", "uniffi"] }
bitwarden-core = { workspace = true, features = ["uniffi"] }
bitwarden-crypto = { workspace = true, features = ["uniffi"] }
bitwarden-generators = { workspace = true, features = ["uniffi"] }
bitwarden-send = { workspace = true, features = ["uniffi"] }
bitwarden-vault = { workspace = true, features = ["uniffi"] }
chrono = { version = ">=0.4.26, <0.5", features = [
"serde",
Expand Down
3 changes: 3 additions & 0 deletions crates/bitwarden/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ default = ["secrets"]
internal = [
"dep:bitwarden-exporters",
"dep:bitwarden-generators",
"dep:bitwarden-send",
"dep:bitwarden-vault",
] # Internal testing methods
no-memory-hardening = [
Expand All @@ -28,6 +29,7 @@ uniffi = [
"bitwarden-core/uniffi",
"bitwarden-crypto/uniffi",
"bitwarden-generators/uniffi",
"bitwarden-send/uniffi",
"bitwarden-vault/uniffi",
"dep:uniffi",
"dep:passkey",
Expand All @@ -46,6 +48,7 @@ bitwarden-core = { workspace = true }
bitwarden-crypto = { workspace = true }
bitwarden-exporters = { workspace = true, optional = true }
bitwarden-generators = { workspace = true, optional = true }
bitwarden-send = { workspace = true, optional = true }
bitwarden-vault = { workspace = true, optional = true }
chrono = { version = ">=0.4.26, <0.5", features = [
"clock",
Expand Down
5 changes: 5 additions & 0 deletions crates/bitwarden/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ pub enum Error {
#[error(transparent)]
PasswordError(#[from] PasswordError),

// Send
#[cfg(feature = "internal")]
#[error(transparent)]
SendParseError(#[from] bitwarden_send::SendParseError),

// Vault
#[cfg(feature = "internal")]
#[error(transparent)]
Expand Down
4 changes: 1 addition & 3 deletions crates/bitwarden/src/tool/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
mod exporters;
pub use exporters::{ClientExporters, ExportFormat};
mod client_generator;
pub use bitwarden_send::*;
pub use client_generator::ClientGenerator;

mod send;
pub use send::{Send, SendListView, SendView};
Loading