diff --git a/cmd/dex/serve.go b/cmd/dex/serve.go index fd07a09a..6164dabb 100644 --- a/cmd/dex/serve.go +++ b/cmd/dex/serve.go @@ -275,6 +275,8 @@ func runServe(options serveOptions) error { // explicitly convert to UTC. now := func() time.Time { return time.Now().UTC() } + healthChecker := gosundheit.New() + serverConfig := server.Config{ SupportedResponseTypes: c.OAuth2.ResponseTypes, SkipApprovalScreen: c.OAuth2.SkipApprovalScreen, @@ -287,6 +289,7 @@ func runServe(options serveOptions) error { Logger: logger, Now: now, PrometheusRegistry: prometheusRegistry, + HealthChecker: healthChecker, } if c.Expiry.SigningKeys != "" { signingKeys, err := time.ParseDuration(c.Expiry.SigningKeys) @@ -329,7 +332,6 @@ func runServe(options serveOptions) error { telemetryRouter.Handle("/metrics", promhttp.HandlerFor(prometheusRegistry, promhttp.HandlerOpts{})) // Configure health checker - healthChecker := gosundheit.New() { handler := gosundheithttp.HandleHealthJSON(healthChecker) telemetryRouter.Handle("/healthz", handler) diff --git a/server/handlers_test.go b/server/handlers_test.go index 4ca182f2..8ad59d94 100644 --- a/server/handlers_test.go +++ b/server/handlers_test.go @@ -10,6 +10,8 @@ import ( "testing" "time" + gosundheit "github.com/AppsFlyer/go-sundheit" + "github.com/AppsFlyer/go-sundheit/checks" "github.com/coreos/go-oidc/v3/oidc" "github.com/gorilla/mux" "github.com/stretchr/testify/require" @@ -33,20 +35,23 @@ func TestHandleHealth(t *testing.T) { } } -type badStorage struct { - storage.Storage -} - -func (b *badStorage) CreateAuthRequest(r storage.AuthRequest) error { - return errors.New("storage unavailable") -} - func TestHandleHealthFailure(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() httpServer, server := newTestServer(ctx, t, func(c *Config) { - c.Storage = &badStorage{c.Storage} + c.HealthChecker = gosundheit.New() + + c.HealthChecker.RegisterCheck(&gosundheit.Config{ + Check: &checks.CustomCheck{ + CheckName: "fail", + CheckFunc: func() (details interface{}, err error) { + return nil, errors.New("error") + }, + }, + InitiallyPassing: false, + ExecutionPeriod: 1 * time.Second, + }) }) defer httpServer.Close() diff --git a/server/server.go b/server/server.go index 6fd4d8b7..a79b7cfd 100644 --- a/server/server.go +++ b/server/server.go @@ -15,6 +15,7 @@ import ( "sync/atomic" "time" + gosundheit "github.com/AppsFlyer/go-sundheit" "github.com/felixge/httpsnoop" "github.com/gorilla/handlers" "github.com/gorilla/mux" @@ -93,6 +94,8 @@ type Config struct { Logger log.Logger PrometheusRegistry *prometheus.Registry + + HealthChecker gosundheit.Health } // WebConfig holds the server's frontend templates and asset configuration. @@ -333,7 +336,13 @@ func newServer(ctx context.Context, c Config, rotationStrategy rotationStrategy) // "authproxy" connector. handleFunc("/callback/{connector}", s.handleConnectorCallback) handleFunc("/approval", s.handleApproval) - handle("/healthz", s.newHealthChecker(ctx)) + handle("/healthz", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if !c.HealthChecker.IsHealthy() { + s.renderError(r, w, http.StatusInternalServerError, "Health check failed.") + return + } + fmt.Fprintf(w, "Health check passed") + })) handlePrefix("/static", static) handlePrefix("/theme", theme) s.mux = r diff --git a/server/server_test.go b/server/server_test.go index 3a918434..87ca6c17 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -21,6 +21,7 @@ import ( "testing" "time" + gosundheit "github.com/AppsFlyer/go-sundheit" "github.com/coreos/go-oidc/v3/oidc" "github.com/kylelemons/godebug/pretty" "github.com/prometheus/client_golang/prometheus" @@ -96,6 +97,7 @@ func newTestServer(ctx context.Context, t *testing.T, updateConfig func(c *Confi }, Logger: logger, PrometheusRegistry: prometheus.NewRegistry(), + HealthChecker: gosundheit.New(), } if updateConfig != nil { updateConfig(&config)