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

plugin: Handle panics from agent requests #785

Merged
merged 1 commit into from
Feb 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion pkg/plugin/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,24 @@ const (
func (e *AutoscaleEnforcer) startPermitHandler(ctx context.Context, logger *zap.Logger) error {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
logger := logger // copy locally, so that we can add fields and refer to it in defers

var finalStatus int
defer func() {
e.metrics.resourceRequests.WithLabelValues(r.RemoteAddr, strconv.Itoa(finalStatus)).Inc()
}()

// Catch any potential panics and report them as 500s
defer func() {
if err := recover(); err != nil {
msg := "request handler panicked"
logger.Error(msg, zap.String("error", fmt.Sprint(err)))
finalStatus = 500
w.WriteHeader(finalStatus)
_, _ = w.Write([]byte(msg))
Copy link
Contributor

Choose a reason for hiding this comment

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

how about sending not just msg here, but msg: err?

Copy link
Member Author

@sharnoff sharnoff Feb 5, 2024

Choose a reason for hiding this comment

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

Hm, yeah. My impression was that it was better from a security perspective to not include the full error message (especially considering it's already visible in the logs), but tbh maybe it doesn't matter.
wdyt?

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, I see your concern. Although this is our internal API, so risk is not high.

But I guess, it can be left as is, it is easy to grep for logs. Maybe make the message more distinct, like "permit handler panicked" or else.

@areyou1or0 do you have an opinion on whether we should return the full error message, from catch-all exception handler?

Choose a reason for hiding this comment

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

@Omrigan somehow I didn't get the notification for the mention, just seeing this now.
Yes, it's best practice to not reveal non-customised error message as this can disclose the details of underlying technology. Low severity but valid.

}
}()

if r.Method != "POST" {
finalStatus = 400
w.WriteHeader(400)
Expand All @@ -58,7 +71,7 @@ func (e *AutoscaleEnforcer) startPermitHandler(ctx context.Context, logger *zap.
return
}

logger := logger.With(zap.Object("pod", req.Pod))
logger = logger.With(zap.Object("pod", req.Pod))
logger.Info(
"Received autoscaler-agent request",
zap.String("client", r.RemoteAddr), zap.Any("request", req),
Expand Down
Loading