Skip to content

Commit 8a02e16

Browse files
committed
Don't require passing in the server_url on change_key()
1 parent b9e4410 commit 8a02e16

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

src/account.rs

+16-6
Original file line numberDiff line numberDiff line change
@@ -336,12 +336,11 @@ impl Account {
336336
///
337337
/// This is useful if you want to change the ACME account key of an existing account, e.g.
338338
/// to mitigate the risk of a key compromise. This method creates a new client key and changes
339-
/// the key associated with the existing account. In case the key rollover succeeds the new
340-
/// account credentials are returned for further usage. After that a new Account object with
341-
/// the updated client key needs to be crated for further interaction with the ACME account.
339+
/// the key associated with the existing account. `self` will be updated with the new key,
340+
/// and a fresh set of [`AccountCredentials`] will be returned to update stored credentials.
342341
///
343342
/// See <https://datatracker.ietf.org/doc/html/rfc8555#section-7.3.5> for more information.
344-
pub async fn change_key(&self, server_url: &str) -> Result<AccountCredentials, Error> {
343+
pub async fn change_key(&mut self) -> Result<AccountCredentials, Error> {
345344
let new_key_url = match self.inner.client.directory.key_change.as_deref() {
346345
Some(url) => url,
347346
None => return Err("Account key rollover not supported by ACME CA".into()),
@@ -366,11 +365,22 @@ impl Account {
366365
let rsp = self.inner.post(Some(&body), None, new_key_url).await?;
367366
let _ = Problem::from_response(rsp).await?;
368367

368+
self.inner = Arc::new(AccountInner {
369+
client: self.inner.client.clone(),
370+
key: new_key,
371+
id: self.inner.id.clone(),
372+
});
373+
374+
let (directory, urls) = match &self.inner.client.server_url {
375+
Some(server_url) => (Some(server_url.clone()), None),
376+
None => (None, Some(self.inner.client.directory.clone())),
377+
};
378+
369379
Ok(AccountCredentials {
370380
id: self.inner.id.clone(),
371381
key_pkcs8: new_key_pkcs8.as_ref().to_vec(),
372-
directory: Some(server_url.to_owned()),
373-
urls: None,
382+
directory,
383+
urls,
374384
})
375385
}
376386

src/types.rs

+1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ impl From<hyper_util::client::legacy::Error> for Error {
8888
/// server URLs from the relevant ACME server. This can be used to serialize
8989
/// the account credentials to a file or secret manager and restore the
9090
/// account from persistent storage.
91+
#[must_use]
9192
#[derive(Deserialize, Serialize)]
9293
pub struct AccountCredentials {
9394
pub(crate) id: String,

tests/pebble.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -340,10 +340,8 @@ async fn change_key() -> Result<(), Box<dyn StdError>> {
340340
// Creat an env/initial account
341341
let mut env = Environment::new(EnvironmentConfig::default()).await?;
342342

343-
let dir = &format!("https://{}/dir", &env.config.pebble.listen_address);
344-
345343
// Change the account key
346-
let new_credentials = env.account.change_key(dir).await?;
344+
let new_credentials = env.account.change_key().await?;
347345

348346
// Using the old ACME account key should now produce malformed error.
349347
let Err(Error::Api(problem)) = env

0 commit comments

Comments
 (0)