-
Notifications
You must be signed in to change notification settings - Fork 14
feat: implement credential caching and retrieval mechanism in auth #1017
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: main
Are you sure you want to change the base?
feat: implement credential caching and retrieval mechanism in auth #1017
Conversation
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.
Pull request overview
This pull request implements a credential caching and retrieval mechanism to address the lack of credential reuse in the authentication layer. The changes ensure that Azure SDK credential objects are created once and reused across subsequent calls, enabling proper utilization of the SDK's built-in token caching and automatic renewal capabilities.
Key changes:
- Introduces a credential holder pattern with thread-safe lazy initialization using
sync.Onceandsync.RWMutex - Refactors all eight authentication methods to use the new
getOrCreateCredentialhelper - Adds comprehensive unit tests with concurrency validation, improving code coverage from 23.2% to 54.8%
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| internal/api/auth.go | Adds credential caching infrastructure (credentialType enum, credentialHolder struct, credentials map with RWMutex) and refactors all authentication methods to use the new getOrCreateCredential helper for credential reuse |
| internal/api/auth_test.go | Implements comprehensive unit tests covering credential caching behavior, thread safety with concurrent access, error handling, and authentication method functionality using mock credentials |
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
| if err != nil { | ||
| return "", time.Time{}, err | ||
| func (client *Auth) getOrCreateCredential(ctx context.Context, credType credentialType, factory func() (azcore.TokenCredential, error)) (azcore.TokenCredential, error) { | ||
| client.credentialsMutex.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.
what about token expiry? When do we invalidate the cache?
|
@copilot fix linter issues |
* Initial plan * fix: remove stray syntax errors in auth_test.go Co-authored-by: mawasile <50197777+mawasile@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: mawasile <50197777+mawasile@users.noreply.github.com>
❌ 1 Tests Failed:
View the top 1 failed test(s) by shortest run time
To view more test analytics, go to the Test Analytics Dashboard |
Fixes: #711
This PR adds caching to the authentication layer to improve performance and reduce unnecessary API calls.
Why it's needed:
The previous code created a new credential object every time the provider needed a token. This caused two problems:
For most credential types (client secret, certificate, managed identity, etc.): The Azure SDK caches tokens internally, but only within the same credential instance. By creating new credentials every time, we threw away the cache and forced the SDK to fetch a new token on each call.
For CLI-based credentials (Azure CLI and Azure Developer CLI): The Azure SDK does NOT cache tokens at all for these types. Every call to get a token runs the az or azd command, which is slow.
What this PR adds:
Credential object caching: We now reuse credential objects instead of creating new ones. This lets the Azure SDK's built-in token cache work properly.
Token caching for CLI credentials: Since CLI credentials don't have built-in caching, we added our own cache. Tokens are reused until they're within 5 minutes of expiring.
Result:
Fewer authentication requests to Azure AD
Faster operations when using CLI-based authentication
No external dependencies—all caching is in-memory
We also introduced a set of tests to improve code coverage;
go test -coverprofile=coverage.out -run "TestUnit" ./internal/api/... && go tool cover -func=coverage.out | grep -E "(auth\.go|total)"Before:
After: