pub struct CertManager { /* private fields */ }Expand description
Certificate manager for TLS certificate provisioning and caching
The CertManager handles:
- Loading existing certificates from disk
- Caching certificates in memory
- Provisioning new certificates via ACME
Implementations§
Source§impl CertManager
impl CertManager
Sourcepub async fn new(
storage_path: String,
acme_email: Option<String>,
) -> Result<Self, Box<dyn Error + Send + Sync>>
pub async fn new( storage_path: String, acme_email: Option<String>, ) -> Result<Self, Box<dyn Error + Send + Sync>>
Sourcepub async fn with_directory(
storage_path: String,
acme_email: Option<String>,
acme_directory: String,
) -> Result<Self, Box<dyn Error + Send + Sync>>
pub async fn with_directory( storage_path: String, acme_email: Option<String>, acme_directory: String, ) -> Result<Self, Box<dyn Error + Send + Sync>>
Create a new certificate manager with a custom ACME directory
§Arguments
storage_path- Directory to store certificatesacme_email- Optional email for ACME account registrationacme_directory- ACME directory URL (e.g., Let’s Encrypt production/staging)
§Errors
Returns an error if the storage directory cannot be created.
Sourcepub fn acme_directory(&self) -> &str
pub fn acme_directory(&self) -> &str
Get the ACME directory URL
Sourcepub async fn get_cert(
&self,
domain: &str,
) -> Result<(String, String), Box<dyn Error + Send + Sync>>
pub async fn get_cert( &self, domain: &str, ) -> Result<(String, String), Box<dyn Error + Send + Sync>>
Get a certificate for a domain
This method:
- Checks the memory cache
- Checks disk storage
- Provisions via ACME if not found (future)
§Arguments
domain- The domain to get a certificate for
§Returns
Tuple of (certificate_pem, private_key_pem)
§Errors
Returns an error if the certificate is not found and ACME provisioning fails.
Sourcepub async fn store_cert(
&self,
domain: &str,
cert: &str,
key: &str,
) -> Result<(), Box<dyn Error + Send + Sync>>
pub async fn store_cert( &self, domain: &str, cert: &str, key: &str, ) -> Result<(), Box<dyn Error + Send + Sync>>
Store a certificate
This method stores the certificate and key to disk, updates the memory cache, and extracts/saves certificate metadata for renewal tracking.
§Arguments
domain- The domaincert- Certificate PEM contentkey- Private key PEM content
§Errors
Returns an error if writing the certificate or key files to disk fails.
Sourcepub fn acme_email(&self) -> Option<&str>
pub fn acme_email(&self) -> Option<&str>
Get the ACME email (if configured)
Sourcepub fn storage_path(&self) -> &PathBuf
pub fn storage_path(&self) -> &PathBuf
Get the storage path
Sourcepub async fn clear_cache(&self)
pub async fn clear_cache(&self)
Clear the certificate cache
Sourcepub async fn cached_count(&self) -> usize
pub async fn cached_count(&self) -> usize
Get cached certificate count
Sourcepub async fn list_cached_domains(&self) -> Vec<String>
pub async fn list_cached_domains(&self) -> Vec<String>
Return the list of domain names currently held in the certificate cache.
Sourcepub fn parse_cert_expiry(
cert_pem: &str,
) -> Result<(DateTime<Utc>, DateTime<Utc>), Box<dyn Error + Send + Sync>>
pub fn parse_cert_expiry( cert_pem: &str, ) -> Result<(DateTime<Utc>, DateTime<Utc>), Box<dyn Error + Send + Sync>>
Sourcepub async fn load_cert_metadata(&self, domain: &str) -> Option<CertMetadata>
pub async fn load_cert_metadata(&self, domain: &str) -> Option<CertMetadata>
Sourcepub async fn get_domains_needing_renewal(&self) -> Vec<String>
pub async fn get_domains_needing_renewal(&self) -> Vec<String>
Get domains with certificates expiring within a threshold
Returns domains with certificates that expire within 30 days (RENEWAL_THRESHOLD_DAYS).
§Returns
Vector of domain names with certificates needing renewal
Sourcepub fn start_renewal_task(
self: Arc<Self>,
sni_resolver: Arc<SniCertResolver>,
) -> JoinHandle<()>
pub fn start_renewal_task( self: Arc<Self>, sni_resolver: Arc<SniCertResolver>, ) -> JoinHandle<()>
Sourcepub async fn run_renewal_check(
&self,
sni_resolver: &SniCertResolver,
) -> Vec<String>
pub async fn run_renewal_check( &self, sni_resolver: &SniCertResolver, ) -> Vec<String>
Run a single renewal check (for testing or on-demand renewal)
This method checks for certificates needing renewal and attempts to renew them.
Unlike start_renewal_task, this runs once and returns immediately.
§Arguments
sni_resolver- The SNI resolver to update with renewed certificates
§Returns
Vector of domain names that were successfully renewed
Sourcepub async fn load_account(&self) -> Option<AcmeAccount>
pub async fn load_account(&self) -> Option<AcmeAccount>
Load an existing ACME account from disk
This loads the account metadata. Use load_credentials() to load the
actual credentials needed for ACME operations.
§Returns
The account if it exists and is valid, None otherwise
Sourcepub async fn save_account(
&self,
account: &AcmeAccount,
) -> Result<(), Box<dyn Error + Send + Sync>>
pub async fn save_account( &self, account: &AcmeAccount, ) -> Result<(), Box<dyn Error + Send + Sync>>
Sourcepub async fn get_or_create_account(
&self,
) -> Result<AcmeAccount, Box<dyn Error + Send + Sync>>
pub async fn get_or_create_account( &self, ) -> Result<AcmeAccount, Box<dyn Error + Send + Sync>>
Get or create an ACME account (returns our metadata struct)
This method:
- Returns the cached account if available
- Loads the account from disk if it exists
- Creates a new account via ACME
§Errors
Returns an error if ACME account creation or restoration fails.
Sourcepub async fn get_account(&self) -> Option<AcmeAccount>
pub async fn get_account(&self) -> Option<AcmeAccount>
Sourcepub async fn has_account(&self) -> bool
pub async fn has_account(&self) -> bool
Check if an ACME account exists (either cached or on disk)
Sourcepub fn store_challenge(
&self,
token: &str,
domain: &str,
key_authorization: &str,
)
pub fn store_challenge( &self, token: &str, domain: &str, key_authorization: &str, )
Store an ACME HTTP-01 challenge token
This stores the challenge token that will be served at
/.well-known/acme-challenge/{token} for domain validation.
§Arguments
token- The challenge token from the ACME serverdomain- The domain being validatedkey_authorization- The key authorization response (token.thumbprint)
Sourcepub fn get_challenge_response(&self, token: &str) -> Option<String>
pub fn get_challenge_response(&self, token: &str) -> Option<String>
Sourcepub fn remove_challenge(&self, token: &str)
pub fn remove_challenge(&self, token: &str)
Remove a challenge token after validation completes
§Arguments
token- The challenge token to remove
Sourcepub fn clear_challenges_for_domain(&self, domain: &str)
pub fn clear_challenges_for_domain(&self, domain: &str)
Clear all challenge tokens for a specific domain
Useful when certificate issuance completes or fails for a domain.
§Arguments
domain- The domain to clear challenges for
Sourcepub fn cleanup_expired_challenges(&self)
pub fn cleanup_expired_challenges(&self)
Clean up expired challenge tokens
Removes all challenge tokens older than 5 minutes. This should be called periodically to prevent memory leaks.
Sourcepub fn challenge_count(&self) -> usize
pub fn challenge_count(&self) -> usize
Get the number of active challenge tokens