2020-04-23 18:16:33 +00:00
|
|
|
use async_trait::async_trait;
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
2020-04-24 10:10:01 +00:00
|
|
|
use garage_table::*;
|
2020-11-20 20:15:24 +00:00
|
|
|
use garage_table::crdt::CRDT;
|
|
|
|
|
2020-04-24 10:10:01 +00:00
|
|
|
use garage_util::error::Error;
|
2020-04-23 18:16:33 +00:00
|
|
|
|
2020-11-20 20:15:24 +00:00
|
|
|
use model010::key_table as prev;
|
|
|
|
|
2020-04-23 18:16:33 +00:00
|
|
|
#[derive(PartialEq, Clone, Debug, Serialize, Deserialize)]
|
|
|
|
pub struct Key {
|
|
|
|
// Primary key
|
2020-04-23 20:25:45 +00:00
|
|
|
pub key_id: String,
|
2020-04-23 18:16:33 +00:00
|
|
|
|
|
|
|
// Associated secret key (immutable)
|
2020-04-23 20:25:45 +00:00
|
|
|
pub secret_key: String,
|
|
|
|
|
|
|
|
// Name
|
2020-11-20 20:15:24 +00:00
|
|
|
pub name: crdt::LWW<String>,
|
2020-04-23 18:16:33 +00:00
|
|
|
|
|
|
|
// Deletion
|
2020-11-20 20:15:24 +00:00
|
|
|
pub deleted: crdt::Bool,
|
2020-04-23 18:16:33 +00:00
|
|
|
|
|
|
|
// Authorized keys
|
2020-11-20 20:15:24 +00:00
|
|
|
pub authorized_buckets: crdt::LWWMap<String, PermissionSet>,
|
|
|
|
|
|
|
|
// CRDT interaction: deleted implies authorized_buckets is empty
|
2020-04-23 18:16:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Key {
|
2020-11-20 20:15:24 +00:00
|
|
|
pub fn new(name: String) -> Self {
|
2020-04-23 20:25:45 +00:00
|
|
|
let key_id = format!("GK{}", hex::encode(&rand::random::<[u8; 12]>()[..]));
|
|
|
|
let secret_key = hex::encode(&rand::random::<[u8; 32]>()[..]);
|
2020-11-20 20:15:24 +00:00
|
|
|
let ret = Self {
|
2020-04-23 20:25:45 +00:00
|
|
|
key_id,
|
|
|
|
secret_key,
|
2020-11-20 20:15:24 +00:00
|
|
|
name: crdt::LWW::new(name),
|
|
|
|
deleted: crdt::Bool::new(false),
|
|
|
|
authorized_buckets: crdt::LWWMap::new(),
|
2020-04-23 18:16:33 +00:00
|
|
|
};
|
|
|
|
ret
|
|
|
|
}
|
2020-04-23 20:25:45 +00:00
|
|
|
pub fn delete(key_id: String) -> Self {
|
2020-04-23 18:16:33 +00:00
|
|
|
Self {
|
2020-04-23 20:25:45 +00:00
|
|
|
key_id,
|
|
|
|
secret_key: "".into(),
|
2020-11-20 20:15:24 +00:00
|
|
|
name: crdt::LWW::new("".to_string()),
|
|
|
|
deleted: crdt::Bool::new(true),
|
|
|
|
authorized_buckets: crdt::LWWMap::new(),
|
2020-04-23 18:16:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/// Add an authorized bucket, only if it wasn't there before
|
2020-04-23 20:25:45 +00:00
|
|
|
pub fn allow_read(&self, bucket: &str) -> bool {
|
|
|
|
self.authorized_buckets
|
2020-11-20 20:15:24 +00:00
|
|
|
.get(&bucket.to_string())
|
2020-04-23 20:25:45 +00:00
|
|
|
.map(|x| x.allow_read)
|
|
|
|
.unwrap_or(false)
|
|
|
|
}
|
|
|
|
pub fn allow_write(&self, bucket: &str) -> bool {
|
|
|
|
self.authorized_buckets
|
2020-11-20 20:15:24 +00:00
|
|
|
.get(&bucket.to_string())
|
2020-04-23 20:25:45 +00:00
|
|
|
.map(|x| x.allow_write)
|
|
|
|
.unwrap_or(false)
|
|
|
|
}
|
2020-04-23 18:16:33 +00:00
|
|
|
}
|
|
|
|
|
2020-11-20 20:15:24 +00:00
|
|
|
#[derive(PartialOrd, Ord, PartialEq, Eq, Clone, Debug, Serialize, Deserialize)]
|
|
|
|
pub struct PermissionSet {
|
2020-04-23 20:25:45 +00:00
|
|
|
pub allow_read: bool,
|
|
|
|
pub allow_write: bool,
|
2020-04-23 18:16:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Entry<EmptyKey, String> for Key {
|
|
|
|
fn partition_key(&self) -> &EmptyKey {
|
|
|
|
&EmptyKey
|
|
|
|
}
|
|
|
|
fn sort_key(&self) -> &String {
|
2020-04-23 20:25:45 +00:00
|
|
|
&self.key_id
|
2020-04-23 18:16:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn merge(&mut self, other: &Self) {
|
2020-11-20 20:15:24 +00:00
|
|
|
self.name.merge(&other.name);
|
|
|
|
self.deleted.merge(&other.deleted);
|
2020-11-20 19:12:32 +00:00
|
|
|
|
2020-11-20 20:15:24 +00:00
|
|
|
if self.deleted.get() {
|
2020-04-23 18:16:33 +00:00
|
|
|
self.authorized_buckets.clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-20 20:15:24 +00:00
|
|
|
self.authorized_buckets.merge(&other.authorized_buckets);
|
2020-04-23 18:16:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct KeyTable;
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
impl TableSchema for KeyTable {
|
|
|
|
type P = EmptyKey;
|
|
|
|
type S = String;
|
|
|
|
type E = Key;
|
2020-11-20 19:11:04 +00:00
|
|
|
type Filter = DeletedFilter;
|
2020-04-23 18:16:33 +00:00
|
|
|
|
|
|
|
async fn updated(&self, _old: Option<Self::E>, _new: Option<Self::E>) -> Result<(), Error> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-11-20 19:11:04 +00:00
|
|
|
fn matches_filter(entry: &Self::E, filter: &Self::Filter) -> bool {
|
2020-11-20 20:15:24 +00:00
|
|
|
filter.apply(entry.deleted.get())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn try_migrate(bytes: &[u8]) -> Option<Self::E> {
|
|
|
|
let old = match rmp_serde::decode::from_read_ref::<_, prev::Key>(bytes) {
|
|
|
|
Ok(x) => x,
|
|
|
|
Err(_) => return None,
|
|
|
|
};
|
|
|
|
let mut new = Self::E {
|
|
|
|
key_id: old.key_id.clone(),
|
|
|
|
secret_key: old.secret_key.clone(),
|
|
|
|
name: crdt::LWW::migrate_from_raw(old.name_timestamp, old.name.clone()),
|
|
|
|
deleted: crdt::Bool::new(old.deleted),
|
|
|
|
authorized_buckets: crdt::LWWMap::new(),
|
|
|
|
};
|
|
|
|
for ab in old.authorized_buckets() {
|
|
|
|
let it = crdt::LWWMap::migrate_from_raw_item(
|
|
|
|
ab.bucket.clone(),
|
|
|
|
ab.timestamp,
|
|
|
|
PermissionSet{
|
|
|
|
allow_read: ab.allow_read,
|
|
|
|
allow_write: ab.allow_write,
|
|
|
|
});
|
|
|
|
new.authorized_buckets.merge(&it);
|
|
|
|
}
|
|
|
|
Some(new)
|
2020-04-23 18:16:33 +00:00
|
|
|
}
|
|
|
|
}
|