Skip to content

Commit

Permalink
--
Browse files Browse the repository at this point in the history
  • Loading branch information
oluceps committed Nov 2, 2024
1 parent 94337ec commit 78dbef1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 53 deletions.
30 changes: 15 additions & 15 deletions src/cmd/renc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::{
};

use crate::{
cmd::stored_sec_path::{HashWithCtx, InCfg, InStore, SecMap, SecPath},
cmd::stored_sec_path::{InCfg, InStore, SecMap, SecPath},
profile::{MasterIdentity, Profile},
};
use crate::{interop::add_to_store, profile};
Expand Down Expand Up @@ -97,11 +97,11 @@ impl Profile {
.into_iter()
.filter(|(k, v)| {
// TODO: extraReceip
let hash = v.get_hash();
let hash = v;
let renc_path = {
let mut path = renc_path.clone();
path.push(hash.to_string());
info!("check {}", path.display());
trace!("checking {}", path.display());
path
};

Expand All @@ -115,25 +115,25 @@ impl Profile {

!exs
})
.collect::<HashMap<profile::Secret, HashWithCtx>>()
.collect::<HashMap<profile::Secret, blake3::Hash>>()
.into_keys()
.collect::<Vec<profile::Secret>>();
// info!("{:?}", sec_need_renc);

// TODO: host pub key type safe
data.makeup(
if let Ok(_) = data.makeup(
instore_map,
sec_need_renc,
self.settings.host_pubkey.clone(),
decrypt,
)

// let o = add_to_store(renc_path)?;
// if !o.status.success() {
// error!("Command executed with failing error code");
// }
// // Another side, calculate with nix `builtins.path` and pass to when deploy as `storage`
// info!("path added to store: {}", String::from_utf8(o.stdout)?);
// Ok(())
) {
let o = add_to_store(renc_path)?;
if !o.status.success() {
error!("Command executed with failing error code");
}
// Another side, calculate with nix `builtins.path` and pass to when deploy as `storage`
info!("path added to store: {}", String::from_utf8(o.stdout)?);
}

Ok(())
}
}
58 changes: 20 additions & 38 deletions src/cmd/stored_sec_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ use std::{
path::{Path, PathBuf},
};

use age::Identity;
use eyre::{Context, ContextCompat};
use nom::Err;
use spdlog::info;

use crate::profile::{self, Profile, SecretSet, Settings};
Expand Down Expand Up @@ -81,21 +79,7 @@ macro_rules! impl_from_iterator_for_secmap {
};
}

impl_from_iterator_for_secmap!(Vec<u8>, HashWithCtx, PathWithCtx);

pub struct HashWithCtx(blake3::Hash, Vec<u8>);

impl HashWithCtx {
pub fn new(b: blake3::Hash, v: Vec<u8>) -> Self {
HashWithCtx(b, v)
}
pub fn get_hash(&self) -> &blake3::Hash {
&self.0
}
pub fn get_ctx(&self) -> &Vec<u8> {
&self.1
}
}
impl_from_iterator_for_secmap!(Vec<u8>, PathWithCtx, blake3::Hash);

#[derive(Debug, Clone)]
pub struct SecMap<P>(HashMap<profile::Secret, P>);
Expand All @@ -119,7 +103,7 @@ impl<T> SecMap<SecPath<PathBuf, T>> {
/// hash of encrypted file content
/// used in: renc, calc and compare
/// deploy, calc and find in store
pub fn calc_renc(self, _host_pubkey: String) -> Result<SecMap<HashWithCtx>> {
pub fn calc_renc(self, _host_pubkey: String) -> Result<SecMap<blake3::Hash>> {
self.bake_ctx().and_then(|h| {
h.inner()
.into_iter()
Expand All @@ -128,9 +112,9 @@ impl<T> SecMap<SecPath<PathBuf, T>> {
hasher.update(v.as_slice());
// hasher.update(host_pubkey.as_bytes());
let hash = hasher.finalize();
Ok((k, HashWithCtx::new(hash, v)))
Ok((k, hash))
})
.try_collect::<SecMap<HashWithCtx>>()
.try_collect::<SecMap<blake3::Hash>>()
})
}
}
Expand Down Expand Up @@ -187,8 +171,6 @@ impl SecMap<SecPath<PathBuf, InCfg>> {
.filter(|(s, _)| target.contains(s))
.collect();

// let map_path_with_ctx: SecMap<PathWithCtx> = SecMap::<SecPath<_, InCfg>>(spm).into();
// info!("{:?}", map_path_with_ctx);
in_store_data.inner().into_iter().try_for_each(|(s, v)| {
let enc_ctx = v.read_buffer()?;
let target_path = spm
Expand Down Expand Up @@ -238,20 +220,20 @@ impl PathWithCtx {
}
}

impl From<SecMap<SecPath<PathBuf, InCfg>>> for SecMap<PathWithCtx> {
fn from(value: SecMap<SecPath<PathBuf, InCfg>>) -> Self {
value
.inner()
.into_iter()
.filter_map(|(s, p)| {
let mut f = p.open_file().ok()?;
let mut buffer = Vec::new();
f.read_to_end(&mut buffer)
.wrap_err_with(|| eyre!("read secret file error"))
.ok()?;
Some((s, PathWithCtx(p, buffer)))
})
.collect()
}
}
// impl From<SecMap<SecPath<PathBuf, InCfg>>> for SecMap<PathWithCtx> {
// fn from(value: SecMap<SecPath<PathBuf, InCfg>>) -> Self {
// value
// .inner()
// .into_iter()
// .filter_map(|(s, p)| {
// let mut f = p.open_file().ok()?;
// let mut buffer = Vec::new();
// f.read_to_end(&mut buffer)
// .wrap_err_with(|| eyre!("read secret file error"))
// .ok()?;
// Some((s, PathWithCtx(p, buffer)))
// })
// .collect()
// }
// }
// impl From<SecMap<PathWithCtx>> for

0 comments on commit 78dbef1

Please sign in to comment.