Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions crates/oidc-provider/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,22 @@ use crate::BackendCredentials;
const EXPIRY_MARGIN_SECS: i64 = 60;

/// Thread-safe TTL cache for cloud credentials.
///
/// `Clone` shares the same underlying store (the entries map is behind an
/// `Arc`), so a cloned [`OidcCredentialProvider`](crate::OidcCredentialProvider)
/// keeps hitting the same cache — letting a runtime hold the provider in a
/// shared/`static` slot and reuse it across requests instead of re-minting and
/// re-exchanging every time.
#[derive(Clone, Default)]
pub struct CredentialCache {
entries: Mutex<HashMap<String, Arc<BackendCredentials>>>,
}

impl Default for CredentialCache {
fn default() -> Self {
Self::new()
}
entries: Arc<Mutex<HashMap<String, Arc<BackendCredentials>>>>,
}

impl CredentialCache {
/// Create an empty credential cache.
pub fn new() -> Self {
Self {
entries: Mutex::new(HashMap::new()),
entries: Arc::new(Mutex::new(HashMap::new())),
}
}

Expand Down
6 changes: 1 addition & 5 deletions crates/oidc-provider/src/exchange/aws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,7 @@ impl AwsExchange {
}

impl<H: HttpExchange> CredentialExchange<H> for AwsExchange {
async fn exchange(
&self,
http: &H,
jwt: &str,
) -> Result<BackendCredentials, OidcProviderError> {
async fn exchange(&self, http: &H, jwt: &str) -> Result<BackendCredentials, OidcProviderError> {
// Build the request with this module's `AssumeRoleWithWebIdentity`, hand
// its (unencoded) pairs to the runtime's HTTP client — which
// form-urlencodes them — then parse the reply.
Expand Down
6 changes: 1 addition & 5 deletions crates/oidc-provider/src/exchange/azure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,7 @@ impl AzureExchange {
}

impl<H: HttpExchange> CredentialExchange<H> for AzureExchange {
async fn exchange(
&self,
http: &H,
jwt: &str,
) -> Result<BackendCredentials, OidcProviderError> {
async fn exchange(&self, http: &H, jwt: &str) -> Result<BackendCredentials, OidcProviderError> {
let form = [
("grant_type", "client_credentials"),
(
Expand Down
6 changes: 1 addition & 5 deletions crates/oidc-provider/src/exchange/gcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,7 @@ impl GcpExchange {
}

impl<H: HttpExchange> CredentialExchange<H> for GcpExchange {
async fn exchange(
&self,
http: &H,
jwt: &str,
) -> Result<BackendCredentials, OidcProviderError> {
async fn exchange(&self, http: &H, jwt: &str) -> Result<BackendCredentials, OidcProviderError> {
// Step 1: Exchange JWT for federated access token via GCP STS
let sts_form = [
(
Expand Down
5 changes: 5 additions & 0 deletions crates/oidc-provider/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ pub trait HttpExchange:
}

/// Top-level provider that combines signing, exchange, and caching.
///
/// `Clone` is cheap and shares the credential cache (and the `Clone` HTTP
/// client), so a runtime can construct one provider and reuse it across requests
/// — keeping the cache warm instead of re-minting + re-exchanging every call.
#[derive(Clone)]
pub struct OidcCredentialProvider<H: HttpExchange> {
signer: JwtSigner,
cache: CredentialCache,
Expand Down
Loading