-
Notifications
You must be signed in to change notification settings - Fork 137
Add automatic authentication monitoring for remote workloads with unauthenticated state detection #2421
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?
Conversation
|
@claude please review this PR and put emphasis on security as well |
|
Claude finished @JAORMX's task —— View job Security-Focused Code ReviewI've completed a thorough security review of this PR, focusing on the authentication monitoring implementation. Overall, this is a well-designed and secure implementation with excellent error handling and safety patterns. Below are my findings: ✅ Security Strengths1. Excellent Error Classification (
|
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #2421 +/- ##
==========================================
+ Coverage 55.35% 55.36% +0.01%
==========================================
Files 277 279 +2
Lines 26451 26524 +73
==========================================
+ Hits 14641 14686 +45
- Misses 10459 10490 +31
+ Partials 1351 1348 -3 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
| @@ -0,0 +1,171 @@ | |||
| package runner | |||
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'm not sure this code goes in the runner; wouldn't it be better if it's part of pkg/auth?
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.
We cannot access statusManager inside the pkg/auth because it creates the import cycle issue. Though this should go to the pkg/auth and I can create a simple adapter interface inside this file that has the SetWorkloadStatus function and it needed to be passed from the runner:
type StatusUpdater interface {
SetWorkloadStatus(ctx context.Context, workloadName string, status string, reason string) error
}
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.
moved under pkg/auth
| ats.stopOnce.Do(func() { close(ats.stopMonitoring) }) | ||
| } | ||
|
|
||
| func isAuthenticationError(err error) bool { |
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 don't think this check is reliable.
Unfortunately the oauth library doesn't always return a consistent error when there's an authentication issue.
For example if there's no refresh token and the access token is expired it just returns a simple error. I can't link the code but in oauth2.go this is the logic:
if tf.refreshToken == "" {
return nil, errors.New("oauth2: token expired and refresh token is not set")
}
Perhaps we can remove this function and always assume that if tokenSource.Token() has an error then it's unauthenticated.
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.
Good point, I considered that too, but added this check to avoid marking the state as unauthenticated due to transient or network errors.
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.
this is now marking the state to unauthenticated if we get the error from Token()
| return tok, nil | ||
| } | ||
|
|
||
| func (ats *AuthenticatedTokenSource) startBackgroundMonitoring() { |
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.
Is the background monitoring necessary in this scenario?
Would it be easier to add a check in ListWorkloads and GetWorkload like we do for the unhealthy status?
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.
Good question, I think background monitoring is needed for proactive authentication checks when there are no client requests or user commands. It’s very lightweight and runs once per proxy after each token expiry.
Summary
This PR implements automatic authentication monitoring for remote workloads using OAuth2 authentication. The system proactively detects when authentication tokens expire or fail, automatically marking workloads as unauthenticated without requiring client requests.
Changes
New Component:
AuthenticatedTokenSourceoauth2.TokenSourcethat provides:Token()methodWorkloadStatusUnauthenticatedon authentication failuresKey Features
1. Intelligent Error Classification
oauth2.RetrieveErrorwith HTTP status codes 400/401invalid_grant,invalid_client, orinvalid_token2. Background Monitoring
3. Exponential Backoff
4. Lifecycle Management
Runnerlifecycle:Runner.Cleanup()is called (via context cancellation)sync.Oncefor idempotent channel closureIntegration Points
Runner Integration
AuthenticatedTokenSourceis created for remote workloads with authenticationStatusManagerinterfaceStatus Updates
StatusManager.SetWorkloadStatus()withWorkloadStatusUnauthenticatedTechnical Details
Error Detection
Monitoring Flow
Testing
StatusManagermocksBenefits
Example Scenarios
Scenario 1: Refresh Token Expires
invalid_granterrorWorkloadStatusUnauthenticatedScenario 2: Temporary Network Issue