/
handlers.go
95 lines (73 loc) 路 2.32 KB
/
handlers.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
package server
import (
"context"
"encoding/json"
"fmt"
"net/http"
"github.com/Shopify/voucher"
"github.com/Shopify/voucher/cmd/config"
"github.com/gorilla/mux"
"github.com/spf13/viper"
)
func handleChecks(w http.ResponseWriter, r *http.Request, name ...string) {
var imageData voucher.ImageData
var err error
defer r.Body.Close()
if err = isAuthorized(r); nil != err {
http.Error(w, "username or password is incorrect", 401)
LogError(err)
return
}
w.Header().Set("content-type", "application/json")
LogRequests(r)
imageData, err = handleInput(r)
if nil != err {
http.Error(w, err.Error(), 422)
LogError(err)
return
}
context, cancel := context.WithTimeout(context.Background(), serverConfig.TimeoutDuration())
defer cancel()
metadataClient := config.NewMetadataClient(context)
checksuite, err := config.NewCheckSuite(metadataClient, name...)
if nil != err {
http.Error(w, "server has been misconfigured", 500)
LogError(err)
return
}
var results []voucher.CheckResult
if viper.GetBool("dryrun") {
results = checksuite.Run(imageData)
} else {
results = checksuite.RunAndAttest(metadataClient, imageData)
}
checkResponse := voucher.NewResponse(imageData, results)
LogResult(checkResponse)
err = json.NewEncoder(w).Encode(checkResponse)
if nil != err {
// if all else fails
http.Error(w, err.Error(), 500)
LogError(err)
return
}
}
// HandleAll is a request handler that makes the calls to create all attestations, this includes DIY, Nobody, Snakeoil
func HandleAll(w http.ResponseWriter, r *http.Request) {
handleChecks(w, r, config.EnabledChecks(voucher.ToMapStringBool(viper.GetStringMap("checks")))...)
}
// HandleIndividualCheck is a request handler that executes an individual check and creates an attestation if applicable.
func HandleIndividualCheck(w http.ResponseWriter, r *http.Request) {
variables := mux.Vars(r)
checkName := variables["check"]
if "" == checkName {
http.Error(w, "failure", 500)
return
}
if voucher.IsCheckFactoryRegistered(checkName) {
handleChecks(w, r, checkName)
return
}
http.Error(w, fmt.Sprintf("check %s is not available", checkName), 404)
}
// HandleHealthCheck is a request handler that returns HTTP Status Code 200 when it is called from shopify cloud
func HandleHealthCheck(w http.ResponseWriter, r *http.Request) { w.WriteHeader(200) }