@@ -218,6 +218,102 @@ pub struct SecretResolver {
218218 denied_env_keys : HashSet < String > ,
219219 identity_bound_env_keys : HashSet < String > ,
220220 revision_fallback_allowed_revisions : HashMap < String , Arc < HashSet < u64 > > > ,
221+ /// Where to report that this resolver handed out a real secret.
222+ use_record : Option < ResolverUseRecord > ,
223+ }
224+
225+ /// Version that never matches a live credential version.
226+ ///
227+ /// Recorded for a value served from an older generation, which by definition
228+ /// is no longer current, so the connection closes at its next check.
229+ pub const STALE_CREDENTIAL_VERSION : u64 = 0 ;
230+
231+ /// Records which provider credentials a connection injected, by version.
232+ ///
233+ /// The supervisor attaches one marker per proxied connection. Every resolver
234+ /// derived for that connection reports each real secret it hands out, and
235+ /// token-grant injection reports each grant before adding it. An empty marker
236+ /// means the connection has never carried a provider credential upstream, so
237+ /// no credential change can leave it holding stale material.
238+ ///
239+ /// The provider credential state owns one version per credential, assigned
240+ /// by comparing content whenever its snapshot changes; a connection keeps only
241+ /// the version it used:
242+ ///
243+ /// - static credentials by environment key;
244+ /// - token grants by `provider:credential` identity.
245+ ///
246+ /// A use that cannot be attributed to a credential is recorded under its raw
247+ /// placeholder or key at [`STALE_CREDENTIAL_VERSION`], so the connection
248+ /// closes on the next credential check rather than never.
249+ ///
250+ /// Each key keeps its first recorded version. Recording is a superset by
251+ /// design: some callers resolve to validate rather than to send, which can
252+ /// only make a connection close more often.
253+ #[ derive( Debug , Default ) ]
254+ pub struct CredentialUseMarker {
255+ uses : std:: sync:: Mutex < CredentialUses > ,
256+ }
257+
258+ /// The credentials one connection has injected; see [`CredentialUseMarker`].
259+ #[ derive( Debug , Clone , Default , PartialEq , Eq ) ]
260+ pub struct CredentialUses {
261+ /// Static credential environment key to the version sent.
262+ pub static_versions : std:: collections:: BTreeMap < String , u64 > ,
263+ /// Token grant `provider:credential` identity to the version injected.
264+ pub grant_versions : std:: collections:: BTreeMap < String , u64 > ,
265+ }
266+
267+ impl CredentialUses {
268+ /// Whether nothing has been injected.
269+ pub fn is_empty ( & self ) -> bool {
270+ self . static_versions . is_empty ( ) && self . grant_versions . is_empty ( )
271+ }
272+ }
273+
274+ impl CredentialUseMarker {
275+ /// Record that version `version` of the static credential `env_key` was sent.
276+ pub fn record_static ( & self , env_key : & str , version : u64 ) {
277+ self . with_uses ( |uses| {
278+ uses. static_versions
279+ . entry ( env_key. to_owned ( ) )
280+ . or_insert ( version) ;
281+ } ) ;
282+ }
283+
284+ /// Record that version `version` of the token grant `identity` was used.
285+ pub fn record_grant ( & self , identity : & str , version : u64 ) {
286+ self . with_uses ( |uses| {
287+ uses. grant_versions
288+ . entry ( identity. to_owned ( ) )
289+ . or_insert ( version) ;
290+ } ) ;
291+ }
292+
293+ /// A copy of everything recorded so far.
294+ pub fn uses ( & self ) -> CredentialUses {
295+ self . uses
296+ . lock ( )
297+ . unwrap_or_else ( std:: sync:: PoisonError :: into_inner)
298+ . clone ( )
299+ }
300+
301+ fn with_uses ( & self , update : impl FnOnce ( & mut CredentialUses ) ) {
302+ // A poisoned lock still holds a valid record; never lose a use.
303+ let mut uses = self
304+ . uses
305+ . lock ( )
306+ . unwrap_or_else ( std:: sync:: PoisonError :: into_inner) ;
307+ update ( & mut uses) ;
308+ }
309+ }
310+
311+ /// A resolver's link to its connection's [`CredentialUseMarker`].
312+ #[ derive( Clone ) ]
313+ struct ResolverUseRecord {
314+ marker : Arc < CredentialUseMarker > ,
315+ /// Static credential versions of that installation.
316+ static_versions : Arc < HashMap < String , u64 > > ,
221317}
222318
223319#[ derive( Clone ) ]
@@ -242,6 +338,76 @@ impl fmt::Debug for SecretResolver {
242338}
243339
244340impl SecretResolver {
341+ /// Report every real secret this resolver hands out to `marker`.
342+ ///
343+ /// `static_versions` is the version of each static credential in the
344+ /// installation the resolver was built from.
345+ #[ must_use]
346+ pub fn with_use_marker (
347+ mut self ,
348+ marker : Arc < CredentialUseMarker > ,
349+ static_versions : Arc < HashMap < String , u64 > > ,
350+ ) -> Self {
351+ self . use_record = Some ( ResolverUseRecord {
352+ marker,
353+ static_versions,
354+ } ) ;
355+ self
356+ }
357+
358+ /// Each environment key's current-generation value.
359+ ///
360+ /// Only canonical entries are read; revision-scoped and stable-handle
361+ /// placeholders alias them or carry older generations.
362+ pub ( crate ) fn canonical_values ( & self ) -> impl Iterator < Item = ( & str , & str ) > {
363+ self . by_placeholder
364+ . iter ( )
365+ . filter_map ( |( placeholder, secret) | {
366+ let key = placeholder. strip_prefix ( PLACEHOLDER_PREFIX ) ?;
367+ ( placeholder_env_key ( placeholder) == Some ( key) )
368+ . then_some ( ( key, secret. value . as_ref ( ) ) )
369+ } )
370+ }
371+
372+ /// The current-generation value of `env_key`, if configured.
373+ pub ( crate ) fn canonical_value ( & self , env_key : & str ) -> Option < & str > {
374+ self . by_placeholder
375+ . get ( & placeholder_for_env_key ( env_key) )
376+ . map ( |secret| secret. value . as_ref ( ) )
377+ }
378+
379+ /// Report that this resolver handed out `served` for `placeholder`.
380+ ///
381+ /// The use is current when `served` is its key's current value, whichever
382+ /// placeholder form named it: canonical, stable handle, the current
383+ /// revision, a retained revision whose value has not changed since, or an
384+ /// aged-out revision that fell back. Revisions are sandbox-wide, so an older
385+ /// revision usually means another provider changed, not this credential.
386+ /// A retained revision whose value has changed, or a key with no current
387+ /// value, records [`STALE_CREDENTIAL_VERSION`]; so does a placeholder with
388+ /// no recognisable key, under the placeholder itself.
389+ fn note_use ( & self , placeholder : & str , served : & str ) {
390+ let Some ( record) = & self . use_record else {
391+ return ;
392+ } ;
393+ let Some ( env_key) = placeholder_env_key ( placeholder) else {
394+ record
395+ . marker
396+ . record_static ( placeholder, STALE_CREDENTIAL_VERSION ) ;
397+ return ;
398+ } ;
399+ let version = if self . canonical_value ( env_key) == Some ( served) {
400+ record
401+ . static_versions
402+ . get ( env_key)
403+ . copied ( )
404+ . unwrap_or ( STALE_CREDENTIAL_VERSION )
405+ } else {
406+ STALE_CREDENTIAL_VERSION
407+ } ;
408+ record. marker . record_static ( env_key, version) ;
409+ }
410+
245411 #[ cfg_attr( not( test) , allow( dead_code) ) ]
246412 pub fn from_provider_env (
247413 provider_env : HashMap < String , String > ,
@@ -330,6 +496,7 @@ impl SecretResolver {
330496 denied_env_keys : HashSet :: new ( ) ,
331497 identity_bound_env_keys : HashSet :: new ( ) ,
332498 revision_fallback_allowed_revisions : HashMap :: new ( ) ,
499+ use_record : None ,
333500 } )
334501 } ;
335502 (
@@ -388,6 +555,7 @@ impl SecretResolver {
388555 denied_env_keys : HashSet :: new ( ) ,
389556 identity_bound_env_keys : HashSet :: new ( ) ,
390557 revision_fallback_allowed_revisions : HashMap :: new ( ) ,
558+ use_record : None ,
391559 } ) ,
392560 )
393561 }
@@ -418,6 +586,7 @@ impl SecretResolver {
418586 denied_env_keys,
419587 identity_bound_env_keys,
420588 revision_fallback_allowed_revisions,
589+ use_record : None ,
421590 } )
422591 }
423592 }
@@ -454,6 +623,7 @@ impl SecretResolver {
454623 denied_env_keys,
455624 identity_bound_env_keys : bound_keys. clone ( ) ,
456625 revision_fallback_allowed_revisions,
626+ use_record : self . use_record . clone ( ) ,
457627 }
458628 }
459629
@@ -605,7 +775,11 @@ impl SecretResolver {
605775 let canonical = placeholder_for_env_key ( key) ;
606776 self . by_placeholder . get ( & canonical) ?
607777 } ;
608- resolve_secret_value ( secret)
778+ let resolved = resolve_secret_value ( secret) ;
779+ if let Some ( served) = resolved {
780+ self . note_use ( value, served) ;
781+ }
782+ resolved
609783 }
610784
611785 /// Resolve the current value for an environment key selected by trusted
@@ -628,10 +802,14 @@ impl SecretResolver {
628802 } ) ;
629803 }
630804 let placeholder = placeholder_for_env_key ( key) ;
631- Ok ( self
805+ let resolved = self
632806 . by_placeholder
633807 . get ( & placeholder)
634- . and_then ( resolve_secret_value) )
808+ . and_then ( resolve_secret_value) ;
809+ if let Some ( served) = resolved {
810+ self . note_use ( & placeholder, served) ;
811+ }
812+ Ok ( resolved)
635813 }
636814
637815 /// Resolve a placeholder while preserving endpoint-denial information.
0 commit comments