-
Notifications
You must be signed in to change notification settings - Fork 11
/
broker.go
345 lines (282 loc) · 10.4 KB
/
broker.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// Copyright 2020-2021 Couchbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package broker
import (
"crypto/tls"
"encoding/base64"
"errors"
"fmt"
"net/http"
"strconv"
"strings"
"time"
"github.com/couchbase/service-broker/pkg/apis"
"github.com/couchbase/service-broker/pkg/client"
"github.com/couchbase/service-broker/pkg/config"
"github.com/couchbase/service-broker/pkg/log"
"github.com/golang/glog"
"github.com/julienschmidt/httprouter"
"k8s.io/client-go/kubernetes/scheme"
)
// ErrInternalError is returned when something really bad happened.
var ErrInternalError = errors.New("internal error")
// ErrRequestMalformed is returned when the request is not as we expect.
var ErrRequestMalformed = errors.New("request malformed")
// ErrRequestUnsupported is raised when something about the request is not supported.
var ErrRequestUnsupported = errors.New("request unsupported")
// ErrServiceUnready is raised when the service is not ready to run.
var ErrServiceUnready = errors.New("service not ready")
// ErrUnauthorized is raised when a user is not permitted to perform the request.
var ErrUnauthorized = errors.New("request is unauthorized")
// getHeader returns the header value for a header name.
func getHeader(r *http.Request, name string) ([]string, error) {
for headerName := range r.Header {
if strings.EqualFold(headerName, name) {
return r.Header[headerName], nil
}
}
return nil, fmt.Errorf("%w: no header found for %s", ErrRequestMalformed, name)
}
// getHeaderSingle returns the header value for a name.
// If the header has more than one value this is an error condition.
func getHeaderSingle(r *http.Request, name string) (string, error) {
headers, err := getHeader(r, name)
if err != nil {
return "", err
}
requiredHeaders := 1
if len(headers) != requiredHeaders {
return "", fmt.Errorf("%w: multiple headers found for %s", ErrRequestMalformed, name)
}
return headers[0], nil
}
// handleReadiness returns 503 until the configuration is correct.
func handleReadiness(w http.ResponseWriter) error {
if config.Config() == nil {
httpResponse(w, http.StatusServiceUnavailable)
return ErrServiceUnready
}
return nil
}
// handleBrokerBearerToken implements RFC-6750.
func handleBrokerBearerToken(c *ServerConfiguration, w http.ResponseWriter, r *http.Request) error {
header, err := getHeaderSingle(r, "Authorization")
if err != nil {
httpResponse(w, http.StatusUnauthorized)
return err
}
if header != "Bearer "+*c.Token {
httpResponse(w, http.StatusUnauthorized)
return fmt.Errorf("%w: authorization failed", ErrUnauthorized)
}
return nil
}
// handleBrokerBasicAuth implements RFC-7617.
func handleBrokerBasicAuth(c *ServerConfiguration, w http.ResponseWriter, r *http.Request) error {
header, err := getHeaderSingle(r, "Authorization")
if err != nil {
httpResponse(w, http.StatusUnauthorized)
return err
}
if header != "Basic "+base64.StdEncoding.EncodeToString([]byte(c.BasicAuth.Username+":"+c.BasicAuth.Password)) {
httpResponse(w, http.StatusUnauthorized)
return fmt.Errorf("%w: authorization failed", ErrUnauthorized)
}
return nil
}
// handleBrokerAPIHeader looks for and verifies the X-Broker-API-Version header.
func handleBrokerAPIHeader(w http.ResponseWriter, r *http.Request) error {
header, err := getHeaderSingle(r, "X-Broker-API-Version")
if err != nil {
httpResponse(w, http.StatusBadRequest)
return err
}
apiVersion, err := strconv.ParseFloat(header, 64)
if err != nil {
httpResponse(w, http.StatusBadRequest)
return fmt.Errorf("%w: malformed X-Broker-Api-Version header: %v", ErrRequestMalformed, err)
}
if apiVersion < minBrokerAPIVersion {
httpResponse(w, http.StatusPreconditionFailed)
return fmt.Errorf("%w: unsupported X-Broker-Api-Version header %v, requires at least %.2f", ErrRequestUnsupported, header, minBrokerAPIVersion)
}
return nil
}
// handleContentTypeHeader looks for and verifies the Content-Type header.
func handleContentTypeHeader(w http.ResponseWriter, r *http.Request) error {
// If no content is specified we don't need a type.
if r.ContentLength == 0 {
return nil
}
header, err := getHeaderSingle(r, "Content-Type")
if err != nil {
httpResponse(w, http.StatusBadRequest)
return err
}
if header != "application/json" {
httpResponse(w, http.StatusBadRequest)
return fmt.Errorf("%w: invalid Content-Type header: %s", ErrRequestMalformed, header)
}
return nil
}
// handleRequestHeaders checks that required headers are sent and are
// valid, and that content encodings are correct.
func handleRequestHeaders(c *ServerConfiguration, w http.ResponseWriter, r *http.Request) error {
switch {
case c.Token != nil:
if err := handleBrokerBearerToken(c, w, r); err != nil {
return err
}
case c.BasicAuth != nil:
if err := handleBrokerBasicAuth(c, w, r); err != nil {
return err
}
default:
httpResponse(w, http.StatusInternalServerError)
return ErrInternalError
}
if err := handleBrokerAPIHeader(w, r); err != nil {
return err
}
if err := handleContentTypeHeader(w, r); err != nil {
return err
}
return nil
}
// OpenServiceBrokerHandler wraps up a standard router but performs Open Service Broker
// specific checks before performing the routing, such as making sure the correct API
// version is being used and the cnntent type is correct.
type openServiceBrokerHandler struct {
http.Handler
configuration *ServerConfiguration
}
// NewOpenServiceBrokerHandler initializes the main router with the Open Service Broker API.
func NewOpenServiceBrokerHandler(configuration *ServerConfiguration) http.Handler {
router := httprouter.New()
router.GET("/readyz", handleReadyz)
router.GET("/v2/catalog", handleReadCatalog)
router.PUT("/v2/service_instances/:instance_id", handleCreateServiceInstance(configuration))
router.GET("/v2/service_instances/:instance_id", handleReadServiceInstance(configuration))
router.PATCH("/v2/service_instances/:instance_id", handleUpdateServiceInstance(configuration))
router.DELETE("/v2/service_instances/:instance_id", handleDeleteServiceInstance(configuration))
router.GET("/v2/service_instances/:instance_id/last_operation", handlePollServiceInstance(configuration))
router.PUT("/v2/service_instances/:instance_id/service_bindings/:binding_id", handleCreateServiceBinding(configuration))
router.DELETE("/v2/service_instances/:instance_id/service_bindings/:binding_id", handleDeleteServiceBinding(configuration))
return &openServiceBrokerHandler{
Handler: router,
configuration: configuration,
}
}
// responseWriter wraps the standard response writer so we can extract the response data.
type responseWriter struct {
writer http.ResponseWriter
status int
}
// Header returns a reference to the response headers.
func (w *responseWriter) Header() http.Header {
return w.writer.Header()
}
// Write writes out data after the headers have been written.
func (w *responseWriter) Write(body []byte) (int, error) {
return w.writer.Write(body)
}
// WriteHeader writes out the headers.
func (w *responseWriter) WriteHeader(statusCode int) {
w.status = statusCode
w.writer.WriteHeader(statusCode)
}
// ServeHTTP performs generic test on all API endpoints.
func (handler *openServiceBrokerHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Start the profiling timer.
start := time.Now()
// The configuration is global, and sadly we cannot pass it around as a variable
// so place a read lock on it for the duration of the request. Requests must
// therefore be non-blocking.
config.Lock()
defer config.Unlock()
// Use the wrapped writer so we can capture the status code etc.
writer := &responseWriter{
writer: w,
}
// Print out request logging information.
// DO NOT print out headers at info level as that will leak credentials into the log stream.
glog.Infof(`HTTP req: "%s %v %s" %s `, r.Method, r.URL, r.Proto, r.RemoteAddr)
for name, values := range r.Header {
for _, value := range values {
glog.V(log.LevelDebug).Infof(`HTTP hdr: "%s: %s"`, name, value)
}
}
defer func() {
glog.Infof(`HTTP rsp: "%d %s" %v`, writer.status, http.StatusText(writer.status), time.Since(start))
}()
// Indicate that the service is not ready until configured.
if err := handleReadiness(writer); err != nil {
glog.V(log.LevelDebug).Info(err)
return
}
// Ignore security checks for the readiness handler
if r.URL.Path != "/readyz" {
// Process headers, API versions, content types.
if err := handleRequestHeaders(handler.configuration, writer, r); err != nil {
glog.V(log.LevelDebug).Info(err)
return
}
}
// Route and process the request.
handler.Handler.ServeHTTP(writer, r)
}
// ServerConfigurationBasicAuth defines basic authentication.
type ServerConfigurationBasicAuth struct {
// Username is the user API requests will require.
Username string
// Password is the password for the user.
Password string
}
// ServerConfiguration is used to propagate server configuration to the server instance
// and its handlers.
type ServerConfiguration struct {
// Namespace is the namespace the broker is running in.
Namespace string
// Token is set when using bearer token authentication.
Token *string
// BasicAuth is set when using basic authentication.
BasicAuth *ServerConfigurationBasicAuth
// Certificate is the TLS key/certificate to serve with.
Certificate tls.Certificate
}
// ConfigureServer is the main entry point for both the container and test.
func ConfigureServer(clients client.Clients, configuration *ServerConfiguration) error {
// Static configuration.
if err := apis.AddToScheme(scheme.Scheme); err != nil {
return err
}
// Setup globals.
if err := config.Configure(clients, configuration.Namespace); err != nil {
return err
}
return nil
}
func RunServer(configuration *ServerConfiguration) error {
// Start the server.
server := &http.Server{
Addr: ":8443",
Handler: NewOpenServiceBrokerHandler(configuration),
TLSConfig: &tls.Config{
Certificates: []tls.Certificate{
configuration.Certificate,
},
},
}
return server.ListenAndServeTLS("", "")
}