Struct FileTokenProvider
pub struct FileTokenProvider { /* private fields */ }Expand description
Reads a bearer token from a file, caching the result for a configurable TTL.
Designed for Kubernetes projected service account tokens and Vault agent sidecars, where the platform writes a short-lived JWT to a known path and rotates it in place before expiry. The cache avoids a filesystem syscall on every call while still picking up rotated tokens within one TTL period.
§Token lifecycle
- On the first
get_token()call (or after the cache TTL expires), the file is read from disk and the result is cached. - Subsequent calls within the TTL return the cached string with no I/O.
- The kubelet guarantees the file is rotated before the old token expires, so there is no window where the cached token is expired but the file has not yet been updated.
§Construction
use rust_auth_lib::{ConfigError, FileTokenProvider};
use std::time::Duration;
// From an explicit path (30 s default TTL):
let provider = FileTokenProvider::new("/var/run/secrets/tokens/my-token");
// From an explicit path with a custom TTL:
let provider = FileTokenProvider::with_ttl(
"/var/run/secrets/tokens/my-token",
Duration::from_secs(60),
);
// From environment variables:
let provider_result: Result<FileTokenProvider, ConfigError> =
FileTokenProvider::from_env();§Cloning
FileTokenProvider wraps an Arc internally. Cloning is cheap and all
clones share the same cache.
Implementations§
§impl FileTokenProvider
impl FileTokenProvider
pub fn new(path: impl Into<PathBuf>) -> FileTokenProvider
pub fn new(path: impl Into<PathBuf>) -> FileTokenProvider
Create a provider that reads from path with a 30-second cache TTL.
pub fn with_ttl(
path: impl Into<PathBuf>,
cache_ttl: Duration,
) -> FileTokenProvider
pub fn with_ttl( path: impl Into<PathBuf>, cache_ttl: Duration, ) -> FileTokenProvider
Create a provider that reads from path with an explicit cache TTL.
pub fn from_env() -> Result<FileTokenProvider, ConfigError>
pub fn from_env() -> Result<FileTokenProvider, ConfigError>
Construct from environment variables.
Reads:
SERVICE_TOKEN_FILE(required) — path to the token fileSERVICE_TOKEN_CACHE_TTL_SECS(optional, default:30) — cache TTL in seconds. If the variable is set but cannot be parsed as au64, it silently falls back to30.
Trait Implementations§
§impl Clone for FileTokenProvider
impl Clone for FileTokenProvider
§fn clone(&self) -> FileTokenProvider
fn clone(&self) -> FileTokenProvider
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read more§impl TokenProvider for FileTokenProvider
impl TokenProvider for FileTokenProvider
Auto Trait Implementations§
impl Freeze for FileTokenProvider
impl RefUnwindSafe for FileTokenProvider
impl Send for FileTokenProvider
impl Sync for FileTokenProvider
impl Unpin for FileTokenProvider
impl UnsafeUnpin for FileTokenProvider
impl UnwindSafe for FileTokenProvider
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
Wrap the input message
T in a tonic::Request§impl<L> LayerExt<L> for L
impl<L> LayerExt<L> for L
§fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>where
L: Layer<S>,
fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>where
L: Layer<S>,
Applies the layer to a service and wraps it in [
Layered].