aboutsummaryrefslogtreecommitdiff
path: root/cmd_collect.go
blob: 742884ab780488fce05c139bea4b08b40e1bb355 (plain)
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package main

import (
	"container/heap"
	"context"
	"encoding/json"
	"fmt"
	logger "log"
	"net/http"
	"os"
	"strings"
	"sync"
	"time"

	"git.cs.kau.se/rasmoste/ct-sans/internal/chunk"
	"git.cs.kau.se/rasmoste/ct-sans/internal/merkle"
	"git.cs.kau.se/rasmoste/ct-sans/internal/x509"
	"github.com/google/certificate-transparency-go/client"
	"github.com/google/certificate-transparency-go/jsonclient"
	"github.com/google/certificate-transparency-go/scanner"
	"gitlab.torproject.org/rgdd/ct/pkg/metadata"
)

func collect(opts options) error {
	b, err := os.ReadFile(fmt.Sprintf("%s/%s", opts.Directory, opts.metadataFile))
	if err != nil {
		return err
	}
	var md metadata.Metadata
	if err := json.Unmarshal(b, &md); err != nil {
		return err
	}

	var await sync.WaitGroup
	defer await.Wait()
	ctx, cancel := context.WithCancel(context.Background())
	go func() {
		await.Add(1)
		defer await.Done()
		handleSignals(ctx, cancel)

		//
		// Sometimes some worker in scanner.Fetcher isn't shutdown
		// properly despite the parent context (including getRanges)
		// being done.  The below is an ugly hack to avoid hanging.
		//
		time.Sleep(time.Second)
		logger.Printf("INFO: about to exit, please wait...")
		time.Sleep(10 * time.Second)
		os.Exit(1)
	}()

	metricsCh := make(chan metrics)
	defer close(metricsCh)
	go func() {
		await.Add(1)
		defer await.Done()
		handleMetrics(ctx, opts, logs(md), metricsCh)
	}()

	defer cancel()
	var wg sync.WaitGroup
	defer wg.Wait()
	for _, log := range logs(md) {
		go func(log metadata.Log) {
			wg.Add(1)
			defer wg.Done()

			id, _ := log.Key.ID()
			th, err := readState(opts, id[:])
			if err != nil {
				logger.Printf("ERROR: %s: %v\n", *log.Description, err)
				cancel()
				return
			}
			sth, err := readSnapshot(opts, id[:])
			if err != nil {
				logger.Printf("ERROR: %s: %v\n", *log.Description, err)
				cancel()
				return
			}
			cli, err := client.New(string(log.URL),
				&http.Client{Transport: &http.Transport{IdleConnTimeout: 120 * time.Second}},
				jsonclient.Options{UserAgent: opts.HTTPAgent},
			)
			if err != nil {
				logger.Printf("ERROR: %s: %v\n", *log.Description, err)
				cancel()
				return
			}
			fetcher := scanner.NewFetcher(cli, &scanner.FetcherOptions{
				BatchSize:     int(opts.BatchSize),
				StartIndex:    th.TreeSize,
				EndIndex:      int64(sth.TreeSize),
				ParallelFetch: maxWorkers(log, opts.WorkersPerLog),
			})
			if uint64(th.TreeSize) == sth.TreeSize {
				metricsCh <- metrics{Description: *log.Description, End: th.TreeSize, Done: true}
				return
			}

			//
			// Sequencer that waits for sufficiently large chunks
			// before verifying inclusion proofs and persisting an
			// intermediate tree head (size and root hash) as well
			// as the SANs that were observed up until that point.
			//
			chunksCh := make(chan *chunk.Chunk)
			defer close(chunksCh)
			cctx, fetchDone := context.WithCancel(ctx)
			defer fetchDone()
			go func() {
				wg.Add(1)
				defer wg.Done()
				sequence(cctx, cancel, opts, log, th.TreeSize, cli, chunksCh, metricsCh)
			}()

			//
			// Callback that puts downloaded certificates into a
			// chunk that a single sequencer can verify and persist
			//
			callback := func(eb scanner.EntryBatch) {
				c := &chunk.Chunk{Start: eb.Start}
				for i := 0; i < len(eb.Entries); i++ {
					c.LeafHashes = append(c.LeafHashes, merkle.HashLeafNode(eb.Entries[i].LeafInput))
				}

				var errs []error
				c.SANs, errs = x509.SANsFromLeafEntries(eb.Start, eb.Entries)
				for _, err := range errs {
					c.Notes = append(c.Notes, fmt.Sprintf("NOTICE: %s: %v", *log.Description, err))
				}

				chunksCh <- c
			}

			if err := fetcher.Run(ctx, callback); err != nil {
				logger.Printf("ERROR: %s: %v\n", *log.Description, err)
				cancel()
				return
			}
			for len(chunksCh) > 0 {
				select {
				case <-ctx.Done():
					return // some Go routine cancelled due to an error, die
				case <-time.After(time.Second):
				}
			}
		}(log)
	}

	logger.Printf("INFO: collect is up-and-running, ctrl+C to exit\n")
	time.Sleep(3 * time.Second) // ensure that Go routines had time to spawn
	return nil
}

func sequence(ctx context.Context, cancel context.CancelFunc,
	opts options, log metadata.Log, nextIndex int64, cli *client.LogClient,
	chunksCh chan *chunk.Chunk, metricsCh chan metrics) {
	desc := *log.Description

	h := &chunk.ChunkHeap{}
	heap.Init(h)
	for {
		select {
		case <-ctx.Done():
			if h.Sequence(nextIndex) {
				c := h.TPop()
				if _, err := persist(c, opts, log, cli, 0, metricsCh); err != nil {
					logger.Printf("ERROR: %s: %v\n", desc, err)
				}
			}
			return
		case c, ok := <-chunksCh:
			if ok {
				h.TPush(c)
			}
			if !h.Sequence(nextIndex) {
				continue
			}

			c = h.TPop()
			putBack, err := persist(c, opts, log, cli, int64(opts.PersistSize), metricsCh)
			if err != nil {
				cancel()
				logger.Printf("ERROR: %s: %v\n", desc, err)
				return
			}
			if putBack {
				h.TPush(c)
				continue
			}

			nextIndex += int64(len(c.LeafHashes))
		}
	}
}

func persist(c *chunk.Chunk,
	opts options, log metadata.Log, cli *client.LogClient, minSequence int64,
	metricsCh chan metrics) (bool, error) {
	logID, _ := log.Key.ID()
	desc := *log.Description

	chunkSize := int64(len(c.LeafHashes))
	if chunkSize == 0 {
		return false, nil // nothing to persist
	}
	if chunkSize < minSequence {
		return true, nil // wait for more leaves
	}

	// Read persisted tree state from disk
	oldTH, err := readState(opts, logID[:])
	if err != nil {
		return false, err
	}
	if oldTH.TreeSize != c.Start {
		return false, fmt.Errorf("disk state says next index is %d, in-memory says %d", oldTH.TreeSize, c.Start)
	}

	// Read signed tree head from disk
	sth, err := readSnapshot(opts, logID[:])
	if err != nil {
		return false, err
	}

	//
	// Derive next intermediate tree state instead of verying all inclusions
	//
	// Independent context because we need to run inclusion and consistency
	// queries after the parent context is cancelled to persist on shutdown
	//
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
	newTH := treeHead{TreeSize: c.Start + chunkSize}
	p, err := cli.GetProofByHash(ctx, c.LeafHashes[0][:], uint64(newTH.TreeSize))
	if err != nil {
		return true, nil // try again later
	}
	if p.LeafIndex != c.Start {
		return false, fmt.Errorf("log says proof for entry %d is at index %d", c.Start, p.LeafIndex)
	}
	if newTH.RootHash, err = merkle.TreeHeadFromRangeProof(c.LeafHashes, uint64(c.Start), proof(p.AuditPath)); err != nil {
		return false, err
	}

	// Check that new tree state is consistent with what we stored on disk
	var hashes [][]byte
	if oldTH.TreeSize > 0 {
		if hashes, err = cli.GetSTHConsistency(ctx, uint64(oldTH.TreeSize), uint64(newTH.TreeSize)); err != nil {
			return true, nil // try again later
		}
	}
	if err := merkle.VerifyConsistency(uint64(oldTH.TreeSize), uint64(newTH.TreeSize), oldTH.RootHash, newTH.RootHash, proof(hashes)); err != nil {
		return false, fmt.Errorf("%d %x is inconsistent with on-disk state: %v", newTH.TreeSize, newTH.RootHash, err)
	}

	// Check that new tree state is consistent with the signed tree head
	if hashes, err = cli.GetSTHConsistency(ctx, uint64(newTH.TreeSize), sth.TreeSize); err != nil {
		return true, nil // try again later
	}
	if err := merkle.VerifyConsistency(uint64(newTH.TreeSize), sth.TreeSize, newTH.RootHash, sth.SHA256RootHash, proof(hashes)); err != nil {
		return false, fmt.Errorf("%d %x is inconsistent with signed tree head: %v", newTH.TreeSize, newTH.RootHash, err)
	}

	// Persist SANs to disk
	fp, err := os.OpenFile(fmt.Sprintf("%s/%x/%s", opts.logDirectory, logID, opts.sansFile), os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
	if err != nil {
		return false, err
	}
	defer fp.Close()
	if _, err := fp.WriteString(strings.Join(c.SANs, "\n") + "\n"); err != nil {
		return false, err
	}
	if err := fp.Sync(); err != nil {
		return false, err
	}

	// Persist notes to disk
	if len(c.Notes) > 0 {
		fpn, err := os.OpenFile(fmt.Sprintf("%s/%x/%s", opts.logDirectory, logID, opts.noticeFile), os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
		if err != nil {
			return false, err
		}
		defer fpn.Close()
		if _, err := fpn.WriteString(strings.Join(c.Notes, "\n") + "\n"); err != nil {
			return false, err
		}
		if err := fpn.Sync(); err != nil {
			return false, err
		}
	}

	// Persist new tree state to disk
	b, err := json.Marshal(&newTH)
	if err != nil {
		return false, err
	}
	if err := os.WriteFile(fmt.Sprintf("%s/%x/%s", opts.logDirectory, logID, opts.stateFile), b, 0644); err != nil {
		return false, err
	}

	// Output metrics
	metricsCh <- metrics{
		Description: desc,
		NumEntries:  newTH.TreeSize - oldTH.TreeSize,
		Timestamp:   time.Now().Unix(),
		Start:       newTH.TreeSize,
		End:         int64(sth.TreeSize),
		Done:        uint64(newTH.TreeSize) == sth.TreeSize,
	}
	//logger.Printf("DEBUG: %s: persisted [%d, %d]\n", desc, oldTH.TreeSize, newTH.TreeSize)
	return false, nil
}