use crate::audit::{AuditHead, AuditQuery, AuditRecord};
use crate::error::{Result, VaultError};
use crate::grant::{Grant, GrantAction, GrantId, GrantTarget};
use crate::policy::Caller;
use crate::recipient::RecipientSealed;
use crate::scope::Scope;
use crate::secret::{NewSecret, Plaintext, SecretId, SecretMeta, VersionMeta, Which};
use crate::vault::Vault;
use std::path::PathBuf;
use std::sync::Arc;
use weirdnet_keys::Key32;
use zeroize::Zeroizing;
#[derive(Clone, Debug)]
pub struct AsyncVault {
inner: Arc<Vault>,
}
impl AsyncVault {
pub fn new(vault: Vault) -> Self {
AsyncVault { inner: Arc::new(vault) }
}
pub fn from_arc(vault: Arc<Vault>) -> Self {
AsyncVault { inner: vault }
}
pub fn vault(&self) -> &Arc<Vault> {
&self.inner
}
pub async fn run<R, F>(&self, f: F) -> Result<R>
where
R: Send + 'static,
F: FnOnce(&Vault) -> Result<R> + Send + 'static,
{
let v = self.inner.clone();
tokio::task::spawn_blocking(move || f(&v)).await.map_err(|e| VaultError::Storage(format!("vault task failed: {e}")))?
}
pub async fn create(&self, caller: Caller, new: NewSecret, value: Zeroizing<Vec<u8>>) -> Result<SecretMeta> {
self.run(move |v| v.create(&caller, new, &value)).await
}
pub async fn meta(&self, caller: Caller, id: SecretId) -> Result<SecretMeta> {
self.run(move |v| v.meta(&caller, &id)).await
}
pub async fn versions(&self, caller: Caller, id: SecretId) -> Result<Vec<VersionMeta>> {
self.run(move |v| v.versions(&caller, &id)).await
}
pub async fn list(&self, caller: Caller, prefix: Scope) -> Result<Vec<SecretMeta>> {
self.run(move |v| v.list(&caller, &prefix)).await
}
pub async fn use_secret<R, F>(&self, caller: Caller, id: SecretId, f: F) -> Result<R>
where
R: Send + 'static,
F: FnOnce(&Plaintext) -> R + Send + 'static,
{
self.run(move |v| v.use_secret(&caller, &id, f)).await
}
pub async fn use_version<R, F>(&self, caller: Caller, id: SecretId, which: Which, f: F) -> Result<R>
where
R: Send + 'static,
F: FnOnce(&Plaintext) -> R + Send + 'static,
{
self.run(move |v| v.use_version(&caller, &id, which, f)).await
}
pub async fn reveal(&self, caller: Caller, id: SecretId) -> Result<Plaintext> {
self.run(move |v| v.reveal(&caller, &id)).await
}
pub async fn reveal_sealed(&self, caller: Caller, id: SecretId, recipient: [u8; 32]) -> Result<RecipientSealed> {
self.run(move |v| v.reveal_sealed(&caller, &id, &recipient)).await
}
pub async fn stage_rotation(&self, caller: Caller, id: SecretId, value: Zeroizing<Vec<u8>>) -> Result<SecretMeta> {
self.run(move |v| v.stage_rotation(&caller, &id, &value)).await
}
pub async fn promote(&self, caller: Caller, id: SecretId) -> Result<SecretMeta> {
self.run(move |v| v.promote(&caller, &id)).await
}
pub async fn discard_pending(&self, caller: Caller, id: SecretId) -> Result<SecretMeta> {
self.run(move |v| v.discard_pending(&caller, &id)).await
}
pub async fn revert(&self, caller: Caller, id: SecretId) -> Result<SecretMeta> {
self.run(move |v| v.revert(&caller, &id)).await
}
pub async fn retire_previous(&self, caller: Caller, id: SecretId) -> Result<SecretMeta> {
self.run(move |v| v.retire_previous(&caller, &id)).await
}
pub async fn delete(&self, caller: Caller, id: SecretId) -> Result<()> {
self.run(move |v| v.delete(&caller, &id)).await
}
pub async fn grant(
&self,
caller: Caller,
target: GrantTarget,
identity: String,
actions: Vec<GrantAction>,
expires_at: Option<i64>,
) -> Result<Grant> {
self.run(move |v| v.grant(&caller, target, &identity, &actions, expires_at)).await
}
pub async fn revoke(&self, caller: Caller, grant: GrantId) -> Result<()> {
self.run(move |v| v.revoke(&caller, &grant)).await
}
pub async fn grants(&self, caller: Caller, target: GrantTarget) -> Result<Vec<Grant>> {
self.run(move |v| v.grants(&caller, &target)).await
}
pub async fn audit(&self, caller: Caller, query: AuditQuery) -> Result<Vec<AuditRecord>> {
self.run(move |v| v.audit(&caller, &query)).await
}
pub async fn verify_audit(&self) -> Result<AuditHead> {
self.run(|v| v.verify_audit()).await
}
pub async fn snapshot_to(&self, caller: Caller, dest: PathBuf, key: Option<Key32>) -> Result<()> {
self.run(move |v| v.snapshot_to(&caller, &dest, key.as_ref())).await
}
}