-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
75 lines (61 loc) · 2.16 KB
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Package api exposes an http.Handler which registers all routes necessary to act as an OSB-compatible router.
// It's the main entrypoint to the public API of crossplace-service-broker.
package api
import (
"io"
"net/http"
"code.cloudfoundry.org/lager"
"github.com/gorilla/mux"
"github.com/pascaldekloe/jwt"
"github.com/pivotal-cf/brokerapi/v8"
"github.com/pivotal-cf/brokerapi/v8/domain"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/vshn/crossplane-service-broker/pkg/api/auth"
"github.com/vshn/crossplane-service-broker/pkg/reqcontext"
)
// API is a http.Handler
type API struct {
r *mux.Router
logger lager.Logger
}
// New creates a new API
func New(sb domain.ServiceBroker, brokerCredentials []auth.Credential, jwtSigningKeys *jwt.KeyRegister, logger lager.Logger) *API {
rootRouter := mux.NewRouter()
rootRouter.
HandleFunc("/healthz", func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = io.WriteString(w, `{"status": "ok"}`)
}).
Methods(http.MethodGet)
rootRouter.
Handle("/metrics", promhttp.Handler()).
Methods(http.MethodGet)
serviceBrokerAuthMiddleware := auth.New(brokerCredentials, jwtSigningKeys)
sbRoutes := brokerapi.NewWithCustomAuth(sb, logger, serviceBrokerAuthMiddleware.Handler)
sbRouter := sbRoutes.(*mux.Router)
sbRouter.Use(LoggerMiddleware(logger))
rootRouter.NewRoute().Handler(sbRoutes)
return &API{rootRouter, logger}
}
func (a *API) ServeHTTP(w http.ResponseWriter, req *http.Request) {
a.r.ServeHTTP(w, req)
}
// LoggerMiddleware logs a debug log with headers.
func LoggerMiddleware(logger lager.Logger) mux.MiddlewareFunc {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
rctx := reqcontext.NewReqContext(req.Context(), logger, nil)
headers := req.Header.Clone()
if a := headers.Get("Authorization"); a != "" {
headers.Set("Authorization", "****")
}
rctx.Logger.Debug("debug-headers", lager.Data{
"headers": headers,
"URI": req.RequestURI,
"method": req.Method,
})
next.ServeHTTP(w, req)
})
}
}