2020-04-23 18:16:33 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
2021-03-10 15:21:56 +00:00
|
|
|
use garage_table::crdt::*;
|
2020-11-20 22:01:12 +00:00
|
|
|
use garage_table::*;
|
2020-11-20 20:15:24 +00:00
|
|
|
|
2021-03-26 20:53:28 +00:00
|
|
|
/// An api key
|
2020-04-23 18:16:33 +00:00
|
|
|
#[derive(PartialEq, Clone, Debug, Serialize, Deserialize)]
|
|
|
|
pub struct Key {
|
2021-04-06 03:25:28 +00:00
|
|
|
/// The id of the key (immutable), used as partition key
|
2020-04-23 20:25:45 +00:00
|
|
|
pub key_id: String,
|
2020-04-23 18:16:33 +00:00
|
|
|
|
2021-03-26 20:53:28 +00:00
|
|
|
/// The secret_key associated
|
2020-04-23 20:25:45 +00:00
|
|
|
pub secret_key: String,
|
|
|
|
|
2021-03-26 20:53:28 +00:00
|
|
|
/// Name for the key
|
2020-11-20 20:15:24 +00:00
|
|
|
pub name: crdt::LWW<String>,
|
2020-04-23 18:16:33 +00:00
|
|
|
|
2021-03-26 20:53:28 +00:00
|
|
|
/// Is the key deleted
|
2020-11-20 20:15:24 +00:00
|
|
|
pub deleted: crdt::Bool,
|
2020-04-23 18:16:33 +00:00
|
|
|
|
2021-03-26 20:53:28 +00:00
|
|
|
/// Buckets in which the key is authorized. Empty if `Key` is deleted
|
2021-04-06 03:25:28 +00:00
|
|
|
// CRDT interaction: deleted implies authorized_buckets is empty
|
2020-11-20 20:15:24 +00:00
|
|
|
pub authorized_buckets: crdt::LWWMap<String, PermissionSet>,
|
2020-04-23 18:16:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Key {
|
2021-03-26 20:53:28 +00:00
|
|
|
/// Create a new 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 22:20:20 +00:00
|
|
|
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-11-20 22:20:20 +00:00
|
|
|
}
|
2020-04-23 18:16:33 +00:00
|
|
|
}
|
2021-03-26 20:53:28 +00:00
|
|
|
|
|
|
|
/// Import a key from it's parts
|
2021-03-18 18:24:59 +00:00
|
|
|
pub fn import(key_id: &str, secret_key: &str, name: &str) -> Self {
|
|
|
|
Self {
|
|
|
|
key_id: key_id.to_string(),
|
|
|
|
secret_key: secret_key.to_string(),
|
|
|
|
name: crdt::LWW::new(name.to_string()),
|
|
|
|
deleted: crdt::Bool::new(false),
|
|
|
|
authorized_buckets: crdt::LWWMap::new(),
|
|
|
|
}
|
|
|
|
}
|
2021-03-26 20:53:28 +00:00
|
|
|
|
|
|
|
/// Create a new Key which can me merged to mark an existing key deleted
|
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
|
|
|
}
|
|
|
|
}
|
2021-03-26 20:53:28 +00:00
|
|
|
|
|
|
|
/// Check if `Key` is allowed to read in bucket
|
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)
|
|
|
|
}
|
2021-03-26 20:53:28 +00:00
|
|
|
|
|
|
|
/// Check if `Key` is allowed to write in bucket
|
2020-04-23 20:25:45 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-03-26 20:53:28 +00:00
|
|
|
/// Permission given to a key in a bucket
|
2020-11-20 20:15:24 +00:00
|
|
|
#[derive(PartialOrd, Ord, PartialEq, Eq, Clone, Debug, Serialize, Deserialize)]
|
|
|
|
pub struct PermissionSet {
|
2021-03-26 20:53:28 +00:00
|
|
|
/// The key can be used to read the bucket
|
2020-04-23 20:25:45 +00:00
|
|
|
pub allow_read: bool,
|
2021-03-26 20:53:28 +00:00
|
|
|
/// The key can be used to write in the bucket
|
2020-04-23 20:25:45 +00:00
|
|
|
pub allow_write: bool,
|
2020-04-23 18:16:33 +00:00
|
|
|
}
|
|
|
|
|
2021-03-10 15:21:56 +00:00
|
|
|
impl AutoCRDT for PermissionSet {
|
|
|
|
const WARN_IF_DIFFERENT: bool = true;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2021-03-10 15:21:56 +00:00
|
|
|
}
|
2020-04-23 18:16:33 +00:00
|
|
|
|
2021-03-10 15:21:56 +00:00
|
|
|
impl CRDT for Key {
|
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();
|
2021-03-10 15:21:56 +00:00
|
|
|
} else {
|
|
|
|
self.authorized_buckets.merge(&other.authorized_buckets);
|
2020-04-23 18:16:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct KeyTable;
|
|
|
|
|
2021-03-15 18:14:26 +00:00
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
|
|
pub enum KeyFilter {
|
|
|
|
Deleted(DeletedFilter),
|
|
|
|
Matches(String),
|
|
|
|
}
|
|
|
|
|
2020-04-23 18:16:33 +00:00
|
|
|
impl TableSchema for KeyTable {
|
|
|
|
type P = EmptyKey;
|
|
|
|
type S = String;
|
|
|
|
type E = Key;
|
2021-03-15 18:14:26 +00:00
|
|
|
type Filter = KeyFilter;
|
2020-04-23 18:16:33 +00:00
|
|
|
|
2021-03-26 18:41:46 +00:00
|
|
|
fn updated(&self, _old: Option<Self::E>, _new: Option<Self::E>) {
|
|
|
|
// nothing to do when updated
|
|
|
|
}
|
|
|
|
|
2020-11-20 19:11:04 +00:00
|
|
|
fn matches_filter(entry: &Self::E, filter: &Self::Filter) -> bool {
|
2021-03-15 18:14:26 +00:00
|
|
|
match filter {
|
|
|
|
KeyFilter::Deleted(df) => df.apply(entry.deleted.get()),
|
|
|
|
KeyFilter::Matches(pat) => {
|
2021-03-15 18:16:42 +00:00
|
|
|
let pat = pat.to_lowercase();
|
2021-03-15 22:14:12 +00:00
|
|
|
entry.key_id.to_lowercase().starts_with(&pat)
|
|
|
|
|| entry.name.get().to_lowercase() == pat
|
2021-03-15 18:14:26 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-20 20:15:24 +00:00
|
|
|
}
|
2020-04-23 18:16:33 +00:00
|
|
|
}
|