-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.go
More file actions
108 lines (96 loc) · 2.69 KB
/
server.go
File metadata and controls
108 lines (96 loc) · 2.69 KB
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
package main
import (
"context"
"fmt"
"log"
"net/http"
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
// ServeOptions has options for Serve()
type ServeOptions struct {
locker *remoteLocker
PORT string
notifierURL string
jar string
}
// Serve creates the web server
func Serve(port string, locker *remoteLocker) context.Context {
router := mux.NewRouter()
endpoints := []string{}
router.HandleFunc("/healthz", healthz).Methods("GET")
endpoints = append(endpoints, "/healthz")
router.HandleFunc("/readiness", readiness(locker)).Methods("GET")
endpoints = append(endpoints, "/readiness")
router.Handle("/metrics", promhttp.Handler())
endpoints = append(endpoints, "/metrics")
ctx := context.Background()
srv := &http.Server{Addr: fmt.Sprintf(":%s", port), Handler: router}
go func() {
<-ctx.Done()
srv.Shutdown(ctx)
}()
go func() {
log.Println("Listening on port", port)
log.Println("Endpoints:")
for _, x := range endpoints {
log.Println(x)
}
if err := srv.ListenAndServe(); err != http.ErrServerClosed {
// unexpected error. port in use?
log.Fatalf("ListenAndServe(): %v", err)
}
}()
return ctx
}
func healthz(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
w.Write([]byte("ok"))
}
func readiness(locker *remoteLocker) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
if locker.EvidencePath != "" {
http.Error(w, "not ready", http.StatusServiceUnavailable)
return
}
w.Write([]byte("ok"))
}
}
func processPayloads(ctx context.Context, payloads []Job, jar string, locker *remoteLocker, notifierURL string) {
metrics := createIpedMetrics()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
var err error
for _, payload := range payloads {
select {
case <-ctx.Done():
if err != nil {
log.Fatalf("error: %v\n", err)
}
return
default:
params := ipedParams{
jar: jar,
evidence: payload.EvidencePath,
output: payload.OutputPath,
profile: payload.Profile,
additionalArgs: payload.AdditionalArgs,
additionalPaths: payload.AdditionalPaths,
mvPath: payload.mvPath,
}
err = runIped(params, locker, notifierURL, metrics)
if err != nil {
fmt.Printf("error: %v\n", err)
}
}
}
}
type Job struct {
EvidencePath string `json:"evidencePath,omitempty"`
OutputPath string `json:"outputPath,omitempty"`
Profile string `json:"profile,omitempty"`
AdditionalArgs string `json:"additionalArgs,omitempty"`
AdditionalPaths string `json:"additionalPaths,omitempty"`
MvPath string `json:"mvPath,omitempty"`
}