Skip to main content

alien_client_core/
runtime_credentials.rs

1//! Refresh bounded runtime credentials without retaining expired authority.
2
3#[cfg(unix)]
4use std::os::unix::fs::{MetadataExt, PermissionsExt};
5use std::{fmt, path::PathBuf, sync::Arc, time::Duration};
6
7use alien_error::AlienError;
8use async_trait::async_trait;
9use chrono::{DateTime, Utc};
10use reqwest::{redirect::Policy, Client, Url};
11use serde::{Deserialize, Serialize};
12use serde_json::{json, Value};
13use tokio::{io::AsyncReadExt, sync::Mutex};
14
15use crate::{ErrorData, Result};
16
17/// How far apart this process's clock and the manager's clock may be when we
18/// check the lifetime of a credential the manager issued. Without it, a
19/// manager clock one second ahead makes a one hour credential look too long
20/// and locks the caller out. AWS uses the same 5 minute tolerance for SigV4.
21pub const ALLOWED_CLOCK_SKEW: chrono::TimeDelta = chrono::TimeDelta::minutes(5);
22
23/// Longest lifetime of a registry pull credential. Pods pull images with it
24/// from a Kubernetes Secret that the operator refreshes, so it may last as
25/// long as the operator's daily key. Pods then still start while the operator
26/// is down. The manager caps it at the key's own expiry.
27pub const REGISTRY_PULL_LIFETIME: chrono::TimeDelta = chrono::TimeDelta::hours(24);
28
29/// Every purpose other than registry pull lasts at most one hour.
30fn max_lifetime(purpose: &Value) -> chrono::TimeDelta {
31    if purpose.get("type").and_then(Value::as_str) == Some("registryPull") {
32        REGISTRY_PULL_LIFETIME
33    } else {
34        chrono::TimeDelta::hours(1)
35    }
36}
37
38/// Supplies fresh identity evidence. Implementations must not return a runtime
39/// capability as proof or fall back to an unrelated stored credential.
40#[async_trait]
41pub trait RuntimeProofProvider: Send + Sync {
42    async fn proof(&self, audience: &str) -> Result<Value>;
43}
44
45/// A bounded credential suitable for an atomically replaced private file.
46/// It deliberately has no Debug implementation.
47#[derive(Clone, Deserialize, Serialize)]
48#[serde(rename_all = "camelCase", deny_unknown_fields)]
49pub struct RuntimeCapability {
50    pub token: String,
51    pub expires_at: DateTime<Utc>,
52}
53
54enum CredentialSource {
55    Exchange(Arc<dyn RuntimeProofProvider>),
56    File(PathBuf),
57}
58
59#[derive(Default)]
60struct CredentialState {
61    cached: Option<CachedCredential>,
62    rejected: Option<String>,
63}
64
65struct CachedCredential {
66    credential: RuntimeCapability,
67    refresh_at: DateTime<Utc>,
68}
69
70/// Nonsecret configuration supplied by deployment provisioning.
71pub struct RuntimeCredentialConfig {
72    pub manager_url: String,
73    /// Exact configured manager identity, independent of its transport URL.
74    pub audience: String,
75    pub deployment_id: String,
76    pub purpose: Value,
77}
78
79/// A single-purpose credential source shared by concurrent requests.
80/// Refresh errors propagate; expired credentials are never returned.
81pub struct RuntimeCredentials {
82    client: Client,
83    endpoint: Url,
84    receiver: Url,
85    audience: String,
86    deployment_id: String,
87    purpose: Value,
88    source: CredentialSource,
89    clock: Arc<dyn Fn() -> DateTime<Utc> + Send + Sync>,
90    state: Mutex<CredentialState>,
91}
92
93impl RuntimeCredentials {
94    /// The deployment these credentials are for.
95    pub fn deployment_id(&self) -> &str {
96        &self.deployment_id
97    }
98
99    /// The purpose these credentials are for, as sent to the manager.
100    pub fn purpose(&self) -> &Value {
101        &self.purpose
102    }
103}
104
105impl fmt::Debug for RuntimeCredentials {
106    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
107        formatter
108            .debug_struct("RuntimeCredentials")
109            .finish_non_exhaustive()
110    }
111}
112
113impl RuntimeCredentials {
114    /// Connect to a manager using a fixed deployment and purpose. The manager
115    /// independently verifies membership and never trusts these selectors.
116    pub fn new(
117        config: RuntimeCredentialConfig,
118        proof: Arc<dyn RuntimeProofProvider>,
119    ) -> Result<Self> {
120        Self::with_source(config, CredentialSource::Exchange(proof))
121    }
122
123    /// Exchange proof at a configured manager path for a credential restricted
124    /// to an independently configured receiver. Neither destination is selected
125    /// by the proof provider or an exchange response.
126    pub fn new_for_receiver(
127        config: RuntimeCredentialConfig,
128        proof: Arc<dyn RuntimeProofProvider>,
129        exchange_path: &str,
130        receiver_url: &str,
131    ) -> Result<Self> {
132        let mut credentials = Self::new(config, proof)?;
133        if !exchange_path.starts_with('/') || exchange_path.starts_with("//") {
134            return Err(invalid("exchange path must be an absolute manager path"));
135        }
136        let endpoint = credentials
137            .endpoint
138            .join(exchange_path)
139            .map_err(|_| invalid("invalid exchange path"))?;
140        if endpoint.origin() != credentials.endpoint.origin()
141            || endpoint.path() != exchange_path
142            || endpoint.query().is_some()
143            || endpoint.fragment().is_some()
144            || !endpoint.username().is_empty()
145            || endpoint.password().is_some()
146        {
147            return Err(invalid(
148                "exchange path must remain on the configured manager",
149            ));
150        }
151        credentials.receiver = secure_endpoint(receiver_url)?;
152        credentials.endpoint = endpoint;
153        Ok(credentials)
154    }
155
156    /// Read an externally refreshed capability on each request. This source
157    /// never exchanges or renews the supplied credential.
158    pub fn from_capability_file(config: RuntimeCredentialConfig, path: PathBuf) -> Result<Self> {
159        Self::with_source(config, CredentialSource::File(path))
160    }
161
162    fn with_source(config: RuntimeCredentialConfig, source: CredentialSource) -> Result<Self> {
163        let manager_url = &config.manager_url;
164        let base = secure_endpoint(manager_url)?;
165        let endpoint = Url::parse(&format!(
166            "{}/v1/identity/exchange",
167            manager_url.trim_end_matches('/')
168        ))
169        .map_err(|_| invalid("invalid exchange URL"))?;
170        let client = Client::builder()
171            .redirect(Policy::none())
172            .http1_only()
173            .timeout(Duration::from_secs(15))
174            .build()
175            .map_err(|_| invalid("cannot initialize credential transport"))?;
176        Ok(Self {
177            client,
178            endpoint,
179            receiver: base,
180            audience: config.audience,
181            deployment_id: config.deployment_id,
182            purpose: config.purpose,
183            source,
184            clock: Arc::new(Utc::now),
185            state: Mutex::new(CredentialState::default()),
186        })
187    }
188
189    /// Credentials may only be attached to their configured receiver origin.
190    pub fn allows_endpoint(&self, endpoint: &str) -> bool {
191        Url::parse(endpoint)
192            .map(|url| {
193                url.username().is_empty()
194                    && url.password().is_none()
195                    && url.origin() == self.receiver.origin()
196            })
197            .unwrap_or(false)
198    }
199
200    /// Inject a clock for precise expiry validation.
201    pub fn with_clock(mut self, clock: Arc<dyn Fn() -> DateTime<Utc> + Send + Sync>) -> Self {
202        self.clock = clock;
203        self
204    }
205
206    /// Return a current credential, refreshing halfway through its remaining
207    /// lifetime. A scheduled refresh failure may retain unexpired authority.
208    pub async fn token(&self) -> Result<String> {
209        Ok(self.capability().await?.token)
210    }
211
212    /// Invalidate only the credential rejected by a receiver. A delayed response
213    /// for an older token must not evict a newer cached credential or replace
214    /// the rejection recorded while that newer generation is being refreshed.
215    pub async fn reject(&self, rejected: &str) {
216        let mut state = self.state.lock().await;
217        if state
218            .cached
219            .as_ref()
220            .is_some_and(|current| current.credential.token == rejected)
221        {
222            state.cached = None;
223            state.rejected = Some(rejected.to_owned());
224        }
225    }
226
227    /// Retry callers share a replacement when they rejected the same token.
228    pub async fn token_after_rejection(&self, rejected: &str) -> Result<String> {
229        self.reject(rejected).await;
230        self.token().await
231    }
232
233    /// The deadline accompanies tokens written to child credential files.
234    pub async fn capability(&self) -> Result<RuntimeCapability> {
235        let mut state = self.state.lock().await;
236        if let CredentialSource::File(path) = &self.source {
237            let file = tokio::fs::File::open(path)
238                .await
239                .map_err(|_| invalid("cannot open capability file"))?;
240            let metadata = file
241                .metadata()
242                .await
243                .map_err(|_| invalid("cannot inspect capability file"))?;
244            if !metadata.is_file() || metadata.len() > 16 * 1024 {
245                return Err(invalid("invalid capability file"));
246            }
247            #[cfg(unix)]
248            {
249                let mode = metadata.permissions().mode();
250                // Projected Secrets are root-owned and readable by the pod's
251                // fsGroup. That group shares the credential trust boundary.
252                if mode & 0o037 != 0 || (mode & 0o040 != 0 && metadata.uid() != 0) {
253                    return Err(invalid(
254                        "capability file must be private or root-owned group-read-only",
255                    ));
256                }
257            }
258            let mut bytes = Vec::new();
259            file.take(16 * 1024 + 1)
260                .read_to_end(&mut bytes)
261                .await
262                .map_err(|_| invalid("cannot read capability file"))?;
263            if bytes.len() > 16 * 1024 {
264                return Err(invalid("capability file exceeds limit"));
265            }
266            let capability: RuntimeCapability =
267                serde_json::from_slice(&bytes).map_err(|_| invalid("invalid capability file"))?;
268            let remaining = capability.expires_at - (self.clock)();
269            if !capability.token.starts_with("ax_cap_v1_")
270                || remaining <= chrono::Duration::zero()
271                || remaining > max_lifetime(&self.purpose) + ALLOWED_CLOCK_SKEW
272            {
273                return Err(invalid("invalid capability file lifetime"));
274            }
275            if state.rejected.as_deref() == Some(capability.token.as_str()) {
276                return Err(invalid(
277                    "capability file still contains a rejected credential",
278                ));
279            }
280            state.cached = Some(CachedCredential {
281                credential: capability.clone(),
282                refresh_at: capability.expires_at,
283            });
284            return Ok(capability);
285        }
286        let now = (self.clock)();
287        if let Some(current) = state.cached.as_ref() {
288            if now < current.refresh_at && now < current.credential.expires_at {
289                return Ok(current.credential.clone());
290            }
291        }
292        match self.exchange().await {
293            Ok(credential) => {
294                if state.rejected.as_deref() == Some(credential.token.as_str()) {
295                    return Err(invalid("identity exchange returned a rejected credential"));
296                }
297                let now = (self.clock)();
298                if now >= credential.expires_at {
299                    return Err(invalid("identity exchange credential expired before use"));
300                }
301                state.cached = Some(CachedCredential {
302                    refresh_at: now + (credential.expires_at - now) / 2,
303                    credential: credential.clone(),
304                });
305                Ok(credential)
306            }
307            Err(error) => {
308                // Only scheduled refresh can retain authority. Explicit rejection
309                // removed its exact cached token before reaching this branch.
310                if let Some(current) = state.cached.as_ref() {
311                    if (self.clock)() < current.credential.expires_at {
312                        return Ok(current.credential.clone());
313                    }
314                }
315                state.cached = None;
316                Err(error)
317            }
318        }
319    }
320
321    async fn exchange(&self) -> Result<RuntimeCapability> {
322        let CredentialSource::Exchange(proof) = &self.source else {
323            unreachable!()
324        };
325        let proof = proof.proof(&self.audience).await?;
326        let mut response = self.client.post(self.endpoint.clone())
327            .json(&json!({"deploymentId": self.deployment_id, "purpose": self.purpose, "proof": proof}))
328            .send().await.map_err(|_| invalid("identity exchange transport failed"))?;
329        if !response.status().is_success() {
330            return Err(invalid(&format!(
331                "identity exchange returned HTTP {}",
332                response.status().as_u16()
333            )));
334        }
335        let mut bytes = Vec::new();
336        while let Some(chunk) = response
337            .chunk()
338            .await
339            .map_err(|_| invalid("cannot read identity exchange response"))?
340        {
341            if bytes.len() + chunk.len() > 16 * 1024 {
342                return Err(invalid("identity exchange response exceeds limit"));
343            }
344            bytes.extend_from_slice(&chunk);
345        }
346        // Never preserve provider/decoder errors which may echo credential data.
347        let credential: RuntimeCapability = serde_json::from_slice(&bytes)
348            .map_err(|_| invalid("invalid identity exchange response"))?;
349        let now = (self.clock)();
350        let remaining = credential.expires_at - now;
351        if credential.token.is_empty()
352            || remaining <= chrono::Duration::zero()
353            || remaining > max_lifetime(&self.purpose) + ALLOWED_CLOCK_SKEW
354        {
355            return Err(invalid(
356                "identity exchange returned an invalid credential lifetime",
357            ));
358        }
359        Ok(credential)
360    }
361}
362
363fn secure_endpoint(endpoint: &str) -> Result<Url> {
364    let url = Url::parse(endpoint).map_err(|_| invalid("invalid credential endpoint"))?;
365    if !url.username().is_empty()
366        || url.password().is_some()
367        || url.query().is_some()
368        || url.fragment().is_some()
369        || !(url.scheme() == "https"
370            || (url.scheme() == "http"
371                && matches!(url.host_str(), Some("localhost" | "127.0.0.1" | "[::1]"))))
372    {
373        return Err(invalid(
374            "credential endpoint must use HTTPS (HTTP is allowed only on loopback)",
375        ));
376    }
377    Ok(url)
378}
379
380fn invalid(message: &str) -> crate::Error {
381    AlienError::new(ErrorData::AuthenticationError {
382        message: message.to_owned(),
383    })
384}
385
386#[cfg(test)]
387mod tests {
388    use super::*;
389    use std::{
390        io::{Read, Write},
391        net::TcpListener,
392        sync::atomic::{AtomicI64, AtomicUsize, Ordering},
393        thread,
394    };
395
396    struct Proof(AtomicUsize);
397    #[async_trait]
398    impl RuntimeProofProvider for Proof {
399        async fn proof(&self, audience: &str) -> Result<Value> {
400            assert_eq!(audience, "manager-identity");
401            self.0.fetch_add(1, Ordering::SeqCst);
402            Ok(json!({"type":"session", "token":"fixture-proof"}))
403        }
404    }
405
406    fn fixture(
407        replies: Vec<(u16, Value)>,
408    ) -> (
409        RuntimeCredentials,
410        Arc<AtomicI64>,
411        Arc<Proof>,
412        thread::JoinHandle<()>,
413    ) {
414        fixture_for(replies, None, json!({"type":"telemetry"}))
415    }
416
417    fn fixture_for_receiver(
418        replies: Vec<(u16, Value)>,
419        receiver: Option<&str>,
420    ) -> (
421        RuntimeCredentials,
422        Arc<AtomicI64>,
423        Arc<Proof>,
424        thread::JoinHandle<()>,
425    ) {
426        fixture_for(replies, receiver, json!({"type":"telemetry"}))
427    }
428
429    fn fixture_for(
430        replies: Vec<(u16, Value)>,
431        receiver: Option<&str>,
432        purpose: Value,
433    ) -> (
434        RuntimeCredentials,
435        Arc<AtomicI64>,
436        Arc<Proof>,
437        thread::JoinHandle<()>,
438    ) {
439        let expected_purpose = purpose.clone();
440        let exchange_path = if receiver.is_some() {
441            "/v1/identity/scoped"
442        } else {
443            "/v1/identity/exchange"
444        };
445        let listener = TcpListener::bind("127.0.0.1:0").unwrap();
446        let url = format!("http://{}", listener.local_addr().unwrap());
447        let server = thread::spawn(move || {
448            for (status, body) in replies {
449                let (mut stream, _) = listener.accept().unwrap();
450                stream
451                    .set_read_timeout(Some(Duration::from_secs(5)))
452                    .unwrap();
453                let mut request = Vec::new();
454                loop {
455                    let mut chunk = [0; 4096];
456                    let count = stream.read(&mut chunk).unwrap();
457                    assert!(count > 0);
458                    request.extend_from_slice(&chunk[..count]);
459                    if let Some(header_end) =
460                        request.windows(4).position(|part| part == b"\r\n\r\n")
461                    {
462                        let headers = String::from_utf8_lossy(&request[..header_end]);
463                        let length: usize = headers
464                            .lines()
465                            .find_map(|line| {
466                                line.to_ascii_lowercase()
467                                    .strip_prefix("content-length: ")
468                                    .map(str::to_owned)
469                            })
470                            .unwrap()
471                            .parse()
472                            .unwrap();
473                        if request.len() >= header_end + 4 + length {
474                            assert!(headers.starts_with(&format!("POST {exchange_path} HTTP/1.1")));
475                            let request: Value =
476                                serde_json::from_slice(&request[header_end + 4..]).unwrap();
477                            assert_eq!(request["deploymentId"], "deployment-one");
478                            assert_eq!(request["purpose"], expected_purpose);
479                            assert_eq!(request["proof"]["token"], "fixture-proof");
480                            break;
481                        }
482                    }
483                }
484                let body = body.to_string();
485                write!(stream, "HTTP/1.1 {status} Test\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}", body.len()).unwrap();
486            }
487        });
488        let clock = Arc::new(AtomicI64::new(1_800_000_000));
489        let proof = Arc::new(Proof(AtomicUsize::new(0)));
490        let observed_clock = clock.clone();
491        let config = RuntimeCredentialConfig {
492            manager_url: url,
493            audience: "manager-identity".into(),
494            deployment_id: "deployment-one".into(),
495            purpose,
496        };
497        let client = match receiver {
498            Some(receiver) => {
499                RuntimeCredentials::new_for_receiver(config, proof.clone(), exchange_path, receiver)
500            }
501            None => RuntimeCredentials::new(config, proof.clone()),
502        }
503        .unwrap()
504        .with_clock(Arc::new(move || {
505            DateTime::from_timestamp(observed_clock.load(Ordering::SeqCst), 0).unwrap()
506        }));
507        (client, clock, proof, server)
508    }
509
510    #[tokio::test]
511    async fn explicit_receiver_keeps_exchange_and_delivery_origins_separate() {
512        let receiver = TcpListener::bind("127.0.0.1:0").unwrap();
513        let receiver_url = format!("http://{}", receiver.local_addr().unwrap());
514        let unauthorized = TcpListener::bind("127.0.0.1:0").unwrap();
515        unauthorized.set_nonblocking(true).unwrap();
516        let wrong_url = format!("http://{}", unauthorized.local_addr().unwrap());
517        let (client, _, _, exchange) = fixture_for_receiver(
518            vec![(200, credential("scoped-child", 100))],
519            Some(&receiver_url),
520        );
521        assert!(!client.allows_endpoint(client.endpoint.as_str()));
522        assert!(!client.allows_endpoint(&wrong_url));
523        assert!(client.allows_endpoint(&receiver_url));
524        let delivery = thread::spawn(move || {
525            let (mut stream, _) = receiver.accept().unwrap();
526            stream
527                .set_read_timeout(Some(Duration::from_secs(5)))
528                .unwrap();
529            let mut bytes = [0; 4096];
530            let count = stream.read(&mut bytes).unwrap();
531            let request = String::from_utf8_lossy(&bytes[..count]);
532            assert!(request
533                .to_ascii_lowercase()
534                .contains("authorization: bearer scoped-child"));
535            write!(
536                stream,
537                "HTTP/1.1 204 No Content\r\nContent-Length: 0\r\nConnection: close\r\n\r\n"
538            )
539            .unwrap();
540        });
541        let token = client.token().await.unwrap();
542        let response = Client::builder()
543            .redirect(Policy::none())
544            .build()
545            .unwrap()
546            .get(&receiver_url)
547            .bearer_auth(token)
548            .send()
549            .await
550            .unwrap();
551        assert_eq!(response.status(), 204);
552        assert_eq!(
553            unauthorized.accept().unwrap_err().kind(),
554            std::io::ErrorKind::WouldBlock
555        );
556        exchange.join().unwrap();
557        delivery.join().unwrap();
558    }
559
560    #[tokio::test]
561    async fn explicit_exchange_refuses_redirect_to_another_listener() {
562        let destination = TcpListener::bind("127.0.0.1:0").unwrap();
563        destination.set_nonblocking(true).unwrap();
564        let destination_url = format!("http://{}", destination.local_addr().unwrap());
565        let manager = TcpListener::bind("127.0.0.1:0").unwrap();
566        let manager_url = format!("http://{}", manager.local_addr().unwrap());
567        let redirect = destination_url.clone();
568        let server = thread::spawn(move || {
569            let (mut stream, _) = manager.accept().unwrap();
570            stream
571                .set_read_timeout(Some(Duration::from_secs(5)))
572                .unwrap();
573            let mut bytes = [0; 4096];
574            assert!(stream.read(&mut bytes).unwrap() > 0);
575            write!(stream, "HTTP/1.1 307 Temporary Redirect\r\nLocation: {redirect}\r\nContent-Length: 0\r\nConnection: close\r\n\r\n").unwrap();
576        });
577        let client = RuntimeCredentials::new_for_receiver(
578            RuntimeCredentialConfig {
579                manager_url,
580                audience: "manager-identity".into(),
581                deployment_id: "deployment-one".into(),
582                purpose: json!({"type":"telemetry"}),
583            },
584            Arc::new(Proof(AtomicUsize::new(0))),
585            "/v1/identity/scoped",
586            &destination_url,
587        )
588        .unwrap();
589        assert!(client.token().await.is_err());
590        server.join().unwrap();
591        assert_eq!(
592            destination.accept().unwrap_err().kind(),
593            std::io::ErrorKind::WouldBlock
594        );
595    }
596
597    #[test]
598    fn explicit_destinations_refuse_ambiguous_or_insecure_configuration() {
599        for (path, receiver) in [
600            ("//other.example/exchange", "https://receiver.example"),
601            ("https://other.example/exchange", "https://receiver.example"),
602            ("/exchange?next=other", "https://receiver.example"),
603            ("/exchange#other", "https://receiver.example"),
604            ("/a/../exchange", "https://receiver.example"),
605            ("/exchange", "http://receiver.example"),
606            ("/exchange", "https://user:pass@receiver.example"),
607            ("/exchange", "https://receiver.example?secret=value"),
608        ] {
609            let result = RuntimeCredentials::new_for_receiver(
610                RuntimeCredentialConfig {
611                    manager_url: "https://manager.example".into(),
612                    audience: "manager-identity".into(),
613                    deployment_id: "deployment-one".into(),
614                    purpose: json!({"type":"telemetry"}),
615                },
616                Arc::new(Proof(AtomicUsize::new(0))),
617                path,
618                receiver,
619            );
620            assert!(result.is_err(), "unexpected accepted destination");
621        }
622    }
623
624    fn credential(token: &str, seconds: i64) -> Value {
625        json!({"token": token, "expiresAt": DateTime::from_timestamp(1_800_000_000 + seconds, 0).unwrap()})
626    }
627
628    #[tokio::test]
629    async fn concurrent_requests_share_refresh_and_refresh_before_expiry() {
630        let (client, clock, proof, server) = fixture(vec![
631            (200, credential("first", 100)),
632            (200, credential("second", 200)),
633        ]);
634        let (one, two) = tokio::join!(client.token(), client.token());
635        assert_eq!(one.unwrap(), "first");
636        assert_eq!(two.unwrap(), "first");
637        assert_eq!(proof.0.load(Ordering::SeqCst), 1);
638        clock.store(1_800_000_049, Ordering::SeqCst);
639        assert_eq!(client.token().await.unwrap(), "first");
640        clock.store(1_800_000_050, Ordering::SeqCst);
641        assert_eq!(client.token().await.unwrap(), "second");
642        assert_eq!(proof.0.load(Ordering::SeqCst), 2);
643        server.join().unwrap();
644    }
645
646    #[tokio::test]
647    async fn early_refresh_outage_retains_only_unexpired_nonrejected_token() {
648        let (client, clock, _, server) = fixture(vec![
649            (200, credential("first", 100)),
650            (503, json!({})),
651            (503, json!({})),
652        ]);
653        assert_eq!(client.token().await.unwrap(), "first");
654        clock.store(1_800_000_050, Ordering::SeqCst);
655        assert_eq!(client.token().await.unwrap(), "first");
656        clock.store(1_800_000_100, Ordering::SeqCst);
657        assert!(client.token().await.is_err());
658        server.join().unwrap();
659    }
660
661    #[tokio::test]
662    async fn refresh_failure_rechecks_expiry_after_io() {
663        struct FailingProof(Arc<AtomicI64>);
664        #[async_trait]
665        impl RuntimeProofProvider for FailingProof {
666            async fn proof(&self, _: &str) -> Result<Value> {
667                self.0.store(1_800_000_100, Ordering::SeqCst);
668                Err(invalid("fixture provider unavailable"))
669            }
670        }
671        let (mut client, clock, _, server) = fixture(vec![(200, credential("first", 100))]);
672        assert_eq!(client.token().await.unwrap(), "first");
673        server.join().unwrap();
674        clock.store(1_800_000_050, Ordering::SeqCst);
675        client.source = CredentialSource::Exchange(Arc::new(FailingProof(clock)));
676        assert!(client.token().await.is_err());
677    }
678
679    #[tokio::test]
680    async fn concurrent_rejections_preserve_the_replacement() {
681        let (client, _, proof, server) = fixture(vec![
682            (200, credential("first", 100)),
683            (200, credential("second", 100)),
684        ]);
685        assert_eq!(client.token().await.unwrap(), "first");
686        let (one, two) = tokio::join!(
687            client.token_after_rejection("first"),
688            client.token_after_rejection("first")
689        );
690        assert_eq!(one.unwrap(), "second");
691        assert_eq!(two.unwrap(), "second");
692        assert_eq!(proof.0.load(Ordering::SeqCst), 2);
693        client.reject("first").await;
694        assert_eq!(client.token().await.unwrap(), "second");
695        assert_eq!(proof.0.load(Ordering::SeqCst), 2);
696        server.join().unwrap();
697    }
698
699    #[tokio::test]
700    async fn stale_rejection_cannot_replace_newer_rejection_during_refresh() {
701        let (client, _, _, server) = fixture(vec![
702            (200, credential("first", 100)),
703            (200, credential("second", 100)),
704            (200, credential("second", 100)),
705            (200, credential("third", 100)),
706        ]);
707        assert_eq!(client.token().await.unwrap(), "first");
708        assert_eq!(
709            client.token_after_rejection("first").await.unwrap(),
710            "second"
711        );
712        client.reject("second").await;
713        client.reject("first").await;
714        assert!(
715            client.token().await.is_err(),
716            "late first rejection restored rejected second credential"
717        );
718        assert_eq!(client.token().await.unwrap(), "third");
719        server.join().unwrap();
720    }
721
722    #[cfg(unix)]
723    #[tokio::test]
724    async fn stale_rejection_cannot_restore_rejected_projected_file() {
725        let directory = tempfile::tempdir().unwrap();
726        let path = directory.path().join("credential");
727        let write = |token: &str| {
728            std::fs::write(
729                &path,
730                serde_json::to_vec(&RuntimeCapability {
731                    token: token.into(),
732                    expires_at: Utc::now() + chrono::Duration::minutes(30),
733                })
734                .unwrap(),
735            )
736            .unwrap();
737            std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o600)).unwrap();
738        };
739        let client = RuntimeCredentials::from_capability_file(
740            RuntimeCredentialConfig {
741                manager_url: "https://manager.example".into(),
742                audience: "manager-one".into(),
743                deployment_id: "deployment-one".into(),
744                purpose: json!({"type":"telemetry"}),
745            },
746            path.clone(),
747        )
748        .unwrap();
749        write("ax_cap_v1_first");
750        assert_eq!(client.token().await.unwrap(), "ax_cap_v1_first");
751        client.reject("ax_cap_v1_first").await;
752        write("ax_cap_v1_second");
753        assert_eq!(client.token().await.unwrap(), "ax_cap_v1_second");
754        client.reject("ax_cap_v1_second").await;
755        client.reject("ax_cap_v1_first").await;
756        assert!(
757            client.token().await.is_err(),
758            "late first rejection restored rejected file bytes"
759        );
760        write("ax_cap_v1_third");
761        assert_eq!(client.token().await.unwrap(), "ax_cap_v1_third");
762    }
763
764    #[tokio::test]
765    async fn rejected_refresh_does_not_restore_cached_or_expired_authority() {
766        let (client, _, _, server) = fixture(vec![
767            (200, credential("first", 100)),
768            (401, json!({"secret":"must-not-appear"})),
769            (200, credential("replacement", 200)),
770        ]);
771        assert_eq!(client.token().await.unwrap(), "first");
772        let error = client.token_after_rejection("first").await.unwrap_err();
773        assert!(!format!("{error:?}").contains("must-not-appear"));
774        assert_eq!(client.token().await.unwrap(), "replacement");
775        server.join().unwrap();
776    }
777
778    #[tokio::test]
779    async fn exchange_cannot_restore_the_rejected_token() {
780        let (client, _, _, server) = fixture(vec![
781            (200, credential("first", 100)),
782            (200, credential("first", 100)),
783            (200, credential("replacement", 100)),
784        ]);
785        assert_eq!(client.token().await.unwrap(), "first");
786        assert!(client.token_after_rejection("first").await.is_err());
787        assert_eq!(client.token().await.unwrap(), "replacement");
788        server.join().unwrap();
789    }
790
791    #[tokio::test]
792    async fn refuses_expired_and_overlong_responses() {
793        let (client, _, _, server) = fixture(vec![
794            (200, credential("expired", 0)),
795            (200, credential("overlong", 3600 + 301)),
796        ]);
797        assert!(client.token().await.is_err());
798        assert!(client.token().await.is_err());
799        server.join().unwrap();
800    }
801
802    #[tokio::test]
803    async fn credential_from_a_manager_clock_two_minutes_ahead_is_accepted() {
804        // The manager issued a one hour credential by its own clock, which
805        // runs two minutes ahead of ours.
806        let (client, _, _, server) = fixture(vec![(200, credential("ahead", 3600 + 120))]);
807        assert_eq!(client.token().await.unwrap(), "ahead");
808        server.join().unwrap();
809    }
810    /// The operator writes the pull credential into a Secret that pods read
811    /// while the operator may be down. It accepts one that lasts as long as
812    /// the daily key, and still refuses longer ones or long credentials for
813    /// any other purpose.
814    #[tokio::test]
815    async fn registry_pull_credential_may_last_as_long_as_the_daily_key() {
816        let day = 24 * 3600;
817        let (client, _, _, server) = fixture_for(
818            vec![
819                (200, credential("pull-day", day)),
820                (200, credential("pull-overlong", day + 301)),
821            ],
822            None,
823            json!({"type":"registryPull"}),
824        );
825        assert_eq!(client.token().await.unwrap(), "pull-day");
826        client.reject("pull-day").await;
827        assert!(client.token().await.is_err());
828        server.join().unwrap();
829
830        let (client, _, _, server) = fixture(vec![(200, credential("telemetry-day", day))]);
831        assert!(client.token().await.is_err());
832        server.join().unwrap();
833    }
834
835    #[cfg(unix)]
836    #[tokio::test]
837    async fn capability_file_reopens_replacement_and_refuses_exact_expiry() {
838        let directory = tempfile::tempdir().unwrap();
839        let path = directory.path().join("telemetry");
840        let clock = Arc::new(std::sync::atomic::AtomicI64::new(1_800_000_000));
841        let write = |name: &str, token: &str, expires: i64| {
842            let file = directory.path().join(name);
843            let capability = RuntimeCapability {
844                token: token.into(),
845                expires_at: DateTime::from_timestamp(expires, 0).unwrap(),
846            };
847            std::fs::write(&file, serde_json::to_vec(&capability).unwrap()).unwrap();
848            std::fs::set_permissions(&file, std::fs::Permissions::from_mode(0o600)).unwrap();
849            file
850        };
851        write("telemetry", "legacy-deployment-token", 1_800_000_100);
852        let observed = clock.clone();
853        let credentials = RuntimeCredentials::from_capability_file(
854            RuntimeCredentialConfig {
855                manager_url: "https://manager.example".into(),
856                audience: "manager-one".into(),
857                deployment_id: "deployment-one".into(),
858                purpose: json!({"type":"telemetry"}),
859            },
860            path.clone(),
861        )
862        .unwrap()
863        .with_clock(Arc::new(move || {
864            DateTime::from_timestamp(observed.load(std::sync::atomic::Ordering::SeqCst), 0).unwrap()
865        }));
866        assert!(
867            credentials.token().await.is_err(),
868            "legacy token must not enter capability-file mode"
869        );
870        write("telemetry", "ax_cap_v1_first", 1_800_000_100);
871        assert_eq!(credentials.token().await.unwrap(), "ax_cap_v1_first");
872        credentials.reject("ax_cap_v1_first").await;
873        assert!(credentials.token().await.is_err());
874        clock.store(1_800_000_100, std::sync::atomic::Ordering::SeqCst);
875        assert!(credentials.token().await.is_err());
876        let next = write("next", "ax_cap_v1_second", 1_800_000_200);
877        std::fs::rename(next, &path).unwrap();
878        assert_eq!(credentials.token().await.unwrap(), "ax_cap_v1_second");
879        credentials.reject("ax_cap_v1_first").await;
880        assert_eq!(credentials.token().await.unwrap(), "ax_cap_v1_second");
881        for mode in [0o644, 0o620, 0o610, 0o601] {
882            std::fs::set_permissions(&path, std::fs::Permissions::from_mode(mode)).unwrap();
883            assert!(credentials.token().await.is_err());
884        }
885        if std::fs::metadata(&path).unwrap().uid() != 0 {
886            std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o440)).unwrap();
887            assert!(credentials.token().await.is_err());
888        }
889        std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o600)).unwrap();
890        assert_eq!(credentials.token().await.unwrap(), "ax_cap_v1_second");
891        std::fs::remove_file(&path).unwrap();
892        assert!(credentials.token().await.is_err());
893    }
894}