mirror of
https://seed.radicle.dev/rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5.git
synced 2026-11-29 05:50:31 +08:00
node: Implement signed refs
Signed-off-by: Alexis Sellier <self@cloudhead.io>
This commit is contained in:
+184
-55
@@ -1,28 +1,32 @@
|
||||
use std::collections::BTreeMap;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::Arc;
|
||||
use std::{fmt, fs, io};
|
||||
|
||||
use git_ref_format::refspec;
|
||||
use git_url::Url;
|
||||
use once_cell::sync::Lazy;
|
||||
use radicle_git_ext as git_ext;
|
||||
|
||||
pub use radicle_git_ext::Oid;
|
||||
|
||||
use crate::collections::HashMap;
|
||||
use crate::crypto::Signer;
|
||||
use crate::crypto::{Signer, Verified};
|
||||
use crate::git;
|
||||
use crate::identity::{ProjId, UserId};
|
||||
|
||||
use super::{
|
||||
Error, Inventory, ReadRepository, ReadStorage, Remote, Remotes, Unverified, WriteRepository,
|
||||
WriteStorage,
|
||||
use crate::storage::refs;
|
||||
use crate::storage::refs::{Refs, SignedRefs};
|
||||
use crate::storage::{
|
||||
Error, Inventory, ReadRepository, ReadStorage, Remote, Remotes, WriteRepository, WriteStorage,
|
||||
};
|
||||
|
||||
use super::RemoteId;
|
||||
|
||||
pub static RADICLE_ID_REF: Lazy<refspec::PatternString> =
|
||||
Lazy::new(|| refspec::pattern!("heads/radicle/id"));
|
||||
pub static REMOTES_GLOB: Lazy<refspec::PatternString> =
|
||||
Lazy::new(|| refspec::pattern!("refs/remotes/*"));
|
||||
pub static SIGNATURES_GLOB: Lazy<refspec::PatternString> =
|
||||
Lazy::new(|| refspec::pattern!("refs/remotes/*/radicle/signature"));
|
||||
|
||||
pub struct Storage {
|
||||
path: PathBuf,
|
||||
@@ -40,16 +44,28 @@ impl ReadStorage for Storage {
|
||||
self.signer.public_key()
|
||||
}
|
||||
|
||||
fn url(&self) -> Url {
|
||||
Url {
|
||||
fn url(&self) -> git::Url {
|
||||
git::Url {
|
||||
scheme: git_url::Scheme::File,
|
||||
host: Some(self.path.to_string_lossy().to_string()),
|
||||
..Url::default()
|
||||
..git::Url::default()
|
||||
}
|
||||
}
|
||||
|
||||
fn get(&self, _id: &ProjId) -> Result<Option<Remotes<Unverified>>, Error> {
|
||||
todo!()
|
||||
fn get(&self, id: &ProjId) -> Result<Option<Remotes<Verified>>, Error> {
|
||||
// TODO: Don't create a repo here if it doesn't exist?
|
||||
// Perhaps for checking we could have a `contains` method?
|
||||
match self.repository(id) {
|
||||
Ok(r) => {
|
||||
let remotes = r.remotes()?;
|
||||
if remotes.is_empty() {
|
||||
Ok(None)
|
||||
} else {
|
||||
Ok(Some(remotes))
|
||||
}
|
||||
}
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
|
||||
fn inventory(&self) -> Result<Inventory, Error> {
|
||||
@@ -63,10 +79,23 @@ impl WriteStorage for Storage {
|
||||
fn repository(&self, proj: &ProjId) -> Result<Self::Repository, Error> {
|
||||
Repository::open(self.path.join(proj.to_string()))
|
||||
}
|
||||
|
||||
fn sign_refs(&self, repository: &Repository) -> Result<SignedRefs<Verified>, Error> {
|
||||
let remote = self.signer.public_key();
|
||||
let refs = repository.references(remote)?;
|
||||
let signed = refs.signed(self.signer.clone())?;
|
||||
|
||||
signed.save(remote, repository)?;
|
||||
|
||||
Ok(signed)
|
||||
}
|
||||
}
|
||||
|
||||
impl Storage {
|
||||
pub fn open<P: AsRef<Path>>(path: P, signer: impl Signer) -> Result<Self, io::Error> {
|
||||
pub fn open<P: AsRef<Path>, S: Signer + 'static>(
|
||||
path: P,
|
||||
signer: S,
|
||||
) -> Result<Self, io::Error> {
|
||||
let path = path.as_ref().to_path_buf();
|
||||
|
||||
match fs::create_dir_all(&path) {
|
||||
@@ -89,6 +118,13 @@ impl Storage {
|
||||
self.signer.clone()
|
||||
}
|
||||
|
||||
pub fn with_signer(&self, signer: impl Signer + 'static) -> Self {
|
||||
Self {
|
||||
path: self.path.clone(),
|
||||
signer: Arc::new(signer),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn projects(&self) -> Result<Vec<ProjId>, Error> {
|
||||
let mut projects = Vec::new();
|
||||
|
||||
@@ -100,10 +136,28 @@ impl Storage {
|
||||
}
|
||||
Ok(projects)
|
||||
}
|
||||
|
||||
pub fn inspect(&self) -> Result<(), Error> {
|
||||
for proj in self.projects()? {
|
||||
let repo = self.repository(&proj)?;
|
||||
|
||||
for r in repo.raw().references()? {
|
||||
let r = r?;
|
||||
let name = r.name().ok_or(Error::InvalidRef)?;
|
||||
let oid = r.target().ok_or(Error::InvalidRef)?;
|
||||
|
||||
println!("{} {} {}", proj, oid, name);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Repository {
|
||||
pub(crate) backend: git2::Repository,
|
||||
// TODO: Add project id here so we can refer to it
|
||||
// in a bunch of places. We could write it to the
|
||||
// git config for later.
|
||||
}
|
||||
|
||||
impl Repository {
|
||||
@@ -117,6 +171,11 @@ impl Repository {
|
||||
.no_reinit(true)
|
||||
.external_template(false),
|
||||
)?;
|
||||
let mut config = backend.config()?;
|
||||
|
||||
// TODO: Get ahold of user name and/or key.
|
||||
config.set_str("user.name", "radicle")?;
|
||||
config.set_str("user.email", "radicle@localhost")?;
|
||||
|
||||
Ok(backend)
|
||||
}
|
||||
@@ -127,15 +186,15 @@ impl Repository {
|
||||
Ok(Self { backend })
|
||||
}
|
||||
|
||||
pub fn find_reference(&self, remote: &UserId, name: &str) -> Result<Oid, Error> {
|
||||
let name = format!("refs/remotes/{}/{}", remote, name);
|
||||
let target = self
|
||||
.backend
|
||||
.find_reference(&name)?
|
||||
.target()
|
||||
.ok_or(Error::InvalidRef)?;
|
||||
pub fn inspect(&self) -> Result<(), Error> {
|
||||
for r in self.backend.references()? {
|
||||
let r = r?;
|
||||
let name = r.name().ok_or(Error::InvalidRef)?;
|
||||
let oid = r.target().ok_or(Error::InvalidRef)?;
|
||||
|
||||
Ok(target.into())
|
||||
println!("{} {}", oid, name);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -144,38 +203,69 @@ impl ReadRepository for Repository {
|
||||
self.backend.path()
|
||||
}
|
||||
|
||||
fn remote(&self, user: &UserId) -> Result<Remote<Unverified>, Error> {
|
||||
// TODO: Only fetch standard refs.
|
||||
fn blob_at<'a>(&'a self, oid: Oid, path: &'a Path) -> Result<git2::Blob<'a>, git_ext::Error> {
|
||||
git_ext::Blob::At {
|
||||
object: oid.into(),
|
||||
path,
|
||||
}
|
||||
.get(&self.backend)
|
||||
}
|
||||
|
||||
fn reference(
|
||||
&self,
|
||||
remote: &RemoteId,
|
||||
name: &str,
|
||||
) -> Result<Option<git2::Reference>, git2::Error> {
|
||||
let name = format!("refs/remotes/{remote}/{name}");
|
||||
self.backend.find_reference(&name).map(Some).or_else(|e| {
|
||||
if git_ext::is_not_found_err(&e) {
|
||||
Ok(None)
|
||||
} else {
|
||||
Err(e)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn reference_oid(&self, user: &RemoteId, reference: &str) -> Result<Option<Oid>, git2::Error> {
|
||||
let reference = self.reference(user, reference)?;
|
||||
Ok(reference.and_then(|r| r.target().map(|o| o.into())))
|
||||
}
|
||||
|
||||
fn remote(&self, remote: &RemoteId) -> Result<Remote<Verified>, refs::Error> {
|
||||
let refs = SignedRefs::load(remote, self)?;
|
||||
Ok(Remote::new(*remote, refs))
|
||||
}
|
||||
|
||||
fn references(&self, remote: &RemoteId) -> Result<Refs, Error> {
|
||||
// TODO: Only return known refs, eg. heads/ rad/ tags/ etc..
|
||||
let entries = self
|
||||
.backend
|
||||
.references_glob(format!("refs/remotes/{}/*", user).as_str())?;
|
||||
let mut refs = HashMap::default();
|
||||
.references_glob(format!("refs/remotes/{remote}/*").as_str())?;
|
||||
let mut refs = BTreeMap::new();
|
||||
|
||||
for e in entries {
|
||||
let e = e?;
|
||||
let name = e.name().ok_or(Error::InvalidRef)?;
|
||||
let (_, refname) = git::parse_ref::<UserId>(name)?;
|
||||
let (_, refname) = git::parse_ref::<RemoteId>(name)?;
|
||||
let oid = e.target().ok_or(Error::InvalidRef)?;
|
||||
|
||||
refs.insert(refname.to_string(), oid.into());
|
||||
}
|
||||
Ok(Remote::new(*user, refs))
|
||||
Ok(refs.into())
|
||||
}
|
||||
|
||||
fn remotes(&self) -> Result<Remotes<Unverified>, Error> {
|
||||
let refs = self.backend.references_glob(REMOTES_GLOB.as_str())?;
|
||||
fn remotes(&self) -> Result<Remotes<Verified>, refs::Error> {
|
||||
// TODO: Should use the id glob here instead of signature.
|
||||
let refs = self.backend.references_glob(SIGNATURES_GLOB.as_str())?;
|
||||
let mut remotes = HashMap::default();
|
||||
|
||||
for r in refs {
|
||||
let r = r?;
|
||||
let name = r.name().ok_or(Error::InvalidRef)?;
|
||||
let (id, refname) = git::parse_ref::<UserId>(name)?;
|
||||
let entry = remotes
|
||||
.entry(id)
|
||||
.or_insert_with(|| Remote::new(id, HashMap::default()));
|
||||
let oid = r.target().ok_or(Error::InvalidRef)?;
|
||||
let name = r.name().ok_or(refs::Error::InvalidRef)?;
|
||||
let (id, _) = git::parse_ref::<RemoteId>(name)?;
|
||||
let remote = self.remote(&id)?;
|
||||
|
||||
entry.refs.insert(refname.to_string(), oid.into());
|
||||
remotes.insert(id, remote);
|
||||
}
|
||||
Ok(Remotes::new(remotes))
|
||||
}
|
||||
@@ -183,7 +273,7 @@ impl ReadRepository for Repository {
|
||||
|
||||
impl WriteRepository for Repository {
|
||||
/// Fetch all remotes of a project from the given URL.
|
||||
fn fetch(&mut self, url: &Url) -> Result<(), git2::Error> {
|
||||
fn fetch(&mut self, url: &git::Url) -> Result<(), git2::Error> {
|
||||
// TODO: Have function to fetch specific remotes.
|
||||
// TODO: Return meaningful info on success.
|
||||
//
|
||||
@@ -207,6 +297,10 @@ impl WriteRepository for Repository {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn raw(&self) -> &git2::Repository {
|
||||
&self.backend
|
||||
}
|
||||
}
|
||||
|
||||
impl From<git2::Repository> for Repository {
|
||||
@@ -219,28 +313,34 @@ impl From<git2::Repository> for Repository {
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::git;
|
||||
use crate::storage::refs::SIGNATURE_REF;
|
||||
use crate::storage::{ReadStorage, WriteRepository};
|
||||
use crate::test::arbitrary;
|
||||
use crate::test::crypto::MockSigner;
|
||||
use crate::test::fixtures;
|
||||
use git_url::Url;
|
||||
|
||||
#[test]
|
||||
fn test_list_remotes() {
|
||||
fn test_remote_refs() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let storage = fixtures::storage(dir.path());
|
||||
let inv = storage.inventory().unwrap();
|
||||
let proj = inv.first().unwrap();
|
||||
let refs = git::list_remotes(&Url {
|
||||
let mut refs = git::remote_refs(&git::Url {
|
||||
host: Some(dir.path().to_string_lossy().to_string()),
|
||||
scheme: git_url::Scheme::File,
|
||||
path: format!("/{}", proj).into(),
|
||||
..Url::default()
|
||||
..git::Url::default()
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
let remotes = storage.repository(proj).unwrap().remotes().unwrap();
|
||||
let project = storage.repository(proj).unwrap();
|
||||
let remotes = project.remotes().unwrap();
|
||||
|
||||
assert_eq!(refs, remotes);
|
||||
// Strip the remote refs of sigrefs so we can compare them.
|
||||
for remote in refs.values_mut() {
|
||||
remote.remove(SIGNATURE_REF).unwrap();
|
||||
}
|
||||
assert_eq!(refs, remotes.into());
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -256,27 +356,56 @@ mod tests {
|
||||
// Have Bob fetch Alice's refs.
|
||||
bob.repository(proj)
|
||||
.unwrap()
|
||||
.fetch(&Url {
|
||||
.fetch(&git::Url {
|
||||
host: Some(alice.path().to_string_lossy().to_string()),
|
||||
scheme: git_url::Scheme::File,
|
||||
path: format!("/{}", proj).into(),
|
||||
..Url::default()
|
||||
..git::Url::default()
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
for (id, _) in remotes.into_iter() {
|
||||
let alice_oid = alice
|
||||
.repository(proj)
|
||||
.unwrap()
|
||||
.find_reference(&id, refname)
|
||||
.unwrap();
|
||||
let bob_oid = bob
|
||||
.repository(proj)
|
||||
.unwrap()
|
||||
.find_reference(&id, refname)
|
||||
.unwrap();
|
||||
let alice_repo = alice.repository(proj).unwrap();
|
||||
let alice_oid = alice_repo.reference(&id, refname).unwrap().unwrap();
|
||||
|
||||
assert_eq!(alice_oid, bob_oid);
|
||||
let bob_repo = bob.repository(proj).unwrap();
|
||||
let bob_oid = bob_repo.reference(&id, refname).unwrap().unwrap();
|
||||
|
||||
assert_eq!(alice_oid.target(), bob_oid.target());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sign_refs() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let mut rng = fastrand::Rng::new();
|
||||
let signer = MockSigner::new(&mut rng);
|
||||
let storage = Storage::open(tmp.path(), signer).unwrap();
|
||||
let proj_id = arbitrary::gen::<ProjId>(1);
|
||||
let alice = *storage.user_id();
|
||||
let project = storage.repository(&proj_id).unwrap();
|
||||
let backend = &project.backend;
|
||||
let sig = git2::Signature::now(&alice.to_string(), "anonymous@radicle.xyz").unwrap();
|
||||
let head = git::initial_commit(backend, &sig).unwrap();
|
||||
|
||||
let head = git::commit(backend, &head, "Second commit", &alice.to_string()).unwrap();
|
||||
backend
|
||||
.reference(
|
||||
&format!("refs/remotes/{alice}/heads/master"),
|
||||
head.id(),
|
||||
false,
|
||||
"test",
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let signed = storage.sign_refs(&project).unwrap();
|
||||
let remote = project.remote(&alice).unwrap();
|
||||
let mut unsigned = project.references(&alice).unwrap();
|
||||
|
||||
// The signed refs doesn't contain the signature ref itself.
|
||||
unsigned.remove(SIGNATURE_REF).unwrap();
|
||||
|
||||
assert_eq!(remote.refs, signed);
|
||||
assert_eq!(*remote.refs, unsigned);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,318 @@
|
||||
use std::collections::BTreeMap;
|
||||
use std::fmt::Debug;
|
||||
use std::io;
|
||||
use std::io::{BufRead, BufReader};
|
||||
use std::marker::PhantomData;
|
||||
use std::ops::{Deref, DerefMut};
|
||||
use std::path::Path;
|
||||
use std::str::FromStr;
|
||||
|
||||
use radicle_git_ext as git_ext;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use thiserror::Error;
|
||||
|
||||
use crate::crypto;
|
||||
use crate::crypto::{PublicKey, Signature, Signer, Unverified, Verified};
|
||||
use crate::git;
|
||||
use crate::git::Oid;
|
||||
use crate::storage;
|
||||
use crate::storage::{ReadRepository, RemoteId, WriteRepository};
|
||||
|
||||
pub const SIGNATURE_REF: &str = "radicle/signature";
|
||||
pub const REFS_BLOB_PATH: &str = "refs";
|
||||
pub const SIGNATURE_BLOB_PATH: &str = "signature";
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Updated {
|
||||
/// The computed [`Refs`] were stored as a new commit.
|
||||
Updated { oid: Oid },
|
||||
/// The stored [`Refs`] were the same as the computed ones, so no new commit
|
||||
/// was created.
|
||||
Unchanged { oid: Oid },
|
||||
}
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum Error {
|
||||
#[error("invalid signature: {0}")]
|
||||
InvalidSignature(#[from] crypto::Error),
|
||||
#[error("canonical refs: {0}")]
|
||||
Canonical(#[from] canonical::Error),
|
||||
#[error("invalid reference")]
|
||||
InvalidRef,
|
||||
#[error("invalid reference: {0}")]
|
||||
Ref(#[from] git::RefError),
|
||||
#[error(transparent)]
|
||||
Git(#[from] git2::Error),
|
||||
#[error(transparent)]
|
||||
GitExt(#[from] git_ext::Error),
|
||||
#[error("refs were not found")]
|
||||
NotFound,
|
||||
}
|
||||
|
||||
/// The published state of a local repository.
|
||||
#[derive(Default, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct Refs(BTreeMap<String, Oid>);
|
||||
|
||||
impl Refs {
|
||||
/// Verify the given signature on these refs, and return [`SignedRefs`] on success.
|
||||
pub fn verified(
|
||||
self,
|
||||
signer: &PublicKey,
|
||||
signature: Signature,
|
||||
) -> Result<SignedRefs<Verified>, Error> {
|
||||
let refs = self;
|
||||
let msg = refs.canonical();
|
||||
|
||||
match signer.verify(&signature, &msg) {
|
||||
Ok(()) => Ok(SignedRefs {
|
||||
refs,
|
||||
signature,
|
||||
_verified: PhantomData,
|
||||
}),
|
||||
Err(e) => Err(e.into()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Sign these refs with the given signer and return [`SignedRefs`].
|
||||
pub fn signed<S>(self, signer: S) -> Result<SignedRefs<Verified>, Error>
|
||||
where
|
||||
S: Signer,
|
||||
{
|
||||
let refs = self;
|
||||
let msg = refs.canonical();
|
||||
let signature = signer.sign(&msg);
|
||||
|
||||
Ok(SignedRefs {
|
||||
refs,
|
||||
signature,
|
||||
_verified: PhantomData,
|
||||
})
|
||||
}
|
||||
|
||||
/// Create refs from a canonical representation.
|
||||
pub fn from_canonical(bytes: &[u8]) -> Result<Self, canonical::Error> {
|
||||
let reader = BufReader::new(bytes);
|
||||
let mut refs = BTreeMap::new();
|
||||
|
||||
for line in reader.lines() {
|
||||
let line = line?;
|
||||
let (oid, name) = line
|
||||
.split_once(' ')
|
||||
.ok_or(canonical::Error::InvalidFormat)?;
|
||||
|
||||
let name = name.to_owned();
|
||||
let oid = Oid::from_str(oid)?;
|
||||
|
||||
refs.insert(name, oid);
|
||||
}
|
||||
Ok(Self(refs))
|
||||
}
|
||||
|
||||
pub fn canonical(&self) -> Vec<u8> {
|
||||
let mut buf = String::new();
|
||||
let refs = self.iter().filter(|(name, _)| *name != SIGNATURE_REF);
|
||||
|
||||
for (name, oid) in refs {
|
||||
buf.push_str(&oid.to_string());
|
||||
buf.push(' ');
|
||||
buf.push_str(name);
|
||||
buf.push('\n');
|
||||
}
|
||||
buf.into_bytes()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Refs> for BTreeMap<String, Oid> {
|
||||
fn from(refs: Refs) -> Self {
|
||||
refs.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<V> From<SignedRefs<V>> for Refs {
|
||||
fn from(signed: SignedRefs<V>) -> Self {
|
||||
signed.refs
|
||||
}
|
||||
}
|
||||
|
||||
impl From<BTreeMap<String, Oid>> for Refs {
|
||||
fn from(refs: BTreeMap<String, Oid>) -> Self {
|
||||
Self(refs)
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for Refs {
|
||||
type Target = BTreeMap<String, Oid>;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl DerefMut for Refs {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
&mut self.0
|
||||
}
|
||||
}
|
||||
|
||||
/// Combination of [`Refs`] and a [`Signature`]. The signature is a cryptographic
|
||||
/// signature over the refs. This allows us to easily verify if a set of refs
|
||||
/// came from a particular user.
|
||||
///
|
||||
/// The type parameter keeps track of whether the signature was [`Verified`] or
|
||||
/// [`Unverified`].
|
||||
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
|
||||
pub struct SignedRefs<V> {
|
||||
refs: Refs,
|
||||
signature: Signature,
|
||||
#[serde(skip)]
|
||||
_verified: PhantomData<V>,
|
||||
}
|
||||
|
||||
impl SignedRefs<Unverified> {
|
||||
pub fn new(refs: Refs, signature: Signature) -> Self {
|
||||
Self {
|
||||
refs,
|
||||
signature,
|
||||
_verified: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn verified(self, signer: &PublicKey) -> Result<SignedRefs<Verified>, crypto::Error> {
|
||||
let canonical = self.refs.canonical();
|
||||
|
||||
match signer.verify(&self.signature, &canonical) {
|
||||
Ok(()) => Ok(SignedRefs {
|
||||
refs: self.refs,
|
||||
signature: self.signature,
|
||||
_verified: PhantomData,
|
||||
}),
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl SignedRefs<Verified> {
|
||||
pub fn load<S>(remote: &RemoteId, repo: &S) -> Result<Self, Error>
|
||||
where
|
||||
S: ReadRepository,
|
||||
{
|
||||
if let Some(oid) = repo.reference_oid(remote, SIGNATURE_REF)? {
|
||||
Self::load_at(oid, remote, repo)
|
||||
} else {
|
||||
Err(Error::NotFound)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn load_at<S>(oid: Oid, remote: &RemoteId, repo: &S) -> Result<Self, Error>
|
||||
where
|
||||
S: storage::ReadRepository,
|
||||
{
|
||||
let refs = repo.blob_at(oid, Path::new(REFS_BLOB_PATH))?;
|
||||
let signature = repo.blob_at(oid, Path::new(SIGNATURE_BLOB_PATH))?;
|
||||
let signature = crypto::Signature::try_from(signature.content())?;
|
||||
|
||||
match remote.verify(&signature, refs.content()) {
|
||||
Ok(()) => {
|
||||
let refs = Refs::from_canonical(refs.content())?;
|
||||
|
||||
Ok(Self {
|
||||
refs,
|
||||
signature,
|
||||
_verified: PhantomData,
|
||||
})
|
||||
}
|
||||
Err(e) => Err(e.into()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Save the signed refs to disk.
|
||||
/// This creates a new commit on the signed refs branch, and updates the branch pointer.
|
||||
pub fn save<S: WriteRepository>(
|
||||
&self,
|
||||
// TODO: This should be part of the signed refs.
|
||||
remote: &RemoteId,
|
||||
repo: &S,
|
||||
) -> Result<Updated, Error> {
|
||||
let parent: Option<git2::Commit> = repo
|
||||
.reference(remote, SIGNATURE_REF)?
|
||||
.map(|r| r.peel_to_commit())
|
||||
.transpose()?;
|
||||
|
||||
let tree = {
|
||||
let raw = repo.raw();
|
||||
let refs_blob_oid = raw.blob(&self.canonical())?;
|
||||
let sig_blob_oid = raw.blob(&self.signature.to_bytes())?;
|
||||
|
||||
let mut builder = raw.treebuilder(None)?;
|
||||
builder.insert(REFS_BLOB_PATH, refs_blob_oid, 0o100_644)?;
|
||||
builder.insert(SIGNATURE_BLOB_PATH, sig_blob_oid, 0o100_644)?;
|
||||
|
||||
let oid = builder.write()?;
|
||||
|
||||
raw.find_tree(oid)
|
||||
}?;
|
||||
|
||||
if let Some(ref parent) = parent {
|
||||
if parent.tree()?.id() == tree.id() {
|
||||
return Ok(Updated::Unchanged {
|
||||
oid: parent.id().into(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
let sigref = format!("refs/remotes/{remote}/{SIGNATURE_REF}");
|
||||
let author = repo.raw().signature()?;
|
||||
let commit = repo.raw().commit(
|
||||
Some(&sigref),
|
||||
&author,
|
||||
&author,
|
||||
&format!("Update {} for {}", SIGNATURE_REF, remote),
|
||||
&tree,
|
||||
&parent.iter().collect::<Vec<&git2::Commit>>(),
|
||||
);
|
||||
|
||||
match commit {
|
||||
Ok(oid) => Ok(Updated::Updated { oid: oid.into() }),
|
||||
Err(e) => match (e.class(), e.code()) {
|
||||
(git2::ErrorClass::Object, git2::ErrorCode::Modified) => {
|
||||
log::warn!("Concurrent modification of refs: {:?}", e);
|
||||
|
||||
Err(Error::Git(e))
|
||||
}
|
||||
_ => Err(e.into()),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub fn unverified(self) -> SignedRefs<Unverified> {
|
||||
SignedRefs {
|
||||
refs: self.refs,
|
||||
signature: self.signature,
|
||||
_verified: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<V> Deref for SignedRefs<V> {
|
||||
type Target = Refs;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.refs
|
||||
}
|
||||
}
|
||||
|
||||
pub mod canonical {
|
||||
use super::*;
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum Error {
|
||||
#[error("invalid canonical format")]
|
||||
InvalidFormat,
|
||||
#[error(transparent)]
|
||||
Io(#[from] io::Error),
|
||||
#[error(transparent)]
|
||||
Git(#[from] git2::Error),
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Test canonical/from_canonical
|
||||
Reference in New Issue
Block a user