2023-05-12 13:19:24 +00:00
|
|
|
use crate::domain::{self, checker, config};
|
2023-05-10 11:34:33 +00:00
|
|
|
use crate::origin;
|
|
|
|
use std::error::Error;
|
|
|
|
|
|
|
|
#[derive(thiserror::Error, Debug)]
|
|
|
|
pub enum GetConfigError {
|
|
|
|
#[error("not found")]
|
|
|
|
NotFound,
|
|
|
|
|
|
|
|
#[error(transparent)]
|
|
|
|
Unexpected(Box<dyn Error>),
|
|
|
|
}
|
|
|
|
|
|
|
|
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)]
|
|
|
|
Unexpected(Box<dyn Error>),
|
|
|
|
}
|
|
|
|
|
|
|
|
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)]
|
|
|
|
Unexpected(Box<dyn Error>),
|
|
|
|
}
|
|
|
|
|
|
|
|
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-10 11:34:33 +00:00
|
|
|
#[error("target CNAME not set")]
|
|
|
|
TargetCNAMENotSet,
|
|
|
|
|
|
|
|
#[error("challenge token not set")]
|
|
|
|
ChallengeTokenNotSet,
|
|
|
|
|
|
|
|
#[error(transparent)]
|
|
|
|
Unexpected(Box<dyn Error>),
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
checker::CheckDomainError::TargetCNAMENotSet => SyncWithConfigError::TargetCNAMENotSet,
|
|
|
|
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-11 09:47:38 +00:00
|
|
|
#[mockall::automock(type Origin=origin::MockOrigin;)]
|
2023-05-12 14:43:28 +00:00
|
|
|
pub trait Manager: std::marker::Send + std::marker::Sync {
|
2023-05-11 09:47:38 +00:00
|
|
|
type Origin<'mgr>: origin::Origin + 'mgr
|
|
|
|
where
|
|
|
|
Self: 'mgr;
|
|
|
|
|
2023-05-12 13:19:24 +00:00
|
|
|
fn get_config(&self, domain: &domain::Name) -> Result<config::Config, GetConfigError>;
|
|
|
|
fn get_origin(&self, domain: &domain::Name) -> Result<Self::Origin<'_>, GetOriginError>;
|
|
|
|
fn sync(&self, domain: &domain::Name) -> Result<(), SyncError>;
|
2023-05-10 11:34:33 +00:00
|
|
|
fn sync_with_config(
|
|
|
|
&self,
|
2023-05-12 13:19:24 +00:00
|
|
|
domain: &domain::Name,
|
2023-05-10 11:34:33 +00:00
|
|
|
config: &config::Config,
|
|
|
|
) -> Result<(), SyncWithConfigError>;
|
|
|
|
}
|
|
|
|
|
2023-05-11 12:19:36 +00:00
|
|
|
pub fn new<OriginStore, DomainConfigStore, DomainChecker>(
|
2023-05-10 11:34:33 +00:00
|
|
|
origin_store: OriginStore,
|
|
|
|
domain_config_store: DomainConfigStore,
|
|
|
|
domain_checker: DomainChecker,
|
|
|
|
) -> impl Manager
|
|
|
|
where
|
2023-05-11 09:47:38 +00:00
|
|
|
OriginStore: origin::store::Store,
|
2023-05-10 11:34:33 +00:00
|
|
|
DomainConfigStore: config::Store,
|
|
|
|
DomainChecker: checker::Checker,
|
|
|
|
{
|
|
|
|
ManagerImpl {
|
|
|
|
origin_store,
|
|
|
|
domain_config_store,
|
|
|
|
domain_checker,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ManagerImpl<OriginStore, DomainConfigStore, DomainChecker>
|
|
|
|
where
|
2023-05-11 09:47:38 +00:00
|
|
|
OriginStore: origin::store::Store,
|
2023-05-10 11:34:33 +00:00
|
|
|
DomainConfigStore: config::Store,
|
|
|
|
DomainChecker: checker::Checker,
|
|
|
|
{
|
|
|
|
origin_store: OriginStore,
|
|
|
|
domain_config_store: DomainConfigStore,
|
|
|
|
domain_checker: DomainChecker,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<OriginStore, DomainConfigStore, DomainChecker> Manager
|
|
|
|
for ManagerImpl<OriginStore, DomainConfigStore, DomainChecker>
|
|
|
|
where
|
2023-05-11 09:47:38 +00:00
|
|
|
OriginStore: origin::store::Store,
|
2023-05-10 11:34:33 +00:00
|
|
|
DomainConfigStore: config::Store,
|
|
|
|
DomainChecker: checker::Checker,
|
|
|
|
{
|
2023-05-11 09:47:38 +00:00
|
|
|
type Origin<'mgr> = OriginStore::Origin<'mgr>
|
|
|
|
where Self: 'mgr;
|
|
|
|
|
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-05-12 13:19:24 +00:00
|
|
|
fn get_origin(&self, domain: &domain::Name) -> Result<Self::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
|
|
|
|
.map_err(|e| GetOriginError::Unexpected(Box::from(e)))?;
|
|
|
|
Ok(origin)
|
|
|
|
}
|
|
|
|
|
2023-05-12 13:19:24 +00:00
|
|
|
fn sync(&self, domain: &domain::Name) -> Result<(), SyncError> {
|
2023-05-10 11:34:33 +00:00
|
|
|
let config = self.domain_config_store.get(domain)?;
|
|
|
|
self.origin_store
|
2023-05-10 13:12:34 +00:00
|
|
|
.sync(config.origin_descr, origin::store::Limits {})
|
|
|
|
.map_err(|e| match e {
|
|
|
|
origin::store::SyncError::AlreadyInProgress => SyncError::AlreadyInProgress,
|
|
|
|
_ => SyncError::Unexpected(Box::from(e)),
|
|
|
|
})?;
|
2023-05-10 11:34:33 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn sync_with_config(
|
|
|
|
&self,
|
2023-05-12 13:19:24 +00:00
|
|
|
domain: &domain::Name,
|
2023-05-10 11:34:33 +00:00
|
|
|
config: &config::Config,
|
|
|
|
) -> Result<(), SyncWithConfigError> {
|
|
|
|
let config_hash = config
|
|
|
|
.hash()
|
|
|
|
.map_err(|e| SyncWithConfigError::Unexpected(Box::from(e)))?;
|
|
|
|
|
2023-05-14 09:18:36 +00:00
|
|
|
self.domain_checker.check_domain(&domain, &config_hash)?;
|
2023-05-10 11:34:33 +00:00
|
|
|
|
|
|
|
self.origin_store
|
2023-05-10 13:12:34 +00:00
|
|
|
.sync(config.origin_descr.clone(), origin::store::Limits {})?;
|
2023-05-10 11:34:33 +00:00
|
|
|
|
|
|
|
self.domain_config_store.set(domain, config)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|