|
18 | 18 | package main
|
19 | 19 |
|
20 | 20 | import (
|
| 21 | + "bytes" |
21 | 22 | "context"
|
22 | 23 | "errors"
|
23 | 24 | "flag"
|
@@ -71,24 +72,63 @@ func main() {
|
71 | 72 | }
|
72 | 73 |
|
73 | 74 | func run(ctx context.Context) error {
|
74 |
| - if *baseURL == "" { |
75 |
| - return errors.New("base_url flag must be provided") |
76 |
| - } |
77 |
| - if *outLogPubKey == "" { |
78 |
| - return errors.New("out_log_pub_key flag must be provided") |
79 |
| - } |
80 | 75 | if *modRoot == "" {
|
81 | 76 | return errors.New("mod_root flag must be provided")
|
82 | 77 | }
|
83 | 78 |
|
84 |
| - // TODO(mhutchinson): Support a non-VIndex version of this that reads the non-verifiable proxy endpoints: |
85 |
| - // 1) https://proxy.golang.org/github.com/transparency-dev/tessera/@v/list |
86 |
| - // 2) https://sum.golang.org/lookup/github.com/transparency-dev/tessera@v1.0.0 |
87 |
| - // This will provide a way to use this tool before the VIndex is widely available |
88 |
| - sumFetcher := func(ctx context.Context, modName string) (map[string]modData, error) { |
89 |
| - vic := newVIndexClientFromFlags() |
| 79 | + var sumFetcher func(ctx context.Context, modName string) (map[string]modData, error) |
| 80 | + if *baseURL == "" { |
| 81 | + klog.Warningf("--base_url is not provided. Using NON-VERIFIABLE lookup to source SumDB data.") |
| 82 | + |
| 83 | + // This constructs the map non-verifiably by calling similar URLs to these: |
| 84 | + // 1) https://proxy.golang.org/github.com/transparency-dev/tessera/@v/list |
| 85 | + // 2) https://sum.golang.org/lookup/github.com/transparency-dev/tessera@v1.0.0 |
| 86 | + sumFetcher = func(ctx context.Context, modName string) (map[string]modData, error) { |
| 87 | + result := make(map[string]modData) |
| 88 | + resp, err := http.Get(fmt.Sprintf("https://proxy.golang.org/%s/@v/list", modName)) |
| 89 | + if err != nil { |
| 90 | + return nil, fmt.Errorf("failed to get module listing: %v", err) |
| 91 | + } |
| 92 | + body, err := io.ReadAll(resp.Body) |
| 93 | + if err != nil { |
| 94 | + return nil, fmt.Errorf("failed to get module listing: %v", err) |
| 95 | + } |
| 96 | + for v := range strings.Lines(string(body)) { |
| 97 | + v = strings.TrimSpace(v) |
| 98 | + resp, err = http.Get(fmt.Sprintf("https://sum.golang.org/lookup/%s@%s", modName, v)) |
| 99 | + if err != nil { |
| 100 | + return nil, fmt.Errorf("failed to get version info: %v", err) |
| 101 | + } |
| 102 | + body, err = io.ReadAll(resp.Body) |
| 103 | + if err != nil { |
| 104 | + return nil, fmt.Errorf("failed to get version info: %v", err) |
| 105 | + } |
| 106 | + lines := bytes.Split(body, []byte{'\n'}) |
| 107 | + idx, err := strconv.ParseInt(string(lines[0]), 10, 64) |
| 108 | + if err != nil { |
| 109 | + return nil, fmt.Errorf("failed to parse index: %v", err) |
| 110 | + } |
| 111 | + leaf := append(append(append(lines[1], byte('\n')), lines[2]...), byte('\n')) |
| 112 | + v2, md, err := parseLeaf(uint64(idx), leaf) |
| 113 | + if err != nil { |
| 114 | + return nil, fmt.Errorf("failed to parse leaf: %v", err) |
| 115 | + } |
| 116 | + if v != v2 { |
| 117 | + return nil, fmt.Errorf("performed lookup for %s@%s but got version %s", modName, v, v2) |
| 118 | + } |
| 119 | + result[v] = md |
| 120 | + } |
| 121 | + return result, nil |
| 122 | + } |
90 | 123 |
|
91 |
| - return queryIndex(ctx, vic, modName) |
| 124 | + } else { |
| 125 | + if *outLogPubKey == "" { |
| 126 | + return errors.New("out_log_pub_key flag must be provided if --base_url is provided") |
| 127 | + } |
| 128 | + sumFetcher = func(ctx context.Context, modName string) (map[string]modData, error) { |
| 129 | + vic := newVIndexClientFromFlags() |
| 130 | + return queryIndex(ctx, vic, modName) |
| 131 | + } |
92 | 132 | }
|
93 | 133 |
|
94 | 134 | report, reportErr := getReport(ctx, *modRoot, sumFetcher)
|
|
0 commit comments