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
|
package secondary
import (
"context"
"crypto"
"fmt"
"net/http"
"time"
"git.sigsum.org/log-go/internal/db"
"git.sigsum.org/log-go/internal/node/handler"
"git.sigsum.org/sigsum-go/pkg/client"
"git.sigsum.org/sigsum-go/pkg/log"
"git.sigsum.org/sigsum-go/pkg/requests"
"git.sigsum.org/sigsum-go/pkg/types"
)
// Config is a collection of log parameters
type Config struct {
LogID string // H(public key), then hex-encoded
TreeID int64 // Merkle tree identifier used by Trillian
Prefix string // The portion between base URL and st/v0 (may be "")
Deadline time.Duration // Deadline used for gRPC requests
Interval time.Duration // Signing frequency
}
// Secondary is an instance of a secondary node
type Secondary struct {
Config
PublicHTTPMux *http.ServeMux
InternalHTTPMux *http.ServeMux
TrillianClient db.Client // provides access to the Trillian backend
Signer crypto.Signer // provides access to Ed25519 private key
Primary client.Client
}
// Implementing handler.Config
func (s Secondary) Prefix() string {
return s.Config.Prefix
}
func (s Secondary) LogID() string {
return s.Config.LogID
}
func (s Secondary) Deadline() time.Duration {
return s.Config.Deadline
}
func (s Secondary) Run(ctx context.Context) {
ticker := time.NewTicker(s.Interval)
defer ticker.Stop()
for {
select {
case <-ticker.C:
s.fetchLeavesFromPrimary(ctx)
case <-ctx.Done():
return
}
}
}
// TODO: nit-pick: the internal endpoint is used by primaries to figure out how much can be signed; not cosigned - update name?
func (s Secondary) InternalHTTPHandlers() []handler.Handler {
return []handler.Handler{
handler.Handler{s, getTreeHeadToCosign, types.EndpointGetTreeHeadToCosign, http.MethodGet},
}
}
func (s Secondary) fetchLeavesFromPrimary(ctx context.Context) {
prim, err := s.Primary.GetUnsignedTreeHead(ctx)
if err != nil {
log.Warning("unable to get tree head from primary: %v", err)
return
}
log.Debug("got tree head from primary, size %d", prim.TreeSize)
curTH, err := treeHeadFromTrillian(ctx, s.TrillianClient)
if err != nil {
log.Warning("unable to get tree head from trillian: %v", err)
return
}
var leaves types.Leaves
for index := int64(curTH.TreeSize); index < int64(prim.TreeSize); index += int64(len(leaves)) {
req := requests.Leaves{
StartSize: uint64(index),
EndSize: prim.TreeSize - 1,
}
// TODO: set context per request
leaves, err = s.Primary.GetLeaves(ctx, req)
if err != nil {
log.Warning("error fetching leaves [%d..%d] from primary: %v", req.StartSize, req.EndSize, err)
return
}
log.Debug("got %d leaves from primary when asking for [%d..%d]", len(leaves), req.StartSize, req.EndSize)
if err := s.TrillianClient.AddSequencedLeaves(ctx, leaves, index); err != nil {
log.Error("AddSequencedLeaves: %v", err)
return
}
}
}
func treeHeadFromTrillian(ctx context.Context, trillianClient db.Client) (*types.TreeHead, error) {
th, err := trillianClient.GetTreeHead(ctx)
if err != nil {
return nil, fmt.Errorf("fetching tree head from trillian: %v", err)
}
log.Debug("got tree head from trillian, size %d", th.TreeSize)
return th, nil
}
|