added: client upload

This commit is contained in:
2022-12-01 22:48:18 +01:00
parent 93337a9b46
commit 94a69e4a53
3 changed files with 63 additions and 26 deletions

View File

@@ -1,8 +1,12 @@
use anyhow::Context;
use ssh_key::{Certificate, PublicKey};
use std::{path::{Path, PathBuf}, fmt::Debug};
use std::{
fmt::Debug,
io::ErrorKind,
path::{Path, PathBuf},
};
use tokio::fs;
use tracing::{trace, instrument};
use tracing::{instrument, trace};
pub async fn read_certs(
ca: &PublicKey,
@@ -31,13 +35,10 @@ pub async fn read_dir(path: impl AsRef<Path> + Debug) -> anyhow::Result<Vec<Cert
);
continue;
}
let contents = fs::read(&entry.path())
.await
.with_context(|| format!("read {:?}", entry.path()))?;
let string_repr = parse_utf8(contents)?;
let cert = Certificate::from_openssh(&string_repr)
.with_context(|| format!("parse {:?} as openssh certificate", entry.path()))?;
certs.push(cert);
let cert = load_cert(entry.path()).await?;
if let Some(cert) = cert {
certs.push(cert);
}
}
Ok(certs)
}
@@ -61,7 +62,6 @@ fn ca_dir(ca: &PublicKey) -> String {
#[instrument]
fn cert_path(ca: &PublicKey, identifier: &str) -> String {
let _ca_fingerprint = ca.fingerprint(Default::default());
format!("{}/{}-cert.pub", ca_dir(ca), identifier)
}
@@ -80,21 +80,23 @@ pub async fn store_cert(
Ok(path)
}
pub async fn load_cert(
pub async fn load_cert_by_id(
cert_dir: impl AsRef<Path>,
ca: &PublicKey,
identifier: &str,
) -> anyhow::Result<Option<Certificate>> {
let path = cert_dir.as_ref().join(cert_path(ca, identifier));
if !path.exists() {
trace!("no certificate at {:?}", path);
return Ok(None);
}
let contents = fs::read(&path)
.await
.with_context(|| format!("read {:?}", &path))?;
load_cert(&path).await
}
pub async fn load_cert(file: impl AsRef<Path> + Debug) -> anyhow::Result<Option<Certificate>> {
let contents = match fs::read(&file).await {
Ok(contents) => contents,
Err(e) if e.kind() == ErrorKind::NotFound => return Ok(None),
Err(e) => return Err(e).with_context(|| format!("read {:?}", &file)),
};
let string_repr = parse_utf8(contents)?;
Ok(Some(Certificate::from_openssh(&string_repr).with_context(
|| format!("parse {:?} as openssh certificate", &path),
|| format!("parse {:?} as openssh certificate", &file),
)?))
}