use std::collections::BTreeMap;
use serde::{Deserialize, Serialize};
use crate::client::Bibby;
use crate::error::Result;
use crate::pagination::{Page, PageParams};
const PRODUCT: &str = "vault";
#[derive(Debug, Clone, Copy)]
pub struct VaultClient<'a> {
client: &'a Bibby,
}
impl<'a> VaultClient<'a> {
pub(crate) fn new(client: &'a Bibby) -> Self {
Self { client }
}
pub async fn list_projects(&self, params: &PageParams) -> Result<Page<VaultProject>> {
self.client
.get_json_query(PRODUCT, "projects", params)
.await
}
pub async fn create_project(&self, request: &CreateProjectRequest) -> Result<VaultProject> {
self.client.post_json(PRODUCT, "projects", request).await
}
pub async fn get_project(&self, id: i64) -> Result<VaultProject> {
self.client
.get_json(PRODUCT, &format!("projects/{id}"))
.await
}
pub async fn update_project(
&self,
id: i64,
changes: &UpdateProjectRequest,
) -> Result<VaultProject> {
self.client
.patch_json(PRODUCT, &format!("projects/{id}"), changes)
.await
}
pub async fn delete_project(&self, id: i64) -> Result<()> {
self.client
.delete_unit(PRODUCT, &format!("projects/{id}"))
.await
}
pub async fn list_environments(&self, project_id: i64) -> Result<Vec<String>> {
self.client
.get_json(PRODUCT, &format!("projects/{project_id}/environments"))
.await
}
pub async fn list_secrets(
&self,
project_id: i64,
params: &ListSecretsParams,
) -> Result<Page<SecretMeta>> {
self.client
.get_json_query(PRODUCT, &format!("projects/{project_id}/secrets"), params)
.await
}
pub async fn set_secret(
&self,
project_id: i64,
request: &SetSecretRequest,
) -> Result<SecretMeta> {
self.client
.post_json(PRODUCT, &format!("projects/{project_id}/secrets"), request)
.await
}
pub async fn import_dotenv(
&self,
project_id: i64,
environment: &str,
dotenv: &str,
) -> Result<ImportSummary> {
#[derive(Serialize)]
struct Body<'a> {
environment: &'a str,
dotenv: &'a str,
}
self.client
.post_json(
PRODUCT,
&format!("projects/{project_id}/secrets/import"),
&Body {
environment,
dotenv,
},
)
.await
}
pub async fn export_dotenv(
&self,
project_id: i64,
environment: Option<&str>,
) -> Result<String> {
#[derive(Serialize)]
struct Query<'a> {
#[serde(skip_serializing_if = "Option::is_none")]
environment: Option<&'a str>,
format: &'static str,
}
self.client
.get_text_query(
PRODUCT,
&format!("projects/{project_id}/export"),
&Query {
environment,
format: "dotenv",
},
)
.await
}
pub async fn export_json(
&self,
project_id: i64,
environment: Option<&str>,
) -> Result<ExportedSecrets> {
#[derive(Serialize)]
struct Query<'a> {
#[serde(skip_serializing_if = "Option::is_none")]
environment: Option<&'a str>,
format: &'static str,
}
self.client
.get_json_query(
PRODUCT,
&format!("projects/{project_id}/export"),
&Query {
environment,
format: "json",
},
)
.await
}
pub async fn reveal_secret(&self, project_id: i64, secret_id: i64) -> Result<RevealedSecret> {
self.client
.get_json(
PRODUCT,
&format!("projects/{project_id}/secrets/{secret_id}/reveal"),
)
.await
}
pub async fn list_versions(
&self,
project_id: i64,
secret_id: i64,
) -> Result<Vec<SecretVersion>> {
self.client
.get_json(
PRODUCT,
&format!("projects/{project_id}/secrets/{secret_id}/versions"),
)
.await
}
pub async fn restore_version(
&self,
project_id: i64,
secret_id: i64,
version: i32,
) -> Result<SecretMeta> {
self.client
.post_empty(
PRODUCT,
&format!("projects/{project_id}/secrets/{secret_id}/versions/{version}/restore"),
)
.await
}
pub async fn delete_secret(&self, project_id: i64, secret_id: i64) -> Result<()> {
self.client
.delete_unit(
PRODUCT,
&format!("projects/{project_id}/secrets/{secret_id}"),
)
.await
}
pub async fn list_shares(&self, params: &PageParams) -> Result<Page<ShareMeta>> {
self.client.get_json_query(PRODUCT, "shares", params).await
}
pub async fn create_share(&self, request: &CreateShareRequest) -> Result<ShareMeta> {
self.client.post_json(PRODUCT, "shares", request).await
}
pub async fn delete_share(&self, id: i64) -> Result<()> {
self.client
.delete_unit(PRODUCT, &format!("shares/{id}"))
.await
}
}
#[derive(Debug, Clone, Serialize)]
pub struct CreateProjectRequest {
pub name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
}
impl CreateProjectRequest {
pub fn new(name: impl Into<String>) -> Self {
Self {
name: name.into(),
description: None,
}
}
pub fn description(mut self, description: impl Into<String>) -> Self {
self.description = Some(description.into());
self
}
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct UpdateProjectRequest {
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct VaultProject {
pub id: i64,
pub name: String,
pub description: Option<String>,
pub secret_count: i64,
#[serde(default)]
pub created_at: Option<String>,
pub updated_at: String,
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct ListSecretsParams {
#[serde(skip_serializing_if = "Option::is_none")]
pub environment: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub q: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub page: Option<i64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub page_size: Option<i64>,
}
#[derive(Debug, Clone, Serialize)]
pub struct SetSecretRequest {
pub environment: String,
pub key: String,
pub value: String,
}
impl SetSecretRequest {
pub fn new(
environment: impl Into<String>,
key: impl Into<String>,
value: impl Into<String>,
) -> Self {
Self {
environment: environment.into(),
key: key.into(),
value: value.into(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SecretMeta {
pub id: i64,
pub project_id: i64,
#[serde(default)]
pub environment: String,
pub key: String,
pub version: i32,
#[serde(default)]
pub created_at: Option<String>,
pub updated_at: String,
}
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RevealedSecret {
#[serde(default)]
pub id: i64,
pub project_id: i64,
pub environment: String,
pub key: String,
pub value: String,
pub version: i32,
#[serde(default)]
pub updated_at: Option<String>,
}
impl std::fmt::Debug for RevealedSecret {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("RevealedSecret")
.field("id", &self.id)
.field("project_id", &self.project_id)
.field("environment", &self.environment)
.field("key", &self.key)
.field("value", &"***")
.field("version", &self.version)
.field("updated_at", &self.updated_at)
.finish()
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SecretVersion {
#[serde(default)]
pub id: i64,
#[serde(default)]
pub secret_id: i64,
pub version: i32,
pub created_at: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ImportSummary {
pub environment: String,
pub imported: usize,
pub created: usize,
pub updated: usize,
}
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ExportedSecrets {
pub project_id: i64,
pub project_name: String,
pub environment: String,
pub format: String,
pub dotenv: String,
pub secrets: BTreeMap<String, String>,
}
impl std::fmt::Debug for ExportedSecrets {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ExportedSecrets")
.field("project_id", &self.project_id)
.field("project_name", &self.project_name)
.field("environment", &self.environment)
.field("secret_count", &self.secrets.len())
.finish()
}
}
#[derive(Debug, Clone, Serialize)]
pub struct CreateShareRequest {
pub secret: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub label: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub passphrase: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub max_views: Option<i32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub ttl_minutes: Option<i64>,
}
impl CreateShareRequest {
pub fn new(secret: impl Into<String>) -> Self {
Self {
secret: secret.into(),
label: None,
passphrase: None,
max_views: None,
ttl_minutes: None,
}
}
pub fn label(mut self, label: impl Into<String>) -> Self {
self.label = Some(label.into());
self
}
pub fn passphrase(mut self, passphrase: impl Into<String>) -> Self {
self.passphrase = Some(passphrase.into());
self
}
pub fn max_views(mut self, max_views: i32) -> Self {
self.max_views = Some(max_views);
self
}
pub fn ttl_minutes(mut self, minutes: i64) -> Self {
self.ttl_minutes = Some(minutes);
self
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ShareMeta {
pub id: i64,
pub token: String,
pub label: Option<String>,
pub has_passphrase: bool,
pub max_views: i32,
pub view_count: i32,
pub expires_at: String,
pub created_at: String,
#[serde(default)]
pub last_viewed_at: Option<String>,
}
impl ShareMeta {
pub fn share_url(&self) -> String {
format!("https://bytebury.com/vault/s/{}", self.token)
}
}