]> git.sesse.net Git - bcachefs-tools-debian/commitdiff
fix reading keys from non-tty inputs
authorJörg Thalheim <joerg@thalheim.io>
Thu, 15 Jun 2023 17:08:41 +0000 (19:08 +0200)
committerJörg Thalheim <joerg@thalheim.io>
Thu, 15 Jun 2023 17:11:54 +0000 (19:11 +0200)
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 <joerg@thalheim.io>
rust-src/src/key.rs

index 2af34b1385b3239efeb3094316236545d5e7707c..2b4fc45e8bc494de023dc877c34fae7c7eb21536 100644 (file)
@@ -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::<LittleEndian>().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(