|
| 1 | +# sigstore-cache |
| 2 | + |
| 3 | +Flexible caching support for [sigstore-rust](https://github.com/sigstore/sigstore-rust) clients. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +This crate provides a pluggable caching mechanism for Sigstore operations. It allows caching of frequently-accessed resources like public keys, trust bundles, and configuration data to reduce network requests and improve performance. |
| 8 | + |
| 9 | +## Features |
| 10 | + |
| 11 | +- **Pluggable adapters**: Choose between filesystem, in-memory, or custom cache backends |
| 12 | +- **TTL support**: Automatic expiration of cached entries |
| 13 | +- **Platform-aware**: Default cache locations follow OS conventions |
| 14 | +- **Thread-safe**: All adapters are safe for concurrent use |
| 15 | + |
| 16 | +## Cache Adapters |
| 17 | + |
| 18 | +| Adapter | Description | Use Case | |
| 19 | +|---------|-------------|----------| |
| 20 | +| `FileSystemCache` | Persistent disk-based cache | Production use, offline support | |
| 21 | +| `InMemoryCache` | Fast in-process cache with TTL | High-performance, single-session | |
| 22 | +| `NoCache` | No-op cache (disabled) | Testing, when caching is not desired | |
| 23 | + |
| 24 | +## Cached Resources |
| 25 | + |
| 26 | +| Resource | Default TTL | Description | |
| 27 | +|----------|-------------|-------------| |
| 28 | +| Rekor Public Key | 24 hours | Transparency log signing key | |
| 29 | +| Rekor Log Info | 1 hour | Log tree size and root hash | |
| 30 | +| Fulcio Trust Bundle | 24 hours | CA certificates | |
| 31 | +| Fulcio Configuration | 7 days | OIDC issuer configuration | |
| 32 | + |
| 33 | +## Usage |
| 34 | + |
| 35 | +```rust |
| 36 | +use sigstore_cache::{FileSystemCache, InMemoryCache, CacheAdapter, CacheKey}; |
| 37 | +use std::time::Duration; |
| 38 | + |
| 39 | +// Filesystem cache (persistent) |
| 40 | +let cache = FileSystemCache::default_location()?; |
| 41 | + |
| 42 | +// Or in-memory cache (fast, non-persistent) |
| 43 | +let cache = InMemoryCache::new(); |
| 44 | + |
| 45 | +// Store and retrieve values |
| 46 | +cache.set(CacheKey::RekorPublicKey, b"key-data", Duration::from_secs(86400)).await?; |
| 47 | +if let Some(data) = cache.get(CacheKey::RekorPublicKey).await? { |
| 48 | + println!("Got cached data"); |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +### With Sigstore Clients |
| 53 | + |
| 54 | +Enable the `cache` feature on the client crates: |
| 55 | + |
| 56 | +```rust |
| 57 | +use sigstore_cache::FileSystemCache; |
| 58 | +use sigstore_fulcio::FulcioClient; |
| 59 | +use sigstore_rekor::RekorClient; |
| 60 | + |
| 61 | +let cache = FileSystemCache::default_location()?; |
| 62 | + |
| 63 | +let fulcio = FulcioClient::builder("https://fulcio.sigstore.dev") |
| 64 | + .with_cache(cache.clone()) |
| 65 | + .build(); |
| 66 | + |
| 67 | +let rekor = RekorClient::builder("https://rekor.sigstore.dev") |
| 68 | + .with_cache(cache) |
| 69 | + .build(); |
| 70 | +``` |
| 71 | + |
| 72 | +## Cache Locations |
| 73 | + |
| 74 | +`FileSystemCache::default_location()` uses platform-specific directories: |
| 75 | + |
| 76 | +- **Linux**: `~/.cache/sigstore-rust/` |
| 77 | +- **macOS**: `~/Library/Caches/dev.sigstore.sigstore-rust/` |
| 78 | +- **Windows**: `C:\Users\<User>\AppData\Local\sigstore\sigstore-rust\cache\` |
| 79 | + |
| 80 | +## Custom Adapters |
| 81 | + |
| 82 | +Implement the `CacheAdapter` trait for custom backends: |
| 83 | + |
| 84 | +```rust |
| 85 | +use sigstore_cache::{CacheAdapter, CacheKey, Result}; |
| 86 | +use std::time::Duration; |
| 87 | +use std::pin::Pin; |
| 88 | +use std::future::Future; |
| 89 | + |
| 90 | +struct MyCache; |
| 91 | + |
| 92 | +impl CacheAdapter for MyCache { |
| 93 | + fn get(&self, key: CacheKey) -> Pin<Box<dyn Future<Output = Result<Option<Vec<u8>>>> + Send + '_>> { |
| 94 | + Box::pin(async { Ok(None) }) |
| 95 | + } |
| 96 | + |
| 97 | + fn set(&self, key: CacheKey, value: &[u8], ttl: Duration) -> Pin<Box<dyn Future<Output = Result<()>> + Send + '_>> { |
| 98 | + Box::pin(async { Ok(()) }) |
| 99 | + } |
| 100 | + |
| 101 | + fn remove(&self, key: CacheKey) -> Pin<Box<dyn Future<Output = Result<()>> + Send + '_>> { |
| 102 | + Box::pin(async { Ok(()) }) |
| 103 | + } |
| 104 | + |
| 105 | + fn clear(&self) -> Pin<Box<dyn Future<Output = Result<()>> + Send + '_>> { |
| 106 | + Box::pin(async { Ok(()) }) |
| 107 | + } |
| 108 | +} |
| 109 | +``` |
| 110 | + |
| 111 | +## Related Crates |
| 112 | + |
| 113 | +Used by: |
| 114 | + |
| 115 | +- [`sigstore-fulcio`](../sigstore-fulcio) - Caches configuration and trust bundles |
| 116 | +- [`sigstore-rekor`](../sigstore-rekor) - Caches public keys and log info |
| 117 | + |
| 118 | +## License |
| 119 | + |
| 120 | +BSD-3-Clause |
0 commit comments