Merge pull request #12 from bartvdbraak/feat/multi-threading

Optimize code to use multi-threading for secret lookups
This commit is contained in:
Bart van der Braak 2023-11-08 01:29:39 +01:00 committed by GitHub
commit 7edf75f0a4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 11 deletions

6
Cargo.lock generated
View file

@ -419,9 +419,9 @@ dependencies = [
[[package]] [[package]]
name = "errno" name = "errno"
version = "0.3.5" version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3e13f66a2f95e32a39eaa81f6b95d42878ca0e1db0c7543723dfe12557e860" checksum = "7c18ee0ed65a5f1f81cac6b1d213b69c35fa47d4252ad41f1486dbd8226fe36e"
dependencies = [ dependencies = [
"libc", "libc",
"windows-sys", "windows-sys",
@ -852,7 +852,7 @@ dependencies = [
[[package]] [[package]]
name = "keyweave" name = "keyweave"
version = "0.1.0" version = "0.2.0"
dependencies = [ dependencies = [
"azure_identity", "azure_identity",
"azure_security_keyvault", "azure_security_keyvault",

View file

@ -1,6 +1,6 @@
[package] [package]
name = "keyweave" name = "keyweave"
version = "0.1.0" version = "0.2.0"
edition = "2021" edition = "2021"
authors = ["Bart van der Braak <bart@vanderbraak.nl>"] authors = ["Bart van der Braak <bart@vanderbraak.nl>"]

View file

@ -1,9 +1,7 @@
<h1 align="center">
<img src="https://github.com/bartvdbraak/keyweave/assets/3996360/bed7f004-e897-46e5-98a4-c654251c0e17" alt="Cluster" width="336">
</h1>
# Keyweave # Keyweave
<img align="right" src="https://github.com/bartvdbraak/keyweave/assets/3996360/bed7f004-e897-46e5-98a4-c654251c0e17" alt="Cluster" height="256">
Keyweave is an open-source tool designed to seamlessly fetch secrets from Azure Key Vault and weave them into a convenient `.env` file. Developed in Rust, Keyweave is efficient and easy to use, making it an ideal choice for managing your application's secrets. Keyweave is an open-source tool designed to seamlessly fetch secrets from Azure Key Vault and weave them into a convenient `.env` file. Developed in Rust, Keyweave is efficient and easy to use, making it an ideal choice for managing your application's secrets.
## Features ## Features

View file

@ -4,6 +4,7 @@ use clap::Parser;
use futures::stream::StreamExt; use futures::stream::StreamExt;
use std::fs::File; use std::fs::File;
use std::io::Write; use std::io::Write;
use tokio::sync::mpsc;
#[derive(Parser)] #[derive(Parser)]
#[clap(author, version, about, long_about = None)] #[clap(author, version, about, long_about = None)]
@ -46,15 +47,44 @@ async fn fetch_secrets_from_key_vault(
while let Some(page) = secret_pages.next().await { while let Some(page) = secret_pages.next().await {
let page = page?; let page = page?;
let (tx, mut rx) = mpsc::channel(32); // Channel for concurrent secret retrieval
for secret in &page.value { for secret in &page.value {
if let Some(filter) = filter { if let Some(filter) = filter {
if !secret.id.contains(filter) { if !secret.id.contains(filter) {
continue; continue;
} }
} }
let secret_name = secret.id.split('/').last().unwrap_or_default(); let tx = tx.clone();
let secret_bundle = client.get(secret_name).await?;
secret_values.push((secret.id.clone(), secret_bundle.value)); // Clone necessary data before moving into the spawned task
let secret_id = secret.id.clone();
let client_clone = client.clone();
tokio::spawn(async move {
let secret_name = secret_id.split('/').last().unwrap_or_default();
let secret_bundle = client_clone.get(secret_name).await;
// Handle the result and send it through the channel
match secret_bundle {
Ok(bundle) => {
tx.send((secret_id, bundle.value))
.await
.expect("Send error");
}
Err(err) => {
eprintln!("Error fetching secret: {}", err);
// You can decide to continue or not in case of an error.
}
}
});
}
drop(tx); // Drop the sender to signal the end of sending tasks
while let Some(result) = rx.recv().await {
let (key, value) = result;
secret_values.push((key, value));
} }
} }