Compare commits
2 Commits
1a1e1e4c5a
...
bd96581c6a
Author | SHA1 | Date | |
---|---|---|---|
|
bd96581c6a | ||
|
0b22801503 |
@ -24,55 +24,62 @@ pub trait Manager: Sync + Send {
|
||||
) -> Result<(PrivateKey, Vec<Certificate>), GetCertificateError>;
|
||||
}
|
||||
|
||||
struct ManagerImpl {
|
||||
pub struct ManagerImpl {
|
||||
store: Box<dyn acme::store::Store>,
|
||||
account: sync::Arc<acme2::Account>,
|
||||
}
|
||||
|
||||
pub async fn new(
|
||||
store: Box<dyn acme::store::Store>,
|
||||
contact_email: &str,
|
||||
) -> Result<Box<dyn Manager>, unexpected::Error> {
|
||||
let dir = acme2::DirectoryBuilder::new(LETS_ENCRYPT_URL.to_string())
|
||||
.build()
|
||||
.await
|
||||
.or_unexpected_while("creating acme2 directory builder")?;
|
||||
impl ManagerImpl {
|
||||
pub async fn new<Store: acme::store::Store + 'static>(
|
||||
store: Store,
|
||||
contact_email: &str,
|
||||
) -> Result<Self, unexpected::Error> {
|
||||
let dir = acme2::DirectoryBuilder::new(LETS_ENCRYPT_URL.to_string())
|
||||
.build()
|
||||
.await
|
||||
.or_unexpected_while("creating acme2 directory builder")?;
|
||||
|
||||
let mut contact = String::from("mailto:");
|
||||
contact.push_str(contact_email);
|
||||
let mut contact = String::from("mailto:");
|
||||
contact.push_str(contact_email);
|
||||
|
||||
let mut builder = acme2::AccountBuilder::new(dir);
|
||||
builder.contact(vec![contact]);
|
||||
builder.terms_of_service_agreed(true);
|
||||
let mut builder = acme2::AccountBuilder::new(dir);
|
||||
builder.contact(vec![contact]);
|
||||
builder.terms_of_service_agreed(true);
|
||||
|
||||
match store.get_account_key() {
|
||||
Ok(account_key) => {
|
||||
builder.private_key(
|
||||
(&account_key)
|
||||
.try_into()
|
||||
.or_unexpected_while("parsing private key")?,
|
||||
);
|
||||
match store.get_account_key() {
|
||||
Ok(account_key) => {
|
||||
builder.private_key(
|
||||
(&account_key)
|
||||
.try_into()
|
||||
.or_unexpected_while("parsing private key")?,
|
||||
);
|
||||
}
|
||||
Err(acme::store::GetAccountKeyError::NotFound) => (),
|
||||
Err(acme::store::GetAccountKeyError::Unexpected(err)) => {
|
||||
return Err(err.into_unexpected())
|
||||
}
|
||||
}
|
||||
Err(acme::store::GetAccountKeyError::NotFound) => (),
|
||||
Err(acme::store::GetAccountKeyError::Unexpected(err)) => return Err(err.into_unexpected()),
|
||||
|
||||
let account = builder
|
||||
.build()
|
||||
.await
|
||||
.or_unexpected_while("building account")?;
|
||||
|
||||
let account_key: acme::PrivateKey = account
|
||||
.private_key()
|
||||
.as_ref()
|
||||
.try_into()
|
||||
.or_unexpected_while("parsing private key back out")?;
|
||||
|
||||
store
|
||||
.set_account_key(&account_key)
|
||||
.or_unexpected_while("storing account key")?;
|
||||
|
||||
Ok(Self {
|
||||
store: Box::from(store),
|
||||
account,
|
||||
})
|
||||
}
|
||||
|
||||
let account = builder
|
||||
.build()
|
||||
.await
|
||||
.or_unexpected_while("building account")?;
|
||||
|
||||
let account_key: acme::PrivateKey = account
|
||||
.private_key()
|
||||
.as_ref()
|
||||
.try_into()
|
||||
.or_unexpected_while("parsing private key back out")?;
|
||||
|
||||
store
|
||||
.set_account_key(&account_key)
|
||||
.or_unexpected_while("storing account key")?;
|
||||
|
||||
Ok(Box::new(ManagerImpl { store, account }))
|
||||
}
|
||||
|
||||
impl Manager for ManagerImpl {
|
||||
|
@ -66,28 +66,29 @@ struct StoredPKeyCert {
|
||||
cert: Vec<Certificate>,
|
||||
}
|
||||
|
||||
struct FSStore {
|
||||
pub struct FSStore {
|
||||
dir_path: path::PathBuf,
|
||||
}
|
||||
|
||||
pub fn new(dir_path: &path::Path) -> Result<Box<dyn Store>, unexpected::Error> {
|
||||
vec![
|
||||
dir_path,
|
||||
dir_path.join("http01_challenge_keys").as_ref(),
|
||||
dir_path.join("certificates").as_ref(),
|
||||
]
|
||||
.iter()
|
||||
.map(|dir| {
|
||||
fs::create_dir_all(dir).map_unexpected_while(|| format!("creating dir {}", dir.display()))
|
||||
})
|
||||
.try_collect()?;
|
||||
|
||||
Ok(Box::new(FSStore {
|
||||
dir_path: dir_path.into(),
|
||||
}))
|
||||
}
|
||||
|
||||
impl FSStore {
|
||||
pub fn new(dir_path: &path::Path) -> Result<Self, unexpected::Error> {
|
||||
vec![
|
||||
dir_path,
|
||||
dir_path.join("http01_challenge_keys").as_ref(),
|
||||
dir_path.join("certificates").as_ref(),
|
||||
]
|
||||
.iter()
|
||||
.map(|dir| {
|
||||
fs::create_dir_all(dir)
|
||||
.map_unexpected_while(|| format!("creating dir {}", dir.display()))
|
||||
})
|
||||
.try_collect()?;
|
||||
|
||||
Ok(Self {
|
||||
dir_path: dir_path.into(),
|
||||
})
|
||||
}
|
||||
|
||||
fn account_key_path(&self) -> path::PathBuf {
|
||||
self.dir_path.join("account.key")
|
||||
}
|
||||
|
@ -36,27 +36,27 @@ pub struct DNSChecker {
|
||||
client: tokio::sync::Mutex<AsyncClient>,
|
||||
}
|
||||
|
||||
pub async fn new(
|
||||
target_a: net::Ipv4Addr,
|
||||
resolver_addr: &str,
|
||||
) -> Result<DNSChecker, NewDNSCheckerError> {
|
||||
let resolver_addr = resolver_addr
|
||||
.parse()
|
||||
.map_err(|_| NewDNSCheckerError::InvalidResolverAddress)?;
|
||||
|
||||
let stream = udp::UdpClientStream::<tokio::net::UdpSocket>::new(resolver_addr);
|
||||
|
||||
let (client, bg) = AsyncClient::connect(stream).await.or_unexpected()?;
|
||||
|
||||
tokio::spawn(bg);
|
||||
|
||||
Ok(DNSChecker {
|
||||
target_a,
|
||||
client: tokio::sync::Mutex::new(client),
|
||||
})
|
||||
}
|
||||
|
||||
impl DNSChecker {
|
||||
pub async fn new(
|
||||
target_a: net::Ipv4Addr,
|
||||
resolver_addr: &str,
|
||||
) -> Result<Self, NewDNSCheckerError> {
|
||||
let resolver_addr = resolver_addr
|
||||
.parse()
|
||||
.map_err(|_| NewDNSCheckerError::InvalidResolverAddress)?;
|
||||
|
||||
let stream = udp::UdpClientStream::<tokio::net::UdpSocket>::new(resolver_addr);
|
||||
|
||||
let (client, bg) = AsyncClient::connect(stream).await.or_unexpected()?;
|
||||
|
||||
tokio::spawn(bg);
|
||||
|
||||
Ok(Self {
|
||||
target_a,
|
||||
client: tokio::sync::Mutex::new(client),
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn check_domain(
|
||||
&self,
|
||||
domain: &domain::Name,
|
||||
|
@ -45,18 +45,18 @@ pub trait Store: Sync + Send {
|
||||
fn all_domains(&self) -> Result<Vec<domain::Name>, unexpected::Error>;
|
||||
}
|
||||
|
||||
struct FSStore {
|
||||
pub struct FSStore {
|
||||
dir_path: PathBuf,
|
||||
}
|
||||
|
||||
pub fn new(dir_path: &Path) -> io::Result<Box<dyn Store>> {
|
||||
fs::create_dir_all(dir_path)?;
|
||||
Ok(Box::new(FSStore {
|
||||
dir_path: dir_path.into(),
|
||||
}))
|
||||
}
|
||||
|
||||
impl FSStore {
|
||||
pub fn new(dir_path: &Path) -> io::Result<Self> {
|
||||
fs::create_dir_all(dir_path)?;
|
||||
Ok(Self {
|
||||
dir_path: dir_path.into(),
|
||||
})
|
||||
}
|
||||
|
||||
fn config_dir_path(&self, domain: &domain::Name) -> PathBuf {
|
||||
self.dir_path.join(domain.as_str())
|
||||
}
|
||||
|
@ -176,18 +176,22 @@ async fn sync_origins(origin_store: &dyn origin::store::Store, canceller: Cancel
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new(
|
||||
pub fn new<
|
||||
OriginStore: origin::store::Store + 'static,
|
||||
DomainConfigStore: config::Store + 'static,
|
||||
AcmeManager: acme::manager::Manager + 'static,
|
||||
>(
|
||||
task_stack: &mut util::TaskStack<unexpected::Error>,
|
||||
origin_store: Box<dyn origin::store::Store>,
|
||||
domain_config_store: Box<dyn config::Store>,
|
||||
origin_store: OriginStore,
|
||||
domain_config_store: DomainConfigStore,
|
||||
domain_checker: checker::DNSChecker,
|
||||
acme_manager: Option<Box<dyn acme::manager::Manager>>,
|
||||
acme_manager: Option<AcmeManager>,
|
||||
) -> sync::Arc<dyn Manager> {
|
||||
let manager = sync::Arc::new(ManagerImpl {
|
||||
origin_store,
|
||||
domain_config_store,
|
||||
domain_checker,
|
||||
acme_manager,
|
||||
origin_store: Box::from(origin_store),
|
||||
domain_config_store: Box::from(domain_config_store),
|
||||
domain_checker: domain_checker,
|
||||
acme_manager: acme_manager.map(|m| Box::new(m) as Box<dyn acme::manager::Manager>),
|
||||
});
|
||||
|
||||
task_stack.push_spawn(|canceller| {
|
||||
|
@ -1,7 +1,7 @@
|
||||
use std::fmt::Write;
|
||||
use std::{error, fmt};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
/// Error is a String which implements the Error trait. It is intended to be used in
|
||||
/// situations where the caller is being given an error they can't really handle, except to pass it
|
||||
/// along or log it.
|
||||
|
@ -1,5 +1,6 @@
|
||||
#![feature(result_option_inspect)]
|
||||
#![feature(iterator_try_collect)]
|
||||
#![feature(iter_collect_into)]
|
||||
|
||||
pub mod domain;
|
||||
pub mod error;
|
||||
|
23
src/main.rs
23
src/main.rs
@ -73,33 +73,38 @@ async fn main() {
|
||||
)
|
||||
.init();
|
||||
|
||||
let origin_store = domani::origin::store::git::new(config.origin_store_git_dir_path)
|
||||
let origin_store = domani::origin::store::git::FSStore::new(config.origin_store_git_dir_path)
|
||||
.expect("git origin store initialization failed");
|
||||
|
||||
let domain_checker = domani::domain::checker::new(
|
||||
let domain_checker = domani::domain::checker::DNSChecker::new(
|
||||
config.domain_checker_target_a,
|
||||
&config.domain_checker_resolver_addr,
|
||||
)
|
||||
.await
|
||||
.expect("domain checker initialization failed");
|
||||
|
||||
let domain_config_store = domani::domain::config::new(&config.domain_config_store_dir_path)
|
||||
.expect("domain config store initialization failed");
|
||||
let domain_config_store =
|
||||
domani::domain::config::FSStore::new(&config.domain_config_store_dir_path)
|
||||
.expect("domain config store initialization failed");
|
||||
|
||||
let domain_acme_manager = if config.https_listen_addr.is_some() {
|
||||
let domain_acme_store_dir_path = config.domain_acme_store_dir_path.unwrap();
|
||||
|
||||
let domain_acme_store = domani::domain::acme::store::new(&domain_acme_store_dir_path)
|
||||
.expect("domain acme store initialization failed");
|
||||
let domain_acme_store =
|
||||
domani::domain::acme::store::FSStore::new(&domain_acme_store_dir_path)
|
||||
.expect("domain acme store initialization failed");
|
||||
|
||||
// if https_listen_addr is set then domain_acme_contact_email is required, see the Cli/clap
|
||||
// settings.
|
||||
let domain_acme_contact_email = config.domain_acme_contact_email.unwrap();
|
||||
|
||||
Some(
|
||||
domani::domain::acme::manager::new(domain_acme_store, &domain_acme_contact_email)
|
||||
.await
|
||||
.expect("domain acme manager initialization failed"),
|
||||
domani::domain::acme::manager::ManagerImpl::new(
|
||||
domain_acme_store,
|
||||
&domain_acme_contact_email,
|
||||
)
|
||||
.await
|
||||
.expect("domain acme manager initialization failed"),
|
||||
)
|
||||
} else {
|
||||
None
|
||||
|
@ -3,13 +3,14 @@ use crate::origin;
|
||||
use std::sync;
|
||||
|
||||
pub mod git;
|
||||
pub mod mux;
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct Limits {
|
||||
// TODO storage limits
|
||||
}
|
||||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
#[derive(thiserror::Error, Clone, Debug, PartialEq)]
|
||||
pub enum SyncError {
|
||||
#[error("invalid url")]
|
||||
InvalidURL,
|
||||
@ -24,7 +25,7 @@ pub enum SyncError {
|
||||
Unexpected(#[from] unexpected::Error),
|
||||
}
|
||||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
#[derive(thiserror::Error, Clone, Debug, PartialEq)]
|
||||
pub enum GetError {
|
||||
#[error("not found")]
|
||||
NotFound,
|
||||
@ -33,7 +34,7 @@ pub enum GetError {
|
||||
Unexpected(#[from] unexpected::Error),
|
||||
}
|
||||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
#[derive(thiserror::Error, Clone, Debug, PartialEq)]
|
||||
pub enum AllDescrsError {
|
||||
#[error(transparent)]
|
||||
Unexpected(#[from] unexpected::Error),
|
||||
|
@ -59,9 +59,9 @@ enum GetOriginError {
|
||||
Unexpected(#[from] unexpected::Error),
|
||||
}
|
||||
|
||||
/// git::Store implements the Store trait for any Descr::Git based Origins. If any non-git
|
||||
/// Descrs are used then this implementation will panic.
|
||||
struct Store {
|
||||
/// Implements the Store trait for any Descr::Git based Origins, storing the git repos on disk. If
|
||||
/// any non-git Descrs are used then this implementation will panic.
|
||||
pub struct FSStore {
|
||||
dir_path: PathBuf,
|
||||
|
||||
// to prevent against syncing the same origin more than once at a time, but still allowing
|
||||
@ -71,16 +71,16 @@ struct Store {
|
||||
origins: sync::RwLock<collections::HashMap<origin::Descr, sync::Arc<Origin>>>,
|
||||
}
|
||||
|
||||
pub fn new(dir_path: PathBuf) -> io::Result<Box<dyn super::Store>> {
|
||||
fs::create_dir_all(&dir_path)?;
|
||||
Ok(Box::new(Store {
|
||||
dir_path,
|
||||
sync_guard: sync::Mutex::new(collections::HashMap::new()),
|
||||
origins: sync::RwLock::new(collections::HashMap::new()),
|
||||
}))
|
||||
}
|
||||
impl FSStore {
|
||||
pub fn new(dir_path: PathBuf) -> io::Result<Self> {
|
||||
fs::create_dir_all(&dir_path)?;
|
||||
Ok(Self {
|
||||
dir_path,
|
||||
sync_guard: sync::Mutex::new(collections::HashMap::new()),
|
||||
origins: sync::RwLock::new(collections::HashMap::new()),
|
||||
})
|
||||
}
|
||||
|
||||
impl Store {
|
||||
fn repo_path(&self, descr: &origin::Descr) -> PathBuf {
|
||||
self.dir_path.join(descr.id())
|
||||
}
|
||||
@ -208,7 +208,7 @@ impl Store {
|
||||
}
|
||||
}
|
||||
|
||||
impl super::Store for Store {
|
||||
impl super::Store for FSStore {
|
||||
fn sync(&self, descr: origin::Descr, limits: store::Limits) -> Result<(), store::SyncError> {
|
||||
// attempt to lock this descr for syncing, doing so within a new scope so the mutex
|
||||
// isn't actually being held for the whole method duration.
|
||||
@ -344,7 +344,7 @@ mod tests {
|
||||
|
||||
let limits = store::Limits {};
|
||||
|
||||
let store = super::new(tmp_dir.path().to_path_buf()).expect("store created");
|
||||
let store = super::FSStore::new(tmp_dir.path().to_path_buf()).expect("store created");
|
||||
|
||||
store
|
||||
.sync(descr.clone(), limits)
|
||||
|
98
src/origin/store/mux.rs
Normal file
98
src/origin/store/mux.rs
Normal file
@ -0,0 +1,98 @@
|
||||
use crate::error::unexpected::Mappable;
|
||||
use crate::origin::{self, store};
|
||||
use std::sync;
|
||||
|
||||
pub struct Store<F, S>
|
||||
where
|
||||
S: store::Store + 'static,
|
||||
F: Fn(&origin::Descr) -> Option<S> + Sync + Send,
|
||||
{
|
||||
mapping_fn: F,
|
||||
stores: Vec<S>,
|
||||
}
|
||||
|
||||
impl<F, S> Store<F, S>
|
||||
where
|
||||
S: store::Store + 'static,
|
||||
F: Fn(&origin::Descr) -> Option<S> + Sync + Send,
|
||||
{
|
||||
pub fn new(mapping_fn: F, stores: Vec<S>) -> Store<F, S> {
|
||||
Store { mapping_fn, stores }
|
||||
}
|
||||
}
|
||||
|
||||
impl<F, S> store::Store for Store<F, S>
|
||||
where
|
||||
S: store::Store + 'static,
|
||||
F: Fn(&origin::Descr) -> Option<S> + Sync + Send,
|
||||
{
|
||||
fn sync(&self, descr: origin::Descr, limits: store::Limits) -> Result<(), store::SyncError> {
|
||||
(self.mapping_fn)(&descr)
|
||||
.or_unexpected_while(format!("fetching store for {:?}", &descr))?
|
||||
.sync(descr, limits)
|
||||
}
|
||||
|
||||
fn get(&self, descr: origin::Descr) -> Result<sync::Arc<dyn origin::Origin>, store::GetError> {
|
||||
(self.mapping_fn)(&descr)
|
||||
.or_unexpected_while(format!("fetching store for {:?}", &descr))?
|
||||
.get(descr)
|
||||
}
|
||||
|
||||
fn all_descrs(&self) -> Result<Vec<origin::Descr>, store::AllDescrsError> {
|
||||
let mut res = Vec::<origin::Descr>::new();
|
||||
|
||||
for store in self.stores.iter() {
|
||||
store.all_descrs()?.into_iter().collect_into(&mut res);
|
||||
}
|
||||
|
||||
Ok(res)
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::origin::{self, store};
|
||||
use mockall::predicate;
|
||||
use std::{cell, rc};
|
||||
|
||||
#[test]
|
||||
fn basic() {
|
||||
let descrA = origin::Descr::Git {
|
||||
url: "A".to_string(),
|
||||
branch_name: "A".to_string(),
|
||||
};
|
||||
|
||||
//let descrB = origin::Descr::Git {
|
||||
// url: "B".to_string(),
|
||||
// branch_name: "B".to_string(),
|
||||
//};
|
||||
|
||||
let sA_mock = rc::Rc::new(store::MockStore::new());
|
||||
|
||||
//let mut sB = store::MockStore::new();
|
||||
|
||||
let s = {
|
||||
let sA: rc::Rc<dyn store::Store> = sA_mock.clone();
|
||||
let sA = sA.as_ref();
|
||||
|
||||
super::new(
|
||||
|descr| match descr {
|
||||
&origin::Descr::Git { ref url, .. } if url == "A" => Some(sA),
|
||||
//&origin::Descr::Git { ref url, .. } if url == "B" => Some(sB),
|
||||
_ => None,
|
||||
},
|
||||
//vec![sA],
|
||||
)
|
||||
};
|
||||
|
||||
sA_mock
|
||||
.expect_sync()
|
||||
.with(predicate::eq(descrA.clone()), predicate::always())
|
||||
.times(1)
|
||||
.return_const(Ok::<(), store::SyncError>(()));
|
||||
|
||||
assert_eq!(Ok(()), s.sync(descrA, store::Limits {}))
|
||||
}
|
||||
}
|
||||
*/
|
Loading…
Reference in New Issue
Block a user