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
|
// Package main provides a sigsum-log-go binary
package main
import (
"context"
"crypto"
"crypto/ed25519"
"encoding/hex"
"flag"
"fmt"
"net/http"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"time"
"github.com/golang/glog"
"github.com/google/trillian"
"github.com/prometheus/client_golang/prometheus/promhttp"
"google.golang.org/grpc"
"git.sigsum.org/sigsum-go/pkg/types"
"git.sigsum.org/sigsum-go/pkg/dns"
"git.sigsum.org/log-go/pkg/db"
"git.sigsum.org/log-go/pkg/instance"
"git.sigsum.org/log-go/pkg/state"
)
var (
httpEndpoint = flag.String("http_endpoint", "localhost:6965", "host:port specification of where sigsum-log-go serves clients")
rpcBackend = flag.String("log_rpc_server", "localhost:6962", "host:port specification of where Trillian serves clients")
prefix = flag.String("prefix", "", "a prefix that proceeds /sigsum/v0/<endpoint>")
trillianID = flag.Int64("trillian_id", 0, "log identifier in the Trillian database")
deadline = flag.Duration("deadline", time.Second*10, "deadline for backend requests")
key = flag.String("key", "", "hex-encoded Ed25519 signing key")
witnesses = flag.String("witnesses", "", "comma-separated list of trusted witness public keys in hex")
maxRange = flag.Int64("max_range", 10, "maximum number of entries that can be retrived in a single request")
interval = flag.Duration("interval", time.Second*30, "interval used to rotate the log's cosigned STH")
shardStart = flag.Int64("shard_interval_start", 0, "start of shard interval since the UNIX epoch in seconds")
gitCommit = "unknown"
)
func main() {
flag.Parse()
defer glog.Flush()
glog.Infof("sigsum-log-go git-commit %s", gitCommit)
// wait for clean-up before exit
var wg sync.WaitGroup
defer wg.Wait()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
glog.V(3).Infof("configuring sigsum-log-go instance...")
instance, err := setupInstanceFromFlags()
if err != nil {
glog.Errorf("setupInstance: %v", err)
return
}
glog.V(3).Infof("spawning state manager")
go func() {
wg.Add(1)
defer wg.Done()
instance.Stateman.Run(ctx)
glog.Errorf("state manager shutdown")
cancel() // must have state manager running
}()
glog.V(3).Infof("spawning await")
server := http.Server{Addr: *httpEndpoint}
go await(ctx, func() {
wg.Add(1)
defer wg.Done()
ctxInner, _ := context.WithTimeout(ctx, time.Second*60)
glog.Infof("Shutting down HTTP server...")
server.Shutdown(ctxInner)
glog.V(3).Infof("HTTP server shutdown")
glog.Infof("Shutting down spawned go routines...")
cancel()
})
glog.Infof("Serving on %v/%v", *httpEndpoint, *prefix)
if err = server.ListenAndServe(); err != http.ErrServerClosed {
glog.Errorf("ListenAndServe: %v", err)
}
}
// SetupInstance sets up a new sigsum-log-go instance from flags
func setupInstanceFromFlags() (*instance.Instance, error) {
var i instance.Instance
var err error
// Setup log configuration
i.Signer, i.LogID, err = newLogIdentity(*key)
if err != nil {
return nil, fmt.Errorf("newLogIdentity: %v", err)
}
i.TreeID = *trillianID
i.Prefix = *prefix
i.MaxRange = *maxRange
i.Deadline = *deadline
i.Interval = *interval
i.ShardStart = uint64(*shardStart)
if *shardStart < 0 {
return nil, fmt.Errorf("shard start must be larger than zero")
}
i.Witnesses, err = newWitnessMap(*witnesses)
if err != nil {
return nil, fmt.Errorf("newWitnessMap: %v", err)
}
// Setup log client
dialOpts := []grpc.DialOption{grpc.WithInsecure(), grpc.WithBlock(), grpc.WithTimeout(i.Deadline)}
conn, err := grpc.Dial(*rpcBackend, dialOpts...)
if err != nil {
return nil, fmt.Errorf("Dial: %v", err)
}
i.Client = &db.TrillianClient{
TreeID: i.TreeID,
GRPC: trillian.NewTrillianLogClient(conn),
}
// Setup state manager
i.Stateman, err = state.NewStateManagerSingle(i.Client, i.Signer, i.Interval, i.Deadline)
if err != nil {
return nil, fmt.Errorf("NewStateManagerSingle: %v", err)
}
// Setup DNS verifier
i.DNS = dns.NewDefaultResolver()
// Register HTTP endpoints
mux := http.NewServeMux()
http.Handle("/", mux)
for _, handler := range i.Handlers() {
glog.V(3).Infof("adding handler: %s", handler.Path())
mux.Handle(handler.Path(), handler)
}
glog.V(3).Infof("Adding prometheus handler on path: /metrics")
http.Handle("/metrics", promhttp.Handler())
return &i, nil
}
func newLogIdentity(key string) (crypto.Signer, string, error) {
buf, err := hex.DecodeString(key)
if err != nil {
return nil, "", fmt.Errorf("DecodeString: %v", err)
}
sk := crypto.Signer(ed25519.PrivateKey(buf))
vk := sk.Public().(ed25519.PublicKey)
return sk, hex.EncodeToString([]byte(vk[:])), nil
}
// newWitnessMap creates a new map of trusted witnesses
func newWitnessMap(witnesses string) (map[types.Hash]types.PublicKey, error) {
w := make(map[types.Hash]types.PublicKey)
if len(witnesses) > 0 {
for _, witness := range strings.Split(witnesses, ",") {
b, err := hex.DecodeString(witness)
if err != nil {
return nil, fmt.Errorf("DecodeString: %v", err)
}
var vk types.PublicKey
if n := copy(vk[:], b); n != types.PublicKeySize {
return nil, fmt.Errorf("Invalid public key size: %v", n)
}
w[*types.HashFn(vk[:])] = vk
}
}
return w, nil
}
// await waits for a shutdown signal and then runs a clean-up function
func await(ctx context.Context, done func()) {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
select {
case <-sigs:
case <-ctx.Done():
}
glog.V(3).Info("received shutdown signal")
done()
}
|