-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.go
87 lines (74 loc) · 1.9 KB
/
main.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
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"path/filepath"
"github.com/google/go-containerregistry/pkg/name"
"github.com/julienschmidt/httprouter"
"github.com/sigstore/cosign/pkg/cosign"
"github.com/sigstore/cosign/pkg/cosign/fulcio"
)
type ImageVerificationReq struct {
Image string
}
type ImageVerificationResp struct {
Verified bool `json:"verified"`
VerificationMessage string `json:"verification_message"`
}
func Verify(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var body ImageVerificationReq
err := json.NewDecoder(r.Body).Decode(&body)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
ctx := context.TODO()
wDir, err := os.Getwd()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
key, err := cosign.LoadPublicKey(ctx, filepath.Join(wDir, "cosign.pub"))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
ref, err := name.ParseReference(body.Image)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
co := &cosign.CheckOpts{
RootCerts: fulcio.Roots,
SigVerifier: key,
Claims: true,
}
var resp ImageVerificationResp
if _, err = cosign.Verify(ctx, ref, co); err != nil {
resp = ImageVerificationResp{
Verified: false,
VerificationMessage: err.Error(),
}
} else {
resp = ImageVerificationResp{
Verified: true,
VerificationMessage: fmt.Sprintf("valid signatures found for an image: %s", body.Image),
}
}
respAsByte, err := json.Marshal(resp)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(respAsByte)
}
func main() {
router := httprouter.New()
router.POST("/verify", Verify)
log.Fatal(http.ListenAndServe(":8080", router))
}