-
Notifications
You must be signed in to change notification settings - Fork 13
/
request_handler.go
366 lines (297 loc) · 10.1 KB
/
request_handler.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package server
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"net/http"
"regexp"
"strings"
"github.com/cloudfoundry/bosh-utils/errors"
"github.com/cloudfoundry/config-server/store"
"github.com/cloudfoundry/config-server/types"
)
type requestHandler struct {
store store.Store
valueGeneratorFactory types.ValueGeneratorFactory
}
func NewRequestHandler(store store.Store, valueGeneratorFactory types.ValueGeneratorFactory) (http.Handler, error) {
if store == nil {
return nil, errors.Error("Data store must be set")
}
return requestHandler{
store: store,
valueGeneratorFactory: valueGeneratorFactory,
}, nil
}
func (handler requestHandler) ServeHTTP(resWriter http.ResponseWriter, req *http.Request) {
switch req.Method {
case "GET":
handler.handleGet(resWriter, req)
case "PUT":
handler.handlePut(resWriter, req)
case "POST":
handler.handlePost(resWriter, req)
case "DELETE":
handler.handleDelete(resWriter, req)
default:
http.Error(resWriter, NewErrorResponse(errors.Error("HTTP method not allowed")).GenerateErrorMsg(), http.StatusMethodNotAllowed)
}
}
func (handler requestHandler) handleGet(resWriter http.ResponseWriter, req *http.Request) {
id, idErr := extractIDFromURLPath(req.URL.Path)
if idErr == nil {
handler.handleGetByID(id, resWriter)
} else {
name := req.URL.Query().Get("name")
if len(name) == 0 {
http.Error(resWriter, NewErrorResponse(idErr).GenerateErrorMsg(), http.StatusBadRequest)
} else {
handler.handleGetByName(name, resWriter)
}
}
}
func (handler requestHandler) handleGetByID(id string, resWriter http.ResponseWriter) {
value, err := handler.store.GetByID(id)
if err != nil {
http.Error(resWriter, NewErrorResponse(err).GenerateErrorMsg(), http.StatusInternalServerError)
return
}
emptyValue := store.Configuration{}
if value == emptyValue {
http.Error(resWriter, NewErrorResponse(errors.Errorf("ID '%s' not found", id)).GenerateErrorMsg(), http.StatusNotFound)
} else {
result, _ := value.StringifiedJSON()
respond(resWriter, result, http.StatusOK)
}
}
func (handler requestHandler) handleGetByName(name string, resWriter http.ResponseWriter) {
if isNameValid, nameError := isValidName(name); !isNameValid {
http.Error(resWriter, NewErrorResponse(nameError).GenerateErrorMsg(), http.StatusBadRequest)
return
}
values, err := handler.store.GetByName(name)
if err != nil {
http.Error(resWriter, NewErrorResponse(err).GenerateErrorMsg(), http.StatusInternalServerError)
return
}
if len(values) == 0 {
http.Error(resWriter, NewErrorResponse(errors.Errorf("Name '%s' not found", name)).GenerateErrorMsg(), http.StatusNotFound)
} else {
result, err := values.StringifiedJSON()
if err == nil {
respond(resWriter, result, http.StatusOK)
}
}
}
func (handler requestHandler) handlePut(resWriter http.ResponseWriter, req *http.Request) {
if contentTypeErr := validateRequestContentType(req); contentTypeErr != nil {
http.Error(resWriter, NewErrorResponse(contentTypeErr).GenerateErrorMsg(), http.StatusUnsupportedMediaType)
return
}
name, value, err := readPutRequest(req)
if err != nil {
http.Error(resWriter, NewErrorResponse(err).GenerateErrorMsg(), http.StatusBadRequest)
return
}
configuration, err := handler.saveToStore(name, value, "")
if err != nil {
http.Error(resWriter, NewErrorResponse(err).GenerateErrorMsg(), http.StatusInternalServerError)
return
}
result, _ := configuration.StringifiedJSON()
respond(resWriter, result, http.StatusOK)
}
func (handler requestHandler) handlePost(resWriter http.ResponseWriter, req *http.Request) {
if contentTypeErr := validateRequestContentType(req); contentTypeErr != nil {
http.Error(resWriter, NewErrorResponse(contentTypeErr).GenerateErrorMsg(), http.StatusUnsupportedMediaType)
return
}
name, generatorType, parameters, mode, err := readPostRequest(req)
if err != nil {
http.Error(resWriter, NewErrorResponse(err).GenerateErrorMsg(), http.StatusBadRequest)
return
}
values, err := handler.store.GetByName(name)
if err != nil {
http.Error(resWriter, NewErrorResponse(err).GenerateErrorMsg(), http.StatusInternalServerError)
return
}
checksum, err := handler.calculateChecksum(parameters)
if err != nil {
http.Error(resWriter, NewErrorResponse(err).GenerateErrorMsg(), http.StatusInternalServerError)
}
if len(values) != 0 {
configuration := values[0]
if "converge" != mode || checksum == configuration.ParameterChecksum {
result, err := configuration.StringifiedJSON()
if err != nil {
http.Error(resWriter, NewErrorResponse(err).GenerateErrorMsg(), http.StatusInternalServerError)
} else {
respond(resWriter, result, http.StatusOK)
}
return
}
}
generator, err := handler.valueGeneratorFactory.GetGenerator(generatorType)
if err != nil {
http.Error(resWriter, NewErrorResponse(err).GenerateErrorMsg(), http.StatusBadRequest)
return
}
generatedValue, err := generator.Generate(parameters)
if err != nil {
http.Error(resWriter, NewErrorResponse(err).GenerateErrorMsg(), http.StatusBadRequest)
return
}
configuration, err := handler.saveToStore(name, generatedValue, checksum)
if err != nil {
http.Error(resWriter, NewErrorResponse(err).GenerateErrorMsg(), http.StatusInternalServerError)
return
}
result, _ := configuration.StringifiedJSON()
respond(resWriter, result, http.StatusCreated)
}
func (handler requestHandler) calculateChecksum(v interface{}) (string, error) {
result, err := json.Marshal(v)
if err != nil {
return "", errors.WrapError(err, "Calculating checksum:")
}
checksum := sha256.New().Sum(result)
return hex.EncodeToString(checksum), nil
}
func (handler requestHandler) handleDelete(resWriter http.ResponseWriter, req *http.Request) {
name := req.URL.Query().Get("name")
if isNameValid, nameError := isValidName(name); !isNameValid {
http.Error(resWriter, NewErrorResponse(nameError).GenerateErrorMsg(), http.StatusBadRequest)
return
}
deleted, err := handler.store.Delete(name)
if err == nil {
if deleted == 0 {
http.Error(resWriter, NewErrorResponse(errors.Errorf("Name '%s' not found", name)).GenerateErrorMsg(), http.StatusNotFound)
} else {
respond(resWriter, "", http.StatusNoContent)
}
} else {
http.Error(resWriter, NewErrorResponse(err).GenerateErrorMsg(), http.StatusInternalServerError)
}
}
func (handler requestHandler) saveToStore(name string, value interface{}, checksum string) (store.Configuration, error) {
configValue := make(map[string]interface{})
configValue["value"] = value
bytes, err := json.Marshal(&configValue)
if err != nil {
return store.Configuration{}, err
}
id, err := handler.store.Put(name, string(bytes), checksum)
if err != nil {
return store.Configuration{}, err
}
configuration, err := handler.store.GetByID(id)
return configuration, err
}
func respond(res http.ResponseWriter, message string, status int) {
res.WriteHeader(status)
_, err := res.Write([]byte(message))
if err != nil {
res.WriteHeader(http.StatusInternalServerError)
}
}
func readPutRequest(req *http.Request) (string, interface{}, error) {
jsonMap, err := readJSONBody(req)
if err != nil {
return "", nil, err
}
name, err := getStringValueFromJSONBody(jsonMap, "name")
if err != nil {
return "", nil, err
}
if isNameValid, nameError := isValidName(name); !isNameValid {
return "", nil, nameError
}
value, keyExists := jsonMap["value"]
if !keyExists {
return "", nil, errors.Error("JSON request body should contain the key 'value'")
}
return name, value, nil
}
func readPostRequest(req *http.Request) (string, string, interface{}, string, error) {
jsonMap, err := readJSONBody(req)
if err != nil {
return "", "", nil, "", err
}
name, err := getStringValueFromJSONBody(jsonMap, "name")
if err != nil {
return name, "", nil, "", err
}
generatorType, err := getStringValueFromJSONBody(jsonMap, "type")
if err != nil {
return name, generatorType, nil, "", err
}
mode, err := getOptionalStringValueFromJSONBody(jsonMap, "mode", "no-overwrite")
if err != nil {
return name, generatorType, nil, "", err
}
return name, generatorType, jsonMap["parameters"], mode, nil
}
func getOptionalStringValueFromJSONBody(jsonMap map[string]interface{}, keyName string, defaultValue string) (string, error) {
value, keyExists := jsonMap[keyName]
if !keyExists {
return defaultValue, nil
}
switch typeOfValue := value.(type) {
case string:
return typeOfValue, nil
default:
return "", errors.Error(fmt.Sprintf("JSON request body key '%s' must be of type string", keyName))
}
}
func getStringValueFromJSONBody(jsonMap map[string]interface{}, keyName string) (string, error) {
value, keyExists := jsonMap[keyName]
if !keyExists {
return "", errors.Error(fmt.Sprintf("JSON request body should contain the key '%s'", keyName))
}
switch typeOfValue := value.(type) {
case string:
return typeOfValue, nil
default:
return "", errors.Error(fmt.Sprintf("JSON request body key '%s' must be of type string", keyName))
}
}
func readJSONBody(req *http.Request) (map[string]interface{}, error) {
if req == nil {
return nil, errors.Error("Request can't be nil")
}
if req.Body == nil {
return nil, errors.Error("Request can't be empty")
}
var f interface{}
if err := json.NewDecoder(req.Body).Decode(&f); err != nil {
return nil, errors.Error("Request Body should be JSON string")
}
return f.(map[string]interface{}), nil
}
func extractIDFromURLPath(path string) (string, error) {
paths := strings.Split(strings.Trim(path, "/"), "/")
if len(paths) < 3 {
return "", errors.Error("Request URL invalid, seems to be missing ID")
}
id := paths[len(paths)-1]
if len(id) == 0 {
return "", errors.Error("Request URL invalid, seems to be missing ID")
}
return id, nil
}
func isValidName(name string) (bool, error) {
var validNameToken = regexp.MustCompile(`^[a-zA-Z0-9_\-/]+$`)
if !validNameToken.MatchString(name) {
return false, errors.Error("Name must consist of alphanumeric, underscores, dashes, and forward slashes")
}
return true, nil
}
func validateRequestContentType(req *http.Request) error {
if !strings.EqualFold(req.Header.Get("content-type"), "application/json") {
return errors.Error("Unsupported Media Type - Accepts application/json only")
}
return nil
}