Skip to content

Commit

Permalink
fix: imported gpg key fails to sign when no tty is present (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
purpleclay authored Jun 29, 2023
1 parent 60984ff commit 1670fa6
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description = "Easily import a GPG key within any CI workflow"
name = "gpg-import"
edition = "2021"
license = "MIT"
version = "0.3.1"
version = "0.3.2"

[dependencies]
base64 = "0.21.1"
Expand Down
15 changes: 14 additions & 1 deletion src/gpg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,18 @@ pub fn detect_version() -> Result<GpgInfo, Box<dyn std::error::Error>> {
Ok(gpg_info)
}

/// Configure GPG with sensible defaults
pub fn configure_defaults(home_dir: &str) -> Result<(), Box<dyn std::error::Error>> {
let path = Path::new(home_dir).join("gpg.conf");
fs::create_dir_all(home_dir)?;
fs::write(
path,
b"use-agent
pinentry-mode loopback",
)?;
Ok(())
}

/// Configure the GPG agent with sensible defaults
pub fn configure_agent_defaults(home_dir: &str) -> Result<(), Box<dyn std::error::Error>> {
let path = Path::new(home_dir).join("gpg-agent.conf");
Expand All @@ -100,7 +112,8 @@ pub fn configure_agent_defaults(home_dir: &str) -> Result<(), Box<dyn std::error
path,
b"default-cache-ttl 21600
max-cache-ttl 31536000
allow-preset-passphrase",
allow-preset-passphrase
allow-loopback-pinentry",
)?;
return reload_agent();
}
Expand Down
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("> Imported GPG key:");
println!("{}", private_key);

gpg::configure_defaults(&info.home_dir)?;
gpg::configure_agent_defaults(&info.home_dir)?;

if let Some(passphrase) = args.passphrase {
gpg::configure_agent_defaults(&info.home_dir)?;
gpg::preset_passphrase(&private_key.secret_key.keygrip, &passphrase)?;
gpg::preset_passphrase(&private_key.secret_subkey.keygrip, &passphrase)?;

Expand Down

0 comments on commit 1670fa6

Please sign in to comment.