-
Notifications
You must be signed in to change notification settings - Fork 256
[0.14] Fix nil dereference for cluster import token #4510
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
[0.14] Fix nil dereference for cluster import token #4510
Conversation
When creating a cluster import token fails, the agent management controller is no longer vulnerable to crashes.
b09efcc to
dc70e8f
Compare
| if err != nil { | ||
| if apierrors.IsAlreadyExists(err) { | ||
| token, err = i.tokens.Get(cluster.Namespace, tokenName) | ||
| if err != nil { | ||
| logrus.Debugf("Failed to get existing ClusterRegistrationToken for cluster %s/%s: %v (requeuing)", cluster.Namespace, cluster.Name, err) | ||
| i.clusters.EnqueueAfter(cluster.Namespace, cluster.Name, durations.TokenClusterEnqueueDelay) | ||
| return status, err | ||
| } | ||
| } else { | ||
| logrus.Debugf("Failed to create ClusterRegistrationToken for cluster %s/%s: %v (requeuing)", cluster.Namespace, cluster.Name, err) | ||
| i.clusters.EnqueueAfter(cluster.Namespace, cluster.Name, durations.TokenClusterEnqueueDelay) | ||
| return status, err | ||
| } | ||
| } |
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.
nitpick about style (I don't like too many nested if-else):
| if err != nil { | |
| if apierrors.IsAlreadyExists(err) { | |
| token, err = i.tokens.Get(cluster.Namespace, tokenName) | |
| if err != nil { | |
| logrus.Debugf("Failed to get existing ClusterRegistrationToken for cluster %s/%s: %v (requeuing)", cluster.Namespace, cluster.Name, err) | |
| i.clusters.EnqueueAfter(cluster.Namespace, cluster.Name, durations.TokenClusterEnqueueDelay) | |
| return status, err | |
| } | |
| } else { | |
| logrus.Debugf("Failed to create ClusterRegistrationToken for cluster %s/%s: %v (requeuing)", cluster.Namespace, cluster.Name, err) | |
| i.clusters.EnqueueAfter(cluster.Namespace, cluster.Name, durations.TokenClusterEnqueueDelay) | |
| return status, err | |
| } | |
| } | |
| if apierrors.IsAlreadyExists(err) { | |
| token, err = i.tokens.Get(cluster.Namespace, tokenName) | |
| if err != nil { | |
| logrus.Debugf("Failed to get existing ClusterRegistrationToken for cluster %s/%s: %v (requeuing)", cluster.Namespace, cluster.Name, err) | |
| i.clusters.EnqueueAfter(cluster.Namespace, cluster.Name, durations.TokenClusterEnqueueDelay) | |
| return status, err | |
| } | |
| } else if err != nil { | |
| logrus.Debugf("Failed to create ClusterRegistrationToken for cluster %s/%s: %v (requeuing)", cluster.Namespace, cluster.Name, err) | |
| i.clusters.EnqueueAfter(cluster.Namespace, cluster.Name, durations.TokenClusterEnqueueDelay) | |
| return status, err | |
| } |
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 missed that this is a backport and not the original PR, nevermind then 😅
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.
Yes, I agree that the style is a bit less than ideal, but this is indeed a backport :)
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 fixes a critical nil pointer dereference bug in the cluster import token creation logic. When token creation failed, the previous code discarded the result and continued execution with a nil token variable, causing crashes when the token was later accessed.
Key Changes:
- Properly capture the created token object instead of discarding it
- Add comprehensive error handling for token creation failures, including race condition handling for AlreadyExists errors
- Return errors appropriately instead of silently ignoring them and continuing with nil token
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
When creating a cluster import token fails, the agent management controller is no longer vulnerable to crashes.
Refers to #4491.
Backport of #4494 to v0.14.
Additional Information
Checklist
- [ ] I have updated the documentation via a pull request in thefleet-docs repository.