-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathclient.go
More file actions
510 lines (457 loc) · 15.3 KB
/
Copy pathclient.go
File metadata and controls
510 lines (457 loc) · 15.3 KB
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
// Copyright 2025 Google LLC. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// sumdbverify defines a binary that uses the Go Checksum Verifiable Index
// to look up versions for a given Go module, and compares these entries with
// the local git tags.
package main
import (
"bytes"
"context"
"errors"
"flag"
"fmt"
"io"
"io/fs"
"net/http"
"net/url"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"text/tabwriter"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/transparency-dev/incubator/vindex/client"
"golang.org/x/mod/modfile"
"golang.org/x/mod/module"
"golang.org/x/mod/semver"
"golang.org/x/mod/sumdb/dirhash"
"golang.org/x/mod/sumdb/note"
"golang.org/x/mod/zip"
"golang.org/x/sync/errgroup"
"k8s.io/klog/v2"
)
var (
baseURL = flag.String("base_url", "", "The base URL of the server hosting the logs and vindex.")
inLogPubKey = flag.String("in_log_pub_key", "sum.golang.org+033de0ae+Ac4zctda0e5eza+HJyk9SxEdh+s3Ux18htTTAD8OuAn8", "The input log public key.")
outLogPubKey = flag.String("out_log_pub_key", "", "The public key to use to verify the output log checkpoint.")
modRoot = flag.String("mod_root", "", "The path to a go module checked out locally via git.")
)
var (
// Example leaf:
// golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
// golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
//
line0RE = regexp.MustCompile(`(.*) (.*) (h1:.*)`)
line1RE = regexp.MustCompile(`(.*) (.*)/go.mod (h1:.*)`)
)
func main() {
klog.InitFlags(nil)
flag.Parse()
if err := run(context.Background()); err != nil {
klog.Exitf("run failed: %v", err)
}
}
func run(ctx context.Context) error {
if *modRoot == "" {
return errors.New("mod_root flag must be provided")
}
var sumFetcher func(ctx context.Context, modName string) (map[string]modData, error)
if *baseURL == "" {
klog.Warningf("--base_url is not provided. Using NON-VERIFIABLE lookup to source SumDB data.")
// This constructs the map non-verifiably by calling similar URLs to these:
// 1) https://proxy.golang.org/github.com/transparency-dev/tessera/@v/list
// 2) https://sum.golang.org/lookup/github.com/transparency-dev/tessera@v1.0.0
sumFetcher = func(ctx context.Context, modName string) (map[string]modData, error) {
result := make(map[string]modData)
resp, err := http.Get(fmt.Sprintf("https://proxy.golang.org/%s/@v/list", modName))
if err != nil {
return nil, fmt.Errorf("failed to get module listing: %v", err)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to get module listing: %v", err)
}
for v := range strings.Lines(string(body)) {
v = strings.TrimSpace(v)
resp, err = http.Get(fmt.Sprintf("https://sum.golang.org/lookup/%s@%s", modName, v))
if err != nil {
return nil, fmt.Errorf("failed to get version info: %v", err)
}
body, err = io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to get version info: %v", err)
}
lines := bytes.Split(body, []byte{'\n'})
idx, err := strconv.ParseInt(string(lines[0]), 10, 64)
if err != nil {
return nil, fmt.Errorf("failed to parse index: %v", err)
}
leaf := append(append(append(lines[1], byte('\n')), lines[2]...), byte('\n'))
v2, md, err := parseLeaf(uint64(idx), leaf)
if err != nil {
return nil, fmt.Errorf("failed to parse leaf: %v", err)
}
if v != v2 {
return nil, fmt.Errorf("performed lookup for %s@%s but got version %s", modName, v, v2)
}
result[v] = md
}
return result, nil
}
} else {
if *outLogPubKey == "" {
return errors.New("out_log_pub_key flag must be provided if --base_url is provided")
}
sumFetcher = func(ctx context.Context, modName string) (map[string]modData, error) {
vic := newVIndexClientFromFlags()
return queryIndex(ctx, vic, modName)
}
}
report, reportErr := getReport(ctx, *modRoot, sumFetcher)
if reportErr != nil && len(report.versions) == 0 {
return fmt.Errorf("failed to compile report: %v", reportErr)
}
fmt.Printf("%s (./%s)\n", report.modName, report.modPath)
tw := tabwriter.NewWriter(os.Stdout, 0, 8, 2, ' ', 0)
if _, err := fmt.Fprintln(tw, "VERSION\tINDEX\tFOUND\tgo.mod\tzip\t"); err != nil {
return fmt.Errorf("failed to output report: %v", err)
}
reportErrors := make([]error, 0)
if reportErr != nil {
reportErrors = append(reportErrors, reportErr)
}
for _, v := range report.versions {
var sumIndex string
if v.sumFound {
sumIndex = strconv.FormatUint(v.sumIndex, 10)
} else {
sumIndex = "--"
}
gitHash := "✅"
if v.gitCommitHash == nil {
gitHash = "❌"
}
goMod := "✅"
if v.gitCommitHash == nil || len(v.gitModHash) == 0 {
goMod = "⚠️"
} else if v.gitModHash != v.sumModHash {
reportErrors = append(reportErrors, fmt.Errorf("failed to reproduce go.mod hash for version %q", v.version))
goMod = "❌"
}
goZip := "✅"
if v.gitCommitHash == nil || len(v.gitModHash) == 0 {
goZip = "⚠️"
} else if v.gitZipHash != v.sumZipHash {
reportErrors = append(reportErrors, fmt.Errorf("failed to reproduce zip hash for version %q", v.version))
goZip = "❌"
}
if _, err := fmt.Fprintf(tw, "%s\t%s\t%s\t%s\t%s\t\n", v.version, sumIndex, gitHash, goMod, goZip); err != nil {
return fmt.Errorf("failed to output report: %v", err)
}
}
if err := tw.Flush(); err != nil {
return fmt.Errorf("failed to flush report to stdout: %v", err)
}
return errors.Join(reportErrors...)
}
func findGoMod(modRoot string) (string, error) {
goModPath := "go.mod"
if s, err := os.Stat(filepath.Join(modRoot, goModPath)); err == nil && !s.IsDir() {
return goModPath, nil
}
err := filepath.WalkDir(modRoot, func(path string, d fs.DirEntry, err error) error {
if !d.IsDir() && d.Name() == "go.mod" {
relPath, err := filepath.Rel(modRoot, path)
if err != nil {
return err
}
goModPath = relPath
return os.ErrExist
}
return nil
})
if errors.Is(err, os.ErrExist) {
return goModPath, nil
}
return "", fmt.Errorf("failed to read go.mod file: %v", err)
}
func getReport(ctx context.Context, modRoot string, sumFetcher func(context.Context, string) (map[string]modData, error)) (diffReport, error) {
if s, err := os.Stat(modRoot); err != nil || !s.IsDir() {
return diffReport{}, errors.New("mod_root flag must be a directory")
}
goModPath, err := findGoMod(modRoot)
if err != nil {
return diffReport{}, fmt.Errorf("failed to find go.mod file in %s", modRoot)
}
modPathBytes, err := os.ReadFile(filepath.Join(modRoot, goModPath))
if err != nil {
return diffReport{}, fmt.Errorf("failed to read go.mod file: %v", err)
}
modName := modfile.ModulePath(modPathBytes)
if modName == "" {
return diffReport{}, fmt.Errorf("failed to parse go.mod file: %v", err)
}
report := diffReport{
modName: modName,
modPath: goModPath,
versions: make([]versionReport, 0),
}
repo, err := git.PlainOpen(modRoot)
if err != nil {
return report, fmt.Errorf("directory at mod_root %q cannot be opened as git repo: %v", modRoot, err)
}
eg, egctx := errgroup.WithContext(ctx)
var versions map[string]modData
var tags map[string]struct{}
klog.V(1).Info("Fetching tag and SumDB info")
eg.Go(func() error {
var err error
versions, err = sumFetcher(egctx, modName)
return err
})
eg.Go(func() error {
refIter, err := repo.References()
if err != nil {
return fmt.Errorf("failed to list references: %v", err)
}
tags = make(map[string]struct{})
if err := refIter.ForEach(func(ref *plumbing.Reference) error {
if ref.Name().IsTag() {
tagName := ref.Name().Short()
tags[tagName] = struct{}{}
}
return nil
}); err != nil {
return fmt.Errorf("failed to list tags: %v", err)
}
return nil
})
if err := eg.Wait(); err != nil {
return report, err
}
klog.V(1).Infof("Fetched %d tags and %d SumDB versions", len(tags), len(versions))
// Create a sorted slice of versions
sv := make([]string, 0, len(versions))
for v := range versions {
sv = append(sv, v)
}
semver.Sort(sv)
vErrors := make([]error, 0)
for _, v := range sv {
sumHashes := versions[v]
vr, err := reportVersion(ctx, modRoot, goModPath, modName, repo, v)
if err != nil {
vErrors = append(vErrors, fmt.Errorf("failed to get report for version %q: %v", v, err))
}
vr.sumFound = true
vr.sumModHash = sumHashes.modHash
vr.sumZipHash = sumHashes.zipHash
vr.sumIndex = sumHashes.index
report.versions = append(report.versions, vr)
if vr.gitCommitHash != nil {
delete(tags, v)
}
}
klog.V(1).Info("Finished compiling report")
// TODO(mhutchinson): Include information on tags in git that aren't in SumDB
// if len(tags) > 0 {
// for t := range tags {
// report.versions = append(report.versions, versionReport{
// version: t,
// sumFound: false,
// // also include git info here
// })
// }
// }
return report, errors.Join(vErrors...)
}
func reportVersion(ctx context.Context, modRoot string, goModPath string, modName string, repo *git.Repository, v string) (versionReport, error) {
report := versionReport{
version: v,
}
// Find the commit this tag points to.
ref := plumbing.NewTagReferenceName(v)
hash, err := repo.ResolveRevision(plumbing.Revision(ref))
if err != nil {
return report, fmt.Errorf("failed to resolve tag '%s' to a commit: %v", ref, err)
}
// Update report to include the sha1 commit hash the tag points to.
report.gitCommitHash = hash[:]
// Find the go.mod file in this commit.
commit, err := repo.CommitObject(*hash)
if err != nil {
return report, fmt.Errorf("failed to get commit object: %v", err)
}
tree, err := commit.Tree()
if err != nil {
return report, fmt.Errorf("failed to get commit tree: %v", err)
}
// There is some gnarliness here:
// - the path passed in must only be "go.mod" as that is used in the hash construction
// - the path _may_ be nested, in reality
// The workaround is to pass in "go.mod" and then ignore it in the function, and just use
// the real path in the git directory when looking it up.
hs, err := dirhash.Hash1([]string{"go.mod"}, func(string) (io.ReadCloser, error) {
modFile, err := tree.File(goModPath)
if err != nil {
return nil, err
}
blob, err := repo.BlobObject(modFile.Hash)
if err != nil {
return nil, fmt.Errorf("failed to get blob object: %v", err)
}
return blob.Reader()
})
if err != nil {
if errors.Is(err, object.ErrFileNotFound) {
// This isn't necessarily an error: old versions didn't have go.mod files
return report, nil
}
return report, fmt.Errorf("failed to calculate file hash: %v", err)
}
// Update the report with the mod hash, and then calculate the zip hash.
report.gitModHash = hs
f, err := os.CreateTemp("", "goModZip")
if err != nil {
return report, fmt.Errorf("failed to create temp file: %v", err)
}
defer func() {
if err := os.Remove(f.Name()); err != nil {
klog.Warningf("failed to remove temporary file: %v", err)
}
}()
modVer := module.Version{
Path: modName,
Version: v,
}
subdir := filepath.Dir(goModPath)
if subdir == "." {
subdir = ""
}
if err := zip.CreateFromVCS(f, modVer, modRoot, hash.String(), subdir); err != nil {
return report, fmt.Errorf("failed to create zip file: %v", err)
}
report.gitZipHash, err = dirhash.HashZip(f.Name(), dirhash.Hash1)
if err != nil {
return report, fmt.Errorf("failed to hash zip file: %v", err)
}
return report, nil
}
type diffReport struct {
modName string
modPath string
versions []versionReport
}
type versionReport struct {
version string
// These fields are written in-order, as further info becomes available.
// If no commit hash is present, then the version tag was not found in git.
gitCommitHash []byte
gitModHash string
gitZipHash string
// If sumFound is not set, then no entry was found in SumDB so fields below
// should not be referenced.
sumFound bool
sumIndex uint64
sumModHash string
sumZipHash string
}
type modData struct {
index uint64
zipHash string
modHash string
}
func queryIndex(ctx context.Context, vic *client.VIndexClient, modName string) (map[string]modData, error) {
idxes, inCp, err := vic.Lookup(ctx, modName)
if err != nil {
return nil, fmt.Errorf("failed to look up key: %v", err)
}
versions := make(map[string]modData, len(idxes))
lr := newInputLogClientFromFlags()
klog.V(1).Infof("Dereferencing %d pointers", len(idxes))
for leaf, err := range lr.Dereference(ctx, inCp, idxes) {
if err != nil {
return nil, fmt.Errorf("failed to get leaf at index %d: %v", leaf.Index, err)
}
version, data, err := parseLeaf(leaf.Index, leaf.Data)
if err != nil {
return nil, fmt.Errorf("failed to parse leaf at index %d: %v", leaf.Index, err)
}
if prev, found := versions[version]; found {
return nil, fmt.Errorf("conflicting versions for version %q found!\n%v\n%v", version, prev, data)
}
versions[version] = data
}
return versions, nil
}
// parseLeaf extracts the version string and the hashes from the raw leaf data.
func parseLeaf(idx uint64, data []byte) (string, modData, error) {
lines := strings.Split(string(data), "\n")
if len(lines) < 2 {
return "", modData{}, fmt.Errorf("expected 2 lines but got %d", len(lines))
}
line0Parts := line0RE.FindStringSubmatch(lines[0])
line0Module, line0Version, zipHashB64 := line0Parts[1], line0Parts[2], line0Parts[3]
line1Parts := line1RE.FindStringSubmatch(lines[1])
line1Module, line1Version, modHashB64 := line1Parts[1], line1Parts[2], line1Parts[3]
if line0Module != line1Module {
return "", modData{}, fmt.Errorf("mismatched module names: (%s, %s)", line0Module, line1Module)
}
if line0Version != line1Version {
return "", modData{}, fmt.Errorf("mismatched version names: (%s, %s)", line0Version, line1Version)
}
return line0Version, modData{
index: idx,
zipHash: zipHashB64,
modHash: modHashB64,
}, nil
}
func newVIndexClientFromFlags() *client.VIndexClient {
inV, err := note.NewVerifier(*inLogPubKey)
if err != nil {
klog.Exitf("failed to construct Input Log verifier: %v", err)
}
outV, err := note.NewVerifier(*outLogPubKey)
if err != nil {
klog.Exitf("failed to construct VIndex verifier: %v", err)
}
u, err := url.JoinPath(*baseURL, "/vindex/")
if err != nil {
klog.Exitf("failed to construct VIndex URL: %v", err)
}
c, err := client.NewVIndexClient(u, inV, outV)
if err != nil {
klog.Exitf("failed to construct VIndex Client: %v", err)
}
return c
}
func newInputLogClientFromFlags() *client.InputLogClient {
v, err := note.NewVerifier(*inLogPubKey)
if err != nil {
klog.Exitf("failed to construct Input Log verifier: %v", err)
}
u, err := url.JoinPath(*baseURL, "/inputlog/")
if err != nil {
klog.Exitf("failed to construct Input Log URL: %v", err)
}
origin := "go.sum database tree"
c, err := client.NewInputLogClient(u, origin, v, http.DefaultClient)
if err != nil {
klog.Exitf("failed to construct Input Log client: %v", err)
}
return c
}