2023-05-18 20:02:57 +00:00
|
|
|
use crate::domain::{self, acme, checker, config};
|
2023-06-17 16:13:02 +00:00
|
|
|
use crate::error::unexpected::{self, Mappable};
|
2023-06-14 18:22:10 +00:00
|
|
|
use crate::origin;
|
2023-05-17 10:34:24 +00:00
|
|
|
|
2023-05-18 20:02:57 +00:00
|
|
|
use std::{future, pin, sync};
|
2023-06-17 16:13:02 +00:00
|
|
|
use tokio_util::sync::CancellationToken;
|
2023-05-10 11:34:33 +00:00
|
|
|
|
|
|
|
#[derive(thiserror::Error, Debug)]
|
|
|
|
pub enum GetConfigError {
|
|
|
|
#[error("not found")]
|
|
|
|
NotFound,
|
|
|
|
|
|
|
|
#[error(transparent)]
|
2023-06-14 18:22:10 +00:00
|
|
|
Unexpected(#[from] unexpected::Error),
|
2023-05-10 11:34:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<config::GetError> for GetConfigError {
|
|
|
|
fn from(e: config::GetError) -> GetConfigError {
|
|
|
|
match e {
|
|
|
|
config::GetError::NotFound => GetConfigError::NotFound,
|
|
|
|
config::GetError::Unexpected(e) => GetConfigError::Unexpected(e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(thiserror::Error, Debug)]
|
|
|
|
pub enum GetOriginError {
|
|
|
|
#[error("not found")]
|
|
|
|
NotFound,
|
|
|
|
|
|
|
|
#[error(transparent)]
|
2023-06-14 18:22:10 +00:00
|
|
|
Unexpected(#[from] unexpected::Error),
|
2023-05-10 11:34:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<config::GetError> for GetOriginError {
|
|
|
|
fn from(e: config::GetError) -> GetOriginError {
|
|
|
|
match e {
|
|
|
|
config::GetError::NotFound => GetOriginError::NotFound,
|
|
|
|
config::GetError::Unexpected(e) => GetOriginError::Unexpected(e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(thiserror::Error, Debug)]
|
|
|
|
pub enum SyncError {
|
|
|
|
#[error("not found")]
|
|
|
|
NotFound,
|
|
|
|
|
2023-05-10 13:12:34 +00:00
|
|
|
#[error("already in progress")]
|
|
|
|
AlreadyInProgress,
|
|
|
|
|
2023-05-10 11:34:33 +00:00
|
|
|
#[error(transparent)]
|
2023-06-14 18:22:10 +00:00
|
|
|
Unexpected(#[from] unexpected::Error),
|
2023-05-10 11:34:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<config::GetError> for SyncError {
|
|
|
|
fn from(e: config::GetError) -> SyncError {
|
|
|
|
match e {
|
|
|
|
config::GetError::NotFound => SyncError::NotFound,
|
|
|
|
config::GetError::Unexpected(e) => SyncError::Unexpected(e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(thiserror::Error, Debug)]
|
|
|
|
pub enum SyncWithConfigError {
|
|
|
|
#[error("invalid url")]
|
|
|
|
InvalidURL,
|
|
|
|
|
|
|
|
#[error("invalid branch name")]
|
|
|
|
InvalidBranchName,
|
|
|
|
|
2023-05-10 13:12:34 +00:00
|
|
|
#[error("already in progress")]
|
|
|
|
AlreadyInProgress,
|
|
|
|
|
2023-05-15 20:16:29 +00:00
|
|
|
#[error("target A/AAAA not set")]
|
|
|
|
TargetANotSet,
|
2023-05-10 11:34:33 +00:00
|
|
|
|
|
|
|
#[error("challenge token not set")]
|
|
|
|
ChallengeTokenNotSet,
|
|
|
|
|
|
|
|
#[error(transparent)]
|
2023-06-14 18:22:10 +00:00
|
|
|
Unexpected(#[from] unexpected::Error),
|
2023-05-10 11:34:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<origin::store::SyncError> for SyncWithConfigError {
|
|
|
|
fn from(e: origin::store::SyncError) -> SyncWithConfigError {
|
|
|
|
match e {
|
|
|
|
origin::store::SyncError::InvalidURL => SyncWithConfigError::InvalidURL,
|
|
|
|
origin::store::SyncError::InvalidBranchName => SyncWithConfigError::InvalidBranchName,
|
2023-05-10 13:12:34 +00:00
|
|
|
origin::store::SyncError::AlreadyInProgress => SyncWithConfigError::AlreadyInProgress,
|
2023-05-10 11:34:33 +00:00
|
|
|
origin::store::SyncError::Unexpected(e) => SyncWithConfigError::Unexpected(e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<checker::CheckDomainError> for SyncWithConfigError {
|
|
|
|
fn from(e: checker::CheckDomainError) -> SyncWithConfigError {
|
|
|
|
match e {
|
2023-05-15 20:16:29 +00:00
|
|
|
checker::CheckDomainError::TargetANotSet => SyncWithConfigError::TargetANotSet,
|
2023-05-10 11:34:33 +00:00
|
|
|
checker::CheckDomainError::ChallengeTokenNotSet => {
|
|
|
|
SyncWithConfigError::ChallengeTokenNotSet
|
|
|
|
}
|
|
|
|
checker::CheckDomainError::Unexpected(e) => SyncWithConfigError::Unexpected(e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<config::SetError> for SyncWithConfigError {
|
|
|
|
fn from(e: config::SetError) -> SyncWithConfigError {
|
|
|
|
match e {
|
|
|
|
config::SetError::Unexpected(e) => SyncWithConfigError::Unexpected(e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-18 20:02:57 +00:00
|
|
|
pub type GetAcmeHttp01ChallengeKeyError = acme::manager::GetHttp01ChallengeKeyError;
|
|
|
|
|
2023-06-17 16:13:02 +00:00
|
|
|
#[mockall::automock]
|
|
|
|
pub trait Manager: Sync + Send {
|
2023-05-12 13:19:24 +00:00
|
|
|
fn get_config(&self, domain: &domain::Name) -> Result<config::Config, GetConfigError>;
|
2023-05-17 12:37:23 +00:00
|
|
|
|
2023-06-17 16:13:02 +00:00
|
|
|
fn get_origin(
|
2023-05-10 11:34:33 +00:00
|
|
|
&self,
|
2023-06-17 16:13:02 +00:00
|
|
|
domain: &domain::Name,
|
|
|
|
) -> Result<sync::Arc<dyn origin::Origin>, GetOriginError>;
|
|
|
|
|
|
|
|
fn sync_with_config<'mgr>(
|
|
|
|
&'mgr self,
|
2023-05-15 15:42:32 +00:00
|
|
|
domain: domain::Name,
|
|
|
|
config: config::Config,
|
2023-06-17 16:13:02 +00:00
|
|
|
) -> pin::Pin<Box<dyn future::Future<Output = Result<(), SyncWithConfigError>> + Send + 'mgr>>;
|
2023-05-18 20:02:57 +00:00
|
|
|
|
|
|
|
fn get_acme_http01_challenge_key(
|
|
|
|
&self,
|
|
|
|
token: &str,
|
|
|
|
) -> Result<String, GetAcmeHttp01ChallengeKeyError>;
|
2023-05-19 11:26:27 +00:00
|
|
|
|
2023-06-18 11:53:02 +00:00
|
|
|
fn all_domains(&self) -> Result<Vec<domain::Name>, unexpected::Error>;
|
2023-05-10 11:34:33 +00:00
|
|
|
}
|
|
|
|
|
2023-06-18 12:28:46 +00:00
|
|
|
pub struct ManagerImpl {
|
2023-06-17 16:13:02 +00:00
|
|
|
origin_store: sync::Arc<dyn origin::store::Store>,
|
2023-06-18 11:53:02 +00:00
|
|
|
domain_config_store: sync::Arc<dyn config::Store>,
|
2023-05-16 15:17:47 +00:00
|
|
|
domain_checker: checker::DNSChecker,
|
2023-06-18 11:44:15 +00:00
|
|
|
acme_manager: Option<sync::Arc<dyn acme::manager::Manager>>,
|
2023-06-17 16:13:02 +00:00
|
|
|
|
|
|
|
canceller: CancellationToken,
|
|
|
|
origin_sync_handler: tokio::task::JoinHandle<()>,
|
2023-05-16 15:17:47 +00:00
|
|
|
}
|
|
|
|
|
2023-06-17 16:13:02 +00:00
|
|
|
fn sync_origins(origin_store: &dyn origin::store::Store) {
|
|
|
|
match origin_store.all_descrs() {
|
|
|
|
Ok(iter) => iter.into_iter(),
|
|
|
|
Err(err) => {
|
|
|
|
log::error!("Error fetching origin descriptors: {err}");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.for_each(|descr| {
|
|
|
|
if let Err(err) = origin_store.sync(descr.clone(), origin::store::Limits {}) {
|
|
|
|
log::error!("Failed to sync store for {:?}: {err}", descr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-06-18 11:53:02 +00:00
|
|
|
pub fn new(
|
2023-06-17 16:13:02 +00:00
|
|
|
origin_store: sync::Arc<dyn origin::store::Store>,
|
2023-06-18 11:53:02 +00:00
|
|
|
domain_config_store: sync::Arc<dyn config::Store>,
|
2023-05-15 15:42:32 +00:00
|
|
|
domain_checker: checker::DNSChecker,
|
2023-06-18 11:44:15 +00:00
|
|
|
acme_manager: Option<sync::Arc<dyn acme::manager::Manager>>,
|
2023-06-18 12:28:46 +00:00
|
|
|
) -> ManagerImpl {
|
2023-06-17 16:13:02 +00:00
|
|
|
let canceller = CancellationToken::new();
|
|
|
|
|
|
|
|
let origin_sync_handler = {
|
|
|
|
let origin_store = origin_store.clone();
|
|
|
|
let canceller = canceller.clone();
|
|
|
|
tokio::spawn(async move {
|
|
|
|
let mut interval = tokio::time::interval(tokio::time::Duration::from_secs(20 * 60));
|
|
|
|
|
|
|
|
loop {
|
|
|
|
tokio::select! {
|
|
|
|
_ = interval.tick() => sync_origins(origin_store.as_ref()),
|
|
|
|
_ = canceller.cancelled() => return,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
2023-06-18 12:28:46 +00:00
|
|
|
ManagerImpl {
|
2023-05-10 11:34:33 +00:00
|
|
|
origin_store,
|
|
|
|
domain_config_store,
|
|
|
|
domain_checker,
|
2023-05-20 12:34:45 +00:00
|
|
|
acme_manager,
|
2023-06-17 16:13:02 +00:00
|
|
|
canceller,
|
|
|
|
origin_sync_handler,
|
2023-06-18 12:28:46 +00:00
|
|
|
}
|
2023-05-10 11:34:33 +00:00
|
|
|
}
|
|
|
|
|
2023-06-18 11:53:02 +00:00
|
|
|
impl ManagerImpl {
|
2023-06-18 12:28:46 +00:00
|
|
|
pub fn stop(self) -> tokio::task::JoinHandle<()> {
|
2023-06-17 16:13:02 +00:00
|
|
|
self.canceller.cancel();
|
|
|
|
self.origin_sync_handler
|
|
|
|
}
|
2023-05-10 11:34:33 +00:00
|
|
|
}
|
|
|
|
|
2023-06-18 11:53:02 +00:00
|
|
|
impl Manager for ManagerImpl {
|
2023-05-12 13:19:24 +00:00
|
|
|
fn get_config(&self, domain: &domain::Name) -> Result<config::Config, GetConfigError> {
|
2023-05-10 11:34:33 +00:00
|
|
|
Ok(self.domain_config_store.get(domain)?)
|
|
|
|
}
|
|
|
|
|
2023-06-17 16:13:02 +00:00
|
|
|
fn get_origin(
|
|
|
|
&self,
|
|
|
|
domain: &domain::Name,
|
|
|
|
) -> Result<sync::Arc<dyn origin::Origin>, GetOriginError> {
|
2023-05-10 11:34:33 +00:00
|
|
|
let config = self.domain_config_store.get(domain)?;
|
|
|
|
let origin = self
|
|
|
|
.origin_store
|
|
|
|
.get(config.origin_descr)
|
|
|
|
// if there's a config there should be an origin, any error here is unexpected
|
2023-06-14 18:22:10 +00:00
|
|
|
.or_unexpected()?;
|
2023-05-17 10:34:24 +00:00
|
|
|
Ok(origin)
|
2023-05-10 11:34:33 +00:00
|
|
|
}
|
|
|
|
|
2023-06-17 16:13:02 +00:00
|
|
|
fn sync_with_config<'mgr>(
|
|
|
|
&'mgr self,
|
2023-05-15 15:42:32 +00:00
|
|
|
domain: domain::Name,
|
|
|
|
config: config::Config,
|
2023-06-17 16:13:02 +00:00
|
|
|
) -> pin::Pin<Box<dyn future::Future<Output = Result<(), SyncWithConfigError>> + Send + 'mgr>>
|
|
|
|
{
|
2023-05-15 15:42:32 +00:00
|
|
|
Box::pin(async move {
|
2023-06-14 18:22:10 +00:00
|
|
|
let config_hash = config
|
|
|
|
.hash()
|
|
|
|
.or_unexpected_while("calculating config hash")?;
|
2023-05-10 11:34:33 +00:00
|
|
|
|
2023-05-15 15:42:32 +00:00
|
|
|
self.domain_checker
|
|
|
|
.check_domain(&domain, &config_hash)
|
|
|
|
.await?;
|
2023-05-10 11:34:33 +00:00
|
|
|
|
2023-05-15 15:42:32 +00:00
|
|
|
self.origin_store
|
|
|
|
.sync(config.origin_descr.clone(), origin::store::Limits {})?;
|
2023-05-10 11:34:33 +00:00
|
|
|
|
2023-05-15 15:42:32 +00:00
|
|
|
self.domain_config_store.set(&domain, &config)?;
|
2023-05-10 11:34:33 +00:00
|
|
|
|
2023-05-18 20:02:57 +00:00
|
|
|
if let Some(ref acme_manager) = self.acme_manager {
|
|
|
|
acme_manager.sync_domain(domain.clone()).await?;
|
|
|
|
}
|
|
|
|
|
2023-05-15 15:42:32 +00:00
|
|
|
Ok(())
|
|
|
|
})
|
2023-05-10 11:34:33 +00:00
|
|
|
}
|
2023-05-17 12:37:23 +00:00
|
|
|
|
2023-05-18 20:02:57 +00:00
|
|
|
fn get_acme_http01_challenge_key(
|
|
|
|
&self,
|
|
|
|
token: &str,
|
|
|
|
) -> Result<String, GetAcmeHttp01ChallengeKeyError> {
|
|
|
|
if let Some(ref acme_manager) = self.acme_manager {
|
|
|
|
return acme_manager.get_http01_challenge_key(token);
|
|
|
|
}
|
|
|
|
|
|
|
|
Err(GetAcmeHttp01ChallengeKeyError::NotFound)
|
|
|
|
}
|
2023-05-19 11:26:27 +00:00
|
|
|
|
2023-06-18 11:53:02 +00:00
|
|
|
fn all_domains(&self) -> Result<Vec<domain::Name>, unexpected::Error> {
|
2023-05-19 11:26:27 +00:00
|
|
|
self.domain_config_store.all_domains()
|
|
|
|
}
|
2023-05-10 11:34:33 +00:00
|
|
|
}
|