-
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.
- Loading branch information
Showing
5 changed files
with
376 additions
and
2 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
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 |
---|---|---|
@@ -0,0 +1,126 @@ | ||
use std::{fs::read_to_string, path::PathBuf}; | ||
|
||
use base64ct::{Base64, Encoding}; | ||
use clap::{Parser, Subcommand}; | ||
use miette::{bail, IntoDiagnostic, Result}; | ||
use minisign::{SecretKey, SecretKeyBox}; | ||
|
||
use super::Context; | ||
|
||
pub mod files; | ||
|
||
/// Sign and verify files. | ||
#[derive(Debug, Clone, Parser)] | ||
pub struct SignArgs { | ||
/// Sign subcommand | ||
#[command(subcommand)] | ||
pub action: SignAction, | ||
} | ||
|
||
#[derive(Debug, Clone, Subcommand)] | ||
pub enum SignAction { | ||
Files(files::FilesArgs), | ||
} | ||
|
||
pub async fn run(ctx: Context<SignArgs>) -> Result<()> { | ||
match ctx.args_top.action.clone() { | ||
SignAction::Files(subargs) => files::run(ctx.with_sub(subargs)).await, | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone, Parser)] | ||
pub(crate) struct KeyArgs { | ||
/// The secret key to sign with. | ||
/// | ||
/// Prefer to use `--key-file` or `--key-env` instead of this. | ||
#[arg(long, value_name = "KEY", required_unless_present_any = &["key_file", "key_env"])] | ||
pub key: Option<String>, | ||
|
||
/// The secret key to sign with, read from a file. | ||
#[arg(long, value_name = "FILE", required_unless_present_any = &["key", "key_env"])] | ||
pub key_file: Option<PathBuf>, | ||
|
||
/// The secret key to sign with, read from an environment variable. | ||
#[arg(long, value_name = "ENVVAR", required_unless_present_any = &["key", "key_file"])] | ||
pub key_env: Option<String>, | ||
|
||
/// The password in plain text to decrypt the secret key, if it's encrypted. | ||
/// | ||
/// Prefer to use `--password-file` or `--password-env` instead of this. | ||
#[arg(long, value_name = "KEY", conflicts_with_all = &["password_file", "password_env"])] | ||
pub password: Option<String>, | ||
|
||
/// The secret key's password, read from a file. | ||
#[arg(long, value_name = "FILE", conflicts_with_all = &["password", "password_env"])] | ||
pub password_file: Option<PathBuf>, | ||
|
||
/// The secret key's password, read from an environment variable. | ||
#[arg(long, value_name = "ENVVAR", conflicts_with_all = &["password", "password_file"])] | ||
pub password_env: Option<String>, | ||
|
||
/// Prompt for the password interactively. | ||
/// | ||
/// Do not use this in scripts or CI. | ||
#[arg(long)] | ||
pub password_prompt: bool, | ||
} | ||
|
||
impl KeyArgs { | ||
fn read(&self) -> Result<SecretKey> { | ||
let password = self.read_password()?; | ||
self.read_key(password) | ||
} | ||
|
||
fn read_password(&self) -> Result<Option<String>> { | ||
// TODO: zero-box the password to avoid it lingering in memory | ||
match &self { | ||
Self { | ||
password_prompt: true, | ||
.. | ||
} => Ok(None), | ||
Self { | ||
password: Some(pass), | ||
.. | ||
} => Ok(Some(pass.into())), | ||
Self { | ||
password_env: Some(env), | ||
.. | ||
} => std::env::var(env).into_diagnostic().map(Some), | ||
Self { | ||
password_file: Some(file), | ||
.. | ||
} => read_to_string(file).into_diagnostic().map(Some), | ||
_ => Ok(Some("".into())), // no password | ||
} | ||
} | ||
|
||
fn read_key(&self, password: Option<String>) -> Result<SecretKey> { | ||
match &self { | ||
Self { key: Some(key), .. } => Self::from_string(key, password), | ||
Self { | ||
key_env: Some(env), .. | ||
} => Self::from_string(&std::env::var(env).into_diagnostic()?, password), | ||
Self { | ||
key_file: Some(file), | ||
.. | ||
} => { | ||
// we'll always assume it's a full minisign key file | ||
SecretKey::from_file(file, password).into_diagnostic() | ||
} | ||
_ => bail!("exactly one of --key, --key-file, or --key-env must be provided"), | ||
} | ||
} | ||
|
||
fn from_string(s: &str, password: Option<String>) -> Result<SecretKey> { | ||
// try parsing as the raw key as base64 first | ||
if let Ok(key) = Base64::decode_vec(s) { | ||
return Ok(SecretKey::from_bytes(&key).into_diagnostic()?); | ||
} | ||
|
||
// then as the full minisign key file | ||
Ok(SecretKeyBox::from_string(s) | ||
.into_diagnostic()? | ||
.into_secret_key(password) | ||
.into_diagnostic()?) | ||
} | ||
} |
Oops, something went wrong.