|
1 | 1 | use super::*; |
| 2 | +use std::sync::Arc; |
2 | 3 |
|
3 | 4 | const ACCOUNT_LOCK_WAIT_TIMEOUT: Duration = Duration::from_secs(10); |
4 | 5 | const ACCOUNT_LOCK_RETRY_INTERVAL: Duration = Duration::from_millis(25); |
5 | 6 |
|
6 | | -pub(crate) async fn load_account_credentials_async( |
| 7 | +/// Loads ACME credentials without blocking a Tokio worker. |
| 8 | +/// |
| 9 | +/// Returns an error when the issuer lifecycle lock cannot be acquired within |
| 10 | +/// Fluxheim's bounded account-lock deadline. |
| 11 | +pub async fn load_account_credentials_async( |
7 | 12 | storage: &Path, |
8 | 13 | issuer_name: &str, |
9 | 14 | ) -> Result<Option<instant_acme::AccountCredentials>, AcmeInstantClientError> { |
10 | 15 | load_account_credentials_async_with_timeout(storage, issuer_name, ACCOUNT_LOCK_WAIT_TIMEOUT) |
11 | 16 | .await |
12 | 17 | } |
13 | 18 |
|
| 19 | +/// Persists ACME credentials without blocking a Tokio worker. |
| 20 | +/// |
| 21 | +/// Returns an error when the issuer lifecycle lock cannot be acquired within |
| 22 | +/// Fluxheim's bounded account-lock deadline. |
| 23 | +pub async fn store_account_credentials_async( |
| 24 | + storage: &Path, |
| 25 | + issuer_name: &str, |
| 26 | + credentials: instant_acme::AccountCredentials, |
| 27 | +) -> Result<AcmeAccountCredentialsPath, AcmeInstantClientError> { |
| 28 | + store_account_credentials_async_with_timeout( |
| 29 | + storage, |
| 30 | + issuer_name, |
| 31 | + credentials, |
| 32 | + ACCOUNT_LOCK_WAIT_TIMEOUT, |
| 33 | + ) |
| 34 | + .await |
| 35 | +} |
| 36 | + |
| 37 | +async fn store_account_credentials_async_with_timeout( |
| 38 | + storage: &Path, |
| 39 | + issuer_name: &str, |
| 40 | + credentials: instant_acme::AccountCredentials, |
| 41 | + wait_timeout: Duration, |
| 42 | +) -> Result<AcmeAccountCredentialsPath, AcmeInstantClientError> { |
| 43 | + let storage = storage.to_path_buf(); |
| 44 | + let issuer = issuer_name.to_owned(); |
| 45 | + let credentials = Arc::new(credentials); |
| 46 | + run_account_store_attempt(issuer_name, "credential store", wait_timeout, move || { |
| 47 | + try_store_account_credentials(&storage, &issuer, credentials.as_ref()) |
| 48 | + }) |
| 49 | + .await |
| 50 | +} |
| 51 | + |
| 52 | +/// Removes ACME credentials without blocking a Tokio worker. |
| 53 | +/// |
| 54 | +/// Returns an error when the issuer lifecycle lock cannot be acquired within |
| 55 | +/// Fluxheim's bounded account-lock deadline. |
| 56 | +pub async fn remove_account_credentials_async( |
| 57 | + storage: &Path, |
| 58 | + issuer_name: &str, |
| 59 | +) -> Result<bool, AcmeInstantClientError> { |
| 60 | + remove_account_credentials_async_with_timeout(storage, issuer_name, ACCOUNT_LOCK_WAIT_TIMEOUT) |
| 61 | + .await |
| 62 | +} |
| 63 | + |
| 64 | +async fn remove_account_credentials_async_with_timeout( |
| 65 | + storage: &Path, |
| 66 | + issuer_name: &str, |
| 67 | + wait_timeout: Duration, |
| 68 | +) -> Result<bool, AcmeInstantClientError> { |
| 69 | + let storage = storage.to_path_buf(); |
| 70 | + let issuer = issuer_name.to_owned(); |
| 71 | + run_account_store_attempt(issuer_name, "credential removal", wait_timeout, move || { |
| 72 | + try_remove_account_credentials(&storage, &issuer) |
| 73 | + }) |
| 74 | + .await |
| 75 | +} |
| 76 | + |
| 77 | +fn try_store_account_credentials( |
| 78 | + storage: &Path, |
| 79 | + issuer_name: &str, |
| 80 | + credentials: &instant_acme::AccountCredentials, |
| 81 | +) -> Result<AccountStoreAttempt<AcmeAccountCredentialsPath>, AcmeAccountStoreError> { |
| 82 | + let credentials_path = account_credentials_path(storage, issuer_name); |
| 83 | + let directory = account_directory(&credentials_path)?; |
| 84 | + ensure_safe_account_directory(directory)?; |
| 85 | + let Some(_lock) = AcmeMutationLock::try_acquire(directory).map_err(|error| { |
| 86 | + account_async_store_io_error(&directory.join(".fluxheim-acme.lock"), error) |
| 87 | + })? |
| 88 | + else { |
| 89 | + return Ok(AccountStoreAttempt::Contended); |
| 90 | + }; |
| 91 | + let pending = directory.join(".credentials.bootstrap.pending"); |
| 92 | + if pending |
| 93 | + .try_exists() |
| 94 | + .map_err(|error| account_async_store_io_error(&pending, error))? |
| 95 | + { |
| 96 | + return Err(AcmeAccountStoreError::UnsafePath { |
| 97 | + path: pending, |
| 98 | + message: "account bootstrap is pending; credentials must be promoted by the bootstrap transaction" |
| 99 | + .to_owned(), |
| 100 | + }); |
| 101 | + } |
| 102 | + store_account_credentials_locked(&credentials_path, directory, credentials) |
| 103 | + .map(AccountStoreAttempt::Acquired) |
| 104 | +} |
| 105 | + |
| 106 | +fn try_remove_account_credentials( |
| 107 | + storage: &Path, |
| 108 | + issuer_name: &str, |
| 109 | +) -> Result<AccountStoreAttempt<bool>, AcmeAccountStoreError> { |
| 110 | + let credentials_path = account_credentials_path(storage, issuer_name); |
| 111 | + let directory = account_directory(&credentials_path)?; |
| 112 | + ensure_safe_account_directory(directory)?; |
| 113 | + let Some(_lock) = AcmeMutationLock::try_acquire(directory).map_err(|error| { |
| 114 | + account_async_store_io_error(&directory.join(".fluxheim-acme.lock"), error) |
| 115 | + })? |
| 116 | + else { |
| 117 | + return Ok(AccountStoreAttempt::Contended); |
| 118 | + }; |
| 119 | + ensure_safe_account_destination(&credentials_path.path)?; |
| 120 | + let removed = match fs::remove_file(&credentials_path.path) { |
| 121 | + Ok(()) => { |
| 122 | + let directory_file = fs::File::open(directory) |
| 123 | + .map_err(|error| account_async_store_io_error(directory, error))?; |
| 124 | + directory_file |
| 125 | + .sync_all() |
| 126 | + .map_err(|error| account_async_store_io_error(directory, error))?; |
| 127 | + true |
| 128 | + } |
| 129 | + Err(error) if error.kind() == io::ErrorKind::NotFound => false, |
| 130 | + Err(error) => return Err(account_async_store_io_error(&credentials_path.path, error)), |
| 131 | + }; |
| 132 | + Ok(AccountStoreAttempt::Acquired(removed)) |
| 133 | +} |
| 134 | + |
| 135 | +fn account_directory( |
| 136 | + credentials_path: &AcmeAccountCredentialsPath, |
| 137 | +) -> Result<&Path, AcmeAccountStoreError> { |
| 138 | + credentials_path |
| 139 | + .path |
| 140 | + .parent() |
| 141 | + .ok_or_else(|| AcmeAccountStoreError::UnsafePath { |
| 142 | + path: credentials_path.path.clone(), |
| 143 | + message: "credentials path has no parent directory".to_owned(), |
| 144 | + }) |
| 145 | +} |
| 146 | + |
| 147 | +fn account_async_store_io_error(path: &Path, error: io::Error) -> AcmeAccountStoreError { |
| 148 | + AcmeAccountStoreError::Io { |
| 149 | + path: path.to_path_buf(), |
| 150 | + error, |
| 151 | + } |
| 152 | +} |
| 153 | + |
14 | 154 | async fn load_account_credentials_async_with_timeout( |
15 | 155 | storage: &Path, |
16 | 156 | issuer_name: &str, |
@@ -169,3 +309,23 @@ pub(crate) async fn load_account_credentials_with_test_timeout( |
169 | 309 | ) -> Result<Option<instant_acme::AccountCredentials>, AcmeInstantClientError> { |
170 | 310 | load_account_credentials_async_with_timeout(storage, issuer_name, wait_timeout).await |
171 | 311 | } |
| 312 | + |
| 313 | +#[cfg(test)] |
| 314 | +pub(crate) async fn store_account_credentials_with_test_timeout( |
| 315 | + storage: &Path, |
| 316 | + issuer_name: &str, |
| 317 | + credentials: instant_acme::AccountCredentials, |
| 318 | + wait_timeout: Duration, |
| 319 | +) -> Result<AcmeAccountCredentialsPath, AcmeInstantClientError> { |
| 320 | + store_account_credentials_async_with_timeout(storage, issuer_name, credentials, wait_timeout) |
| 321 | + .await |
| 322 | +} |
| 323 | + |
| 324 | +#[cfg(test)] |
| 325 | +pub(crate) async fn remove_account_credentials_with_test_timeout( |
| 326 | + storage: &Path, |
| 327 | + issuer_name: &str, |
| 328 | + wait_timeout: Duration, |
| 329 | +) -> Result<bool, AcmeInstantClientError> { |
| 330 | + remove_account_credentials_async_with_timeout(storage, issuer_name, wait_timeout).await |
| 331 | +} |
0 commit comments