added an option to enable GCP healthcheck endpoints

This commit is contained in:
timothy-spencer 2019-03-20 14:29:44 -07:00
parent ca89bb833d
commit 3476daf322
No known key found for this signature in database
GPG Key ID: B9DC267D2CB410CD
3 changed files with 31 additions and 5 deletions

17
http.go
View File

@ -24,6 +24,23 @@ func (s *Server) ListenAndServe() {
}
}
// gcpHealthcheck handles healthcheck queries from GCP
func gcpHealthcheck(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.EscapedPath() == "/liveness_check" {
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
return
}
if r.URL.EscapedPath() == "/readiness_check" {
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
return
}
h.ServeHTTP(w, r)
})
}
// ServeHTTP constructs a net.Listener and starts handling HTTP requests
func (s *Server) ServeHTTP() {
HTTPAddress := s.Opts.HTTPAddress

10
main.go
View File

@ -5,6 +5,7 @@ import (
"fmt"
"log"
"math/rand"
"net/http"
"os"
"runtime"
"strings"
@ -92,6 +93,7 @@ func main() {
flagSet.String("acr-values", "http://idmanagement.gov/ns/assurance/loa/1", "acr values string: optional, used by login.gov")
flagSet.String("jwt-key", "", "private key used to sign JWT: required by login.gov")
flagSet.String("pubjwk-url", "", "JWK pubkey access endpoint: required by login.gov")
flagSet.Bool("gcp-healthchecks", false, "Enable GCP healthcheck endpoints")
flagSet.Parse(os.Args[1:])
@ -139,8 +141,14 @@ func main() {
rand.Seed(time.Now().UnixNano())
var myhandler http.Handler
if opts.GCPHealthChecks {
myhandler = gcpHealthcheck(LoggingHandler(os.Stdout, oauthproxy, opts.RequestLogging, opts.RequestLoggingFormat))
} else {
myhandler = LoggingHandler(os.Stdout, oauthproxy, opts.RequestLogging, opts.RequestLoggingFormat)
}
s := &Server{
Handler: LoggingHandler(os.Stdout, oauthproxy, opts.RequestLogging, opts.RequestLoggingFormat),
Handler: myhandler,
Opts: opts,
}
s.ListenAndServe()

View File

@ -90,6 +90,7 @@ type Options struct {
AcrValues string `flag:"acr-values" cfg:"acr_values" env:"OAUTH2_PROXY_ACR_VALUES"`
JWTKey string `flag:"jwt-key" cfg:"jwt_key" env:"OAUTH2_PROXY_JWT_KEY"`
PubJWKURL string `flag:"pubjwk-url" cfg:"pubjwk_url" env:"OAUTH2_PROXY_PUBJWK_URL"`
GCPHealthChecks bool `flag:"gcp-healthchecks" cfg:"gcp_healthchecks" env:"OAUTH2_PROXY_GCP_HEALTHCHECKS"`
// internal values that are set after config validation
redirectURL *url.URL