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::*;
|
|
|
|
use garage_util::data::*;
|
|
|
|
use garage_util::error::Error;
|
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
|
|
|
|
pub name: String,
|
|
|
|
pub name_timestamp: u64,
|
2020-04-23 18:16:33 +00:00
|
|
|
|
|
|
|
// Deletion
|
|
|
|
pub deleted: bool,
|
|
|
|
|
|
|
|
// Authorized keys
|
|
|
|
authorized_buckets: Vec<AllowedBucket>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Key {
|
2020-04-23 20:25:45 +00:00
|
|
|
pub fn new(name: String, buckets: Vec<AllowedBucket>) -> Self {
|
|
|
|
let key_id = format!("GK{}", hex::encode(&rand::random::<[u8; 12]>()[..]));
|
|
|
|
let secret_key = hex::encode(&rand::random::<[u8; 32]>()[..]);
|
2020-04-23 18:16:33 +00:00
|
|
|
let mut ret = Self {
|
2020-04-23 20:25:45 +00:00
|
|
|
key_id,
|
|
|
|
secret_key,
|
|
|
|
name,
|
|
|
|
name_timestamp: now_msec(),
|
2020-04-23 18:16:33 +00:00
|
|
|
deleted: false,
|
|
|
|
authorized_buckets: vec![],
|
|
|
|
};
|
|
|
|
for b in buckets {
|
2020-04-23 18:36:12 +00:00
|
|
|
ret.add_bucket(b)
|
|
|
|
.expect("Duplicate AllowedBucket in Key constructor");
|
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(),
|
|
|
|
name: "".into(),
|
|
|
|
name_timestamp: now_msec(),
|
2020-04-23 18:16:33 +00:00
|
|
|
deleted: true,
|
|
|
|
authorized_buckets: vec![],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// Add an authorized bucket, only if it wasn't there before
|
|
|
|
pub fn add_bucket(&mut self, new: AllowedBucket) -> Result<(), ()> {
|
|
|
|
match self
|
|
|
|
.authorized_buckets
|
|
|
|
.binary_search_by(|b| b.bucket.cmp(&new.bucket))
|
|
|
|
{
|
|
|
|
Err(i) => {
|
|
|
|
self.authorized_buckets.insert(i, new);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
Ok(_) => Err(()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn authorized_buckets(&self) -> &[AllowedBucket] {
|
|
|
|
&self.authorized_buckets[..]
|
|
|
|
}
|
2020-04-23 20:25:45 +00:00
|
|
|
pub fn clear_buckets(&mut self) {
|
|
|
|
self.authorized_buckets.clear();
|
|
|
|
}
|
|
|
|
pub fn allow_read(&self, bucket: &str) -> bool {
|
|
|
|
self.authorized_buckets
|
|
|
|
.iter()
|
|
|
|
.find(|x| x.bucket.as_str() == bucket)
|
|
|
|
.map(|x| x.allow_read)
|
|
|
|
.unwrap_or(false)
|
|
|
|
}
|
|
|
|
pub fn allow_write(&self, bucket: &str) -> bool {
|
|
|
|
self.authorized_buckets
|
|
|
|
.iter()
|
|
|
|
.find(|x| x.bucket.as_str() == bucket)
|
|
|
|
.map(|x| x.allow_write)
|
|
|
|
.unwrap_or(false)
|
|
|
|
}
|
2020-04-23 18:16:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(PartialEq, Clone, Debug, Serialize, Deserialize)]
|
|
|
|
pub struct AllowedBucket {
|
|
|
|
pub bucket: String,
|
|
|
|
pub timestamp: u64,
|
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) {
|
|
|
|
if other.deleted {
|
|
|
|
self.deleted = true;
|
2020-04-23 20:25:45 +00:00
|
|
|
}
|
|
|
|
if self.deleted {
|
2020-04-23 18:16:33 +00:00
|
|
|
self.authorized_buckets.clear();
|
|
|
|
return;
|
|
|
|
}
|
2020-04-23 20:25:45 +00:00
|
|
|
if other.name_timestamp > self.name_timestamp {
|
|
|
|
self.name_timestamp = other.name_timestamp;
|
|
|
|
self.name = other.name.clone();
|
|
|
|
}
|
2020-04-23 18:16:33 +00:00
|
|
|
|
|
|
|
for ab in other.authorized_buckets.iter() {
|
|
|
|
match self
|
|
|
|
.authorized_buckets
|
|
|
|
.binary_search_by(|our_ab| our_ab.bucket.cmp(&ab.bucket))
|
|
|
|
{
|
|
|
|
Ok(i) => {
|
|
|
|
let our_ab = &mut self.authorized_buckets[i];
|
|
|
|
if ab.timestamp > our_ab.timestamp {
|
|
|
|
*our_ab = ab.clone();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(i) => {
|
|
|
|
self.authorized_buckets.insert(i, ab.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
filter.apply(entry.deleted)
|
2020-04-23 18:16:33 +00:00
|
|
|
}
|
|
|
|
}
|