Skip to content
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

fix(auth): sync claim/token times in SA creds #789

Merged
merged 1 commit into from
Jan 22, 2025

Conversation

dbolduc
Copy link
Member

@dbolduc dbolduc commented Jan 22, 2025

Fixes #733

Make sure the token's expires_at time matches the exp field in the claim.

We drop the JwsClaimsBuilder and do any defaulting in service_account_credential.rs. Note that the exp and iat fields are required, so they are probably better represented as OffsetDateTime than Option<OffsetDateTime>.

Copy link

codecov bot commented Jan 22, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.62%. Comparing base (31c8a5e) to head (85f26a7).
Report is 4 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #789      +/-   ##
==========================================
- Coverage   95.63%   95.62%   -0.02%     
==========================================
  Files          37       37              
  Lines        1375     1371       -4     
==========================================
- Hits         1315     1311       -4     
  Misses         60       60              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@dbolduc dbolduc marked this pull request as ready for review January 22, 2025 16:30
Copy link
Contributor

@coryan coryan left a comment

Choose a reason for hiding this comment

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

Please remove any unnecessary dependencies and then ship it!

@@ -14,7 +14,6 @@

use crate::credentials::CredentialError;
use crate::credentials::Result;
use derive_builder::Builder;
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks. I think there is an unused dependency in src/auth/Cargo.toml now.

Copy link
Member Author

Choose a reason for hiding this comment

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

Still used elsewhere:

Comment on lines +71 to +81
let now = OffsetDateTime::now_utc() - CLOCK_SKEW_FUDGE;
let exp = now + DEFAULT_TOKEN_TIMEOUT;
let claims = JwsClaims {
iss: self.service_account_info.client_email.clone(),
scope: Some(DEFAULT_SCOPES.map(|s| s.to_string()).to_vec()),
aud: None,
exp,
iat: now,
typ: None,
sub: None,
};
Copy link
Contributor

Choose a reason for hiding this comment

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

Alternatively, you could create a JwsClaims::new(now: OffsetDateTime) -> Self function.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think we are going to support options to override the scope, so that function will make less sense in the future.

(Also, we should not bother to have typ or sub if they are always None. I assume there will be some code path where they have concrete values in the future)

pub iss: &'a str,
#[derive(Serialize)]
pub struct JwsClaims {
pub iss: String,
Copy link
Contributor

Choose a reason for hiding this comment

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

I like the simplicity of using String. It is more expensive, but this function will be called like once per hour so we should not complicate things too much in search of small performance improvements.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ack

@@ -116,44 +112,30 @@ mod tests {
)
.unwrap();

// 5 seconds is like 640KiB, good enough for everybody
Copy link
Contributor

Choose a reason for hiding this comment

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

🎉

@dbolduc dbolduc merged commit e25a672 into googleapis:main Jan 22, 2025
14 checks passed
@dbolduc dbolduc deleted the fix-auth-sa-times branch January 22, 2025 21:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Mismatched expiration time in service account credentials
3 participants