2022-06-15 18:20:28 +00:00
|
|
|
use garage_table::crdt::*;
|
2020-04-24 10:10:01 +00:00
|
|
|
use garage_table::*;
|
2021-12-14 12:55:11 +00:00
|
|
|
use garage_util::data::*;
|
|
|
|
use garage_util::time::*;
|
2020-11-20 22:01:12 +00:00
|
|
|
|
2021-12-14 12:55:11 +00:00
|
|
|
use crate::permission::BucketKeyPerm;
|
2020-11-20 22:01:12 +00:00
|
|
|
|
2023-01-03 13:44:47 +00:00
|
|
|
mod v08 {
|
2023-01-03 14:08:37 +00:00
|
|
|
use crate::permission::BucketKeyPerm;
|
|
|
|
use garage_util::crdt;
|
|
|
|
use garage_util::data::Uuid;
|
|
|
|
use serde::{Deserialize, Serialize};
|
2020-04-23 18:16:33 +00:00
|
|
|
|
2023-01-03 13:44:47 +00:00
|
|
|
/// A bucket is a collection of objects
|
|
|
|
///
|
|
|
|
/// Its parameters are not directly accessible as:
|
|
|
|
/// - It must be possible to merge paramaters, hence the use of a LWW CRDT.
|
|
|
|
/// - A bucket has 2 states, Present or Deleted and parameters make sense only if present.
|
|
|
|
#[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<BucketParams>,
|
|
|
|
}
|
2022-01-07 15:23:04 +00:00
|
|
|
|
2023-01-03 13:44:47 +00:00
|
|
|
/// 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<String, BucketKeyPerm>,
|
2022-01-07 15:23:04 +00:00
|
|
|
|
2023-01-03 13:44:47 +00:00
|
|
|
/// 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<String, bool>,
|
|
|
|
/// 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>,
|
2020-12-12 16:00:31 +00:00
|
|
|
|
2023-01-03 13:44:47 +00:00
|
|
|
/// 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<Option<WebsiteConfig>>,
|
|
|
|
/// CORS rules
|
|
|
|
pub cors_config: crdt::Lww<Option<Vec<CorsRule>>>,
|
2023-08-29 14:44:27 +00:00
|
|
|
/// Lifecycle configuration
|
|
|
|
#[serde(default)]
|
|
|
|
pub lifecycle_config: crdt::Lww<Option<Vec<LifecycleRule>>>,
|
2023-01-03 13:44:47 +00:00
|
|
|
/// Bucket quotas
|
|
|
|
#[serde(default)]
|
|
|
|
pub quotas: crdt::Lww<BucketQuotas>,
|
|
|
|
}
|
2021-12-22 17:50:08 +00:00
|
|
|
|
2023-01-03 13:44:47 +00:00
|
|
|
#[derive(PartialEq, Eq, Clone, Debug, Serialize, Deserialize)]
|
|
|
|
pub struct WebsiteConfig {
|
|
|
|
pub index_document: String,
|
|
|
|
pub error_document: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(PartialEq, Eq, Clone, Debug, Serialize, Deserialize)]
|
|
|
|
pub struct CorsRule {
|
|
|
|
pub id: Option<String>,
|
|
|
|
pub max_age_seconds: Option<u64>,
|
|
|
|
pub allow_origins: Vec<String>,
|
|
|
|
pub allow_methods: Vec<String>,
|
|
|
|
pub allow_headers: Vec<String>,
|
|
|
|
pub expose_headers: Vec<String>,
|
|
|
|
}
|
2022-01-07 15:23:04 +00:00
|
|
|
|
2023-08-29 14:44:27 +00:00
|
|
|
/// Lifecycle configuration rule
|
|
|
|
#[derive(PartialEq, Eq, Clone, Debug, Serialize, Deserialize)]
|
|
|
|
pub struct LifecycleRule {
|
|
|
|
/// The ID of the rule
|
|
|
|
pub id: Option<String>,
|
|
|
|
/// Whether the rule is active
|
|
|
|
pub enabled: bool,
|
|
|
|
/// The filter to check whether rule applies to a given object
|
|
|
|
pub filter: LifecycleFilter,
|
|
|
|
/// Number of days after which incomplete multipart uploads are aborted
|
|
|
|
pub abort_incomplete_mpu_days: Option<usize>,
|
|
|
|
/// Expiration policy for stored objects
|
|
|
|
pub expiration: Option<LifecycleExpiration>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A lifecycle filter is a set of conditions that must all be true.
|
|
|
|
/// For each condition, if it is None, it is not verified (always true),
|
|
|
|
/// and if it is Some(x), then it is verified for value x
|
2023-08-29 16:22:03 +00:00
|
|
|
#[derive(PartialEq, Eq, Clone, Debug, Serialize, Deserialize, Default)]
|
2023-08-29 14:44:27 +00:00
|
|
|
pub struct LifecycleFilter {
|
|
|
|
/// If Some(x), object key has to start with prefix x
|
|
|
|
pub prefix: Option<String>,
|
|
|
|
/// If Some(x), object size has to be more than x
|
2023-08-30 12:28:48 +00:00
|
|
|
pub size_gt: Option<u64>,
|
2023-08-29 14:44:27 +00:00
|
|
|
/// If Some(x), object size has to be less than x
|
2023-08-30 12:28:48 +00:00
|
|
|
pub size_lt: Option<u64>,
|
2023-08-29 14:44:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(PartialEq, Eq, Clone, Debug, Serialize, Deserialize)]
|
|
|
|
pub enum LifecycleExpiration {
|
|
|
|
/// Objects expire x days after they were created
|
|
|
|
AfterDays(usize),
|
|
|
|
/// Objects expire at date x (must be in yyyy-mm-dd format)
|
2023-08-30 18:02:07 +00:00
|
|
|
AtDate(String),
|
2023-08-29 14:44:27 +00:00
|
|
|
}
|
|
|
|
|
2023-01-03 13:44:47 +00:00
|
|
|
#[derive(Default, PartialEq, Eq, PartialOrd, Ord, Clone, Debug, Serialize, Deserialize)]
|
|
|
|
pub struct BucketQuotas {
|
|
|
|
/// Maximum size in bytes (bucket size = sum of sizes of objects in the bucket)
|
|
|
|
pub max_size: Option<u64>,
|
|
|
|
/// Maximum number of non-deleted objects in the bucket
|
|
|
|
pub max_objects: Option<u64>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl garage_util::migrate::InitialFormat for Bucket {}
|
2022-06-15 18:20:28 +00:00
|
|
|
}
|
|
|
|
|
2023-01-03 13:44:47 +00:00
|
|
|
pub use v08::*;
|
|
|
|
|
2022-06-15 18:20:28 +00:00
|
|
|
impl AutoCrdt for BucketQuotas {
|
|
|
|
const WARN_IF_DIFFERENT: bool = true;
|
|
|
|
}
|
|
|
|
|
2020-12-14 20:46:49 +00:00
|
|
|
impl BucketParams {
|
2021-04-08 13:13:02 +00:00
|
|
|
/// Create an empty BucketParams with no authorized keys and no website accesss
|
2020-12-14 20:46:49 +00:00
|
|
|
pub fn new() -> Self {
|
|
|
|
BucketParams {
|
2021-12-14 12:55:11 +00:00
|
|
|
creation_date: now_msec(),
|
|
|
|
authorized_keys: crdt::Map::new(),
|
|
|
|
aliases: crdt::LwwMap::new(),
|
|
|
|
local_aliases: crdt::LwwMap::new(),
|
2022-01-07 15:23:04 +00:00
|
|
|
website_config: crdt::Lww::new(None),
|
|
|
|
cors_config: crdt::Lww::new(None),
|
2023-08-29 14:44:27 +00:00
|
|
|
lifecycle_config: crdt::Lww::new(None),
|
2022-06-15 18:20:28 +00:00
|
|
|
quotas: crdt::Lww::new(BucketQuotas::default()),
|
2020-12-14 20:46:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-02 21:13:08 +00:00
|
|
|
impl Crdt for BucketParams {
|
2021-04-23 19:57:32 +00:00
|
|
|
fn merge(&mut self, o: &Self) {
|
2022-01-03 17:32:15 +00:00
|
|
|
self.creation_date = std::cmp::min(self.creation_date, o.creation_date);
|
2021-04-23 19:57:32 +00:00
|
|
|
self.authorized_keys.merge(&o.authorized_keys);
|
2022-01-07 15:23:04 +00:00
|
|
|
|
2021-12-14 12:55:11 +00:00
|
|
|
self.aliases.merge(&o.aliases);
|
|
|
|
self.local_aliases.merge(&o.local_aliases);
|
2022-01-07 15:23:04 +00:00
|
|
|
|
|
|
|
self.website_config.merge(&o.website_config);
|
|
|
|
self.cors_config.merge(&o.cors_config);
|
2023-08-30 18:05:53 +00:00
|
|
|
self.lifecycle_config.merge(&o.lifecycle_config);
|
2022-06-15 18:20:28 +00:00
|
|
|
self.quotas.merge(&o.quotas);
|
2021-12-14 12:55:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-30 18:02:07 +00:00
|
|
|
pub fn parse_lifecycle_date(date: &str) -> Result<chrono::NaiveDate, &'static str> {
|
|
|
|
use chrono::prelude::*;
|
|
|
|
|
|
|
|
if let Ok(datetime) = NaiveDateTime::parse_from_str(date, "%Y-%m-%dT%H:%M:%SZ") {
|
|
|
|
if datetime.time() == NaiveTime::MIN {
|
|
|
|
Ok(datetime.date())
|
|
|
|
} else {
|
|
|
|
Err("date must be at midnight")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
NaiveDate::parse_from_str(date, "%Y-%m-%d").map_err(|_| "date has invalid format")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-14 12:55:11 +00:00
|
|
|
impl Default for Bucket {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
2021-04-23 19:57:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for BucketParams {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-23 18:16:33 +00:00
|
|
|
impl Bucket {
|
2021-04-08 13:13:02 +00:00
|
|
|
/// Initializes a new instance of the Bucket struct
|
2021-12-14 12:55:11 +00:00
|
|
|
pub fn new() -> Self {
|
2020-11-20 22:20:20 +00:00
|
|
|
Bucket {
|
2021-12-14 12:55:11 +00:00
|
|
|
id: gen_uuid(),
|
|
|
|
state: crdt::Deletable::present(BucketParams::new()),
|
2020-11-20 22:20:20 +00:00
|
|
|
}
|
2020-04-23 18:16:33 +00:00
|
|
|
}
|
2021-03-26 20:53:28 +00:00
|
|
|
|
2021-04-07 11:39:34 +00:00
|
|
|
/// Returns true if this represents a deleted bucket
|
2020-11-20 22:01:12 +00:00
|
|
|
pub fn is_deleted(&self) -> bool {
|
2021-12-14 12:55:11 +00:00
|
|
|
self.state.is_deleted()
|
2020-04-23 18:16:33 +00:00
|
|
|
}
|
2021-03-26 20:53:28 +00:00
|
|
|
|
2022-01-04 17:59:17 +00:00
|
|
|
/// Returns an option representing the parameters (None if in deleted state)
|
|
|
|
pub fn params(&self) -> Option<&BucketParams> {
|
|
|
|
self.state.as_option()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Mutable version of `.params()`
|
|
|
|
pub fn params_mut(&mut self) -> Option<&mut BucketParams> {
|
|
|
|
self.state.as_option_mut()
|
|
|
|
}
|
|
|
|
|
2021-03-26 20:53:28 +00:00
|
|
|
/// Return the list of authorized keys, when each was updated, and the permission associated to
|
|
|
|
/// the key
|
2021-12-14 12:55:11 +00:00
|
|
|
pub fn authorized_keys(&self) -> &[(String, BucketKeyPerm)] {
|
2022-01-04 17:59:17 +00:00
|
|
|
self.params()
|
|
|
|
.map(|s| s.authorized_keys.items())
|
|
|
|
.unwrap_or(&[])
|
2020-04-23 20:25:45 +00:00
|
|
|
}
|
2020-04-19 15:15:48 +00:00
|
|
|
|
2022-01-03 18:06:04 +00:00
|
|
|
pub fn aliases(&self) -> &[(String, u64, bool)] {
|
2022-01-04 17:59:17 +00:00
|
|
|
self.params().map(|s| s.aliases.items()).unwrap_or(&[])
|
2022-01-03 18:06:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn local_aliases(&self) -> &[((String, String), u64, bool)] {
|
2022-01-04 17:59:17 +00:00
|
|
|
self.params()
|
|
|
|
.map(|s| s.local_aliases.items())
|
|
|
|
.unwrap_or(&[])
|
2020-04-19 15:15:48 +00:00
|
|
|
}
|
2022-01-03 18:06:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Entry<EmptyKey, Uuid> for Bucket {
|
|
|
|
fn partition_key(&self) -> &EmptyKey {
|
2021-12-14 12:55:11 +00:00
|
|
|
&EmptyKey
|
2020-04-19 15:15:48 +00:00
|
|
|
}
|
2022-01-03 18:06:04 +00:00
|
|
|
fn sort_key(&self) -> &Uuid {
|
|
|
|
&self.id
|
|
|
|
}
|
2021-03-10 15:21:56 +00:00
|
|
|
}
|
2020-04-19 15:15:48 +00:00
|
|
|
|
2021-05-02 21:13:08 +00:00
|
|
|
impl Crdt for Bucket {
|
2020-04-19 15:15:48 +00:00
|
|
|
fn merge(&mut self, other: &Self) {
|
2020-11-20 22:01:12 +00:00
|
|
|
self.state.merge(&other.state);
|
2020-04-19 15:15:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct BucketTable;
|
|
|
|
|
|
|
|
impl TableSchema for BucketTable {
|
2021-12-14 12:55:11 +00:00
|
|
|
const TABLE_NAME: &'static str = "bucket_v2";
|
2021-12-14 11:34:01 +00:00
|
|
|
|
2022-01-03 18:06:04 +00:00
|
|
|
type P = EmptyKey;
|
|
|
|
type S = Uuid;
|
2020-04-19 15:15:48 +00:00
|
|
|
type E = Bucket;
|
2020-11-20 19:11:04 +00:00
|
|
|
type Filter = DeletedFilter;
|
2020-04-19 15:15:48 +00:00
|
|
|
|
2020-11-20 19:11:04 +00:00
|
|
|
fn matches_filter(entry: &Self::E, filter: &Self::Filter) -> bool {
|
2020-11-20 22:01:12 +00:00
|
|
|
filter.apply(entry.is_deleted())
|
|
|
|
}
|
2020-04-19 15:15:48 +00:00
|
|
|
}
|