Skip to content

Commit

Permalink
Rename health-endpoint and variables;
Browse files Browse the repository at this point in the history
  • Loading branch information
joergdw authored and asalan316 committed Mar 8, 2023
1 parent 6c251f3 commit 445429d
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 57 deletions.
12 changes: 6 additions & 6 deletions src/autoscaler/api/cmd/api/api_test.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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()
})
Expand All @@ -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))
Expand Down Expand Up @@ -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())

Expand All @@ -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())

Expand Down
61 changes: 31 additions & 30 deletions src/autoscaler/healthendpoint/health_readiness_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package healthendpoint_test

import (
"code.cloudfoundry.org/app-autoscaler/src/autoscaler/routes"
"io"
"net/http"
"net/url"
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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").
Expand All @@ -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").
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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).
Expand All @@ -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).
Expand All @@ -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).
Expand All @@ -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).
Expand All @@ -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).
Expand All @@ -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).
Expand All @@ -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()
Expand All @@ -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).
Expand Down Expand Up @@ -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).
Expand All @@ -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()
Expand All @@ -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).
Expand All @@ -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()
Expand Down Expand Up @@ -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().
Expand Down
30 changes: 12 additions & 18 deletions src/autoscaler/healthendpoint/server.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package healthendpoint

import (
"code.cloudfoundry.org/app-autoscaler/src/autoscaler/routes"
"fmt"
"net/http"
"net/http/pprof"
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
Expand All @@ -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
Expand All @@ -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)
}
Expand All @@ -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
Expand Down
7 changes: 4 additions & 3 deletions src/autoscaler/routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ const (
PublicApiInfoRouteName = "GetPublicApiInfo"

PublicApiHealthPath = "/health"
LivenessPath = "/health/liveness"
ReadinessPath = "/health/readiness"
PrometheusPath = "/health/prometheus"
PprofPath = "/debug/pprof"
PublicApiHealthRouteName = "GetPublicApiHealth"
)

Expand Down Expand Up @@ -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?

0 comments on commit 445429d

Please sign in to comment.