aboutsummaryrefslogtreecommitdiff
path: root/cmd_assemble.go
blob: 2b56d350aaa2c3b4bde4a3de4f08a2a372522b2b (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
package main

import (
	"bufio"
	"bytes"
	"encoding/json"
	"errors"
	"fmt"
	logger "log"
	"os"
	"os/exec"
	"strconv"
	"time"

	ct "github.com/google/certificate-transparency-go"
	"gitlab.torproject.org/rgdd/ct/pkg/metadata"
)

func assemble(opts options) error {
	now := time.Now()
	metadataBytes, err := os.ReadFile(fmt.Sprintf("%s/%s", opts.Directory, opts.metadataFile))
	if err != nil {
		return err
	}
	var md metadata.Metadata
	if err := json.Unmarshal(metadataBytes, &md); err != nil {
		return err
	}
	var sanFiles []string
	var noticeFiles []string
	var sths []ct.SignedTreeHead
	for _, log := range logs(md) {
		id, _ := log.Key.ID()
		th, err := readState(opts, id[:])
		if err != nil {
			return err
		}
		sth, err := readSnapshot(opts, id[:])
		if err != nil {
			return err
		}
		if uint64(th.TreeSize) != sth.TreeSize {
			return fmt.Errorf("%s: at tree size %d, want %d", *log.Description, th.TreeSize, sth.TreeSize)
		}
		if th.RootHash != sth.SHA256RootHash {
			return fmt.Errorf("%s: root hash mismatch")
		}

		sanFiles = append(sanFiles, fmt.Sprintf("%s/%x/%s", opts.logDirectory, id[:], opts.sansFile))
		noticeFiles = append(noticeFiles, fmt.Sprintf("%s/%x/%s", opts.logDirectory, id[:], opts.noticeFile))
		sths = append(sths, sth)
	}

	logger.Printf("INFO: merging and de-duplicating %d input files with GNU sort", len(sanFiles))
	archiveDir := fmt.Sprintf("%s/%s-ct-sans", opts.archiveDirectory, now.Format("2006-01-02"))
	if err := os.MkdirAll(archiveDir, os.ModePerm); err != nil {
		return err
	}
	sansFile := fmt.Sprintf("%s/%s", archiveDir, opts.sansFile)
	if err := dedup(opts, sansFile, sanFiles); err != nil {
		return err
	}
	size, err := fileSize(sansFile)
	if err != nil {
		return err
	}
	logger.Printf("INFO: created %s (%s)", sansFile, size)

	logger.Printf("INFO: adding notice file")
	var notes []byte
	for _, noticeFile := range noticeFiles {
		b, err := os.ReadFile(noticeFile)
		if errors.Is(err, os.ErrNotExist) {
			continue // no notes, great
		} else if err != nil {
			return err
		}

		notes = append(notes, b...)
	}
	if err := os.WriteFile(fmt.Sprintf("%s/%s", archiveDir, opts.noticeFile), notes, 0644); err != nil {
		return err
	}
	numNotes := len(bytes.Split(notes, []byte("\n"))) - 1

	logger.Printf("INFO: adding README")
	readme, err := makeREADME(opts, sths, numNotes, now)
	if err != nil {
		return err
	}
	if err := os.WriteFile(fmt.Sprintf("%s/README.md", archiveDir), []byte(readme), 0644); err != nil {
		return err
	}

	logger.Printf("INFO: adding signed metadata file")
	sigBytes, err := os.ReadFile(fmt.Sprintf("%s/%s", opts.Directory, opts.metadataSignatureFile))
	if err != nil {
		return err
	}
	if err := os.WriteFile(fmt.Sprintf("%s/%s", archiveDir, opts.metadataFile), metadataBytes, 0644); err != nil {
		return err
	}
	if err := os.WriteFile(fmt.Sprintf("%s/%s", archiveDir, opts.metadataSignatureFile), sigBytes, 0644); err != nil {
		return err
	}

	logger.Printf("INFO: adding signed tree heads")
	sthsBytes, err := json.MarshalIndent(sths, "", "\t")
	if err != nil {
		return err
	}
	if err := os.WriteFile(fmt.Sprintf("%s/%s", archiveDir, opts.sthsFile), sthsBytes, 0644); err != nil {
		return err
	}

	logger.Printf("INFO: uncompressed dataset available in %s", archiveDir)
	return nil
}

func dedup(opts options, outputFile string, inputFiles []string) error {
	cmd := exec.Command("sort", append([]string{
		"-Vuo", outputFile,
		"--buffer-size", fmt.Sprintf("%dG", opts.BufferSize),
		"--temporary-directory", fmt.Sprintf("%s", opts.TempDir),
		"--parallel", fmt.Sprintf("%d", opts.Parallel),
	}, inputFiles...)...)
	if errors.Is(cmd.Err, exec.ErrDot) {
		cmd.Err = nil
	}
	stderr := bytes.NewBuffer(nil)
	cmd.Stderr = stderr
	if _, err := cmd.Output(); err != nil {
		return fmt.Errorf("%s", string(stderr.Bytes()))
	}
	return nil
}

func makeREADME(opts options, sths []ct.SignedTreeHead, numNotes int, now time.Time) (string, error) {
	snapshotTime, err := readSnapshotTime(opts)
	if err != nil {
		return "", err
	}
	return fmt.Sprintf(`# ct-sans dataset

Dataset assembled at %s.  Contents:

  - README.md
  - %s
  - %s
  - %s
  - %s
  - %s

The signed [metadata file][] and tree heads were downloaded at
%s.

[metadata file]: https://groups.google.com/a/chromium.org/g/ct-policy/c/IdbrdAcDQto

In total, %d certificates were downloaded from %d CT logs;
%d certificates contained SANs that could not be parsed.
For more information about these errors, see %s.

The SANs data set is sorted and de-duplicated, one SAN per line.
`, now.Format(time.UnixDate), opts.metadataFile, opts.metadataSignatureFile, opts.sthsFile, opts.noticeFile, opts.sansFile,
		snapshotTime.Format(time.UnixDate), numCertificates(sths), len(sths), numNotes, opts.noticeFile), nil
}

func fileSize(name string) (string, error) {
	fi, err := os.Stat(name)
	if err != nil {
		return "", err
	}
	size := fmt.Sprintf("%.1f GiB", float64(fi.Size())/float64((1024*1024*1024)))
	if fi.Size() < 1024*1024*1024 {
		size = fmt.Sprintf("%.1f MiB", float64(fi.Size())/float64((1024*1024)))
	}
	return size, nil
}

func noticeReport(path string) (string, error) {
	fp, err := os.OpenFile(path, os.O_RDONLY, 0644)
	if err != nil {
		return "", err
	}
	defer fp.Close()

	scanner := bufio.NewScanner(fp)
	num := 0
	for scanner.Scan() {
		_ = scanner.Text()
		num += 1
	}
	return fmt.Sprintf("%d", num), nil
}

func numCertificates(sths []ct.SignedTreeHead) (sum uint64) {
	for _, sth := range sths {
		sum += sth.TreeSize
	}
	return
}

func readSnapshotTime(opts options) (time.Time, error) {
	b, err := os.ReadFile(fmt.Sprintf("%s/%s", opts.Directory, opts.metadataTimestampFile))
	if err != nil {
		return time.Time{}, err
	}
	num, err := strconv.ParseInt(string(b), 10, 64)
	if err != nil {
		return time.Time{}, err
	}
	return time.Unix(num, 0), nil
}