use crate::model::vault::serde::to_uuid;
use getset::Getters;
use serde::{Deserialize, Serialize};
use typed_builder::TypedBuilder;
use uuid::Uuid;
#[cfg(feature = "wrapped")]
#[derive(Clone, Debug, Deserialize, Getters, Serialize, TypedBuilder)]
#[getset(get = "pub(crate)")]
#[builder(builder_method_doc = "")]
pub struct Config {
#[builder(setter(into))]
vault_base_url: String,
#[builder(setter(into))]
app_role: String,
#[builder(setter(into))]
secrets_path: String,
#[serde(deserialize_with = "to_uuid")]
role_id: Uuid,
#[builder(setter(into))]
toad_base_url: String,
}
#[cfg(not(feature = "wrapped"))]
#[derive(Clone, Debug, Deserialize, Getters, Serialize, TypedBuilder)]
#[getset(get = "pub(crate)")]
#[builder(builder_method_doc = "")]
pub struct Config {
#[builder(setter(into))]
vault_base_url: String,
#[builder(setter(into))]
app_role: String,
#[builder(setter(into))]
secrets_path: String,
#[serde(deserialize_with = "to_uuid")]
role_id: Uuid,
#[builder(setter(into))]
wrapping_token: String,
}
impl Config {
#[cfg(feature = "wrapped")]
pub(crate) fn toad_approle_url(&self) -> String {
format!("{}approle", self.toad_base_url)
}
#[cfg(feature = "check_approle")]
pub(crate) fn toad_approles_url(&self) -> String {
format!("{}approles", self.toad_base_url)
}
pub(crate) fn unwrap_url(&self) -> String {
format!("{}sys/wrapping/unwrap", self.vault_base_url)
}
pub(crate) fn login_url(&self) -> String {
format!("{}auth/approle/login", self.vault_base_url)
}
pub(crate) fn secrets_url(&self) -> String {
format!(
"{}{}",
self.vault_base_url,
self.secrets_path,
)
}
pub(crate) fn revoke_url(&self) -> String {
format!("{}auth/token/revoke-self", self.vault_base_url)
}
}
#[cfg(test)]
mod test {
use super::Config;
use uuid::Uuid;
#[test]
#[cfg(feature = "wrapped")]
fn config_builds() {
let config = Config::builder()
.vault_base_url("")
.app_role("finnhub")
.secrets_path("finnhub/data/config")
.role_id(Uuid::new_v4())
.toad_base_url("")
.build();
assert_eq!(config.app_role(), "finnhub");
}
#[test]
#[cfg(not(feature = "wrapped"))]
fn config_builds() {
let config = Config::builder()
.vault_base_url("")
.app_role("finnhub")
.secrets_path("finnhub/data/config")
.role_id(Uuid::new_v4())
.wrapping_token("")
.build();
assert_eq!(config.app_role(), "finnhub");
}
}