ringline-momento 0.3.0

Momento cache client for the ringline io_uring runtime
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
//! Momento credential handling.
//!
//! Momento API tokens contain encoded endpoint information.

use crate::error::{Error, Result};

/// Default protosocket port.
const DEFAULT_PORT: u16 = 9004;

/// Momento credential containing API token, endpoint, and TLS configuration.
#[derive(Debug, Clone)]
pub struct Credential {
    /// The API token for authentication.
    token: String,
    /// The cache endpoint (host:port or just host).
    endpoint: String,
    /// Optional SNI hostname for TLS (used when connecting to IP addresses).
    sni_host: Option<String>,
}

impl Credential {
    /// Create a credential from an API token.
    ///
    /// The token is expected to be a Momento API key. The endpoint
    /// can be extracted from the token or provided separately.
    pub fn from_token(token: impl Into<String>) -> Result<Self> {
        let token = token.into();

        // Try to extract endpoint from token
        let endpoint = Self::extract_endpoint(&token)
            .unwrap_or_else(|| "cache.cell-4-us-east-1-1.prod.a.momentohq.com".to_string());

        Ok(Self {
            token,
            endpoint,
            sni_host: None,
        })
    }

    /// Create a credential with explicit endpoint.
    pub fn with_endpoint(token: impl Into<String>, endpoint: impl Into<String>) -> Self {
        Self {
            token: token.into(),
            endpoint: endpoint.into(),
            sni_host: None,
        }
    }

    /// Create a credential from environment variables.
    ///
    /// Uses:
    /// - `MOMENTO_API_KEY` or `MOMENTO_AUTH_TOKEN` for the token
    /// - `MOMENTO_ENDPOINT` for explicit endpoint (e.g., "cache.us-west-2.momentohq.com" or IP:port)
    /// - `MOMENTO_REGION` for region-based endpoint (e.g., "us-west-2")
    /// - `MOMENTO_SNI_HOST` for TLS hostname when connecting to IP addresses
    pub fn from_env() -> Result<Self> {
        let token = std::env::var("MOMENTO_API_KEY")
            .or_else(|_| std::env::var("MOMENTO_AUTH_TOKEN"))
            .map_err(|_| Error::Config("MOMENTO_API_KEY environment variable not set".into()))?;

        // Check for SNI hostname (used when connecting to IP addresses)
        let sni_host = std::env::var("MOMENTO_SNI_HOST").ok();

        // Check for explicit endpoint (treat empty string as unset)
        if let Ok(endpoint) = std::env::var("MOMENTO_ENDPOINT")
            && !endpoint.is_empty()
        {
            // Add "cache." prefix if not already present
            let endpoint = if endpoint.starts_with("cache.") {
                endpoint
            } else {
                format!("cache.{}", endpoint)
            };
            return Ok(Self {
                token,
                endpoint,
                sni_host,
            });
        }

        // Check for region-based endpoint (treat empty string as unset)
        if let Ok(region) = std::env::var("MOMENTO_REGION")
            && !region.is_empty()
        {
            let endpoint = format!("cache.cell-4-{}-1.prod.a.momentohq.com", region);
            return Ok(Self {
                token,
                endpoint,
                sni_host,
            });
        }

        // Try to extract from token (legacy tokens)
        let mut cred = Self::from_token(token)?;
        cred.sni_host = sni_host;
        Ok(cred)
    }

    /// Set the SNI hostname for TLS connections.
    pub fn with_sni_host(mut self, host: impl Into<String>) -> Self {
        self.sni_host = Some(host.into());
        self
    }

    /// Get the API token.
    pub fn token(&self) -> &str {
        &self.token
    }

    /// Get the cache endpoint.
    pub fn endpoint(&self) -> &str {
        &self.endpoint
    }

    /// Get the host portion of the endpoint.
    pub fn host(&self) -> &str {
        self.endpoint.split(':').next().unwrap_or(&self.endpoint)
    }

    /// Get the TLS hostname for SNI.
    ///
    /// Returns the explicit SNI host if set, otherwise returns the endpoint host.
    pub fn tls_host(&self) -> &str {
        self.sni_host.as_deref().unwrap_or_else(|| self.host())
    }

    /// Get the port (defaults to 9004 for protosocket).
    pub fn port(&self) -> u16 {
        if let Some(port_str) = self.endpoint.split(':').nth(1)
            && let Ok(port) = port_str.parse()
        {
            return port;
        }
        DEFAULT_PORT
    }

    /// Extract endpoint from a Momento token.
    fn extract_endpoint(token: &str) -> Option<String> {
        let parts: Vec<&str> = token.split('.').collect();
        if parts.len() < 2 {
            return None;
        }

        let payload = parts[1];
        let decoded = Self::base64url_decode(payload)?;
        let json = String::from_utf8(decoded).ok()?;

        let base_endpoint = Self::extract_json_field(&json, "c")
            .or_else(|| Self::extract_json_field(&json, "endpoint"))
            .or_else(|| Self::extract_json_field(&json, "cp"))?;

        if base_endpoint.starts_with("cache.") {
            Some(base_endpoint)
        } else {
            Some(format!("cache.{}", base_endpoint))
        }
    }

    /// Debug: dump the JWT payload for inspection.
    pub fn debug_jwt_payload(token: &str) -> Option<String> {
        let parts: Vec<&str> = token.split('.').collect();
        if parts.len() < 2 {
            return None;
        }
        let payload = parts[1];
        let decoded = Self::base64url_decode(payload)?;
        String::from_utf8(decoded).ok()
    }

    /// Decode base64url (URL-safe base64 without padding).
    fn base64url_decode(input: &str) -> Option<Vec<u8>> {
        let mut s = input.replace('-', "+").replace('_', "/");
        match s.len() % 4 {
            2 => s.push_str("=="),
            3 => s.push('='),
            _ => {}
        }
        Self::base64_decode(&s)
    }

    /// Simple base64 decoder.
    fn base64_decode(input: &str) -> Option<Vec<u8>> {
        const ALPHABET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

        let mut output = Vec::with_capacity(input.len() * 3 / 4);
        let mut buffer = 0u32;
        let mut bits = 0;

        for c in input.bytes() {
            if c == b'=' {
                break;
            }

            let value = ALPHABET.iter().position(|&x| x == c)? as u32;
            buffer = (buffer << 6) | value;
            bits += 6;

            if bits >= 8 {
                bits -= 8;
                output.push((buffer >> bits) as u8);
                buffer &= (1 << bits) - 1;
            }
        }

        Some(output)
    }

    /// Extract a string field from JSON (minimal parsing).
    fn extract_json_field(json: &str, field: &str) -> Option<String> {
        let pattern = format!("\"{}\"", field);
        let start = json.find(&pattern)?;
        let rest = &json[start + pattern.len()..];

        let rest = rest.trim_start();
        let rest = rest.strip_prefix(':')?;
        let rest = rest.trim_start();

        let rest = rest.strip_prefix('"')?;
        let end = rest.find('"')?;

        Some(rest[..end].to_string())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serial_test::serial;

    #[test]
    fn test_credential_with_endpoint() {
        let cred = Credential::with_endpoint("my-token", "cache.example.com:9004");
        assert_eq!(cred.token(), "my-token");
        assert_eq!(cred.endpoint(), "cache.example.com:9004");
        assert_eq!(cred.host(), "cache.example.com");
        assert_eq!(cred.port(), 9004);
    }

    #[test]
    fn test_credential_default_port() {
        let cred = Credential::with_endpoint("token", "cache.example.com");
        assert_eq!(cred.port(), 9004);
    }

    #[test]
    fn test_credential_custom_port() {
        let cred = Credential::with_endpoint("token", "cache.example.com:8080");
        assert_eq!(cred.port(), 8080);
        assert_eq!(cred.host(), "cache.example.com");
    }

    #[test]
    fn test_credential_invalid_port() {
        let cred = Credential::with_endpoint("token", "cache.example.com:notaport");
        assert_eq!(cred.port(), 9004);
    }

    #[test]
    fn test_credential_tls_host_default() {
        let cred = Credential::with_endpoint("token", "cache.example.com");
        assert_eq!(cred.tls_host(), "cache.example.com");
    }

    #[test]
    fn test_credential_tls_host_explicit() {
        let cred =
            Credential::with_endpoint("token", "1.2.3.4:9004").with_sni_host("cache.example.com");
        assert_eq!(cred.tls_host(), "cache.example.com");
        assert_eq!(cred.host(), "1.2.3.4");
    }

    #[test]
    fn test_credential_from_token_simple() {
        let cred = Credential::from_token("simple-api-key").unwrap();
        assert_eq!(cred.token(), "simple-api-key");
        assert!(cred.endpoint().contains("momentohq.com"));
    }

    #[test]
    fn test_credential_from_token_with_jwt() {
        let payload = r#"{"c":"cell-test.example.com"}"#;
        let encoded_payload = base64url_encode(payload.as_bytes());
        let token = format!("header.{}.signature", encoded_payload);

        let cred = Credential::from_token(&token).unwrap();
        assert_eq!(cred.endpoint(), "cache.cell-test.example.com");
    }

    #[test]
    fn test_credential_from_token_with_cache_prefix() {
        let payload = r#"{"c":"cache.already-prefixed.example.com"}"#;
        let encoded_payload = base64url_encode(payload.as_bytes());
        let token = format!("header.{}.signature", encoded_payload);

        let cred = Credential::from_token(&token).unwrap();
        assert_eq!(cred.endpoint(), "cache.already-prefixed.example.com");
    }

    #[test]
    fn test_credential_from_token_with_endpoint_field() {
        let payload = r#"{"endpoint":"cell-endpoint.example.com"}"#;
        let encoded_payload = base64url_encode(payload.as_bytes());
        let token = format!("header.{}.signature", encoded_payload);

        let cred = Credential::from_token(&token).unwrap();
        assert_eq!(cred.endpoint(), "cache.cell-endpoint.example.com");
    }

    #[test]
    fn test_credential_clone() {
        let cred = Credential::with_endpoint("token", "endpoint.com");
        let cloned = cred.clone();
        assert_eq!(cloned.token(), cred.token());
        assert_eq!(cloned.endpoint(), cred.endpoint());
    }

    // Base64 decode tests

    #[test]
    fn test_base64_decode() {
        let decoded = Credential::base64_decode("aGVsbG8=").unwrap();
        assert_eq!(&decoded, b"hello");
    }

    #[test]
    fn test_base64_decode_no_padding() {
        let decoded = Credential::base64_decode("aGk").unwrap();
        assert_eq!(&decoded, b"hi");
    }

    #[test]
    fn test_base64_decode_empty() {
        let decoded = Credential::base64_decode("").unwrap();
        assert!(decoded.is_empty());
    }

    #[test]
    fn test_base64_decode_invalid_char() {
        let result = Credential::base64_decode("!!!!");
        assert!(result.is_none());
    }

    #[test]
    fn test_base64_decode_longer_string() {
        let decoded = Credential::base64_decode("SGVsbG8sIFdvcmxkIQ==").unwrap();
        assert_eq!(&decoded, b"Hello, World!");
    }

    // Base64url decode tests

    #[test]
    fn test_base64url_decode_with_url_chars() {
        let input = "dGVzdC1kYXRh";
        let decoded = Credential::base64url_decode(input);
        assert!(decoded.is_some());
    }

    #[test]
    fn test_base64url_decode_padding_2() {
        let decoded = Credential::base64url_decode("YQ").unwrap();
        assert_eq!(&decoded, b"a");
    }

    #[test]
    fn test_base64url_decode_padding_1() {
        let decoded = Credential::base64url_decode("YWI").unwrap();
        assert_eq!(&decoded, b"ab");
    }

    #[test]
    fn test_base64url_decode_no_padding_needed() {
        let decoded = Credential::base64url_decode("YWJj").unwrap();
        assert_eq!(&decoded, b"abc");
    }

    // JSON field extraction tests

    #[test]
    fn test_extract_json_field() {
        let json = r#"{"c":"cache.example.com","other":"value"}"#;
        let endpoint = Credential::extract_json_field(json, "c");
        assert_eq!(endpoint, Some("cache.example.com".to_string()));
    }

    #[test]
    fn test_extract_json_field_with_spaces() {
        let json = r#"{ "c" : "cache.example.com" }"#;
        let endpoint = Credential::extract_json_field(json, "c");
        assert_eq!(endpoint, Some("cache.example.com".to_string()));
    }

    #[test]
    fn test_extract_json_field_not_found() {
        let json = r#"{"other":"value"}"#;
        let result = Credential::extract_json_field(json, "c");
        assert!(result.is_none());
    }

    #[test]
    fn test_extract_json_field_empty_value() {
        let json = r#"{"c":""}"#;
        let result = Credential::extract_json_field(json, "c");
        assert_eq!(result, Some(String::new()));
    }

    // Extract endpoint tests

    #[test]
    fn test_extract_endpoint_invalid_jwt() {
        let result = Credential::extract_endpoint("not-a-jwt");
        assert!(result.is_none());
    }

    #[test]
    fn test_extract_endpoint_invalid_base64() {
        let result = Credential::extract_endpoint("header.!!!!.signature");
        assert!(result.is_none());
    }

    #[test]
    fn test_extract_endpoint_invalid_json() {
        let payload = base64url_encode(b"not json");
        let token = format!("header.{}.signature", payload);
        let result = Credential::extract_endpoint(&token);
        assert!(result.is_none());
    }

    // Debug JWT payload tests

    #[test]
    fn test_debug_jwt_payload() {
        let payload = r#"{"test":"value"}"#;
        let encoded = base64url_encode(payload.as_bytes());
        let token = format!("header.{}.signature", encoded);

        let result = Credential::debug_jwt_payload(&token);
        assert_eq!(result, Some(payload.to_string()));
    }

    #[test]
    fn test_debug_jwt_payload_invalid_token() {
        let result = Credential::debug_jwt_payload("no-dots-here");
        assert!(result.is_none());
    }

    #[test]
    fn test_debug_jwt_payload_invalid_base64() {
        let result = Credential::debug_jwt_payload("header.!!!invalid!!!.sig");
        assert!(result.is_none());
    }

    // Helper function for tests
    fn base64url_encode(data: &[u8]) -> String {
        const ALPHABET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

        let mut result = String::new();
        let mut bits = 0u32;
        let mut num_bits = 0;

        for &byte in data {
            bits = (bits << 8) | byte as u32;
            num_bits += 8;

            while num_bits >= 6 {
                num_bits -= 6;
                let index = ((bits >> num_bits) & 0x3F) as usize;
                result.push(ALPHABET[index] as char);
            }
        }

        if num_bits > 0 {
            bits <<= 6 - num_bits;
            let index = (bits & 0x3F) as usize;
            result.push(ALPHABET[index] as char);
        }

        // Convert to base64url
        result.replace('+', "-").replace('/', "_")
    }

    #[test]
    #[serial]
    fn test_from_env_with_explicit_endpoint() {
        let orig_key = std::env::var("MOMENTO_API_KEY").ok();
        let orig_endpoint = std::env::var("MOMENTO_ENDPOINT").ok();
        let orig_region = std::env::var("MOMENTO_REGION").ok();

        // SAFETY: This test runs with --test-threads=1 to avoid data races
        unsafe {
            std::env::set_var("MOMENTO_API_KEY", "test-token");
            std::env::set_var("MOMENTO_ENDPOINT", "cell-test.example.com");
            std::env::remove_var("MOMENTO_REGION");
        }

        let cred = Credential::from_env().expect("from_env should succeed");
        assert_eq!(cred.token(), "test-token");
        assert_eq!(cred.host(), "cache.cell-test.example.com");

        // SAFETY: Restore original values
        unsafe {
            if let Some(val) = orig_key {
                std::env::set_var("MOMENTO_API_KEY", val);
            } else {
                std::env::remove_var("MOMENTO_API_KEY");
            }
            if let Some(val) = orig_endpoint {
                std::env::set_var("MOMENTO_ENDPOINT", val);
            } else {
                std::env::remove_var("MOMENTO_ENDPOINT");
            }
            if let Some(val) = orig_region {
                std::env::set_var("MOMENTO_REGION", val);
            }
        }
    }

    #[test]
    #[serial]
    fn test_from_env_with_cache_prefixed_endpoint() {
        let orig_key = std::env::var("MOMENTO_API_KEY").ok();
        let orig_endpoint = std::env::var("MOMENTO_ENDPOINT").ok();
        let orig_region = std::env::var("MOMENTO_REGION").ok();

        // SAFETY: This test runs with --test-threads=1 to avoid data races
        unsafe {
            std::env::set_var("MOMENTO_API_KEY", "test-token");
            std::env::set_var("MOMENTO_ENDPOINT", "cache.already-prefixed.example.com");
            std::env::remove_var("MOMENTO_REGION");
        }

        let cred = Credential::from_env().expect("from_env should succeed");
        assert_eq!(cred.token(), "test-token");
        assert_eq!(cred.host(), "cache.already-prefixed.example.com");

        // SAFETY: Restore original values
        unsafe {
            if let Some(val) = orig_key {
                std::env::set_var("MOMENTO_API_KEY", val);
            } else {
                std::env::remove_var("MOMENTO_API_KEY");
            }
            if let Some(val) = orig_endpoint {
                std::env::set_var("MOMENTO_ENDPOINT", val);
            } else {
                std::env::remove_var("MOMENTO_ENDPOINT");
            }
            if let Some(val) = orig_region {
                std::env::set_var("MOMENTO_REGION", val);
            }
        }
    }

    #[test]
    #[serial]
    fn test_from_env_with_region() {
        let orig_key = std::env::var("MOMENTO_API_KEY").ok();
        let orig_endpoint = std::env::var("MOMENTO_ENDPOINT").ok();
        let orig_region = std::env::var("MOMENTO_REGION").ok();

        // SAFETY: This test runs with --test-threads=1 to avoid data races
        unsafe {
            std::env::set_var("MOMENTO_API_KEY", "test-token");
            std::env::remove_var("MOMENTO_ENDPOINT");
            std::env::set_var("MOMENTO_REGION", "us-west-2");
        }

        let cred = Credential::from_env().expect("from_env should succeed");
        assert_eq!(cred.token(), "test-token");
        assert!(cred.host().contains("us-west-2"));

        // SAFETY: Restore original values
        unsafe {
            if let Some(val) = orig_key {
                std::env::set_var("MOMENTO_API_KEY", val);
            } else {
                std::env::remove_var("MOMENTO_API_KEY");
            }
            if let Some(val) = orig_endpoint {
                std::env::set_var("MOMENTO_ENDPOINT", val);
            } else {
                std::env::remove_var("MOMENTO_ENDPOINT");
            }
            if let Some(val) = orig_region {
                std::env::set_var("MOMENTO_REGION", val);
            } else {
                std::env::remove_var("MOMENTO_REGION");
            }
        }
    }

    #[test]
    #[serial]
    fn test_from_env_missing_token() {
        let orig_key = std::env::var("MOMENTO_API_KEY").ok();
        let orig_auth = std::env::var("MOMENTO_AUTH_TOKEN").ok();

        // SAFETY: This test runs with --test-threads=1 to avoid data races
        unsafe {
            std::env::remove_var("MOMENTO_API_KEY");
            std::env::remove_var("MOMENTO_AUTH_TOKEN");
        }

        let result = Credential::from_env();
        assert!(result.is_err());

        // SAFETY: Restore original values
        unsafe {
            if let Some(val) = orig_key {
                std::env::set_var("MOMENTO_API_KEY", val);
            }
            if let Some(val) = orig_auth {
                std::env::set_var("MOMENTO_AUTH_TOKEN", val);
            }
        }
    }
}