Skip to content

Commit caaf1c1

Browse files
authored
feat: Create a verifier as a service (#182)
* update * update * update * tests * update * update * update * update * update * update * update * update * update * update * comments * update * update * update * update * update
1 parent 8dab07b commit caaf1c1

File tree

91 files changed

+354
-209
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

91 files changed

+354
-209
lines changed

.github/workflows/pre-submit.yml

+7-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ jobs:
2020
set -euo pipefail
2121
2222
go mod vendor
23-
# Build
24-
go build -mod=vendor
23+
24+
# Build cli
25+
go build -mod=vendor -o slsa-verifier ./cli/slsa-verifier/main.go
26+
27+
# Builder service
28+
go build -mod=vendor -o service ./cli/experimental/service/main.go
29+
2530
# Tests
2631
go test -mod=vendor -v ./...

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ $ slsa-verifier <options>
3030
#### Option 2: Compile manually
3131
```
3232
$ git clone [email protected]:slsa-framework/slsa-verifier.git
33-
$ cd slsa-verifier && git checkout v1.2.0
33+
$ cd slsa-verifier && git checkout v1.2.1
3434
$ go run . <options>
3535
```
3636

3737
### Download the binary
3838

39-
Download the binary from the latest release at [https://github.com/slsa-framework/slsa-verifier/releases/tag/v1.2.0](https://github.com/slsa-framework/slsa-verifier/releases/tag/v1.2.0)
39+
Download the binary from the latest release at [https://github.com/slsa-framework/slsa-verifier/releases/tag/v1.1.1](https://github.com/slsa-framework/slsa-verifier/releases/tag/v1.1.2)
4040

4141
Download the [SHA256SUM.md](https://github.com/slsa-framework/slsa-verifier/blob/main/SHA256SUM.md).
4242

cli/experimental/service/Dockerfile

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
FROM golang:1.18@sha256:9349ed889adb906efa5ebc06485fe1b6a12fb265a01c9266a137bb1352565560 AS base
2+
WORKDIR /src
3+
ENV CGO_ENABLED=0
4+
COPY . ./
5+
RUN go mod vendor
6+
RUN go build -o slsa-verifier -trimpath -ldflags "-s -w -extldflags=-static" -mod=vendor ./cli/experimental/service/main.go
7+
8+
# For testing.
9+
# COPY ./service/service slsa-verifier
10+
# RUN chmod o+x ./slsa-verifier
11+
12+
FROM gcr.io/distroless/base:nonroot@sha256:49d2923f35d66b8402487a7c01bc62a66d8279cd42e89c11b64cdce8d5826c03
13+
COPY --from=base /src/slsa-verifier /
14+
ENTRYPOINT ["/slsa-verifier"]

cli/experimental/service/README.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# SLSA verifier as a service
2+
3+
This document is WIP.
4+
5+
Command to run the service locally:
6+
7+
```bash
8+
$ docker build -t slsa-verifier-rest:latest -f cli/experimental/service/Dockerfile .
9+
$ docker run -p 8000:8000 slsa-verifier-rest:latest # This did not work for me.
10+
$ docker run --network=host slsa-verifier-rest:latest
11+
```
12+
13+
```bash
14+
$ curl -s 127.0.0.1:8000/v1/verify -d @cli/experimental/service/testdata/request.txt
15+
```

cli/experimental/service/main.go

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"net/http"
7+
"time"
8+
9+
"github.com/gorilla/mux"
10+
11+
"github.com/slsa-framework/slsa-verifier/experimental/rest"
12+
)
13+
14+
func main() {
15+
r := mux.NewRouter().StrictSlash(true)
16+
17+
r.HandleFunc("/", HomeHandler).Methods(http.MethodGet)
18+
r.HandleFunc("/v1/verify", rest.VerifyHandlerV1).Methods(http.MethodPost)
19+
http.Handle("/", r)
20+
21+
address := ":8000"
22+
fmt.Printf("Starting HTTP server on %v ...\n", address)
23+
srv := &http.Server{
24+
Handler: r,
25+
Addr: address,
26+
// Good practice: enforce timeouts for servers you create!
27+
WriteTimeout: 15 * time.Second,
28+
ReadTimeout: 15 * time.Second,
29+
}
30+
31+
if err := srv.ListenAndServe(); err != nil {
32+
log.Fatal(err)
33+
}
34+
}
35+
36+
func HomeHandler(w http.ResponseWriter, r *http.Request) {
37+
w.WriteHeader(http.StatusOK)
38+
}

cli/experimental/service/testdata/request.txt

+6
Large diffs are not rendered by default.

main.go cli/slsa-verifier/main.go

+3-45
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,14 @@ package main
33
import (
44
"context"
55
"crypto/sha256"
6-
"encoding/base64"
76
"encoding/hex"
87
"flag"
98
"fmt"
109
"io"
1110
"log"
1211
"os"
1312

14-
"github.com/sigstore/cosign/cmd/cosign/cli/rekor"
15-
"github.com/slsa-framework/slsa-verifier/pkg"
13+
"github.com/slsa-framework/slsa-verifier/verification"
1614
)
1715

1816
var (
@@ -25,46 +23,6 @@ var (
2523
printProvenance bool
2624
)
2725

28-
var defaultRekorAddr = "https://rekor.sigstore.dev"
29-
30-
func verify(ctx context.Context,
31-
provenance []byte, artifactHash, source string, provenanceOpts *pkg.ProvenanceOpts) ([]byte, error) {
32-
rClient, err := rekor.NewClient(defaultRekorAddr)
33-
if err != nil {
34-
return nil, err
35-
}
36-
37-
/* Verify signature on the intoto attestation. */
38-
env, cert, err := pkg.VerifyProvenanceSignature(ctx, rClient, provenance, artifactHash)
39-
if err != nil {
40-
return nil, err
41-
}
42-
43-
/* Verify properties of the signing identity. */
44-
// Get the workflow info given the certificate information.
45-
workflowInfo, err := pkg.GetWorkflowInfoFromCertificate(cert)
46-
if err != nil {
47-
return nil, err
48-
}
49-
50-
// Verify the workflow identity.
51-
if err := pkg.VerifyWorkflowIdentity(workflowInfo, source); err != nil {
52-
return nil, err
53-
}
54-
55-
/* Verify properties of the SLSA provenance. */
56-
// Unpack and verify info in the provenance, including the Subject Digest.
57-
if err := pkg.VerifyProvenance(env, provenanceOpts); err != nil {
58-
return nil, err
59-
}
60-
61-
fmt.Fprintf(os.Stderr, "Verified build using builder https://github.com%s at commit %s\n",
62-
workflowInfo.JobWobWorkflowRef,
63-
workflowInfo.CallerHash)
64-
// Return verified provenance.
65-
return base64.StdEncoding.DecodeString(env.Payload)
66-
}
67-
6826
func main() {
6927
flag.StringVar(&provenancePath, "provenance", "", "path to a provenance file")
7028
flag.StringVar(&artifactPath, "artifact-path", "", "path to an artifact to verify")
@@ -138,15 +96,15 @@ func runVerify(artifactPath, provenancePath, source, branch string, ptag, pversi
13896
}
13997
artifactHash := hex.EncodeToString(h.Sum(nil))
14098

141-
provenanceOpts := &pkg.ProvenanceOpts{
99+
provenanceOpts := &verification.ProvenanceOpts{
142100
ExpectedBranch: branch,
143101
ExpectedDigest: artifactHash,
144102
ExpectedVersionedTag: pversiontag,
145103
ExpectedTag: ptag,
146104
}
147105

148106
ctx := context.Background()
149-
return verify(ctx, provenance,
107+
return verification.Verify(ctx, provenance,
150108
artifactHash,
151109
source, provenanceOpts)
152110
}

0 commit comments

Comments
 (0)