Skip to content

Commit

Permalink
fix: json support in 1p (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
zifeo authored May 4, 2024
1 parent a9af8be commit df4daee
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 36 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ resolver = "2"

[package]
name = "lade"
version = "0.11.2-beta.1"
version = "0.11.2"
edition = "2021"
description = "Automatically load secrets from your preferred vault as environment variables, and clear them once your shell command is over."
license = "MPL-2.0"
Expand All @@ -20,11 +20,11 @@ self_update = { version = "0.40.0", features = [
"compression-zip-deflate",
"compression-zip-bzip2",
] }
serde = { version = "1.0.198", features = ["derive"] }
serde = { version = "1.0.200", features = ["derive"] }
serde_yaml = "0.9.34"
clap = { version = "4.5.4", features = ["derive"] }
regex = "1.10.4"
lade-sdk = { path = "./sdk", version = "0.11.2-beta.1" }
lade-sdk = { path = "./sdk", version = "0.11.2" }
tokio = { version = "1", features = ["full"] }
indexmap = { version = "2.2.6", features = ["serde"] }
clap-verbosity-flag = "2.2.0"
Expand Down
1 change: 1 addition & 0 deletions lade.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
D2: op://my.1password.eu/Personal/Lade/password
D3: op://my.1password.eu/Personal/Lade/with space
D4: op://my.1password.eu/Personal/Lade/file
D5: op://my.1password.eu/Personal/Lade/json

^echo e:
# export VAULT_TOKEN=token
Expand Down
4 changes: 2 additions & 2 deletions sdk/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "lade-sdk"
version = "0.11.2-beta.1"
version = "0.11.2"
edition = "2021"
description = "Lade SDK"
license = "MPL-2.0"
Expand All @@ -17,7 +17,7 @@ log = "0.4.21"
once_cell = "1.19.0"
regex = "1.10.4"
rust-ini = "0.21.0"
serde = { version = "1.0.198", features = ["derive"] }
serde = { version = "1.0.200", features = ["derive"] }
serde_json = "1.0.116"
serde_yaml = "0.9.34"
tempfile = "3.10.1"
Expand Down
45 changes: 14 additions & 31 deletions sdk/src/providers/onepassword.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{collections::HashMap, path::Path};

use anyhow::{anyhow, bail, Result};
use anyhow::{bail, Result};
use async_process::{Command, Stdio};
use async_trait::async_trait;
use futures::{future::try_join_all, AsyncWriteExt};
Expand All @@ -24,6 +24,8 @@ impl OnePassword {
}
}

static SEP: &str = "'Km5Ge8AbNc+QSBauOIN0jg'";

#[async_trait]
impl Provider for OnePassword {
fn add(&mut self, value: String) -> Result<()> {
Expand Down Expand Up @@ -54,10 +56,9 @@ impl Provider for OnePassword {
return Ok(HashMap::new());
}

let json = &vars
.iter()
.map(|(k, v)| (k, v.replace(&format!("{host}/"), "").replace("%20", " ")))
.collect::<HashMap<_, _>>();
let input = &vars.values()
.map(|v| v.replace(&format!("{host}/"), "").replace("%20", " "))
.join(SEP);
let cmd = &["op", "inject", "--account", &host.to_string()];
debug!("Lade run: {}", cmd.join(" "));

Expand All @@ -69,11 +70,11 @@ impl Provider for OnePassword {
.stdin(Stdio::piped())
.spawn()?;

debug!("stdin: {:?}", json);
debug!("stdin: {:?}", input);

let mut stdin = process.stdin.take().expect("Failed to open stdin");
stdin
.write_all(serde_json::to_string(&json)?.as_bytes())
.write_all(input.as_bytes())
.await?;
drop(stdin);

Expand All @@ -88,33 +89,15 @@ impl Provider for OnePassword {
};

let output = String::from_utf8_lossy(&child.stdout).trim().replace('\n', "\\n");
let loaded =
serde_json::from_str::<Hydration>(&output).map_err(|err| {
let stderr = String::from_utf8_lossy(&child.stderr);
if stderr.contains("could not resolve item UUID") {
anyhow!(
"One item does not seem to exist in the vault: {stderr}",
)
} else {
anyhow!("1Password error: {err} (stderr: {stderr})",)
}

})?;
debug!("stdout: {:?}", output);
let loaded = output.split(SEP).collect::<Vec<_>>();

let hydration = vars
.iter()
.map(|(key, value)| {
let var = match (loaded.get(key), json.get(key)) {
(Some(loaded), Some(original)) if loaded == original => None,
(Some(loaded), _) => Some(loaded),
_ => None,
};

.iter().zip_eq(loaded)
.map(|((_, key), value)| {
(
value.clone(),
var
.unwrap_or_else(|| panic!("Variable not found in 1Password: {}", key))
.clone(),
key.clone(),
value.to_string(),
)
})
.collect::<Hydration>();
Expand Down

0 comments on commit df4daee

Please sign in to comment.