cachekit/config.rs
1use std::time::Duration;
2
3use zeroize::Zeroizing;
4
5use crate::error::CachekitError;
6
7/// Maximum number of decrypt-only previous master keys.
8///
9/// Mirrors `cachekit_core::MAX_DECRYPT_ONLY_KEYS` (spec/encryption.md → "Key
10/// Rotation (Keyring)"), which is feature-gated behind `encryption` and so
11/// cannot be referenced here unconditionally. A drift-guard test in
12/// `config_tests.rs` asserts the two stay equal.
13pub const MAX_PREVIOUS_MASTER_KEYS: usize = 3;
14
15// ── CachekitConfig ────────────────────────────────────────────────────────────
16
17/// Configuration values for a CacheKit client.
18///
19/// A client reads a `CachekitConfig` on one path only: `CacheKit::from_env()`
20/// builds one with [`CachekitConfig::from_env`] and applies it. No `CacheKit`
21/// constructor takes a `CachekitConfig` value, so a config made with
22/// [`CachekitConfigBuilder`] configures no client.
23pub struct CachekitConfig {
24 /// API key for cachekit.io authentication.
25 pub api_key: Option<Zeroizing<String>>,
26 /// Base URL of the cachekit.io API.
27 pub api_url: String,
28 /// Master key used for zero-knowledge encryption (AES-256-GCM).
29 pub master_key: Option<Zeroizing<Vec<u8>>>,
30 /// Decrypt-only previous master keys for a rotation grace window, in
31 /// attempt order. At most [`MAX_PREVIOUS_MASTER_KEYS`] entries.
32 ///
33 /// These hold key material only. See
34 /// [`CachekitConfigBuilder::previous_master_keys`] for the paths that
35 /// apply previous keys to a client.
36 pub previous_master_keys: Vec<Zeroizing<Vec<u8>>>,
37 /// Default TTL for cache entries when none is specified at call site.
38 pub default_ttl: Duration,
39 /// Optional namespace prefix applied to all cache keys.
40 pub namespace: Option<String>,
41 /// Maximum number of entries in the L1 in-process cache.
42 pub l1_capacity: usize,
43 /// Maximum allowed payload size in bytes. Larger payloads are rejected.
44 pub max_payload_bytes: usize,
45}
46
47impl std::fmt::Debug for CachekitConfig {
48 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
49 let api_key_repr = if self.api_key.is_some() {
50 "[REDACTED]"
51 } else {
52 "None"
53 };
54 let master_key_repr = if self.master_key.is_some() {
55 "[REDACTED]"
56 } else {
57 "None"
58 };
59
60 f.debug_struct("CachekitConfig")
61 .field("api_key", &api_key_repr)
62 .field("api_url", &self.api_url)
63 .field("master_key", &master_key_repr)
64 .field(
65 "previous_master_keys",
66 &format_args!("[REDACTED; {}]", self.previous_master_keys.len()),
67 )
68 .field("default_ttl", &self.default_ttl)
69 .field("namespace", &self.namespace)
70 .field("l1_capacity", &self.l1_capacity)
71 .field("max_payload_bytes", &self.max_payload_bytes)
72 .finish()
73 }
74}
75
76impl Default for CachekitConfig {
77 fn default() -> Self {
78 Self {
79 api_key: None,
80 api_url: "https://api.cachekit.io".to_owned(),
81 master_key: None,
82 previous_master_keys: Vec::new(),
83 default_ttl: Duration::from_secs(300),
84 namespace: None,
85 l1_capacity: 1000,
86 max_payload_bytes: 5 * 1024 * 1024, // 5 MiB
87 }
88 }
89}
90
91impl CachekitConfig {
92 /// Build configuration from environment variables.
93 ///
94 /// | Variable | Description |
95 /// |---|---|
96 /// | `CACHEKIT_API_KEY` | API key for cachekit.io |
97 /// | `CACHEKIT_API_URL` | Override API base URL (must be HTTPS) |
98 /// | `CACHEKIT_MASTER_KEY` | Hex-encoded master key (min 32 bytes); set but empty, not hex or not UTF-8 is an error, never unset |
99 /// | `CACHEKIT_PREVIOUS_MASTER_KEYS` | Comma-separated hex-encoded decrypt-only previous master keys (max 3; blank value = unset) |
100 /// | `CACHEKIT_DEFAULT_TTL` | Default TTL in seconds (min 1) |
101 pub fn from_env() -> Result<Self, CachekitError> {
102 let mut config = Self::default();
103
104 // API key
105 if let Ok(val) = std::env::var("CACHEKIT_API_KEY") {
106 config.api_key = Some(Zeroizing::new(val));
107 }
108
109 // API URL — must be HTTPS
110 if let Ok(val) = std::env::var("CACHEKIT_API_URL") {
111 validate_https(&val)?;
112 config.api_url = val;
113 }
114
115 // Master key — hex-decode and validate length >= 32 bytes.
116 // Deliberately NO blank-value tolerance here (unlike the previous-keys
117 // var below): a blank CACHEKIT_MASTER_KEY treated as unset would
118 // silently turn encryption off. Set but not UTF-8 (a raw key put in
119 // the environment without hex-encoding it) is an error for the same
120 // reason, never unset.
121 match std::env::var("CACHEKIT_MASTER_KEY") {
122 Ok(val) => {
123 // `env::var` hands back an owned copy of the hex secret. Wrap it
124 // so that copy is wiped on drop too — the decoded bytes below are
125 // already `Zeroizing`, but the hex form is the same key material.
126 // Defence in depth over the heap copy only: the process `environ`
127 // block still holds the identical hex for the process lifetime
128 // and is not wiped here, so this narrows post-lifetime recovery
129 // (core dumps, swap, heap reuse), it does not eliminate the
130 // exposure.
131 let val = Zeroizing::new(val);
132 config.master_key = Some(decode_master_key_hex(&val, "CACHEKIT_MASTER_KEY")?);
133 }
134 Err(std::env::VarError::NotPresent) => {}
135 Err(std::env::VarError::NotUnicode(_)) => {
136 return Err(CachekitError::Config(
137 "CACHEKIT_MASTER_KEY is not valid UTF-8: set it to the key's 64 hex chars"
138 .to_owned(),
139 ))
140 }
141 }
142
143 config.previous_master_keys =
144 previous_master_keys_from_env(config.master_key.as_deref().map(Vec::as_slice))?;
145
146 // Default TTL — minimum 1 second
147 if let Ok(val) = std::env::var("CACHEKIT_DEFAULT_TTL") {
148 let secs: u64 = val.parse().map_err(|e| {
149 CachekitError::Config(format!("CACHEKIT_DEFAULT_TTL must be an integer: {e}"))
150 })?;
151 if secs < 1 {
152 return Err(CachekitError::Config(
153 "CACHEKIT_DEFAULT_TTL must be at least 1 second".to_owned(),
154 ));
155 }
156 config.default_ttl = Duration::from_secs(secs);
157 }
158
159 Ok(config)
160 }
161}
162
163// ── CachekitConfigBuilder ─────────────────────────────────────────────────────
164
165/// Fluent builder for a [`CachekitConfig`] value.
166///
167/// The builder validates and holds values. No `CacheKit` constructor takes
168/// the [`CachekitConfig`] it builds, so nothing set here (keys, namespace,
169/// TTL, capacity) reaches a client. To configure a client, use
170/// `CacheKit::builder()` or `CacheKit::from_env()`.
171#[derive(Default)]
172#[must_use]
173pub struct CachekitConfigBuilder {
174 inner: CachekitConfig,
175}
176
177impl CachekitConfigBuilder {
178 /// Create a new builder with defaults.
179 pub fn new() -> Self {
180 Self {
181 inner: CachekitConfig::default(),
182 }
183 }
184
185 /// Set the API key.
186 pub fn api_key(mut self, key: impl Into<String>) -> Self {
187 self.inner.api_key = Some(Zeroizing::new(key.into()));
188 self
189 }
190
191 /// Set the API base URL. Must use HTTPS.
192 pub fn api_url(mut self, url: impl Into<String>) -> Result<Self, CachekitError> {
193 let url = url.into();
194 validate_https(&url)?;
195 self.inner.api_url = url;
196 Ok(self)
197 }
198
199 /// Set the master key from a hex string. Must decode to at least 32 bytes.
200 pub fn master_key(mut self, hex_key: &str) -> Result<Self, CachekitError> {
201 let bytes = decode_master_key_hex(hex_key, "master_key")?;
202 validate_previous_master_keys(
203 Some(bytes.as_slice()),
204 &self.inner.previous_master_keys,
205 "previous_master_keys",
206 )?;
207 self.inner.master_key = Some(bytes);
208 Ok(self)
209 }
210
211 /// Set decrypt-only previous master keys from hex strings, in attempt
212 /// order, on the [`CachekitConfig`] value.
213 ///
214 /// No client reads keys set here. With the `encryption` feature enabled,
215 /// these paths apply previous keys to a client; each one decrypts with
216 /// the current key first, then each previous key in order:
217 ///
218 /// * `CacheKit::from_env()` and `CacheKit::secure_from_env()` read them
219 /// from `CACHEKIT_PREVIOUS_MASTER_KEYS`.
220 /// * `CacheKitBuilder::encryption_from_bytes_with_previous()` takes them
221 /// as arguments. It requires every key to be exactly 32 raw bytes,
222 /// while this method accepts hex that decodes to 32 bytes or more.
223 ///
224 /// Without the `encryption` feature no path accepts a master key, so
225 /// none can silently drop one: `CacheKit::secure_from_env()` does not
226 /// exist, `CacheKit::from_env()` returns [`CachekitError::Config`] when
227 /// `CACHEKIT_MASTER_KEY` is set, and
228 /// `CacheKitBuilder::encryption_from_bytes_with_previous()` always
229 /// returns [`CachekitError::Config`].
230 ///
231 /// Validation is identical to [`Self::master_key`] per entry (valid hex,
232 /// at least 32 bytes). At most [`MAX_PREVIOUS_MASTER_KEYS`] entries —
233 /// more is a [`CachekitError::Config`], never truncated. The current
234 /// master key must not reappear here (forward-only rotation: a retired
235 /// key is never re-promoted).
236 ///
237 /// # Examples
238 ///
239 /// ```
240 /// use cachekit::config::CachekitConfigBuilder;
241 ///
242 /// // k2 is current after rotation; k1 is kept as a decrypt-only previous key.
243 /// let k1 = "11".repeat(32);
244 /// let k2 = "22".repeat(32);
245 ///
246 /// let config = CachekitConfigBuilder::new()
247 /// .master_key(&k2)?
248 /// .previous_master_keys(&[k1.as_str()])?
249 /// .build();
250 ///
251 /// assert_eq!(config.previous_master_keys.len(), 1);
252 /// # Ok::<(), cachekit::CachekitError>(())
253 /// ```
254 pub fn previous_master_keys(mut self, hex_keys: &[&str]) -> Result<Self, CachekitError> {
255 let mut previous = Vec::with_capacity(hex_keys.len());
256 for hex_key in hex_keys {
257 previous.push(decode_master_key_hex(
258 hex_key,
259 "previous_master_keys entry",
260 )?);
261 }
262 validate_previous_master_keys(
263 self.inner.master_key.as_deref().map(Vec::as_slice),
264 &previous,
265 "previous_master_keys",
266 )?;
267 self.inner.previous_master_keys = previous;
268 Ok(self)
269 }
270
271 /// Set the default TTL. Must be non-zero; a positive sub-second TTL is
272 /// sent to the backend as 1 second.
273 pub fn default_ttl(mut self, ttl: Duration) -> Result<Self, CachekitError> {
274 if ttl.is_zero() {
275 return Err(CachekitError::Config(
276 "default_ttl must be greater than zero".to_owned(),
277 ));
278 }
279 self.inner.default_ttl = ttl;
280 Ok(self)
281 }
282
283 /// Set the namespace prefix.
284 pub fn namespace(mut self, ns: impl Into<String>) -> Self {
285 self.inner.namespace = Some(ns.into());
286 self
287 }
288
289 /// Set the L1 cache capacity (max entries).
290 pub fn l1_capacity(mut self, capacity: usize) -> Self {
291 self.inner.l1_capacity = capacity;
292 self
293 }
294
295 /// Finalise and return the [`CachekitConfig`].
296 pub fn build(self) -> CachekitConfig {
297 self.inner
298 }
299}
300
301// ── Helpers ───────────────────────────────────────────────────────────────────
302
303/// Hex-decode a master key and require at least 32 bytes. Shared by every
304/// master-key hex path — the current and previous keys here, the `secure`
305/// preset and the builder's `.encryption()` — so validation cannot drift.
306///
307/// Returns `Zeroizing` so the decoded key material is wiped on drop for its
308/// whole lifetime — including the early-drop paths where a caller's later
309/// validation step fails.
310pub(crate) fn decode_master_key_hex(
311 hex_key: &str,
312 what: &str,
313) -> Result<Zeroizing<Vec<u8>>, CachekitError> {
314 // Decode straight into zeroizing storage sized once: `hex::decode` grows
315 // its Vec by reallocation and frees each outgrown buffer unwiped.
316 let mut bytes = Zeroizing::new(vec![0u8; hex_key.len() / 2]);
317 hex::decode_to_slice(hex_key, &mut bytes).map_err(|e| {
318 // `FromHexError`'s Display quotes the offending character — one
319 // character of the key in an error string (CWE-532). Name the class.
320 // Exhaustive on purpose: a new variant must be reviewed for the same leak.
321 let reason = match e {
322 hex::FromHexError::InvalidHexCharacter { .. } => "contains a non-hex character",
323 hex::FromHexError::OddLength => "odd number of hex digits",
324 hex::FromHexError::InvalidStringLength => "invalid length",
325 };
326 CachekitError::Config(format!("{what} is not valid hex: {reason}"))
327 })?;
328 if bytes.len() < 32 {
329 return Err(CachekitError::Config(format!(
330 "{what} must be at least 32 bytes (64 hex chars); got {} bytes",
331 bytes.len()
332 )));
333 }
334 Ok(bytes)
335}
336
337/// `CACHEKIT_PREVIOUS_MASTER_KEYS`, decoded and validated against the
338/// decoded current key. The one reader of the variable, shared by
339/// [`CachekitConfig::from_env`] and `CacheKit::secure_from_env` so the two
340/// env paths cannot drift. Unset or blank yields no keys.
341pub(crate) fn previous_master_keys_from_env(
342 master_key: Option<&[u8]>,
343) -> Result<Vec<Zeroizing<Vec<u8>>>, CachekitError> {
344 // Same reasoning as CACHEKIT_MASTER_KEY in `from_env`: wipe the owned
345 // copy of the hex list on drop.
346 let val = match std::env::var("CACHEKIT_PREVIOUS_MASTER_KEYS") {
347 Ok(val) => Zeroizing::new(val),
348 Err(std::env::VarError::NotPresent) => return Ok(Vec::new()),
349 // Set but unreadable is not unset: dropping it would silently strip
350 // the decrypt-only keys, and every old-key read would fail closed.
351 Err(std::env::VarError::NotUnicode(_)) => {
352 return Err(CachekitError::Config(
353 "CACHEKIT_PREVIOUS_MASTER_KEYS is not valid UTF-8".to_owned(),
354 ))
355 }
356 };
357 // A wholly blank value retires the variable (the common way to disable
358 // it in shell profiles, Compose files, and k8s manifests) and is treated
359 // as unset; a blank entry inside a non-blank list is still an operator
360 // mistake.
361 if val.trim().is_empty() {
362 return Ok(Vec::new());
363 }
364 let mut previous = Vec::new();
365 for entry in val.split(',') {
366 let entry = entry.trim();
367 if entry.is_empty() {
368 return Err(CachekitError::Config(
369 "CACHEKIT_PREVIOUS_MASTER_KEYS contains an empty entry".to_owned(),
370 ));
371 }
372 previous.push(decode_master_key_hex(
373 entry,
374 "CACHEKIT_PREVIOUS_MASTER_KEYS entry",
375 )?);
376 }
377 // Previous keys without a current key is a broken rotation deploy:
378 // nothing would ever consume them, and the operator would only find out
379 // at the first secure_cache() call. Fail at load.
380 let Some(master_key) = master_key else {
381 return Err(CachekitError::Config(
382 "CACHEKIT_PREVIOUS_MASTER_KEYS requires CACHEKIT_MASTER_KEY to be set".to_owned(),
383 ));
384 };
385 validate_previous_master_keys(Some(master_key), &previous, "CACHEKIT_PREVIOUS_MASTER_KEYS")?;
386 Ok(previous)
387}
388
389/// Enforce the keyring config invariants: at most [`MAX_PREVIOUS_MASTER_KEYS`]
390/// previous keys (rejected, never truncated), and the current master key must
391/// not also appear in the previous list (the detectable subset of the
392/// forward-only rotation rule — re-promoting a retired key would resume a
393/// used, unknowable AES-GCM nonce budget).
394///
395/// Fail-fast mirror of the checks `cachekit_core::Keyring::new` repeats at
396/// client build time; plain equality is fine — both operands are
397/// operator-supplied configuration, not secrets under timing attack.
398fn validate_previous_master_keys(
399 master_key: Option<&[u8]>,
400 previous: &[Zeroizing<Vec<u8>>],
401 source: &str,
402) -> Result<(), CachekitError> {
403 if previous.len() > MAX_PREVIOUS_MASTER_KEYS {
404 return Err(CachekitError::Config(format!(
405 "{source} accepts at most {MAX_PREVIOUS_MASTER_KEYS} entries; got {}",
406 previous.len()
407 )));
408 }
409 if let Some(master) = master_key {
410 if previous.iter().any(|key| key.as_slice() == master) {
411 return Err(CachekitError::Config(format!(
412 "the current master key must not appear in {source} \
413 (rotation is forward-only; retired keys are never re-promoted)"
414 )));
415 }
416 }
417 Ok(())
418}
419
420fn validate_https(url: &str) -> Result<(), CachekitError> {
421 if !url.starts_with("https://") {
422 return Err(CachekitError::Config(format!(
423 "API URL must use HTTPS; got: {url}"
424 )));
425 }
426 Ok(())
427}