Skip to content

Remove AccessToken::is_expired() #2611

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open

Remove AccessToken::is_expired() #2611

wants to merge 5 commits into from

Conversation

chlowell
Copy link
Member

...because it's unnecessary and its name is misleading. This PR also adds test coverage and fixes a bug: the policy currently returns errors from proactive refresh attempts even when it has a valid cached token. (There's no value in doing that because the client request could otherwise succeed.)

@github-actions github-actions bot added Azure.Core The azure_core crate Azure.Identity The azure_identity crate labels May 15, 2025
@chlowell chlowell marked this pull request as ready for review May 15, 2025 23:26
@Copilot Copilot AI review requested due to automatic review settings May 15, 2025 23:26
@chlowell chlowell self-assigned this May 15, 2025
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR removes the misleading AccessToken::is_expired() method and replaces it with a more robust token refresh mechanism while also adding test coverage and fixing an error in the proactive token refresh logic.

  • Removed AccessToken::is_expired() and updated the token checking logic in cache and bearer token policy.
  • Implemented a new helper (should_refresh) to determine token refresh timing.
  • Enhanced error handling during token refresh and updated tests accordingly.

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
sdk/identity/azure_identity/src/credentials/cache.rs Replaced usage of is_expired() with should_refresh and updated test expiration.
sdk/core/azure_core/src/http/policies/bearer_token_policy.rs Refactored token refresh logic and improved error handling during refresh.
sdk/core/azure_core/src/credentials.rs Removed the is_expired() method as part of the deprecation.
sdk/core/azure_core/CHANGELOG.md Documented the breaking change and bug fix.

Copy link
Member

@heaths heaths left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! I see nothing blocking. Just a couple of notes and one suggestion.

### Bugs Fixed

- `BearerTokenCredentialPolicy` returns an error when a proactive token refresh attempt fails
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Especially if this was a customer-filed bug, we reference the bug number such that it renders as (#{num}). If not a customer-filed issue, no need.

Some(token) if should_refresh(&token.expires_on) => {
// token is expired or within its refresh window. Upgrade the lock and
// acquire a new token, provided another thread hasn't already done so
let expires_on = token.expires_on;
drop(access_token);
let mut access_token = self.access_token.write().await;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably don't need to take a write lock this soon - only when we write. Is there any disadvantage to a couple of threads potentially authenticating but only the last writer wins? IIRC, this is how creds in the .NET SDK work, and generally advised if a guarded operation is non-destructive and, in this case, wouldn't get us throttled (I doubt that for auth - not within reason, anyway).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Throttling is actually a real danger because IMDS allows only 5 requests per second. We may need to add backoff between proactive refresh attempts to reduce the risk further; a highly concurrent app could hammer IMDS during an outage and get throttled.

use typespec_client_core::http::{policies::TransportPolicy, Method, TransportOptions};

#[derive(Debug, Clone)]
struct MockCredential {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just FYI: we actually have one in azure_core_test, though maybe this one is purpose-built for this task.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Azure.Core The azure_core crate Azure.Identity The azure_identity crate
Projects
Status: Untriaged
Development

Successfully merging this pull request may close these issues.

2 participants