add never_expires to remove expiration dates of admin tokens and access keys

This commit is contained in:
Alex Auvolat
2025-04-17 11:44:09 +02:00
parent 590c9bb4db
commit 5d338f0b8f
7 changed files with 61 additions and 6 deletions
+14 -3
View File
@@ -105,7 +105,7 @@ impl RequestHandler for CreateKeyRequest {
) -> Result<CreateKeyResponse, Error> {
let mut key = Key::new("Unnamed key");
apply_key_updates(&mut key, self.0);
apply_key_updates(&mut key, self.0)?;
garage.key_table.insert(&key).await?;
@@ -152,7 +152,7 @@ impl RequestHandler for UpdateKeyRequest {
) -> Result<UpdateKeyResponse, Error> {
let mut key = garage.key_helper().get_existing_key(&self.id).await?;
apply_key_updates(&mut key, self.body);
apply_key_updates(&mut key, self.body)?;
garage.key_table.insert(&key).await?;
@@ -265,7 +265,13 @@ async fn key_info_results(
Ok(res)
}
fn apply_key_updates(key: &mut Key, updates: UpdateKeyRequestBody) {
fn apply_key_updates(key: &mut Key, updates: UpdateKeyRequestBody) -> Result<(), Error> {
if updates.never_expires && updates.expiration.is_some() {
return Err(Error::bad_request(
"cannot specify `expiration` and `never_expires`",
));
}
let key_state = key.state.as_option_mut().unwrap();
if let Some(new_name) = updates.name {
@@ -276,6 +282,9 @@ fn apply_key_updates(key: &mut Key, updates: UpdateKeyRequestBody) {
.expiration
.update(Some(expiration.timestamp_millis() as u64));
}
if updates.never_expires {
key_state.expiration.update(None);
}
if let Some(allow) = updates.allow {
if allow.create_bucket {
key_state.allow_create_bucket.update(true);
@@ -286,4 +295,6 @@ fn apply_key_updates(key: &mut Key, updates: UpdateKeyRequestBody) {
key_state.allow_create_bucket.update(false);
}
}
Ok(())
}