diff --git a/src/api/common/common_error.rs b/src/api/common/common_error.rs index dddf6f61..0de490c2 100644 --- a/src/api/common/common_error.rs +++ b/src/api/common/common_error.rs @@ -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", } } diff --git a/src/api/s3/bucket.rs b/src/api/s3/bucket.rs index b84a06bb..4abc614f 100644 --- a/src/api/s3/bucket.rs +++ b/src/api/s3/bucket.rs @@ -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