Implement error 409 BucketAlreadyOwnedByYou (#1352)

Fix for Deuxfleurs/garage#1322

Reviewed-on: https://git.deuxfleurs.fr/Deuxfleurs/garage/pulls/1352
Co-authored-by: Roman Ivanov <xatikopro@gmail.com>
Co-committed-by: Roman Ivanov <xatikopro@gmail.com>
This commit is contained in:
Roman Ivanov
2026-02-18 11:20:24 +00:00
committed by Alex
parent 2803c73045
commit ce1ea79bf1
2 changed files with 13 additions and 4 deletions
+8 -1
View File
@@ -55,6 +55,10 @@ pub enum CommonError {
/// Bucket name is not valid according to AWS S3 specs
#[error("Invalid bucket name: {0}")]
InvalidBucketName(String),
/// Tried to create bucket that is already owned by you
#[error("Bucket already owned by you")]
BucketAlreadyOwnedByYou,
}
#[macro_export]
@@ -98,7 +102,9 @@ impl CommonError {
CommonError::BadRequest(_) => StatusCode::BAD_REQUEST,
CommonError::Forbidden(_) => StatusCode::FORBIDDEN,
CommonError::NoSuchBucket(_) => StatusCode::NOT_FOUND,
CommonError::BucketNotEmpty | CommonError::BucketAlreadyExists => StatusCode::CONFLICT,
CommonError::BucketNotEmpty
| CommonError::BucketAlreadyExists
| CommonError::BucketAlreadyOwnedByYou => StatusCode::CONFLICT,
CommonError::InvalidBucketName(_) | CommonError::InvalidHeader(_) => {
StatusCode::BAD_REQUEST
}
@@ -120,6 +126,7 @@ impl CommonError {
CommonError::BucketNotEmpty => "BucketNotEmpty",
CommonError::InvalidBucketName(_) => "InvalidBucketName",
CommonError::InvalidHeader(_) => "InvalidHeaderValue",
CommonError::BucketAlreadyOwnedByYou => "BucketAlreadyOwnedByYou",
}
}
+5 -3
View File
@@ -198,12 +198,14 @@ pub async fn handle_create_bucket(
.await?;
if let Some(bucket) = existing_bucket {
// Check we have write or owner permission on the bucket,
// in that case it's fine, return 200 OK, bucket exists;
// otherwise return a forbidden error.
// According to https://docs.aws.amazon.com/AmazonS3/latest/API/API_CreateBucket.html
// in such case we have to return 409 BucketAlreadyOwnedByYou if request was sent
// by bucket owner and 409 BucketAlreadyExists otherwise.
let kp = api_key.bucket_permissions(&bucket.id);
if !(kp.allow_write || kp.allow_owner) {
return Err(CommonError::BucketAlreadyExists.into());
} else {
return Err(CommonError::BucketAlreadyOwnedByYou.into());
}
} else {
// Check user is allowed to create bucket