Skip to content
Open
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
26 changes: 26 additions & 0 deletions src/bin/rbw-agent/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,32 @@ pub async fn decrypt(
Ok(())
}

pub async fn decrypt_many(
sock: &mut crate::sock::Sock,
state: std::sync::Arc<tokio::sync::Mutex<crate::state::State>>,
environment: &rbw::protocol::Environment,
items: &[rbw::protocol::DecryptItem],
) -> anyhow::Result<()> {
let mut plaintexts = Vec::with_capacity(items.len());
for item in items {
plaintexts.push(
decrypt_cipher(
state.clone(),
environment,
&item.cipherstring,
item.entry_key.as_deref(),
item.org_id.as_deref(),
)
.await
.map_err(|e| format!("{e:#}")),
);
}
sock.send(&rbw::protocol::Response::DecryptMany { plaintexts })
.await?;

Ok(())
}

pub async fn encrypt(
sock: &mut crate::sock::Sock,
state: std::sync::Arc<tokio::sync::Mutex<crate::state::State>>,
Expand Down
10 changes: 10 additions & 0 deletions src/bin/rbw-agent/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,16 @@ async fn handle_request(
.await?;
true
}
rbw::protocol::Action::DecryptMany { items } => {
crate::actions::decrypt_many(
sock,
state.clone(),
&environment,
items,
)
.await?;
true
}
rbw::protocol::Action::Encrypt { plaintext, org_id } => {
crate::actions::encrypt(
sock,
Expand Down
21 changes: 21 additions & 0 deletions src/bin/rbw/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,27 @@ pub fn decrypt(
}
}

// decrypt a batch of cipherstrings with a single agent round trip; each
// item succeeds or fails individually
pub fn decrypt_many(
items: Vec<rbw::protocol::DecryptItem>,
) -> anyhow::Result<Vec<Result<String, String>>> {
let mut sock = connect()?;
sock.send(&rbw::protocol::Request::new(
get_environment(),
rbw::protocol::Action::DecryptMany { items },
))?;

let res = sock.recv()?;
match res {
rbw::protocol::Response::DecryptMany { plaintexts } => Ok(plaintexts),
rbw::protocol::Response::Error { error } => {
Err(anyhow::anyhow!("failed to decrypt: {error}"))
}
_ => Err(anyhow::anyhow!("unexpected message: {res:?}")),
}
}

pub fn encrypt(
plaintext: &str,
org_id: Option<&str>,
Expand Down
Loading
Loading