forked from quay/claircore
/
handler.go
192 lines (172 loc) · 4.65 KB
/
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
package libindex
import (
"encoding/json"
"fmt"
"net/http"
"path"
"github.com/quay/zlog"
"go.opentelemetry.io/otel/baggage"
"go.opentelemetry.io/otel/label"
"github.com/Panzer1119/claircore"
"github.com/Panzer1119/claircore/pkg/jsonerr"
)
var _ http.Handler = (*HTTP)(nil)
type HTTP struct {
*http.ServeMux
l *Libindex
}
func NewHandler(l *Libindex) *HTTP {
h := &HTTP{l: l}
m := http.NewServeMux()
m.HandleFunc("/index_report", h.Index)
m.HandleFunc("/index_report/", h.IndexReport)
m.HandleFunc("/index_state", h.State)
m.HandleFunc("/affected_manifests", h.AffectedManifests)
h.ServeMux = m
return h
}
func (h *HTTP) AffectedManifests(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
if r.Method != http.MethodPost {
resp := &jsonerr.Response{
Code: "method-not-allowed",
Message: "endpoint only allows POST",
}
jsonerr.Error(w, resp, http.StatusMethodNotAllowed)
return
}
var vulnerabilities struct {
V []claircore.Vulnerability `json:"vulnerabilities"`
}
err := json.NewDecoder(r.Body).Decode(&vulnerabilities)
if err != nil {
resp := &jsonerr.Response{
Code: "bad-request",
Message: err.Error(),
}
jsonerr.Error(w, resp, http.StatusBadRequest)
return
}
affected, err := h.l.AffectedManifests(ctx, vulnerabilities.V)
if err != nil {
resp := &jsonerr.Response{
Code: "internal-server-error",
Message: err.Error(),
}
jsonerr.Error(w, resp, http.StatusInternalServerError)
return
}
json.NewEncoder(w).Encode(affected)
return
}
func (h *HTTP) State(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
w.Header().Set("content-type", "text/plain")
s, _ := h.l.State(ctx)
fmt.Fprintln(w, s)
}
func (h *HTTP) Index(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
if r.Method != http.MethodPost {
resp := &jsonerr.Response{
Code: "method-not-allowed",
Message: "endpoint only allows POST",
}
jsonerr.Error(w, resp, http.StatusMethodNotAllowed)
return
}
// deserialize manifest
var m claircore.Manifest
err := json.NewDecoder(r.Body).Decode(&m)
if err != nil {
resp := &jsonerr.Response{
Code: "bad-request",
Message: fmt.Sprintf("could not deserialize manifest: %v", err),
}
zlog.Debug(ctx).Err(err).Msg("could not deserialize manifest")
jsonerr.Error(w, resp, http.StatusBadRequest)
return
}
// call scan
ir, err := h.l.Index(ctx, &m)
if err != nil {
resp := &jsonerr.Response{
Code: "scan-error",
Message: fmt.Sprintf("failed to start scan: %v", err),
}
zlog.Error(ctx).
Err(err).
Msg("failed to start scan")
jsonerr.Error(w, resp, http.StatusInternalServerError)
return
}
w.Header().Set("location", path.Join(r.URL.Path, m.Hash.String()))
w.WriteHeader(http.StatusCreated)
if err := json.NewEncoder(w).Encode(&ir); err != nil {
zlog.Error(ctx).Err(err).Msg("failed to serialize results")
}
return
}
func (h *HTTP) IndexReport(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
// Added to Context later.
if r.Method != http.MethodGet {
resp := &jsonerr.Response{
Code: "method-not-allowed",
Message: "endpoint only allows GET",
}
jsonerr.Error(w, resp, http.StatusMethodNotAllowed)
return
}
// extract manifest from path
hashStr := path.Base(r.URL.Path)
if hashStr == "" {
resp := &jsonerr.Response{
Code: "bad-request",
Message: "could not find manifest hash in path",
}
zlog.Debug(ctx).Str("path", r.URL.Path).Msg(resp.Message)
jsonerr.Error(w, resp, http.StatusBadRequest)
return
}
hash, err := claircore.ParseDigest(hashStr)
if err != nil {
resp := &jsonerr.Response{
Code: "bad-request",
Message: "could not find manifest hash in path",
}
zlog.Debug(ctx).Str("path", r.URL.Path).Msg(resp.Message)
jsonerr.Error(w, resp, http.StatusBadRequest)
return
}
ctx = baggage.ContextWithValues(ctx,
label.String("manifest", hash.String()))
// issue retrieval
sr, ok, err := h.l.IndexReport(ctx, hash)
if err != nil {
const msg = "error receiving index report"
resp := &jsonerr.Response{
Code: "index-report",
Message: msg,
}
zlog.Warn(ctx).Err(err).Msg(msg)
jsonerr.Error(w, resp, http.StatusInternalServerError)
return
}
if !ok {
resp := &jsonerr.Response{
Code: "not-found",
Message: fmt.Sprintf("index report for %v does not exist", hash),
}
zlog.Debug(ctx).Msg("index report does not exist")
jsonerr.Error(w, resp, http.StatusNotFound)
return
}
w.Header().Set("content-type", "application/json")
// serialize and return scanresult
if err = json.NewEncoder(w).Encode(sr); err != nil {
const msg = "could not return index report"
zlog.Error(ctx).Err(err).Msg(msg)
// Too late to change our header, now.
}
}