forked from oknozor/toml-bombadil
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allow encrypting/decrypting to/from multiple keys
This uses the key `gpg_user_ids`, but keeps `gpg_user_id` as alias for backwards compatibility. Fixes oknozor#103.
- Loading branch information
Showing
5 changed files
with
140 additions
and
31 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,14 +9,12 @@ const PGP_FOOTER: &str = "\n-----END PGP MESSAGE-----"; | |
|
||
#[derive(Clone, Debug)] | ||
pub struct Gpg { | ||
pub user_id: String, | ||
pub user_ids: Vec<String>, | ||
} | ||
|
||
impl Gpg { | ||
pub(crate) fn new(user_id: &str) -> Self { | ||
Gpg { | ||
user_id: user_id.to_string(), | ||
} | ||
pub(crate) fn new(user_ids: Vec<String>) -> Self { | ||
Gpg { user_ids } | ||
} | ||
|
||
pub(crate) fn push_secret<S: AsRef<Path> + ?Sized>( | ||
|
@@ -43,11 +41,12 @@ impl Gpg { | |
} | ||
|
||
fn encrypt(&self, content: &str) -> Result<String> { | ||
let mut child = Command::new("gpg") | ||
.arg("--encrypt") | ||
.arg("--armor") | ||
.arg("-r") | ||
.arg(&self.user_id) | ||
let mut cmd_binding = Command::new("gpg"); | ||
let cmd = cmd_binding.arg("--encrypt").arg("--armor"); | ||
for user_id in &self.user_ids { | ||
cmd.arg("-r").arg(user_id); | ||
} | ||
let mut child = cmd | ||
.stdin(Stdio::piped()) | ||
.stdout(Stdio::piped()) | ||
.spawn() | ||
|
@@ -73,12 +72,12 @@ impl Gpg { | |
} | ||
|
||
fn decrypt(&self, content: &str) -> Result<String> { | ||
let mut child = Command::new("gpg") | ||
.arg("--decrypt") | ||
.arg("--armor") | ||
.arg("-r") | ||
.arg(&self.user_id) | ||
.arg("-q") | ||
let mut cmd_binding = Command::new("gpg"); | ||
let cmd = cmd_binding.arg("--decrypt").arg("--armor").arg("-q"); | ||
for user_id in &self.user_ids { | ||
cmd.arg("-r").arg(user_id); | ||
} | ||
let mut child = cmd | ||
.stdin(Stdio::piped()) | ||
.stdout(Stdio::piped()) | ||
.spawn() | ||
|
@@ -116,7 +115,7 @@ mod test { | |
use std::env; | ||
use toml::Value; | ||
|
||
const GPG_ID: &str = "[email protected]"; | ||
const GPG_IDS: [&'static str; 2] = ["[email protected]", "[email protected]"]; | ||
|
||
fn gpg_setup() { | ||
let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); | ||
|
@@ -131,7 +130,7 @@ mod test { | |
|
||
#[sealed_test(before = gpg_setup())] | ||
fn should_encrypt() { | ||
let gpg = Gpg::new(GPG_ID); | ||
let gpg = Gpg::new(GPG_IDS.map(String::from).to_vec()); | ||
|
||
let result = gpg.encrypt("test"); | ||
|
||
|
@@ -140,7 +139,7 @@ mod test { | |
|
||
#[sealed_test(before = gpg_setup())] | ||
fn should_not_encrypt_unkown_gpg_user() { | ||
let gpg = Gpg::new("unknown.user"); | ||
let gpg = Gpg::new(vec!("unknown.user".to_string())); | ||
|
||
let result = gpg.encrypt("test"); | ||
|
||
|
@@ -149,7 +148,7 @@ mod test { | |
|
||
#[sealed_test(before = gpg_setup())] | ||
fn should_decrypt() -> Result<()> { | ||
let gpg = Gpg::new(GPG_ID); | ||
let gpg = Gpg::new(GPG_IDS.map(String::from).to_vec()); | ||
|
||
let encrypted = gpg.encrypt("value")?; | ||
let decrypted = gpg.decrypt(&encrypted); | ||
|
@@ -163,7 +162,7 @@ mod test { | |
|
||
#[sealed_test(before = gpg_setup())] | ||
fn should_push_to_var() -> Result<()> { | ||
let gpg = Gpg::new(GPG_ID); | ||
let gpg = Gpg::new(GPG_IDS.map(String::from).to_vec()); | ||
std::fs::write("vars.toml", "")?; | ||
gpg.push_secret("key", "value", "vars.toml")?; | ||
|
||
|
@@ -177,7 +176,7 @@ mod test { | |
|
||
#[sealed_test(before = gpg_setup())] | ||
fn should_decrypt_from_file() -> Result<()> { | ||
let gpg = Gpg::new(GPG_ID); | ||
let gpg = Gpg::new(GPG_IDS.map(String::from).to_vec()); | ||
std::fs::write("vars.toml", "")?; | ||
gpg.push_secret("key", "value", "vars.toml")?; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters