use super::fingerprint::{TargetTlsFingerprint, TargetTlsGeneration};
use ::arc_swap::ArcSwap;
use serde::Serialize;
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TargetTlsInputSet {
pub ca_path: String,
pub client_cert_path: String,
pub client_key_path: String,
pub target_label: String,
}
impl TargetTlsInputSet {
pub fn is_empty(&self) -> bool {
self.ca_path.is_empty() && self.client_cert_path.is_empty() && self.client_key_path.is_empty()
}
}
pub struct TargetTlsPublishedState<M> {
pub generation: TargetTlsGeneration,
pub fingerprint: TargetTlsFingerprint,
pub material: Arc<M>,
pub loaded_at_unix_ms: u64,
}
pub struct TargetTlsRuntimeState<M> {
pub current: ArcSwap<TargetTlsPublishedState<M>>,
pub last_good: ArcSwap<TargetTlsPublishedState<M>>,
pub last_attempt_unix_ms: AtomicU64,
pub last_success_unix_ms: AtomicU64,
pub last_error: parking_lot::RwLock<Option<String>>,
pub inputs: TargetTlsInputSet,
pub reload_lock: tokio::sync::Mutex<()>,
}
impl<M> TargetTlsRuntimeState<M> {
pub fn new(initial: Arc<TargetTlsPublishedState<M>>, inputs: TargetTlsInputSet) -> Self {
Self {
current: ArcSwap::from(initial.clone()),
last_good: ArcSwap::from(initial),
last_attempt_unix_ms: AtomicU64::new(0),
last_success_unix_ms: AtomicU64::new(0),
last_error: parking_lot::RwLock::new(None),
inputs,
reload_lock: tokio::sync::Mutex::new(()),
}
}
pub fn current_generation(&self) -> TargetTlsGeneration {
self.current.load().generation
}
pub fn bump_generation(&self) -> TargetTlsGeneration {
let current = self.current.load();
TargetTlsGeneration(current.generation.0.saturating_add(1))
}
pub fn mark_attempt(&self, unix_ms: u64) {
self.last_attempt_unix_ms.store(unix_ms, Ordering::Release);
}
pub fn mark_success(&self, unix_ms: u64) {
self.last_success_unix_ms.store(unix_ms, Ordering::Release);
}
pub fn last_attempt_unix_ms(&self) -> u64 {
self.last_attempt_unix_ms.load(Ordering::Acquire)
}
pub fn last_success_unix_ms(&self) -> u64 {
self.last_success_unix_ms.load(Ordering::Acquire)
}
}
#[derive(Debug, Clone, Serialize)]
pub struct TargetTlsStatusSnapshot {
pub target_label: String,
pub generation: u64,
pub reload_enabled: bool,
pub detect_mode: &'static str,
pub apply_mode: &'static str,
pub last_attempt_time: Option<u64>,
pub last_success_time: Option<u64>,
pub last_error: Option<String>,
pub ca_path: String,
pub client_cert_path: String,
pub client_key_path: String,
}