From 445429d390944b261d5f90ec28be323301f987d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Weisbarth?= Date: Wed, 8 Mar 2023 16:08:12 +0100 Subject: [PATCH] Rename health-endpoint and variables; --- src/autoscaler/api/cmd/api/api_test.go | 12 ++-- .../healthendpoint/health_readiness_test.go | 61 ++++++++++--------- src/autoscaler/healthendpoint/server.go | 30 ++++----- src/autoscaler/routes/routes.go | 7 ++- 4 files changed, 53 insertions(+), 57 deletions(-) diff --git a/src/autoscaler/api/cmd/api/api_test.go b/src/autoscaler/api/cmd/api/api_test.go index 758412fed1..3612cbdde0 100644 --- a/src/autoscaler/api/cmd/api/api_test.go +++ b/src/autoscaler/api/cmd/api/api_test.go @@ -1,13 +1,13 @@ package main_test import ( + "code.cloudfoundry.org/app-autoscaler/src/autoscaler/routes" "fmt" "io" "net/http" "os" "time" - "code.cloudfoundry.org/app-autoscaler/src/autoscaler/healthendpoint" . "code.cloudfoundry.org/app-autoscaler/src/autoscaler/testhelpers" "code.cloudfoundry.org/app-autoscaler/src/autoscaler/api/config" @@ -208,8 +208,8 @@ var _ = Describe("Api", func() { basicAuthConfig.Health.HealthCheckUsername = "" basicAuthConfig.Health.HealthCheckPassword = "" basicAuthConfig.Health.ReadinessCheckEnabled = true - basicAuthConfig.Health.UnprotectedEndpoints = []string{"/", healthendpoint.LIVELINESS_PATH, - healthendpoint.READINESS_PATH, healthendpoint.PPROF_PATH, healthendpoint.PROMETHEUS_PATH} + basicAuthConfig.Health.UnprotectedEndpoints = []string{"/", routes.LivenessPath, + routes.ReadinessPath, routes.PprofPath, routes.PrometheusPath} runner.configPath = writeConfig(&basicAuthConfig).Name() runner.Start() }) @@ -219,7 +219,7 @@ var _ = Describe("Api", func() { }) Context("when a request to query health comes", func() { It("returns with a 200", func() { - url := fmt.Sprintf("http://127.0.0.1:%d%s", healthport, healthendpoint.PROMETHEUS_PATH) + url := fmt.Sprintf("http://127.0.0.1:%d%s", healthport, routes.PrometheusPath) rsp, err := healthHttpClient.Get(url) Expect(err).NotTo(HaveOccurred()) Expect(rsp.StatusCode).To(Equal(http.StatusOK)) @@ -252,7 +252,7 @@ var _ = Describe("Api", func() { }) Context("when username and password are incorrect for basic authentication during health check", func() { It("should return 401", func() { - url := fmt.Sprintf("http://127.0.0.1:%d%s", healthport, healthendpoint.LIVELINESS_PATH) + url := fmt.Sprintf("http://127.0.0.1:%d%s", healthport, routes.LivenessPath) req, err := http.NewRequest(http.MethodGet, url, nil) Expect(err).NotTo(HaveOccurred()) @@ -266,7 +266,7 @@ var _ = Describe("Api", func() { Context("when username and password are correct for basic authentication during health check", func() { It("should return 200", func() { - url := fmt.Sprintf("http://127.0.0.1:%d%s", healthport, healthendpoint.LIVELINESS_PATH) + url := fmt.Sprintf("http://127.0.0.1:%d%s", healthport, routes.LivenessPath) req, err := http.NewRequest(http.MethodGet, url, nil) Expect(err).NotTo(HaveOccurred()) diff --git a/src/autoscaler/healthendpoint/health_readiness_test.go b/src/autoscaler/healthendpoint/health_readiness_test.go index 609b431dd3..323a55da23 100644 --- a/src/autoscaler/healthendpoint/health_readiness_test.go +++ b/src/autoscaler/healthendpoint/health_readiness_test.go @@ -1,6 +1,7 @@ package healthendpoint_test import ( + "code.cloudfoundry.org/app-autoscaler/src/autoscaler/routes" "io" "net/http" "net/url" @@ -82,7 +83,7 @@ var _ = Describe("Health Readiness", func() { It("should require basic auth", func() { apitest.New(). Handler(healthRoute). - Get(healthendpoint.PROMETHEUS_PATH). + Get(routes.PrometheusPath). Expect(t). Status(http.StatusUnauthorized). End() @@ -100,7 +101,7 @@ var _ = Describe("Health Readiness", func() { It("should require basic auth", func() { apitest.New(). Handler(healthRoute). - Get(healthendpoint.PROMETHEUS_PATH). + Get(routes.PrometheusPath). Expect(t). Status(http.StatusUnauthorized). End() @@ -113,14 +114,14 @@ var _ = Describe("Health Readiness", func() { BeforeEach(func() { config.HealthCheckUsername = "" config.HealthCheckPassword = "" - config.UnprotectedEndpoints = []string{"", healthendpoint.LIVELINESS_PATH, - healthendpoint.READINESS_PATH, healthendpoint.PROMETHEUS_PATH, healthendpoint.PPROF_PATH} + config.UnprotectedEndpoints = []string{"", routes.LivenessPath, + routes.ReadinessPath, routes.PrometheusPath, routes.PprofPath} }) When("Prometheus Health or / endpoint is called", func() { It("should respond OK", func() { apitest.New().Debug(). Handler(healthRoute). - Get(healthendpoint.PROMETHEUS_PATH). + Get(routes.PrometheusPath). Expect(t). Status(http.StatusOK). Header("Content-Type", "text/plain; version=0.0.4; charset=utf-8"). @@ -136,22 +137,22 @@ var _ = Describe("Health Readiness", func() { }) }) When("/health/liveness endpoint is called", func() { - It("should response OK", func(){ + It("should response OK", func() { apitest.New().Debug(). - Handler(healthRoute). - Get(healthendpoint.LIVELINESS_PATH). - Expect(t). - Status(http.StatusOK). - Header("Content-Type", "application/json"). - Body(`{"overall_status" : "UP", "checks" : [] }`). - End() + Handler(healthRoute). + Get(routes.LivenessPath). + Expect(t). + Status(http.StatusOK). + Header("Content-Type", "application/json"). + Body(`{"overall_status" : "UP", "checks" : [] }`). + End() }) }) When("/health/readiness endpoint is called", func() { It("should response OK", func() { apitest.New().Debug(). Handler(healthRoute). - Get(healthendpoint.READINESS_PATH). + Get(routes.ReadinessPath). Expect(t). Status(http.StatusOK). Header("Content-Type", "application/json"). @@ -164,7 +165,7 @@ var _ = Describe("Health Readiness", func() { It("should respond with 404", func() { apitest.New().Debug(). Handler(healthRoute). - Get(healthendpoint.READINESS_PATH). + Get(routes.ReadinessPath). Expect(t). Status(http.StatusNotFound). End() @@ -177,7 +178,7 @@ var _ = Describe("Health Readiness", func() { It("should return 401", func() { apitest.New(). Handler(healthRoute). - Get(healthendpoint.READINESS_PATH). + Get(routes.ReadinessPath). Expect(t). Status(http.StatusUnauthorized). End() @@ -188,7 +189,7 @@ var _ = Describe("Health Readiness", func() { It("should have json response", func() { apitest.New(). Handler(healthRoute). - Get(healthendpoint.READINESS_PATH). + Get(routes.ReadinessPath). BasicAuth("test-user-name", "test-user-password"). Expect(t). Status(http.StatusOK). @@ -208,7 +209,7 @@ var _ = Describe("Health Readiness", func() { It("should have database check passing", func() { apitest.New(). Handler(healthRoute). - Get(healthendpoint.READINESS_PATH). + Get(routes.ReadinessPath). BasicAuth("test-user-name", "test-user-password"). Expect(t). Status(http.StatusOK). @@ -222,7 +223,7 @@ var _ = Describe("Health Readiness", func() { It("should cache health result", func() { apitest.New(). Handler(healthRoute). - Get(healthendpoint.READINESS_PATH). + Get(routes.ReadinessPath). BasicAuth("test-user-name", "test-user-password"). Expect(t). Status(http.StatusOK). @@ -232,7 +233,7 @@ var _ = Describe("Health Readiness", func() { timesetter = &(tmsttr) apitest.New(). Handler(healthRoute). - Get(healthendpoint.READINESS_PATH). + Get(routes.ReadinessPath). BasicAuth("test-user-name", "test-user-password"). Expect(t). Status(http.StatusOK). @@ -242,7 +243,7 @@ var _ = Describe("Health Readiness", func() { It("should expire the cache entry after 30 seconds", func() { apitest.New(). Handler(healthRoute). - Get(healthendpoint.READINESS_PATH). + Get(routes.ReadinessPath). BasicAuth("test-user-name", "test-user-password"). Expect(t). Status(http.StatusOK). @@ -252,7 +253,7 @@ var _ = Describe("Health Readiness", func() { timesetter = &(tmsttr) apitest.New(). Handler(healthRoute). - Get(healthendpoint.READINESS_PATH). + Get(routes.ReadinessPath). BasicAuth("test-user-name", "test-user-password"). Expect(t). Status(http.StatusOK). @@ -269,7 +270,7 @@ var _ = Describe("Health Readiness", func() { It("should respond with 404", func() { apitest.New().Debug(). Handler(healthRoute). - Get(healthendpoint.READINESS_PATH). + Get(routes.ReadinessPath). Expect(t). Status(http.StatusNotFound). End() @@ -288,7 +289,7 @@ var _ = Describe("Health Readiness", func() { It("should have overall status down", func() { apitest.New(). Handler(healthRoute). - Get(healthendpoint.READINESS_PATH). + Get(routes.ReadinessPath). BasicAuth("test-user-name", "test-user-password"). Expect(t). Status(http.StatusOK). @@ -323,7 +324,7 @@ var _ = Describe("Health Readiness", func() { mu.RLock() apitest.New(). Handler(healthRoute). - Get(healthendpoint.READINESS_PATH). + Get(routes.ReadinessPath). BasicAuth("test-user-name", "test-user-password"). Expect(t). Status(http.StatusOK). @@ -341,7 +342,7 @@ var _ = Describe("Health Readiness", func() { It("should return 401", func() { apitest.New(). Handler(healthRoute). - Get(healthendpoint.LIVELINESS_PATH). + Get(routes.LivenessPath). Expect(t). Status(http.StatusUnauthorized). End() @@ -352,7 +353,7 @@ var _ = Describe("Health Readiness", func() { It("should have json response", func() { apitest.New(). Handler(healthRoute). - Get(healthendpoint.LIVELINESS_PATH). + Get(routes.LivenessPath). BasicAuth("test-user-name", "test-user-password"). Expect(t). Status(http.StatusOK). @@ -367,7 +368,7 @@ var _ = Describe("Health Readiness", func() { It("should require basic auth", func() { apitest.New(). Handler(healthRoute). - Get(healthendpoint.PROMETHEUS_PATH). + Get(routes.PrometheusPath). Expect(t). Status(http.StatusUnauthorized). End() @@ -402,8 +403,8 @@ var _ = Describe("Health Readiness", func() { BeforeEach(func() { config.HealthCheckUsername = "" config.HealthCheckPassword = "" - config.UnprotectedEndpoints = []string{"", healthendpoint.LIVELINESS_PATH, - healthendpoint.READINESS_PATH, healthendpoint.PROMETHEUS_PATH, healthendpoint.PPROF_PATH} + config.UnprotectedEndpoints = []string{"", routes.LivenessPath, + routes.ReadinessPath, routes.PrometheusPath, routes.PprofPath} }) It("should work", func() { apitest.New(). diff --git a/src/autoscaler/healthendpoint/server.go b/src/autoscaler/healthendpoint/server.go index f412a3262f..7a095f78e6 100644 --- a/src/autoscaler/healthendpoint/server.go +++ b/src/autoscaler/healthendpoint/server.go @@ -1,6 +1,7 @@ package healthendpoint import ( + "code.cloudfoundry.org/app-autoscaler/src/autoscaler/routes" "fmt" "net/http" "net/http/pprof" @@ -21,13 +22,6 @@ import ( "golang.org/x/crypto/bcrypt" ) -const ( - LIVELINESS_PATH string = "/health/liveliness" - READINESS_PATH string = "/health/readiness" - PPROF_PATH string = "/debug/pprof" - PROMETHEUS_PATH string = "/health/prometheus" -) - // basic authentication credentials struct type basicAuthenticationMiddleware struct { usernameHash []byte @@ -114,12 +108,12 @@ func addLivelinessHandlers(conf models.HealthConfig, mainRouter *mux.Router, tim authMiddleware *basicAuthenticationMiddleware) error { livenessHandler := common.VarsFunc(readiness([]Checker{}, time)) - livenessRouter := mainRouter.PathPrefix(LIVELINESS_PATH).Subrouter() + livenessRouter := mainRouter.PathPrefix(routes.LivenessPath).Subrouter() - if endpointsNeedsProtection(LIVELINESS_PATH, conf) { + if endpointsNeedsProtection(routes.LivenessPath, conf) { if !conf.BasicAuthPossible() { msg := "Basic authentication required for endpoint %s, but credentials not set up properly." - return fmt.Errorf(msg, LIVELINESS_PATH) + return fmt.Errorf(msg, routes.LivenessPath) } livenessRouter.Use(authMiddleware.middleware) } @@ -137,11 +131,11 @@ func addReadinessHandler(conf models.HealthConfig, mainRouter *mux.Router, ) error { readinessRouter := mainRouter.PathPrefix("/health").Subrouter() - if endpointsNeedsProtection(READINESS_PATH, conf) { + if endpointsNeedsProtection(routes.ReadinessPath, conf) { readinessRouter.Use(authMiddleware.middleware) if !conf.BasicAuthPossible() { msg := "Basic authentication required for endpoint %s, but credentials not set up properly." - return fmt.Errorf(msg, READINESS_PATH) + return fmt.Errorf(msg, routes.ReadinessPath) } } // unauthenticated route @@ -157,12 +151,12 @@ func addReadinessHandler(conf models.HealthConfig, mainRouter *mux.Router, func addPprofHandlers(conf models.HealthConfig, mainRouter *mux.Router, authMiddleware *basicAuthenticationMiddleware) error { - pprofRouter := mainRouter.PathPrefix(PPROF_PATH).Subrouter() + pprofRouter := mainRouter.PathPrefix(routes.PprofPath).Subrouter() - if endpointsNeedsProtection(PPROF_PATH, conf) { + if endpointsNeedsProtection(routes.PprofPath, conf) { if !conf.BasicAuthPossible() { msg := "Basic authentication required for endpoint %s, but credentials not set up properly." - return fmt.Errorf(msg, PPROF_PATH) + return fmt.Errorf(msg, routes.PprofPath) } pprofRouter.Use(authMiddleware.middleware) } @@ -185,12 +179,12 @@ func addPrometheusHandler(mainRouter *mux.Router, conf models.HealthConfig, promHandler := promhttp.HandlerFor(gatherer, promhttp.HandlerOpts{}) - prometheusRouter := mainRouter.PathPrefix(PROMETHEUS_PATH).Subrouter() - if endpointsNeedsProtection(PROMETHEUS_PATH, conf) { + prometheusRouter := mainRouter.PathPrefix(routes.PrometheusPath).Subrouter() + if endpointsNeedsProtection(routes.PrometheusPath, conf) { prometheusRouter.Use(authMiddleware.middleware) if !conf.BasicAuthPossible() { msg := "Basic authentication required for endpoint %s, but credentials not set up properly." - return fmt.Errorf(msg, PROMETHEUS_PATH) + return fmt.Errorf(msg, routes.PrometheusPath) } } // unauthenticated routes diff --git a/src/autoscaler/routes/routes.go b/src/autoscaler/routes/routes.go index 532fa65292..4d42c4870d 100644 --- a/src/autoscaler/routes/routes.go +++ b/src/autoscaler/routes/routes.go @@ -60,6 +60,10 @@ const ( PublicApiInfoRouteName = "GetPublicApiInfo" PublicApiHealthPath = "/health" + LivenessPath = "/health/liveness" + ReadinessPath = "/health/readiness" + PrometheusPath = "/health/prometheus" + PprofPath = "/debug/pprof" PublicApiHealthRouteName = "GetPublicApiHealth" ) @@ -165,6 +169,3 @@ func ApiPolicyRoutes() *mux.Router { func ApiCredentialRoutes() *mux.Router { return autoScalerRouteInstance.apiCredentialRoutes } - -// TODO: Add the generation for the health endpoints? At least to the constants -// at the top of this file? \ No newline at end of file