From 44ce6ae5b4c5b30a144d37aab1d11e7237a954c1 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Sat, 4 Jan 2025 18:50:49 +0100 Subject: [PATCH] properly implement new bucket model using a migration --- src/model/bucket_table.rs | 130 ++++++++++++++++++++++++++++++------- src/util/crdt/deletable.rs | 10 +++ src/util/crdt/lww.rs | 10 +++ 3 files changed, 126 insertions(+), 24 deletions(-) diff --git a/src/model/bucket_table.rs b/src/model/bucket_table.rs index b6daab75..b2679323 100644 --- a/src/model/bucket_table.rs +++ b/src/model/bucket_table.rs @@ -60,29 +60,6 @@ mod v08 { pub struct WebsiteConfig { pub index_document: String, pub error_document: Option, - #[serde(default, skip_serializing_if = "Vec::is_empty")] - pub routing_rules: Vec, - } - - #[derive(PartialEq, Eq, Clone, Debug, Serialize, Deserialize)] - pub struct RoutingRule { - pub condition: Option, - pub redirect: Redirect, - } - - #[derive(PartialEq, Eq, Clone, Debug, Serialize, Deserialize)] - pub struct Condition { - pub http_error_code: Option, - pub prefix: Option, - } - - #[derive(PartialEq, Eq, Clone, Debug, Serialize, Deserialize)] - pub struct Redirect { - pub hostname: Option, - pub http_redirect_code: u16, - pub protocol: Option, - pub replace_key_prefix: Option, - pub replace_key: Option, } #[derive(PartialEq, Eq, Clone, Debug, Serialize, Deserialize)] @@ -142,7 +119,112 @@ mod v08 { impl garage_util::migrate::InitialFormat for Bucket {} } -pub use v08::*; +mod v2 { + use crate::permission::BucketKeyPerm; + use garage_util::crdt; + use garage_util::data::Uuid; + use serde::{Deserialize, Serialize}; + + use super::v08; + + pub use v08::{BucketQuotas, CorsRule, LifecycleExpiration, LifecycleFilter, LifecycleRule}; + + #[derive(PartialEq, Eq, Clone, Debug, Serialize, Deserialize)] + pub struct Bucket { + /// ID of the bucket + pub id: Uuid, + /// State, and configuration if not deleted, of the bucket + pub state: crdt::Deletable, + } + + /// Configuration for a bucket + #[derive(PartialEq, Eq, Clone, Debug, Serialize, Deserialize)] + pub struct BucketParams { + /// Bucket's creation date + pub creation_date: u64, + /// Map of key with access to the bucket, and what kind of access they give + pub authorized_keys: crdt::Map, + + /// Map of aliases that are or have been given to this bucket + /// in the global namespace + /// (not authoritative: this is just used as an indication to + /// map back to aliases when doing ListBuckets) + pub aliases: crdt::LwwMap, + /// Map of aliases that are or have been given to this bucket + /// in namespaces local to keys + /// key = (access key id, alias name) + pub local_aliases: crdt::LwwMap<(String, String), bool>, + + /// Whether this bucket is allowed for website access + /// (under all of its global alias names), + /// and if so, the website configuration XML document + pub website_config: crdt::Lww>, + /// CORS rules + pub cors_config: crdt::Lww>>, + /// Lifecycle configuration + pub lifecycle_config: crdt::Lww>>, + /// Bucket quotas + pub quotas: crdt::Lww, + } + + #[derive(PartialEq, Eq, Clone, Debug, Serialize, Deserialize)] + pub struct WebsiteConfig { + pub index_document: String, + pub error_document: Option, + pub routing_rules: Vec, + } + + #[derive(PartialEq, Eq, Clone, Debug, Serialize, Deserialize)] + pub struct RoutingRule { + pub condition: Option, + pub redirect: Redirect, + } + + #[derive(PartialEq, Eq, Clone, Debug, Serialize, Deserialize)] + pub struct Condition { + pub http_error_code: Option, + pub prefix: Option, + } + + #[derive(PartialEq, Eq, Clone, Debug, Serialize, Deserialize)] + pub struct Redirect { + pub hostname: Option, + pub http_redirect_code: u16, + pub protocol: Option, + pub replace_key_prefix: Option, + pub replace_key: Option, + } + + impl garage_util::migrate::Migrate for Bucket { + const VERSION_MARKER: &'static [u8] = b"G2bkt"; + + type Previous = v08::Bucket; + + fn migrate(old: v08::Bucket) -> Bucket { + Bucket { + id: old.id, + state: old.state.map(|x| BucketParams { + creation_date: x.creation_date, + authorized_keys: x.authorized_keys, + aliases: x.aliases, + local_aliases: x.local_aliases, + website_config: x.website_config.map(|wc_opt| { + wc_opt.map(|wc| WebsiteConfig { + index_document: wc.index_document, + error_document: wc.error_document, + routing_rules: vec![], + }) + }), + cors_config: x.cors_config, + lifecycle_config: x.lifecycle_config, + quotas: x.quotas, + }), + } + } + } +} + +pub use v2::*; impl AutoCrdt for BucketQuotas { const WARN_IF_DIFFERENT: bool = true; diff --git a/src/util/crdt/deletable.rs b/src/util/crdt/deletable.rs index e771aceb..0594d850 100644 --- a/src/util/crdt/deletable.rs +++ b/src/util/crdt/deletable.rs @@ -9,6 +9,16 @@ pub enum Deletable { Deleted, } +impl Deletable { + /// Map value, used for migrations + pub fn map U>(self, f: F) -> Deletable { + match self { + Self::Present(x) => Deletable::::Present(f(x)), + Self::Deleted => Deletable::::Deleted, + } + } +} + impl Deletable { /// Create a new deletable object that isn't deleted pub fn present(v: T) -> Self { diff --git a/src/util/crdt/lww.rs b/src/util/crdt/lww.rs index 958844c9..2e5875ea 100644 --- a/src/util/crdt/lww.rs +++ b/src/util/crdt/lww.rs @@ -43,6 +43,16 @@ pub struct Lww { v: T, } +impl Lww { + /// Map value, used for migrations + pub fn map U>(self, f: F) -> Lww { + Lww:: { + ts: self.ts, + v: f(self.v), + } + } +} + impl Lww where T: Crdt,