-
Notifications
You must be signed in to change notification settings - Fork 4.6k
credentials: implement file-based JWT Call Credentials (part 1 for A97) #8431
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
base: master
Are you sure you want to change the base?
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #8431 +/- ##
==========================================
- Coverage 82.27% 82.04% -0.24%
==========================================
Files 414 414
Lines 40424 40647 +223
==========================================
+ Hits 33259 33347 +88
- Misses 5795 5910 +115
- Partials 1370 1390 +20
🚀 New features to boost your workflow:
|
@dfawley hey 👋 Given you approved A97, would you mind having a cursory look at the PR to confirm if at least at a high level the approach looks good? |
I will take a look at this , I need to go through the gRFC first. |
Sorry for the delay here. @easwars would you be able to review this change? I think you have more background into some of the things than I do, like the bootstrap integration. Thank you! |
Thank you for your contribution @dimpavloff. Yes, it would be nice if you can split this into smaller PRs. I will continue to use this PR to review the JWT call credentials implementation. If you can move the xDS implementation out to one or more PRs, I would greatly appreciate that and would be happy to review them as well. |
AuthInfo: &testAuthInfo{secLevel: credentials.PrivacyAndIntegrity}, | ||
}) | ||
|
||
// First call should read from file |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here and elsewhere in this PR, please use appropriate punctuations in comment sentences. See: https://google.github.io/styleguide/go/decisions#comment-sentences
t.Fatalf("NewTokenFileCallCredentials() failed: %v", err) | ||
} | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please define a const
for default test timeout and use it in all tests that require a timeout. We use this pattern a lot in our tests. Please search for defaultTestTimeout
and defaultTestShortTimeout
for commonly used patterns.
|
||
// Verify cached expiration is 30 seconds before actual token expiration | ||
impl := creds.(*jwtTokenFileCallCreds) | ||
impl.mu.RLock() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please only test the API surface. Relying on implementation internals in tests makes them brittle and would result in test changes when any changes to implementation is made.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume you are referring to using a private field rather than obtaining mu specifically.
In general I agree -- white box tests may get fragile and break during a refactor. However, this test and the next couple of ones are about the caching behaviour -- it is meant to be transparent to the external API. If I don't make assertions about the private fields, the tests may pass trivially and become more flaky (e.g. when testing the backoff in the next test).
One alternative could be factoring out these behaviours out into a separate private struct with "public" functions which expose the same information. Given that it would require shifting the majority of the implementation into that struct, I'm not sure it is an improvement from the current approach.
Please do let me know your thoughts and if you have other suggestions.
Apologies for the delay on this. Have been busy with some other stuff. Will definitely try to make another pass today. FYI, we recommend authors to not mark comments as resolved. Instead, we recommend the reviewer to do that once the comment has been satisfactorily addressed, because now I have to unresolve every comment to see what I commented and then verify that the comment has been satisfactorily addressed. Thanks for understanding. |
credentials/jwt/doc.go
Outdated
// - Errors in reading tokens or parsing JWTs will result in RPC UNAVAILALBE or | ||
// UNAUTHENTICATED errors | ||
// - These errors are cached and retried with exponential backoff. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: It looks like these two bullet points can/should be merged?
credentials/jwt/jwt_token_file.go
Outdated
} | ||
|
||
// isTokenValid checks if the cached token is still valid. | ||
// Caller must hold c.mu.RLock(). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, I personally haven't considered it so far, because I tend to avoid RWLocks after being bitten by them once (in a bad way). But, I'm not opposed to it and it does sound more explicit when using RWLocks.
credentials/jwt/jwt_token_file.go
Outdated
// invalid or expired, the next RPC will handle synchronous refresh instead. | ||
// Caller must hold c.mu.RLock(). | ||
func (c *jwtTokenFileCallCreds) needsPreemptiveRefreshLocked() bool { | ||
return c.isTokenValidLocked() && time.Until(c.cachedExpiration) < time.Minute |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we make a const for the one minute pre-emptive refresh trigger?
credentials/jwt/jwt_token_file.go
Outdated
nextRetryTime time.Time // When next retry is allowed | ||
|
||
// Pre-emptive refresh mutex | ||
refreshMu sync.Mutex |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a valid point. Even if it is a single goroutine, we cannot leak it, because our tests check for leaked goroutines and therefore unit tests will fail. Thanks for trying though.
credentials/jwt/jwt_token_file.go
Outdated
// Multiple concurrent calls are safe - only one refresh will run at a time. | ||
// The refresh runs in a separate goroutine and does not block the caller. | ||
func (c *jwtTokenFileCallCreds) triggerPreemptiveRefresh() { | ||
go func() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ack
Again, apologies for the delay in the review. I did a little bit of thinking and I feel we can avoid the second mutex and also avoid locking and unlocking multiple times in several codepaths with the following approach. (this is mostly going to be pseudocode ... so please bear with me)
Let me know what you think. |
This PR is labeled as requiring an update from the reporter, and no update has been received after 6 days. If no update is provided in the next 7 days, this issue will be automatically closed. |
Hi @easwars , apologies for the delay, I was on holiday. Thanks for the review and for the suggested approach. I don't have the code very fresh in my mind anymore 😅 but I think that makes sense. I will see what I can do in the coming days and get back to you. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@easwars , I managed to iterate on top of your suggestion to simplify it further, we no longer need the condition variable, just one lock! LMK what you think
credentials/jwt/jwt_token_file.go
Outdated
return map[string]string{ | ||
"authorization": "Bearer " + token, | ||
}, nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI, I've cached it just in case
} | ||
|
||
// newJWTFileReader creates a new JWTFileReader for the specified file path. | ||
func newJWTFileReader(tokenFilePath string) *jWTFileReader { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to highlight, I made both the struct and function private. LMK if you intended for them to be public
Part one for grpc/proposal#492 (A97).
This is done in a new
credentials/jwt
package to provide file-based PerRPCCallCredentials. It can be used beyond XDS. The package handles token reloading, caching, and validation as per A97 .There will be a separate PR which uses it in
xds/bootstrap
.Whilst implementing the above, I considered
credentials/oauth
andcredentials/xds
packages instead of creating a new one. The former package hasNewJWTAccessFromKey
andjwtAccess
which seem very relevant at first. However, I think thejwtAccess
behaviour seems more tailored towards Google services. Also, the refresh, caching, and error behaviour for A97 is quite different than what's already there and therefore a separate implementation would have still made sense.WRT
credentials/xds
, it could have been extended to both handle transport and call credentials. However, this is a bit at odds with A97 which says that the implementation should be non-XDS specific and, from reading between the lines, usable beyond XDS.I think the current approach makes review easier but because of the similarities with the other two packages, it is a bit confusing to navigate. Please let me know whether the structure should change.
Relates to istio/istio#53532
RELEASE NOTES:
credentials/jwt
package providing file-based JWT PerRPCCredentials (A97)