From 607a392ffe8eefea304c247b3c916c68b776d1dd Mon Sep 17 00:00:00 2001 From: Tiago Carvalho Date: Fri, 23 Feb 2024 14:20:04 +0000 Subject: [PATCH] store pubkeys in repo --- src/main.rs | 8 ++++++++ src/store.rs | 29 +++++++++++++++++++++++++++++ src/utils.rs | 5 +++++ 3 files changed, 42 insertions(+) create mode 100644 src/store.rs diff --git a/src/main.rs b/src/main.rs index 7962cd7..3a038a6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ mod pull; mod push; mod raw; mod sign; +mod store; mod utils; mod verify; @@ -60,6 +61,12 @@ enum Action { /// The name of the remote repository remote: Option>, }, + /// Store a reference to a public key + Store { + /// The path to the base64 encoded public key to store + #[arg(short = 'k', long)] + key: PathBuf, + }, } #[derive(Subcommand)] @@ -112,5 +119,6 @@ fn main() -> Result<()> { } => verify::command(public_key, rev), Action::Push { remote } => push::command(&remote.unwrap_or(Cow::Borrowed("origin"))), Action::Pull { remote } => pull::command(&remote.unwrap_or(Cow::Borrowed("origin"))), + Action::Store { key } => store::command(key), } } diff --git a/src/store.rs b/src/store.rs new file mode 100644 index 0000000..2bf6d67 --- /dev/null +++ b/src/store.rs @@ -0,0 +1,29 @@ +//! Store git references to [`libsignify`] public keys. + +use std::path::PathBuf; + +use anyhow::{Context, Result}; + +use crate::utils; + +/// Execute the `store` command. +pub fn command(key_path: PathBuf) -> Result<()> { + let repo = utils::open_repository()?; + let public_key = utils::get_public_key(key_path)?; + let public_key_oid = repo + .blob(public_key.key().as_ref()) + .context("Failed to write public key to the object store")?; + let key_fingerprint = utils::hash_bytes(public_key.key().as_ref())?; + let reference = utils::craft_pubkey_reference(key_fingerprint); + repo.reference( + &reference, + public_key_oid, + // references to public keys will never change, so it is + // safe to force overwriting faulty references + true, + "", + ) + .context("Failed to store reference to public key")?; + println!("Public key stored under: {reference}"); + Ok(()) +} diff --git a/src/utils.rs b/src/utils.rs index 37e969e..18e77ab 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -163,5 +163,10 @@ pub fn craft_signature_reference(key_fingerprint: Oid, signed_object: Oid) -> St format!("refs/signify/signatures/{key_fingerprint}/{signed_object}") } +/// Craft a git reference to a public key. +pub fn craft_pubkey_reference(key_fingerprint: Oid) -> String { + format!("refs/signify/pubkeys/{key_fingerprint}") +} + /// Git refspec describing all signify references. pub const ALL_SIGNIFY_REFS: &str = "refs/signify/*"; -- 2.51.2