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-16221] uniffi bindings for ssh agent and keygen #89

Merged
merged 5 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 2 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions crates/bitwarden-ssh/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ wasm = [
"dep:tsify-next",
"dep:wasm-bindgen"
] # WASM support
uniffi = ["dep:uniffi"] # Uniffi bindings

[dependencies]
bitwarden-error = { workspace = true }
Expand All @@ -36,6 +37,7 @@ ssh-key = { version = ">=0.6.7, <0.7", features = [
], default-features = false }
thiserror = { workspace = true }
tsify-next = { workspace = true, optional = true }
uniffi = { workspace = true, optional = true }
wasm-bindgen = { workspace = true, optional = true }

[dev-dependencies]
Expand Down
1 change: 1 addition & 0 deletions crates/bitwarden-ssh/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{error, error::KeyGenerationError, SshKey};

#[derive(Serialize, Deserialize)]
#[cfg_attr(feature = "wasm", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
pub enum KeyAlgorithm {
Ed25519,
Rsa3072,
Expand Down
4 changes: 4 additions & 0 deletions crates/bitwarden-ssh/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@
#[cfg(feature = "wasm")]
use tsify_next::Tsify;

#[cfg(feature = "uniffi")]
uniffi::setup_scaffolding!();

Check warning on line 13 in crates/bitwarden-ssh/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-ssh/src/lib.rs#L13

Added line #L13 was not covered by tests

#[derive(Serialize, Deserialize, Debug)]
#[cfg_attr(feature = "wasm", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
pub struct SshKey {
/// The private key in OpenSSH format
pub private_key: String,
Expand Down
9 changes: 9 additions & 0 deletions crates/bitwarden-ssh/uniffi.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[bindings.kotlin]
package_name = "com.bitwarden.ssh"
generate_immutable_records = true
android = true

[bindings.swift]
ffi_module_name = "BitwardenSshFFI"
module_name = "BitwardenSsh"
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-exporters = { workspace = true, features = ["uniffi"] }
bitwarden-fido = { workspace = true, features = ["uniffi"] }
bitwarden-generators = { workspace = true, features = ["uniffi"] }
bitwarden-send = { workspace = true, features = ["uniffi"] }
bitwarden-ssh = { workspace = true, features = ["uniffi"] }
bitwarden-vault = { workspace = true, features = ["uniffi"] }
chrono = { workspace = true, features = ["std"] }
env_logger = "0.11.1"
Expand Down
5 changes: 5 additions & 0 deletions crates/bitwarden-uniffi/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,9 @@ pub enum Error {
DecryptFido2AutofillCredentials(#[from] bitwarden_fido::DecryptFido2AutofillCredentialsError),
#[error(transparent)]
Fido2Client(#[from] bitwarden_fido::Fido2ClientError),

#[error(transparent)]
SshGeneration(#[from] bitwarden_ssh::error::KeyGenerationError),
#[error(transparent)]
SshImport(#[from] bitwarden_ssh::error::SshKeyImportError),
}
7 changes: 6 additions & 1 deletion crates/bitwarden-uniffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use crypto::CryptoClient;
use error::Result;
use platform::PlatformClient;
use tool::{ExporterClient, GeneratorClients, SendClient};
use tool::{ExporterClient, GeneratorClients, SendClient, SshClient};
use vault::VaultClient;

#[derive(uniffi::Object)]
Expand Down Expand Up @@ -67,6 +67,11 @@
Arc::new(SendClient(self))
}

/// SSH operations
pub fn ssh(self: Arc<Self>) -> Arc<SshClient> {
Arc::new(SshClient(self))
}

Check warning on line 73 in crates/bitwarden-uniffi/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-uniffi/src/lib.rs#L71-L73

Added lines #L71 - L73 were not covered by tests

/// Auth operations
pub fn auth(self: Arc<Self>) -> Arc<AuthClient> {
Arc::new(AuthClient(self))
Expand Down
3 changes: 3 additions & 0 deletions crates/bitwarden-uniffi/src/tool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ use crate::{
mod sends;
pub use sends::SendClient;

mod ssh;
pub use ssh::SshClient;

#[derive(uniffi::Object)]
pub struct GeneratorClients(pub(crate) Arc<Client>);

Expand Down
29 changes: 29 additions & 0 deletions crates/bitwarden-uniffi/src/tool/ssh.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use std::sync::Arc;

use crate::{
error::{BitwardenError, Error},
Client, Result,
};

#[derive(uniffi::Object)]

Check warning on line 8 in crates/bitwarden-uniffi/src/tool/ssh.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-uniffi/src/tool/ssh.rs#L8

Added line #L8 was not covered by tests
pub struct SshClient(pub Arc<Client>);

#[uniffi::export]

Check warning on line 11 in crates/bitwarden-uniffi/src/tool/ssh.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-uniffi/src/tool/ssh.rs#L11

Added line #L11 was not covered by tests
impl SshClient {
pub fn generate_ssh_key(
&self,
key_algorithm: bitwarden_ssh::generator::KeyAlgorithm,
) -> Result<bitwarden_ssh::SshKey> {
bitwarden_ssh::generator::generate_sshkey(key_algorithm)
Copy link
Member

Choose a reason for hiding this comment

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

observation: We would probably benefit from taking a look at the public interface of bitwarden_ssh. Exposing modules is a bit clunky and the library is small enough that re-publishing everything in the root would result in a cleaner interface.

.map_err(|e| BitwardenError::E(Error::SshGeneration(e)))
}

Check warning on line 19 in crates/bitwarden-uniffi/src/tool/ssh.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-uniffi/src/tool/ssh.rs#L13-L19

Added lines #L13 - L19 were not covered by tests

pub fn import_ssh_key(
&self,
imported_key: String,
password: Option<String>,
) -> Result<bitwarden_ssh::SshKey> {
bitwarden_ssh::import::import_key(imported_key, password)
.map_err(|e| BitwardenError::E(Error::SshImport(e)))
}

Check warning on line 28 in crates/bitwarden-uniffi/src/tool/ssh.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-uniffi/src/tool/ssh.rs#L21-L28

Added lines #L21 - L28 were not covered by tests
}
Loading