cachekit-rs 0.11.1

Caching for Rust (beta). Supports cachekit.io SaaS, Redis, Memcached, local File, and Cloudflare Workers.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
mod common;

use cachekit::config::{CachekitConfig, CachekitConfigBuilder};
use common::EnvGuard;
use serial_test::serial;
use std::time::Duration;

// ── from_env defaults ────────────────────────────────────────────────────────

#[test]
#[serial]
fn config_from_env_defaults() {
    // Clear every from_env-read variable so we get defaults.
    let _env = EnvGuard::set(&[
        ("CACHEKIT_API_KEY", None),
        ("CACHEKIT_API_URL", None),
        ("CACHEKIT_MASTER_KEY", None),
        ("CACHEKIT_PREVIOUS_MASTER_KEYS", None),
        ("CACHEKIT_DEFAULT_TTL", None),
    ]);

    let config = CachekitConfig::from_env().expect("from_env failed with no env vars");

    assert_eq!(config.api_url, "https://api.cachekit.io");
    assert_eq!(config.default_ttl, Duration::from_secs(300));
    assert_eq!(config.l1_capacity, 1000);
    assert_eq!(config.max_payload_bytes, 5 * 1024 * 1024);
    // api_key absent by default
    assert!(config.api_key.is_none());
    // master_key absent by default
    assert!(config.master_key.is_none());
}

#[test]
#[serial]
fn config_from_env_reads_api_key() {
    let _env = EnvGuard::set(&[("CACHEKIT_API_KEY", Some("test-key-123"))]);
    let config = CachekitConfig::from_env().expect("from_env failed");

    // Use .as_ref().map(|k| k.as_str()) NOT .as_deref()
    assert_eq!(
        config.api_key.as_ref().map(|k| k.as_str()),
        Some("test-key-123")
    );
}

#[test]
#[serial]
fn config_from_env_rejects_http_url() {
    let _env = EnvGuard::set(&[("CACHEKIT_API_URL", Some("http://insecure.example.com"))]);

    assert!(
        CachekitConfig::from_env().is_err(),
        "expected error for non-HTTPS api_url"
    );
}

#[test]
#[serial]
fn config_from_env_accepts_https_url() {
    let _env = EnvGuard::set(&[("CACHEKIT_API_URL", Some("https://custom.cachekit.io"))]);
    let config = CachekitConfig::from_env().expect("from_env failed");

    assert_eq!(config.api_url, "https://custom.cachekit.io");
}

#[test]
#[serial]
fn config_from_env_rejects_short_master_key() {
    // 31 bytes = 62 hex chars — too short
    let _env = EnvGuard::set(&[("CACHEKIT_MASTER_KEY", Some(&"aa".repeat(31)))]);

    assert!(
        CachekitConfig::from_env().is_err(),
        "expected error for short master key"
    );
}

/// Set but not UTF-8 is not unset: a raw key exported without hex-encoding
/// it must fail, never yield a config with no key (and so a plaintext
/// client from `CacheKit::from_env`).
#[cfg(unix)]
#[test]
#[serial]
fn config_from_env_rejects_non_utf8_master_key() {
    use std::os::unix::ffi::OsStrExt;

    // Records the prior values (CACHEKIT_MASTER_KEY included) and restores
    // them on drop; the raw bytes are set directly because EnvGuard takes str.
    let _env = EnvGuard::set(&[
        ("CACHEKIT_API_KEY", None),
        ("CACHEKIT_API_URL", None),
        ("CACHEKIT_MASTER_KEY", None),
        ("CACHEKIT_PREVIOUS_MASTER_KEYS", None),
        ("CACHEKIT_DEFAULT_TTL", None),
    ]);
    std::env::set_var(
        "CACHEKIT_MASTER_KEY",
        std::ffi::OsStr::from_bytes(&[0xff; 32]),
    );

    let err = CachekitConfig::from_env().expect_err("non-UTF-8 master key must be rejected");
    assert!(
        matches!(&err, cachekit::CachekitError::Config(msg) if msg.contains("CACHEKIT_MASTER_KEY")),
        "expected a Config error naming the variable, got {err:?}"
    );
}

#[test]
#[serial]
fn config_from_env_accepts_32_byte_master_key() {
    // 32 bytes = 64 hex chars — minimum valid
    let _env = EnvGuard::set(&[("CACHEKIT_MASTER_KEY", Some(&"ab".repeat(32)))]);
    let config = CachekitConfig::from_env().expect("from_env failed");

    assert!(config.master_key.is_some());
    assert_eq!(config.master_key.as_ref().unwrap().len(), 32);
}

#[test]
#[serial]
fn config_from_env_rejects_ttl_zero() {
    let _env = EnvGuard::set(&[("CACHEKIT_DEFAULT_TTL", Some("0"))]);

    assert!(
        CachekitConfig::from_env().is_err(),
        "expected error for TTL=0"
    );
}

#[test]
#[serial]
fn config_from_env_accepts_ttl_one() {
    let _env = EnvGuard::set(&[("CACHEKIT_DEFAULT_TTL", Some("1"))]);
    let config = CachekitConfig::from_env().expect("from_env failed");

    assert_eq!(config.default_ttl, Duration::from_secs(1));
}

// ── Debug redaction ───────────────────────────────────────────────────────────

#[test]
#[serial]
fn config_debug_redacts_secrets() {
    let _env = EnvGuard::set(&[
        ("CACHEKIT_API_KEY", Some("super-secret-key")),
        ("CACHEKIT_MASTER_KEY", Some(&"ab".repeat(32))),
    ]);
    let config = CachekitConfig::from_env().expect("from_env failed");

    let debug_str = format!("{config:?}");
    assert!(
        !debug_str.contains("super-secret-key"),
        "api_key leaked in debug: {debug_str}"
    );
    assert!(
        debug_str.contains("[REDACTED]"),
        "expected [REDACTED] in debug: {debug_str}"
    );
}

// ── Builder ───────────────────────────────────────────────────────────────────

#[test]
fn config_builder_basic() {
    let config = CachekitConfigBuilder::new()
        .api_key("my-api-key")
        .api_url("https://api.example.io")
        .expect("valid url")
        .default_ttl(Duration::from_secs(60))
        .expect("valid ttl")
        .namespace("myapp")
        .l1_capacity(500)
        .build();

    assert_eq!(config.api_url, "https://api.example.io");
    assert_eq!(config.default_ttl, Duration::from_secs(60));
    assert_eq!(config.namespace.as_deref(), Some("myapp"));
    assert_eq!(config.l1_capacity, 500);
    assert_eq!(
        config.api_key.as_ref().map(|k| k.as_str()),
        Some("my-api-key")
    );
}

#[test]
fn config_builder_rejects_http_url() {
    let result = CachekitConfigBuilder::new().api_url("http://not-secure.example.com");
    assert!(result.is_err(), "expected error for HTTP url in builder");
}

#[test]
fn config_builder_rejects_short_master_key() {
    let short_hex = "aa".repeat(31); // 31 bytes
    let result = CachekitConfigBuilder::new().master_key(&short_hex);
    assert!(
        result.is_err(),
        "expected error for short master key in builder"
    );
}

#[test]
fn config_builder_accepts_valid_master_key() {
    let valid_hex = "ab".repeat(32); // 32 bytes
    let config = CachekitConfigBuilder::new()
        .master_key(&valid_hex)
        .expect("valid master key")
        .build();
    assert!(config.master_key.is_some());
    assert_eq!(config.master_key.as_ref().unwrap().len(), 32);
}

// ── Previous master keys (key rotation) ───────────────────────────────────────

fn hexkey(byte: u8) -> String {
    hex::encode([byte; 32])
}

fn assert_config_err(result: Result<CachekitConfigBuilder, cachekit::CachekitError>, what: &str) {
    match result {
        Err(cachekit::CachekitError::Config(_)) => {}
        other => panic!(
            "{what}: expected CachekitError::Config, got {:?}",
            other.map(|_| "Ok(builder)")
        ),
    }
}

#[test]
fn config_builder_accepts_previous_master_keys() {
    let config = CachekitConfigBuilder::new()
        .master_key(&hexkey(0x22))
        .expect("valid master key")
        .previous_master_keys(&[hexkey(0x11).as_str(), hexkey(0x33).as_str()])
        .expect("valid previous keys")
        .build();

    assert_eq!(config.previous_master_keys.len(), 2);
    // Attempt order preserved: slice order.
    assert_eq!(config.previous_master_keys[0].as_slice(), &[0x11u8; 32]);
    assert_eq!(config.previous_master_keys[1].as_slice(), &[0x33u8; 32]);
}

#[test]
fn config_builder_accepts_exactly_three_previous_keys() {
    let keys: Vec<String> = (1..=3).map(hexkey).collect();
    let refs: Vec<&str> = keys.iter().map(String::as_str).collect();

    let config = CachekitConfigBuilder::new()
        .master_key(&hexkey(0x22))
        .expect("valid master key")
        .previous_master_keys(&refs)
        .expect("exactly three previous keys must be accepted")
        .build();

    assert_eq!(config.previous_master_keys.len(), 3);
}

#[test]
fn config_builder_rejects_more_than_three_previous_keys() {
    let keys: Vec<String> = (1..=4).map(hexkey).collect();
    let refs: Vec<&str> = keys.iter().map(String::as_str).collect();

    let result = CachekitConfigBuilder::new()
        .master_key(&hexkey(0x22))
        .expect("valid master key")
        .previous_master_keys(&refs);
    assert_config_err(result, "cap of 3 must reject, never truncate");
}

#[test]
fn config_builder_rejects_invalid_hex_previous_key() {
    let result = CachekitConfigBuilder::new().previous_master_keys(&["not-hex"]);
    assert_config_err(result, "invalid hex previous key");
}

#[test]
fn config_builder_rejects_short_previous_key() {
    let short_hex = "aa".repeat(31);
    let result = CachekitConfigBuilder::new().previous_master_keys(&[short_hex.as_str()]);
    assert_config_err(result, "short previous key");
}

#[test]
fn config_builder_rejects_master_key_in_previous_list() {
    let current = hexkey(0x22);

    // previous set after master_key
    let result = CachekitConfigBuilder::new()
        .master_key(&current)
        .expect("valid master key")
        .previous_master_keys(&[current.as_str()]);
    assert_config_err(result, "self-collision (previous after master)");

    // master_key set after previous — same invariant, other call order
    let result = CachekitConfigBuilder::new()
        .previous_master_keys(&[current.as_str()])
        .expect("valid previous keys")
        .master_key(&current);
    assert_config_err(result, "self-collision (master after previous)");
}

#[test]
#[serial]
fn config_from_env_reads_previous_master_keys() {
    let _env = EnvGuard::set(&[
        ("CACHEKIT_MASTER_KEY", Some(&hexkey(0x22))),
        (
            "CACHEKIT_PREVIOUS_MASTER_KEYS",
            // whitespace around commas tolerated
            Some(&format!("{}, {}", hexkey(0x11), hexkey(0x33))),
        ),
    ]);

    let config = CachekitConfig::from_env().expect("from_env failed");
    assert_eq!(config.previous_master_keys.len(), 2);
    assert_eq!(config.previous_master_keys[0].as_slice(), &[0x11u8; 32]);
    assert_eq!(config.previous_master_keys[1].as_slice(), &[0x33u8; 32]);
}

#[test]
#[serial]
fn config_from_env_rejects_more_than_three_previous_keys() {
    let val: Vec<String> = (1..=4).map(hexkey).collect();
    let _env = EnvGuard::set(&[
        ("CACHEKIT_MASTER_KEY", Some(&hexkey(0x99))),
        ("CACHEKIT_PREVIOUS_MASTER_KEYS", Some(&val.join(","))),
    ]);

    assert!(
        matches!(
            CachekitConfig::from_env(),
            Err(cachekit::CachekitError::Config(_))
        ),
        "cap of 3 must reject at load, never truncate"
    );
}

#[test]
#[serial]
fn config_from_env_rejects_master_key_in_previous_list() {
    let _env = EnvGuard::set(&[
        ("CACHEKIT_MASTER_KEY", Some(&hexkey(0x22))),
        (
            "CACHEKIT_PREVIOUS_MASTER_KEYS",
            Some(&format!("{},{}", hexkey(0x11), hexkey(0x22))),
        ),
    ]);

    assert!(
        matches!(
            CachekitConfig::from_env(),
            Err(cachekit::CachekitError::Config(_))
        ),
        "current key in previous list must fail at load"
    );
}

#[test]
#[serial]
fn config_from_env_rejects_empty_previous_key_entry() {
    let _env = EnvGuard::set(&[
        ("CACHEKIT_MASTER_KEY", Some(&hexkey(0x22))),
        (
            "CACHEKIT_PREVIOUS_MASTER_KEYS",
            // trailing comma → empty entry
            Some(&format!("{},", hexkey(0x11))),
        ),
    ]);

    assert!(
        CachekitConfig::from_env().is_err(),
        "empty entry must be rejected, not skipped"
    );
}

#[test]
#[serial]
fn config_from_env_tolerates_blank_previous_master_keys() {
    // Blanking the variable is how shell profiles, Compose files, and k8s
    // manifests retire it after a completed rotation — that must be a clean
    // cut-over, not a start-up failure.
    let _env = EnvGuard::set(&[
        ("CACHEKIT_MASTER_KEY", None),
        ("CACHEKIT_PREVIOUS_MASTER_KEYS", Some("  ")),
    ]);

    let config = CachekitConfig::from_env().expect("blank value must be treated as unset");
    assert!(config.previous_master_keys.is_empty());
}

/// Drift guard: the config-level cap (`MAX_PREVIOUS_MASTER_KEYS` is declared
/// outside the `encryption` feature gate, so `config.rs` cannot reference the
/// core const directly) must equal the core keyring's cap. CI's main test job
/// enables `encryption`, so this guard runs there.
#[test]
#[cfg(feature = "encryption")]
fn previous_key_cap_matches_core_keyring_cap() {
    assert_eq!(
        cachekit::config::MAX_PREVIOUS_MASTER_KEYS,
        cachekit_core::MAX_DECRYPT_ONLY_KEYS
    );
}

#[test]
#[serial]
fn config_from_env_rejects_previous_keys_without_master_key() {
    let _env = EnvGuard::set(&[
        ("CACHEKIT_MASTER_KEY", None),
        ("CACHEKIT_PREVIOUS_MASTER_KEYS", Some(&hexkey(0x11))),
    ]);

    assert!(
        matches!(
            CachekitConfig::from_env(),
            Err(cachekit::CachekitError::Config(_))
        ),
        "previous keys without a current master key must fail at load, not be silently dropped"
    );
}

/// A positive sub-second default TTL is accepted (the wire ceils it to 1 s);
/// zero is still a `Config` error.
#[test]
fn builder_default_ttl_accepts_sub_second_and_rejects_zero() {
    let config = CachekitConfigBuilder::new()
        .default_ttl(Duration::from_millis(500))
        .expect("a 500 ms default TTL must be accepted")
        .build();
    assert_eq!(config.default_ttl, Duration::from_millis(500));

    assert_config_err(
        CachekitConfigBuilder::new().default_ttl(Duration::ZERO),
        "zero default_ttl",
    );
}