uu-objstore-r 0.4.1

Object stores with generation preconditions for immutable-snapshot data (local directory, in-memory with fault injection, Google Cloud Storage), fenced single-writer publishing, and graph-r store mirroring
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
// Author: spicyzhug@gmail.com
// Copyright: lituus-io, all rights reserved
//
//! OAuth access tokens for Google APIs (feature `gcs`).
//!
//! [`Adc::discover`] follows Application Default Credentials the way `gcloud`
//! does for the cases that need no private-key signing: the metadata server on
//! Cloud Run / GCE / GKE, and user credentials from
//! `gcloud auth application-default login`. Service-account key files are not
//! supported on purpose (use workload identity or impersonation instead of
//! long-lived keys). Applications with their own credential stack implement
//! [`TokenProvider`] directly.
//!
//! Tokens are cached until a minute before they expire and are never printed:
//! [`AccessToken`]'s `Debug` is redacted.
//!
//! A process that must act only as its caller passes the caller's token with
//! [`Adc::from_access_token`], or discovers with [`MetadataPolicy::Forbid`],
//! under which the metadata server is never contacted: no user credential
//! means [`Error::NoCredentials`], not the instance's own identity.

use crate::{Error, Result};
use graph_r::fsio::now_ms;
use std::future::Future;
use std::sync::Mutex;
use std::time::Duration;

const METADATA_HOST: &str = "metadata.google.internal";
const METADATA_TOKEN_PATH: &str = "/computeMetadata/v1/instance/service-accounts/default/token";
const TOKEN_URI: &str = "https://oauth2.googleapis.com/token";
/// Refresh this long before expiry.
const SKEW_MS: u64 = 60_000;

/// A bearer token.
#[derive(Clone)]
pub struct AccessToken {
    secret: String,
    /// Expiry, Unix milliseconds (`u64::MAX`: never).
    pub expires_at_ms: u64,
}

impl AccessToken {
    /// A token that never expires (tests, emulators, externally managed tokens).
    #[must_use]
    pub fn new(secret: impl Into<String>, expires_at_ms: u64) -> Self {
        Self { secret: secret.into(), expires_at_ms }
    }

    /// The token text, for an `Authorization: Bearer` header.
    #[must_use]
    pub fn secret(&self) -> &str {
        &self.secret
    }

    fn fresh(&self, now: u64) -> bool {
        self.expires_at_ms > now.saturating_add(SKEW_MS)
    }
}

impl std::fmt::Debug for AccessToken {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("AccessToken")
            .field("secret", &"<redacted>")
            .field("expires_at_ms", &self.expires_at_ms)
            .finish()
    }
}

/// A source of access tokens.
pub trait TokenProvider {
    /// Future of [`token`](TokenProvider::token).
    type Token<'a>: Future<Output = Result<AccessToken>> + 'a
    where
        Self: 'a;

    /// A token valid for at least a minute (an empty secret sends no header).
    fn token(&self) -> Self::Token<'_>;

    /// The project billed for API quota (user credentials only).
    fn quota_project(&self) -> Option<&str> {
        None
    }
}

/// A fixed token.
#[derive(Debug, Clone)]
pub struct Static(AccessToken);

impl Static {
    /// A fixed bearer token.
    #[must_use]
    pub fn new(secret: impl Into<String>) -> Self {
        Self(AccessToken::new(secret, u64::MAX))
    }

    /// No token at all (emulators such as fake-gcs-server).
    #[must_use]
    pub fn none() -> Self {
        Self::new(String::new())
    }
}

impl TokenProvider for Static {
    type Token<'a> = std::future::Ready<Result<AccessToken>>;
    fn token(&self) -> Self::Token<'_> {
        std::future::ready(Ok(self.0.clone()))
    }
}

#[derive(Debug, Default)]
struct Cache(Mutex<Option<AccessToken>>);

impl Cache {
    fn get(&self, now: u64) -> Option<AccessToken> {
        self.0
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
            .as_ref()
            .filter(|t| t.fresh(now))
            .cloned()
    }
    fn put(&self, t: &AccessToken) {
        *self.0.lock().unwrap_or_else(std::sync::PoisonError::into_inner) = Some(t.clone());
    }
}

#[derive(serde::Deserialize)]
struct TokenResponse {
    access_token: String,
    expires_in: Option<u64>,
}

fn http_client() -> Result<reqwest::Client> {
    reqwest::Client::builder()
        .timeout(Duration::from_secs(10))
        .redirect(reqwest::redirect::Policy::none())
        .build()
        .map_err(|e| Error::unavailable(format!("http client: {e}"), false))
}

async fn parse_token(resp: reqwest::Response) -> Result<AccessToken> {
    let status = resp.status();
    if !status.is_success() {
        return Err(Error::Auth { message: format!("token endpoint answered {status}") });
    }
    let body =
        resp.bytes().await.map_err(|_| Error::unavailable("token response truncated", true))?;
    let t: TokenResponse = serde_json::from_slice(&body)
        .map_err(|_| Error::Auth { message: "malformed token response".into() })?;
    Ok(AccessToken::new(
        t.access_token,
        now_ms().saturating_add(t.expires_in.unwrap_or(300).saturating_mul(1000)),
    ))
}

/// The metadata server (Cloud Run, GCE, GKE workload identity).
#[derive(Debug)]
pub struct Metadata {
    http: reqwest::Client,
    url: String,
    cache: Cache,
}

impl Metadata {
    /// The default service account of the instance, at
    /// `metadata.google.internal` or the `host[:port]` in `GCE_METADATA_HOST`
    /// (as Google's client libraries honour it).
    pub fn new() -> Result<Self> {
        let host = std::env::var("GCE_METADATA_HOST").ok().filter(|h| !h.is_empty());
        Self::with_url(&format!(
            "http://{}{METADATA_TOKEN_PATH}",
            host.as_deref().unwrap_or(METADATA_HOST)
        ))
    }

    /// Another token URL (tests).
    pub fn with_url(url: &str) -> Result<Self> {
        Ok(Self { http: http_client()?, url: url.to_owned(), cache: Cache::default() })
    }
}

impl TokenProvider for Metadata {
    type Token<'a> = impl Future<Output = Result<AccessToken>> + 'a;
    fn token(&self) -> Self::Token<'_> {
        async move {
            if let Some(t) = self.cache.get(now_ms()) {
                return Ok(t);
            }
            let resp = self
                .http
                .get(&self.url)
                .header("Metadata-Flavor", "Google")
                .send()
                .await
                .map_err(|_| Error::unavailable("metadata server unreachable", true))?;
            let t = parse_token(resp).await?;
            self.cache.put(&t);
            Ok(t)
        }
    }
}

/// User credentials from `gcloud auth application-default login`.
pub struct GcloudUser {
    http: reqwest::Client,
    client_id: String,
    client_secret: String,
    refresh_token: String,
    quota_project: Option<String>,
    token_uri: String,
    cache: Cache,
}

impl std::fmt::Debug for GcloudUser {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("GcloudUser")
            .field("quota_project", &self.quota_project)
            .finish_non_exhaustive()
    }
}

#[derive(serde::Deserialize)]
struct UserFile {
    #[serde(rename = "type")]
    kind: String,
    client_id: Option<String>,
    client_secret: Option<String>,
    refresh_token: Option<String>,
    quota_project_id: Option<String>,
}

impl GcloudUser {
    /// Parse an `authorized_user` credentials file.
    pub fn from_json(json: &[u8]) -> Result<Self> {
        let f: UserFile = serde_json::from_slice(json)
            .map_err(|_| Error::Auth { message: "unreadable credentials file".into() })?;
        if f.kind != "authorized_user" {
            return Err(Error::Auth {
                message: format!("credentials of type {:?} are not supported", f.kind),
            });
        }
        let (Some(client_id), Some(client_secret), Some(refresh_token)) =
            (f.client_id, f.client_secret, f.refresh_token)
        else {
            return Err(Error::Auth { message: "incomplete user credentials".into() });
        };
        Ok(Self {
            http: http_client()?,
            client_id,
            client_secret,
            refresh_token,
            quota_project: f.quota_project_id,
            token_uri: TOKEN_URI.to_owned(),
            cache: Cache::default(),
        })
    }

    /// Another token endpoint (tests).
    #[must_use]
    pub fn with_token_uri(mut self, uri: &str) -> Self {
        uri.clone_into(&mut self.token_uri);
        self
    }
}

impl TokenProvider for GcloudUser {
    type Token<'a> = impl Future<Output = Result<AccessToken>> + 'a;
    fn token(&self) -> Self::Token<'_> {
        async move {
            if let Some(t) = self.cache.get(now_ms()) {
                return Ok(t);
            }
            let form = [
                ("grant_type", "refresh_token"),
                ("client_id", self.client_id.as_str()),
                ("client_secret", self.client_secret.as_str()),
                ("refresh_token", self.refresh_token.as_str()),
            ];
            let body = form
                .iter()
                .map(|(k, v)| format!("{k}={}", form_encode(v)))
                .collect::<Vec<_>>()
                .join("&");
            let resp = self
                .http
                .post(&self.token_uri)
                .header("content-type", "application/x-www-form-urlencoded")
                .body(body)
                .send()
                .await
                .map_err(|_| Error::unavailable("token endpoint unreachable", true))?;
            let t = parse_token(resp).await?;
            self.cache.put(&t);
            Ok(t)
        }
    }

    fn quota_project(&self) -> Option<&str> {
        self.quota_project.as_deref()
    }
}

fn form_encode(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    crate::key::encode_component(s, &mut out);
    out
}

/// Whether credential discovery may fall through to the metadata server.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum MetadataPolicy {
    /// After user credentials, use the instance's own identity (Cloud Run,
    /// GCE, GKE). The historical behaviour.
    #[default]
    Allow,
    /// Never contact the metadata server: without user credentials,
    /// discovery is [`Error::NoCredentials`].
    Forbid,
}

/// Application Default Credentials (no private-key signing).
#[derive(Debug)]
pub enum Adc {
    /// The instance metadata server.
    Metadata(Metadata),
    /// gcloud user credentials.
    User(GcloudUser),
    /// A fixed token.
    Static(Static),
}

impl Adc {
    /// `GOOGLE_APPLICATION_CREDENTIALS` (user credentials), then gcloud's
    /// well-known file, then the metadata server
    /// ([`discover_with`](Self::discover_with) under [`MetadataPolicy::Allow`]).
    pub fn discover() -> Result<Self> {
        Self::discover_with(MetadataPolicy::Allow)
    }

    /// Discovery as [`discover`](Self::discover), with the metadata server
    /// allowed or forbidden.
    pub fn discover_with(metadata: MetadataPolicy) -> Result<Self> {
        Self::discover_from(
            |name: &str| std::env::var_os(name),
            |p: &std::path::Path| std::fs::read(p).ok(),
            cfg!(windows),
            metadata,
        )
    }

    fn discover_from<F, R>(var: F, read: R, windows: bool, metadata: MetadataPolicy) -> Result<Self>
    where
        F: Fn(&str) -> Option<std::ffi::OsString>,
        R: Fn(&std::path::Path) -> Option<Vec<u8>>,
    {
        match user_credentials(var, read, windows)? {
            Some(user) => Ok(Adc::User(user)),
            None => match metadata {
                MetadataPolicy::Allow => Metadata::new().map(Adc::Metadata),
                MetadataPolicy::Forbid => Err(Error::NoCredentials {
                    reason: "no user credentials and the metadata server is forbidden",
                }),
            },
        }
    }

    /// A caller-supplied access token, used as is and never refreshed
    /// ([`Adc::Static`]). An empty or blank token is
    /// [`Error::NoCredentials`]: it would send unauthenticated requests.
    pub fn from_access_token(secret: impl Into<String>) -> Result<Self> {
        let secret = secret.into();
        if secret.trim().is_empty() {
            return Err(Error::NoCredentials { reason: "empty access token" });
        }
        Ok(Adc::Static(Static::new(secret)))
    }
}

/// User credentials: the file named by `GOOGLE_APPLICATION_CREDENTIALS`, else
/// gcloud's well-known file; `None` when neither exists. `var` and `read`
/// are the environment and the file system, passed in for tests.
fn user_credentials<F, R>(var: F, read: R, windows: bool) -> Result<Option<GcloudUser>>
where
    F: Fn(&str) -> Option<std::ffi::OsString>,
    R: Fn(&std::path::Path) -> Option<Vec<u8>>,
{
    if let Some(path) = var("GOOGLE_APPLICATION_CREDENTIALS") {
        let json = read(std::path::Path::new(&path))
            .ok_or_else(|| Error::Auth { message: "unreadable credentials file".into() })?;
        return GcloudUser::from_json(&json).map(Some);
    }
    if let Some(dir) = gcloud_config_dir(&var, windows) {
        if let Some(json) = read(&dir.join("application_default_credentials.json")) {
            return GcloudUser::from_json(&json).map(Some);
        }
    }
    Ok(None)
}

/// gcloud's configuration directory, where `gcloud auth
/// application-default login` writes the well-known credentials file:
/// `CLOUDSDK_CONFIG` when set, else `%APPDATA%\gcloud` on Windows and
/// `$HOME/.config/gcloud` elsewhere.
///
/// Windows never consults `HOME`: OpenSSH, Git Bash and MSYS set it to the
/// profile directory, and `%HOME%\gcloud` is not where gcloud writes, so a
/// HOME-first lookup missed the file and fell through to the metadata server --
/// on a GCE VM that silently authenticated as the VM's own service account.
/// `var` is the environment, passed in so the rule is tested without touching
/// the process's own.
fn gcloud_config_dir<F>(var: F, windows: bool) -> Option<std::path::PathBuf>
where
    F: Fn(&str) -> Option<std::ffi::OsString>,
{
    let set = |name: &str| var(name).filter(|v| !v.is_empty());
    if let Some(dir) = set("CLOUDSDK_CONFIG") {
        return Some(std::path::PathBuf::from(dir));
    }
    if windows {
        return set("APPDATA").map(|a| std::path::PathBuf::from(a).join("gcloud"));
    }
    set("HOME").map(|h| std::path::PathBuf::from(h).join(".config").join("gcloud"))
}

impl TokenProvider for Adc {
    type Token<'a> = impl Future<Output = Result<AccessToken>> + 'a;
    fn token(&self) -> Self::Token<'_> {
        async move {
            match self {
                Adc::Metadata(m) => m.token().await,
                Adc::User(u) => u.token().await,
                Adc::Static(s) => s.token().await,
            }
        }
    }

    fn quota_project(&self) -> Option<&str> {
        match self {
            Adc::User(u) => u.quota_project(),
            _ => None,
        }
    }
}

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

    fn env<'a>(
        pairs: &'a [(&'a str, &'a str)],
    ) -> impl Fn(&str) -> Option<std::ffi::OsString> + 'a {
        move |name| pairs.iter().find(|(k, _)| *k == name).map(|(_, v)| std::ffi::OsString::from(v))
    }

    #[test]
    fn windows_reads_appdata_even_when_home_is_set() {
        let e = [("HOME", r"C:\Users\a"), ("APPDATA", r"C:\Users\a\AppData\Roaming")];
        assert_eq!(
            gcloud_config_dir(env(&e), true),
            Some(std::path::PathBuf::from(r"C:\Users\a\AppData\Roaming").join("gcloud"))
        );
    }

    #[test]
    fn posix_reads_home_dot_config() {
        let e = [("HOME", "/home/a"), ("APPDATA", "/ignored")];
        assert_eq!(
            gcloud_config_dir(env(&e), false),
            Some(std::path::PathBuf::from("/home/a").join(".config").join("gcloud"))
        );
    }

    #[test]
    fn cloudsdk_config_wins_on_every_os() {
        let e = [("CLOUDSDK_CONFIG", "/custom"), ("HOME", "/home/a"), ("APPDATA", "/appdata")];
        for windows in [true, false] {
            assert_eq!(
                gcloud_config_dir(env(&e), windows),
                Some(std::path::PathBuf::from("/custom"))
            );
        }
    }

    #[test]
    fn empty_values_count_as_unset() {
        let e = [("CLOUDSDK_CONFIG", ""), ("APPDATA", "")];
        assert_eq!(gcloud_config_dir(env(&e), true), None);
        assert_eq!(gcloud_config_dir(env(&[("HOME", "")]), false), None);
    }

    #[test]
    fn tokens_are_never_printed() {
        let t = AccessToken::new("ya29.secret", 1);
        assert!(!format!("{t:?}").contains("ya29"));
        let u = GcloudUser::from_json(
            br#"{"type":"authorized_user","client_id":"id","client_secret":"cs","refresh_token":"rt","quota_project_id":"p"}"#,
        )
        .unwrap();
        let dbg = format!("{u:?}");
        assert!(!dbg.contains("cs") && !dbg.contains("rt"), "{dbg}");
        assert_eq!(u.quota_project(), Some("p"));
        assert!(GcloudUser::from_json(br#"{"type":"service_account"}"#).is_err());
    }

    #[test]
    fn from_access_token_is_static_and_refuses_blank() {
        let a = Adc::from_access_token("ya29.caller").unwrap();
        assert!(matches!(&a, Adc::Static(_)));
        let t = futures::executor::block_on(a.token()).unwrap();
        assert_eq!((t.secret(), t.expires_at_ms), ("ya29.caller", u64::MAX));
        assert_eq!(a.quota_project(), None);
        for blank in ["", "  ", "\n"] {
            assert!(matches!(
                Adc::from_access_token(blank),
                Err(Error::NoCredentials { reason: "empty access token" })
            ));
        }
    }

    #[test]
    fn static_tokens_are_fixed_and_none_sends_nothing() {
        let t = futures::executor::block_on(Static::new("abc").token()).unwrap();
        assert_eq!(t.secret(), "abc");
        assert!(futures::executor::block_on(Static::none().token()).unwrap().secret().is_empty());
    }

    #[test]
    fn user_credentials_come_from_the_env_file_then_the_well_known_file() {
        let user = br#"{"type":"authorized_user","client_id":"i","client_secret":"s","refresh_token":"r","quota_project_id":"q"}"#;
        let files = |want: &'static str| {
            move |p: &std::path::Path| (p == std::path::Path::new(want)).then(|| user.to_vec())
        };
        let e = [("GOOGLE_APPLICATION_CREDENTIALS", "/k.json")];
        let u = user_credentials(env(&e), files("/k.json"), false).unwrap().unwrap();
        assert_eq!(u.quota_project(), Some("q"));
        assert!(user_credentials(env(&e), |_: &std::path::Path| None, false).is_err());
        let wk = "/h/.config/gcloud/application_default_credentials.json";
        assert!(user_credentials(env(&[("HOME", "/h")]), files(wk), false).unwrap().is_some());
        assert!(user_credentials(env(&[("HOME", "/h")]), files("/x"), false).unwrap().is_none());
    }

    #[test]
    fn forbid_never_builds_the_metadata_provider() {
        let none = |_: &std::path::Path| None;
        let e = [("HOME", "/nowhere")];
        assert!(matches!(
            Adc::discover_from(env(&e), none, false, MetadataPolicy::Forbid),
            Err(Error::NoCredentials { .. })
        ));
        // The same environment under Allow falls through, as it always has.
        assert!(matches!(
            Adc::discover_from(env(&e), none, false, MetadataPolicy::Allow),
            Ok(Adc::Metadata(_))
        ));
        assert_eq!(MetadataPolicy::default(), MetadataPolicy::Allow, "compatible default");
        // User credentials win under either policy.
        let user = br#"{"type":"authorized_user","client_id":"i","client_secret":"s","refresh_token":"r"}"#;
        let e = [("GOOGLE_APPLICATION_CREDENTIALS", "/k.json")];
        let read = |_: &std::path::Path| Some(user.to_vec());
        assert!(matches!(
            Adc::discover_from(env(&e), read, false, MetadataPolicy::Forbid),
            Ok(Adc::User(_))
        ));
    }

    #[test]
    fn freshness_has_skew() {
        let now = 1_000_000;
        assert!(!AccessToken::new("x", now + 30_000).fresh(now));
        assert!(AccessToken::new("x", now + 120_000).fresh(now));
    }
}