use std::fmt;
use std::ops::Deref;
use std::sync::Arc;
use std::time::Duration;
use inodedb::{Client, Grant};
use crate::Error;
#[derive(Clone, PartialEq, Eq)]
pub struct S3Location {
pub endpoint: String,
pub bucket: String,
pub prefix: String,
pub region: String,
pub access_key: String,
pub secret_key: String,
}
impl fmt::Debug for S3Location {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("S3Location")
.field("endpoint", &self.endpoint)
.field("bucket", &self.bucket)
.field("prefix", &self.prefix)
.field("region", &self.region)
.field("access_key", &self.access_key)
.field("secret_key", &"…")
.finish()
}
}
impl S3Location {
pub fn disk_endpoint(&self) -> String {
format!("{}/{}/{}/", self.endpoint.trim_end_matches('/'), self.bucket, self.prefix)
}
pub fn key_prefix(&self) -> String {
format!("{}/", self.prefix)
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Tenant {
pub store: String,
pub root: String,
pub s3: S3Location,
}
impl Tenant {
pub fn store_url(&self) -> String {
store_url(&self.store)
}
pub async fn connect(&self, key: &str) -> Result<Client, Error> {
Ok(Client::connect(self.store_url(), key).await?)
}
pub async fn mint_key(&self, minter: &Client, grant: Grant, ttl: Duration) -> Result<String, Error> {
Ok(minter.mint_key(self.root.as_bytes(), grant, ttl).await?)
}
}
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct TenantKey(Arc<str>);
impl TenantKey {
pub fn as_str(&self) -> &str {
&self.0
}
pub fn id(&self) -> &str {
self.0.split_once('.').map_or(&*self.0, |(id, _)| id)
}
pub(crate) fn handles(&self) -> usize {
Arc::strong_count(&self.0)
}
}
impl Deref for TenantKey {
type Target = str;
fn deref(&self) -> &str {
&self.0
}
}
impl fmt::Debug for TenantKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "TenantKey({}.…)", self.id())
}
}
impl From<&str> for TenantKey {
fn from(key: &str) -> TenantKey {
TenantKey(Arc::from(key))
}
}
impl From<String> for TenantKey {
fn from(key: String) -> TenantKey {
TenantKey(Arc::from(key))
}
}
impl From<&String> for TenantKey {
fn from(key: &String) -> TenantKey {
TenantKey(Arc::from(key.as_str()))
}
}
impl From<&TenantKey> for TenantKey {
fn from(key: &TenantKey) -> TenantKey {
key.clone()
}
}
pub fn store_url(store: &str) -> String {
if store.contains("://") { store.to_string() } else { format!("http://{store}") }
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn disk_endpoint_joins_service_bucket_and_prefix() {
let s3 = S3Location {
endpoint: "http://127.0.0.1:11211/".into(),
bucket: "libch".into(),
prefix: "tenants/t1".into(),
region: "us-east-1".into(),
access_key: "a".into(),
secret_key: "s".into(),
};
assert_eq!(s3.disk_endpoint(), "http://127.0.0.1:11211/libch/tenants/t1/");
assert_eq!(s3.key_prefix(), "tenants/t1/");
}
#[test]
fn debug_output_hides_secrets() {
let key = TenantKey::from("0000002a.c2VjcmV0");
assert_eq!(key.id(), "0000002a");
assert_eq!(&*key, "0000002a.c2VjcmV0");
assert_eq!(format!("{key:?}"), "TenantKey(0000002a.…)");
assert_eq!(TenantKey::from("bare").id(), "bare");
assert_eq!((key.handles(), key.clone().handles()), (1, 2));
let s3 = S3Location {
endpoint: "http://127.0.0.1:11211".into(),
bucket: "libch".into(),
prefix: "tenants/t1".into(),
region: "us-east-1".into(),
access_key: "access".into(),
secret_key: "hunter2".into(),
};
let shown = format!("{s3:?}");
assert!(shown.contains("tenants/t1") && !shown.contains("hunter2"), "{shown}");
}
#[test]
fn store_url_adds_a_scheme_once() {
assert_eq!(store_url("127.0.0.1:7170"), "http://127.0.0.1:7170");
assert_eq!(store_url("http://127.0.0.1:7170"), "http://127.0.0.1:7170");
}
}