github.com/google/trillian-examples@v0.0.0-20240520080811-0d40d35cef0e/binary_transparency/firmware/cmd/ft_witness/impl/ft_witness.go (about) 1 // Copyright 2021 Google LLC. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Package impl is the implementation of the Firmware Transparency witness server. 16 // This requires a FT Log to be running at a known address. 17 package impl 18 19 import ( 20 "context" 21 "errors" 22 "fmt" 23 "net/http" 24 "time" 25 26 "github.com/golang/glog" 27 ih "github.com/google/trillian-examples/binary_transparency/firmware/cmd/ft_witness/internal/http" 28 "github.com/google/trillian-examples/binary_transparency/firmware/cmd/ft_witness/internal/ws" 29 "github.com/gorilla/mux" 30 "golang.org/x/mod/sumdb/note" 31 ) 32 33 // WitnessOpts encapsulates options for running an FT witness. 34 type WitnessOpts struct { 35 ListenAddr string 36 WSFile string 37 FtLogURL string 38 FtLogSigVerifier note.Verifier 39 PollInterval time.Duration 40 } 41 42 // Main kickstarts the witness 43 func Main(ctx context.Context, opts WitnessOpts) error { 44 if len(opts.WSFile) == 0 { 45 return errors.New("Witness Store file is required") 46 } 47 48 ws, err := ws.NewStorage(opts.WSFile) 49 if err != nil { 50 return fmt.Errorf("failed to connect witness store: %w", err) 51 } 52 53 glog.Infof("Starting FT witness server...") 54 witness, err := ih.NewWitness(ws, opts.FtLogURL, opts.FtLogSigVerifier, opts.PollInterval) 55 if err != nil { 56 return fmt.Errorf("failed to create new witness: %w", err) 57 } 58 r := mux.NewRouter() 59 witness.RegisterHandlers(r) 60 61 go func() { 62 if err := witness.Poll(ctx); err != nil { 63 glog.Errorf("witness.Poll(): %v", err) 64 } 65 }() 66 67 hServer := &http.Server{ 68 Addr: opts.ListenAddr, 69 Handler: r, 70 } 71 e := make(chan error, 1) 72 go func() { 73 e <- hServer.ListenAndServe() 74 close(e) 75 }() 76 <-ctx.Done() 77 glog.Info("Server shutting down") 78 if err := hServer.Shutdown(ctx); err != nil { 79 glog.Errorf("server.Shutdown(): %v", err) 80 } 81 return <-e 82 }