From 9090bf3c36bbfaffffd6581f2ac2a8dcc2e4c5cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Thu, 15 Jun 2023 19:08:41 +0200 Subject: [PATCH] fix reading keys from non-tty inputs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit read_password_from_tty doesn't have a fallback if input is a pipe. This makes scripting and integration testing harder. Noticed while updating our nixos integration test. Signed-off-by: Jörg Thalheim --- rust-src/src/key.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/rust-src/src/key.rs b/rust-src/src/key.rs index 2af34b13..2b4fc45e 100644 --- a/rust-src/src/key.rs +++ b/rust-src/src/key.rs @@ -72,7 +72,13 @@ fn ask_for_key(sb: &bch_sb_handle) -> anyhow::Result<()> { let bch_key_magic = BCH_KEY_MAGIC.as_bytes().read_u64::().unwrap(); let crypt = sb.sb().crypt().unwrap(); - let pass = rpassword::read_password_from_tty(Some("Enter passphrase: "))?; + let pass = if atty::is(atty::Stream::Stdin) { + rpassword::read_password_from_tty(Some("Enter passphrase: "))? + } else { + let mut line = String::new(); + std::io::stdin().read_line(&mut line)?; + line + }; let pass = std::ffi::CString::new(pass.trim_end())?; // bind to keep the CString alive let mut output: bch_key = unsafe { bcachefs::derive_passphrase(