1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
use std::sync::Arc;
use super::entry::{Entry, EntryKind, EntryOperation, EntryTag, Scan, TagFilter};
use crate::{
backend::{Backend, QueryBackend},
error::Error,
kms::{KeyEntry, KeyParams, KmsCategory, LocalKey},
protect::{PassKey, StoreKeyMethod},
};
#[derive(Debug)]
/// An instance of an opened store
pub struct Store<B: Backend>(B);
impl<B: Backend> Store<B> {
pub(crate) fn new(inner: B) -> Self {
Self(inner)
}
#[cfg(test)]
#[allow(unused)]
pub(crate) fn inner(&self) -> &B {
&self.0
}
pub(crate) fn into_inner(self) -> B {
self.0
}
}
impl<B: Backend> Store<B> {
/// Get the default profile name used when starting a scan or a session
pub fn get_profile_name(&self) -> &str {
self.0.get_profile_name()
}
/// Replace the wrapping key on a store
pub async fn rekey(
&mut self,
method: StoreKeyMethod,
pass_key: PassKey<'_>,
) -> Result<(), Error> {
self.0.rekey_backend(method, pass_key).await
}
/// Create a new profile with the given profile name
pub async fn create_profile(&self, name: Option<String>) -> Result<String, Error> {
self.0.create_profile(name).await
}
/// Remove an existing profile with the given profile name
pub async fn remove_profile(&self, name: String) -> Result<bool, Error> {
self.0.remove_profile(name).await
}
/// Create a new scan instance against the store
///
/// The result will keep an open connection to the backend until it is consumed
pub async fn scan(
&self,
profile: Option<String>,
category: String,
tag_filter: Option<TagFilter>,
offset: Option<i64>,
limit: Option<i64>,
) -> Result<Scan<'static, Entry>, Error> {
self.0
.scan(
profile,
EntryKind::Item,
category,
tag_filter,
offset,
limit,
)
.await
}
/// Create a new session against the store
pub async fn session(&self, profile: Option<String>) -> Result<Session<B::Session>, Error> {
// FIXME - add 'immediate' flag
Ok(Session::new(self.0.session(profile, false)?))
}
/// Create a new transaction session against the store
pub async fn transaction(&self, profile: Option<String>) -> Result<Session<B::Session>, Error> {
Ok(Session::new(self.0.session(profile, true)?))
}
/// Close the store instance, waiting for any shutdown procedures to complete.
pub async fn close(self) -> Result<(), Error> {
self.0.close().await
}
pub(crate) async fn arc_close(self: Arc<Self>) -> Result<(), Error> {
self.0.close().await
}
}
/// An active connection to the store backend
#[derive(Debug)]
pub struct Session<Q: QueryBackend>(Q);
impl<Q: QueryBackend> Session<Q> {
pub(crate) fn new(inner: Q) -> Self {
Self(inner)
}
}
impl<Q: QueryBackend> Session<Q> {
/// Count the number of entries for a given record category
pub async fn count(
&mut self,
category: &str,
tag_filter: Option<TagFilter>,
) -> Result<i64, Error> {
self.0.count(EntryKind::Item, category, tag_filter).await
}
/// Retrieve the current record at `(category, name)`.
///
/// Specify `for_update` when in a transaction to create an update lock on the
/// associated record, if supported by the store backend
pub async fn fetch(
&mut self,
category: &str,
name: &str,
for_update: bool,
) -> Result<Option<Entry>, Error> {
self.0
.fetch(EntryKind::Item, category, name, for_update)
.await
}
/// Retrieve all records matching the given `category` and `tag_filter`.
///
/// Unlike `Store::scan`, this method may be used within a transaction. It should
/// not be used for very large result sets due to correspondingly large memory
/// requirements
pub async fn fetch_all(
&mut self,
category: &str,
tag_filter: Option<TagFilter>,
limit: Option<i64>,
for_update: bool,
) -> Result<Vec<Entry>, Error> {
self.0
.fetch_all(EntryKind::Item, category, tag_filter, limit, for_update)
.await
}
/// Insert a new record into the store
pub async fn insert(
&mut self,
category: &str,
name: &str,
value: &[u8],
tags: Option<&[EntryTag]>,
expiry_ms: Option<i64>,
) -> Result<(), Error> {
self.0
.update(
EntryKind::Item,
EntryOperation::Insert,
category,
name,
Some(value),
tags,
expiry_ms,
)
.await
}
/// Remove a record from the store
pub async fn remove(&mut self, category: &str, name: &str) -> Result<(), Error> {
self.0
.update(
EntryKind::Item,
EntryOperation::Remove,
category,
name,
None,
None,
None,
)
.await
}
/// Replace the value and tags of a record in the store
pub async fn replace(
&mut self,
category: &str,
name: &str,
value: &[u8],
tags: Option<&[EntryTag]>,
expiry_ms: Option<i64>,
) -> Result<(), Error> {
self.0
.update(
EntryKind::Item,
EntryOperation::Replace,
category,
name,
Some(value),
tags,
expiry_ms,
)
.await
}
/// Remove all records in the store matching a given `category` and `tag_filter`
pub async fn remove_all(
&mut self,
category: &str,
tag_filter: Option<TagFilter>,
) -> Result<i64, Error> {
self.0
.remove_all(EntryKind::Item, category, tag_filter)
.await
}
/// Perform a record update
///
/// This may correspond to an record insert, replace, or remove depending on
/// the provided `operation`
pub async fn update(
&mut self,
operation: EntryOperation,
category: &str,
name: &str,
value: Option<&[u8]>,
tags: Option<&[EntryTag]>,
expiry_ms: Option<i64>,
) -> Result<(), Error> {
self.0
.update(
EntryKind::Item,
operation,
category,
name,
value,
tags,
expiry_ms,
)
.await
}
/// Insert a local key instance into the store
pub async fn insert_key(
&mut self,
name: &str,
key: &LocalKey,
metadata: Option<&str>,
tags: Option<&[EntryTag]>,
expiry_ms: Option<i64>,
) -> Result<(), Error> {
let data = key.encode()?;
let params = KeyParams {
metadata: metadata.map(str::to_string),
reference: None,
data: Some(data),
};
let value = params.to_bytes()?;
let mut ins_tags = Vec::with_capacity(10);
let alg = key.algorithm().as_str();
if !alg.is_empty() {
ins_tags.push(EntryTag::Encrypted("alg".to_string(), alg.to_string()));
}
let thumbs = key.to_jwk_thumbprints()?;
for thumb in thumbs {
ins_tags.push(EntryTag::Encrypted("thumb".to_string(), thumb));
}
if let Some(tags) = tags {
for t in tags {
ins_tags.push(t.map_ref(|k, v| (format!("user:{}", k), v.to_string())));
}
}
self.0
.update(
EntryKind::Kms,
EntryOperation::Insert,
KmsCategory::CryptoKey.as_str(),
name,
Some(value.as_ref()),
Some(ins_tags.as_slice()),
expiry_ms,
)
.await?;
Ok(())
}
/// Fetch an existing key from the store
///
/// Specify `for_update` when in a transaction to create an update lock on the
/// associated record, if supported by the store backend
pub async fn fetch_key(
&mut self,
name: &str,
for_update: bool,
) -> Result<Option<KeyEntry>, Error> {
Ok(
if let Some(row) = self
.0
.fetch(
EntryKind::Kms,
KmsCategory::CryptoKey.as_str(),
name,
for_update,
)
.await?
{
Some(KeyEntry::from_entry(row)?)
} else {
None
},
)
}
/// Retrieve all keys matching the given filters.
pub async fn fetch_all_keys(
&mut self,
algorithm: Option<&str>,
thumbprint: Option<&str>,
tag_filter: Option<TagFilter>,
limit: Option<i64>,
for_update: bool,
) -> Result<Vec<KeyEntry>, Error> {
let mut query_parts = Vec::with_capacity(3);
if let Some(query) = tag_filter.map(|f| f.query) {
query_parts.push(TagFilter::from(
query
.map_names(|mut k| {
k.replace_range(0..0, "user:");
Result::<_, ()>::Ok(k)
})
.unwrap(),
));
}
if let Some(algorithm) = algorithm {
query_parts.push(TagFilter::is_eq("alg", algorithm));
}
if let Some(thumbprint) = thumbprint {
query_parts.push(TagFilter::is_eq("thumb", thumbprint));
}
let tag_filter = if query_parts.is_empty() {
None
} else {
Some(TagFilter::all_of(query_parts))
};
let rows = self
.0
.fetch_all(
EntryKind::Kms,
KmsCategory::CryptoKey.as_str(),
tag_filter,
limit,
for_update,
)
.await?;
let mut entries = Vec::with_capacity(rows.len());
for row in rows {
entries.push(KeyEntry::from_entry(row)?)
}
Ok(entries)
}
/// Remove an existing key from the store
pub async fn remove_key(&mut self, name: &str) -> Result<(), Error> {
self.0
.update(
EntryKind::Kms,
EntryOperation::Remove,
KmsCategory::CryptoKey.as_str(),
name,
None,
None,
None,
)
.await
}
/// Replace the metadata and tags on an existing key in the store
pub async fn update_key(
&mut self,
name: &str,
metadata: Option<&str>,
tags: Option<&[EntryTag]>,
expiry_ms: Option<i64>,
) -> Result<(), Error> {
let row = self
.0
.fetch(EntryKind::Kms, KmsCategory::CryptoKey.as_str(), name, true)
.await?
.ok_or_else(|| err_msg!(NotFound, "Key entry not found"))?;
let mut params = KeyParams::from_slice(&row.value)?;
params.metadata = metadata.map(str::to_string);
let value = params.to_bytes()?;
let mut upd_tags = Vec::with_capacity(10);
if let Some(tags) = tags {
for t in tags {
upd_tags.push(t.map_ref(|k, v| (format!("user:{}", k), v.to_string())));
}
}
for t in row.tags {
if !t.name().starts_with("user:") {
upd_tags.push(t);
}
}
self.0
.update(
EntryKind::Kms,
EntryOperation::Replace,
KmsCategory::CryptoKey.as_str(),
name,
Some(value.as_ref()),
Some(upd_tags.as_slice()),
expiry_ms,
)
.await?;
Ok(())
}
/// Commit the pending transaction
pub async fn commit(self) -> Result<(), Error> {
self.0.close(true).await
}
/// Roll back the pending transaction
pub async fn rollback(self) -> Result<(), Error> {
self.0.close(false).await
}
}