-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathmonitor_handler.go
70 lines (57 loc) · 1.75 KB
/
monitor_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
package main
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"time"
log "github.com/sirupsen/logrus"
)
const monitorAuthID = "monitor"
// MonitoringInputData is the data signed by the monitoring handler
var MonitoringInputData = []byte(`AUTOGRAPH MONITORING`)
func (m *monitor) handleMonitor(w http.ResponseWriter, r *http.Request) {
rid := getRequestID(r)
starttime := time.Now()
userid, err := m.authorize(r, []byte(""))
if err != nil {
httpError(w, r, http.StatusUnauthorized, "authorization verification failed: %v", err)
return
}
if userid != monitorAuthID {
httpError(w, r, http.StatusUnauthorized, "user is not permitted to call this endpoint")
return
}
// Wait until the results have been populated with an initial check
<-m.initialized
m.RLock()
defer m.RUnlock()
var failures []string
for _, errstr := range m.sigerrstrs {
if errstr != "" {
failures = append(failures, fmt.Sprintf("%d. %s", len(failures)+1, errstr))
}
}
if len(failures) > 0 {
httpError(w, r, http.StatusInternalServerError, "Errors encountered during signing: %s", strings.Join(failures, "\n"))
return
}
if m.debug {
log.Printf("signature response: %s", m.sigresps)
}
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
enc := json.NewEncoder(w)
for _, response := range m.sigresps {
response.X5U = rewriteLocalX5U(r, response.SignerID, response.X5U)
if err := enc.Encode(&response); err != nil {
httpError(w, r, http.StatusInternalServerError, "encoding failed with error: %v", err)
return
}
}
log.WithFields(log.Fields{
"rid": rid,
"user_id": userid,
"t": int32(time.Since(starttime) / time.Millisecond), // request processing time in ms
}).Info("monitoring operation succeeded")
}