Skip to content

Commit

Permalink
Merge pull request #448 from str4d/code-cleanups
Browse files Browse the repository at this point in the history
Code cleanups
  • Loading branch information
str4d authored Jan 20, 2024
2 parents cbf2710 + 912c61c commit 0669c2e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 24 deletions.
12 changes: 5 additions & 7 deletions age/src/cli_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,15 @@ pub fn read_identities(
#[cfg(feature = "armor")]
// Try parsing as an encrypted age identity.
if let Ok(identity) = crate::encrypted::Identity::from_buffer(
ArmoredReader::new(BufReader::new(File::open(&filename)?)),
ArmoredReader::new(File::open(&filename)?),
Some(filename.clone()),
UiCallbacks,
max_work_factor,
) {
if let Some(identity) = identity {
identities.push(Box::new(identity));
continue;
} else {
return Err(ReadError::IdentityEncryptedWithoutPassphrase(filename));
}
identities.push(Box::new(
identity.ok_or(ReadError::IdentityEncryptedWithoutPassphrase(filename))?,
));
continue;
}

// Try parsing as a single multi-line SSH identity.
Expand Down
5 changes: 2 additions & 3 deletions age/src/encrypted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,6 @@ impl<R: io::Read, C: Callbacks> crate::Identity for Identity<R, C> {

#[cfg(test)]
mod tests {
use std::io::BufReader;
use std::sync::{Arc, Mutex};

use age_core::secrecy::{ExposeSecret, SecretString};
Expand Down Expand Up @@ -272,7 +271,7 @@ fOrxrKTj7xCdNS3+OrCdnBC8Z9cKDxjCGWW3fkjLsYha0Jo=

// Unwrapping with the wrong passphrase fails.
{
let buf = ArmoredReader::new(BufReader::new(TEST_ENCRYPTED_IDENTITY.as_bytes()));
let buf = ArmoredReader::new(TEST_ENCRYPTED_IDENTITY.as_bytes());
let identity =
Identity::from_buffer(buf, None, MockCallbacks::new("wrong passphrase"), None)
.unwrap()
Expand All @@ -285,7 +284,7 @@ fOrxrKTj7xCdNS3+OrCdnBC8Z9cKDxjCGWW3fkjLsYha0Jo=
}
}

let buf = ArmoredReader::new(BufReader::new(TEST_ENCRYPTED_IDENTITY.as_bytes()));
let buf = ArmoredReader::new(TEST_ENCRYPTED_IDENTITY.as_bytes());
let identity = Identity::from_buffer(
buf,
None,
Expand Down
30 changes: 16 additions & 14 deletions rage/src/bin/rage/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ macro_rules! warning {
}};
}

/// Handles error mapping for the given SSH recipient parser.
///
/// Returns `Ok(None)` if the parser finds a parseable value that should be ignored. This
/// case is for handling SSH recipient types that may occur in files we want to be able to
/// parse, but that we do not directly support.
#[cfg(feature = "ssh")]
fn parse_ssh_recipient<F, G>(
parser: F,
Expand Down Expand Up @@ -152,19 +157,16 @@ fn read_recipients(
for filename in identity_strings {
// Try parsing as an encrypted age identity.
if let Ok(identity) = age::encrypted::Identity::from_buffer(
ArmoredReader::new(BufReader::new(File::open(&filename)?)),
ArmoredReader::new(File::open(&filename)?),
Some(filename.clone()),
UiCallbacks,
max_work_factor,
) {
if let Some(identity) = identity {
recipients.extend(identity.recipients()?);
continue;
} else {
return Err(error::EncryptError::IdentityEncryptedWithoutPassphrase(
filename,
));
}
let identity = identity.ok_or(
error::EncryptError::IdentityEncryptedWithoutPassphrase(filename),
)?;
recipients.extend(identity.recipients()?);
continue;
}

// Try parsing as a single multi-line SSH identity.
Expand All @@ -177,14 +179,14 @@ fn read_recipients(
return Err(error::EncryptError::UnsupportedKey(filename, k))
}
Ok(identity) => {
if let Some(recipient) = parse_ssh_recipient(
let recipient = parse_ssh_recipient(
|| age::ssh::Recipient::try_from(identity),
|| Err(error::EncryptError::InvalidRecipient(filename.clone())),
&filename,
)? {
recipients.push(recipient);
continue;
}
)?
.expect("unsupported identities were already handled");
recipients.push(recipient);
continue;
}
Err(_) => (),
}
Expand Down
11 changes: 11 additions & 0 deletions rage/tests/cmd/rage/decrypt-missing-identities-file.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
bin.name = "rage"
args = "--decrypt -i key.txt"
status = "failed"
stdin = ""
stdout = ""
stderr = """
Error: failed to fill whole buffer
[ Did rage not do what you expected? Could an error be more useful? ]
[ Tell us: https://str4d.xyz/rage/report ]
"""

0 comments on commit 0669c2e

Please sign in to comment.