|
| 1 | +use super::*; |
| 2 | + |
| 3 | +const ACCOUNT_LOCK_WAIT_TIMEOUT: Duration = Duration::from_secs(10); |
| 4 | +const ACCOUNT_LOCK_RETRY_INTERVAL: Duration = Duration::from_millis(25); |
| 5 | + |
| 6 | +pub(crate) async fn load_account_credentials_async( |
| 7 | + storage: &Path, |
| 8 | + issuer_name: &str, |
| 9 | +) -> Result<Option<instant_acme::AccountCredentials>, AcmeInstantClientError> { |
| 10 | + load_account_credentials_async_with_timeout(storage, issuer_name, ACCOUNT_LOCK_WAIT_TIMEOUT) |
| 11 | + .await |
| 12 | +} |
| 13 | + |
| 14 | +async fn load_account_credentials_async_with_timeout( |
| 15 | + storage: &Path, |
| 16 | + issuer_name: &str, |
| 17 | + wait_timeout: Duration, |
| 18 | +) -> Result<Option<instant_acme::AccountCredentials>, AcmeInstantClientError> { |
| 19 | + let storage = storage.to_path_buf(); |
| 20 | + let issuer = issuer_name.to_owned(); |
| 21 | + run_account_store_attempt(issuer_name, "credential load", wait_timeout, move || { |
| 22 | + try_load_account_credentials(&storage, &issuer) |
| 23 | + }) |
| 24 | + .await |
| 25 | +} |
| 26 | + |
| 27 | +pub(crate) async fn begin_account_bootstrap_async( |
| 28 | + storage: &Path, |
| 29 | + issuer_name: &str, |
| 30 | + issuer_directory: &str, |
| 31 | +) -> Result<AccountBootstrap, AcmeInstantClientError> { |
| 32 | + let storage = storage.to_path_buf(); |
| 33 | + let issuer = issuer_name.to_owned(); |
| 34 | + let directory = issuer_directory.to_owned(); |
| 35 | + run_account_store_attempt( |
| 36 | + issuer_name, |
| 37 | + "bootstrap lock", |
| 38 | + ACCOUNT_LOCK_WAIT_TIMEOUT, |
| 39 | + move || try_begin_account_bootstrap(&storage, &issuer, &directory), |
| 40 | + ) |
| 41 | + .await |
| 42 | +} |
| 43 | + |
| 44 | +pub(crate) async fn promote_account_bootstrap_async( |
| 45 | + bootstrap: PendingAccountBootstrap, |
| 46 | + credentials: instant_acme::AccountCredentials, |
| 47 | + issuer_name: &str, |
| 48 | +) -> Result<(), AcmeInstantClientError> { |
| 49 | + run_account_store_task(issuer_name, "credential promotion", move || { |
| 50 | + bootstrap.promote(&credentials) |
| 51 | + }) |
| 52 | + .await |
| 53 | +} |
| 54 | + |
| 55 | +pub(crate) async fn begin_account_deactivation_async( |
| 56 | + storage: &Path, |
| 57 | + issuer_name: &str, |
| 58 | +) -> Result<AccountDeactivationTransaction, AcmeInstantClientError> { |
| 59 | + let storage = storage.to_path_buf(); |
| 60 | + let issuer = issuer_name.to_owned(); |
| 61 | + run_account_store_attempt( |
| 62 | + issuer_name, |
| 63 | + "deactivation lock", |
| 64 | + ACCOUNT_LOCK_WAIT_TIMEOUT, |
| 65 | + move || try_begin_account_deactivation(&storage, &issuer), |
| 66 | + ) |
| 67 | + .await |
| 68 | +} |
| 69 | + |
| 70 | +pub(crate) async fn complete_account_deactivation_async( |
| 71 | + transaction: AccountDeactivationTransaction, |
| 72 | + issuer_name: &str, |
| 73 | +) -> Result<(), AcmeInstantClientError> { |
| 74 | + run_account_store_task(issuer_name, "deactivation completion", move || { |
| 75 | + transaction.complete() |
| 76 | + }) |
| 77 | + .await |
| 78 | +} |
| 79 | + |
| 80 | +pub(crate) async fn rollback_account_deactivation_async( |
| 81 | + transaction: AccountDeactivationTransaction, |
| 82 | + issuer_name: &str, |
| 83 | +) -> Result<(), AcmeInstantClientError> { |
| 84 | + run_account_store_task(issuer_name, "deactivation rollback", move || { |
| 85 | + transaction.rollback() |
| 86 | + }) |
| 87 | + .await |
| 88 | +} |
| 89 | + |
| 90 | +async fn run_account_store_attempt<T, F>( |
| 91 | + issuer_name: &str, |
| 92 | + operation_name: &'static str, |
| 93 | + wait_timeout: Duration, |
| 94 | + operation: F, |
| 95 | +) -> Result<T, AcmeInstantClientError> |
| 96 | +where |
| 97 | + T: Send + 'static, |
| 98 | + F: Fn() -> Result<AccountStoreAttempt<T>, AcmeAccountStoreError> + Clone + Send + 'static, |
| 99 | +{ |
| 100 | + let started = std::time::Instant::now(); |
| 101 | + let mut contention_logged = false; |
| 102 | + loop { |
| 103 | + let attempt = tokio::task::spawn_blocking(operation.clone()) |
| 104 | + .await |
| 105 | + .map_err(|error| { |
| 106 | + account_async_error( |
| 107 | + issuer_name, |
| 108 | + format!("ACME account {operation_name} task failed: {error}"), |
| 109 | + ) |
| 110 | + })? |
| 111 | + .map_err(AcmeInstantClientError::AccountStore)?; |
| 112 | + match attempt { |
| 113 | + AccountStoreAttempt::Acquired(value) => return Ok(value), |
| 114 | + AccountStoreAttempt::Contended => { |
| 115 | + if !contention_logged { |
| 116 | + log::warn!( |
| 117 | + target: "fluxheim::acme", |
| 118 | + "ACME account {operation_name} waiting for issuer {issuer_name:?} lifecycle lock" |
| 119 | + ); |
| 120 | + contention_logged = true; |
| 121 | + } |
| 122 | + if started.elapsed() >= wait_timeout { |
| 123 | + return Err(account_async_error( |
| 124 | + issuer_name, |
| 125 | + format!( |
| 126 | + "ACME account {operation_name} timed out after {} seconds", |
| 127 | + wait_timeout.as_secs_f64() |
| 128 | + ), |
| 129 | + )); |
| 130 | + } |
| 131 | + tokio::time::sleep(ACCOUNT_LOCK_RETRY_INTERVAL).await; |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +async fn run_account_store_task<T, F>( |
| 138 | + issuer_name: &str, |
| 139 | + operation_name: &'static str, |
| 140 | + operation: F, |
| 141 | +) -> Result<T, AcmeInstantClientError> |
| 142 | +where |
| 143 | + T: Send + 'static, |
| 144 | + F: FnOnce() -> Result<T, AcmeAccountStoreError> + Send + 'static, |
| 145 | +{ |
| 146 | + tokio::task::spawn_blocking(operation) |
| 147 | + .await |
| 148 | + .map_err(|error| { |
| 149 | + account_async_error( |
| 150 | + issuer_name, |
| 151 | + format!("ACME account {operation_name} task failed: {error}"), |
| 152 | + ) |
| 153 | + })? |
| 154 | + .map_err(AcmeInstantClientError::AccountStore) |
| 155 | +} |
| 156 | + |
| 157 | +fn account_async_error(issuer: &str, message: impl Into<String>) -> AcmeInstantClientError { |
| 158 | + AcmeInstantClientError::Account { |
| 159 | + issuer: issuer.to_owned(), |
| 160 | + message: message.into(), |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +#[cfg(test)] |
| 165 | +pub(crate) async fn load_account_credentials_with_test_timeout( |
| 166 | + storage: &Path, |
| 167 | + issuer_name: &str, |
| 168 | + wait_timeout: Duration, |
| 169 | +) -> Result<Option<instant_acme::AccountCredentials>, AcmeInstantClientError> { |
| 170 | + load_account_credentials_async_with_timeout(storage, issuer_name, wait_timeout).await |
| 171 | +} |
0 commit comments