@@ -3,102 +3,44 @@ package main
33import (
44 "context"
55 "flag"
6- "fmt"
76 "net/http"
8- "regexp"
9- "strings"
107
8+ "go.skia.org/infra/attest/go/attestation"
9+ "go.skia.org/infra/attest/go/types"
1110 "go.skia.org/infra/go/common"
12- "go.skia.org/infra/go/gcloud/binaryauthorization"
1311 "go.skia.org/infra/go/httputils"
14- "go.skia.org/infra/go/skerr"
1512 "go.skia.org/infra/go/sklog"
16- "golang.org/x/oauth2/google"
1713)
1814
1915var (
2016 // Flags.
21- attestorProject = flag .String ("attestor_project" , "" , "ID of the project containing the attestor" )
22- attestor = flag .String ("attestor" , "" , "ID of the attestor" )
23- host = flag .String ("host" , "localhost" , "HTTP service host" )
24- port = flag .String ("port" , ":8000" , "HTTP service port (e.g., ':8000')" )
25- promPort = flag .String ("prom_port" , ":20000" , "Metrics service address (e.g., ':10110')" )
26- local = flag .Bool ("local" , false , "Running locally if true. As opposed to in production." )
27-
28- // Global Binary Authorization API client.
29- binauthClient binaryauthorization.Client
17+ attestor = flag .String ("attestor" , "" , "Fully-qualified resource name of the attestor (e.g., 'projects/my-project/attestors/my-attestor')" )
18+ host = flag .String ("host" , "localhost" , "HTTP service host" )
19+ port = flag .String ("port" , ":8000" , "HTTP service port (e.g., ':8000')" )
20+ promPort = flag .String ("prom_port" , ":20000" , "Metrics service address (e.g., ':10110')" )
21+ local = flag .Bool ("local" , false , "Running locally if true. As opposed to in production." )
3022)
3123
32- func checkAttestation (ctx context.Context , attestorProject , attestor , imageID string ) (bool , error ) {
33- split := strings .Split (imageID , "@sha256:" )
34- if len (split ) != 2 {
35- return false , skerr .Fmt ("incorrect image format" )
36- }
37- attestations , err := binauthClient .ListAttestations (ctx , attestorProject , attestor , split [1 ])
38- if err != nil {
39- return false , skerr .Wrap (err )
40- }
41- return len (attestations ) > 0 , nil
42- }
43-
44- var validImageRegex = regexp .MustCompile (`[0-9A-Za-z_.]+\/[0-9A-Za-z_-]+\/[0-9A-Za-z_-]+@sha256:[0-9a-f]{64}` )
45-
46- func handler (w http.ResponseWriter , r * http.Request ) {
47- values := r .URL .Query ()["image" ]
48- if len (values ) != 1 {
49- http .Error (w , "expected a single value for `image`" , http .StatusBadRequest )
50- return
51- }
52- imageID := values [0 ]
53- if ! validImageRegex .MatchString (imageID ) {
54- http .Error (w , "expected image of the form gcr.io/project/repository@sha256:digest" , http .StatusBadRequest )
55- return
56- }
57- hasAttestation , err := checkAttestation (r .Context (), * attestorProject , * attestor , imageID )
58- if err != nil {
59- sklog .Errorf ("Failed checking attestation of %s: %s" , imageID , err )
60- http .Error (w , "internal server error" , http .StatusInternalServerError )
61- return
62- }
63- if ! hasAttestation {
64- // TODO(borenet): We could consider using a different status code here,
65- // for example 200 (or possibly 204 No Content) but still return
66- // "no attestation found", to differentiate from a typical 404.
67- http .Error (w , "no attestation found" , http .StatusNotFound )
68- return
69- }
70- _ , _ = fmt .Fprintln (w , "found valid attestation" )
71- }
72-
7324func main () {
7425 common .InitWithMust (
7526 "attest" ,
7627 common .PrometheusOpt (promPort ),
7728 )
7829 defer common .Defer ()
7930
80- if * attestorProject == "" {
81- sklog .Fatal ("--attestor_project is required." )
82- }
83-
84- if * attestor == "" {
85- sklog .Fatal ("--attestor is required." )
86- }
87-
8831 serverURL := "https://" + * host
8932 if * local {
9033 serverURL = "http://" + * host + * port
9134 }
9235
9336 ctx := context .Background ()
94- ts , err := google . DefaultTokenSource (ctx , binaryauthorization . Scope )
37+ client , err := attestation . NewClient (ctx , * attestor )
9538 if err != nil {
9639 sklog .Fatal (err )
9740 }
98- httpClient := httputils .DefaultClientConfig ().WithTokenSource (ts ).With2xxAnd3xx ().Client ()
99- binauthClient = (* binaryauthorization .ApiClient )(httpClient )
41+ server := types .NewServer (client )
10042
101- h := httputils .LoggingRequestResponse (http . HandlerFunc ( handler ) )
43+ h := httputils .LoggingRequestResponse (server )
10244 h = httputils .XFrameOptionsDeny (h )
10345 if ! * local {
10446 h = httputils .HealthzAndHTTPS (h )
0 commit comments