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

Improvement: report unauthorized error as a health check #295

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-295.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: improvement
improvement:
description: If user is unauthorized to view health checks return an unauthorized error health check rather than no health check.
links:
- https://github.com/palantir/witchcraft-go-server/pull/295
10 changes: 9 additions & 1 deletion status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package status

import (
"github.com/palantir/witchcraft-go-health/sources"
"net/http"
"sync/atomic"

Expand All @@ -24,6 +25,13 @@ import (
"github.com/palantir/witchcraft-go-health/status"
)


var healthStatusUnauthorized = health.HealthStatus{
Checks: map[health.CheckType]health.HealthCheckResult{
"HEALTH_STATUS_UNAUTHORIZED": sources.UnhealthyHealthCheckResult("HEALTH_STATUS_UNAUTHORIZED", "unauthorized to access health status; please verify the health-check-shared-secret", nil),
},
}

// HealthHandler is responsible for checking the health-check-shared-secret if it is provided and
// invoking a HealthCheckSource if the secret is correct or unset.
type healthHandlerImpl struct {
Expand Down Expand Up @@ -66,7 +74,7 @@ func (h *healthHandlerImpl) computeNewHealthStatus(req *http.Request) (health.He
if sharedSecret := h.healthCheckSharedSecret.CurrentString(); sharedSecret != "" {
token, err := httpserver.ParseBearerTokenHeader(req)
if err != nil || sharedSecret != token {
return health.HealthStatus{}, http.StatusUnauthorized
return healthStatusUnauthorized, http.StatusUnauthorized
Copy link
Author

Choose a reason for hiding this comment

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

My preference was to log when we hit this case but there's no accessible ctx and the loggingHealthStatusChangeHandler used above derives the status code from the health status rather than from the status returned by computeNewHealthStatus

}
}
metadata := h.check.HealthStatus(req.Context())
Expand Down