github.com/google/trillian-examples@v0.0.0-20240520080811-0d40d35cef0e/experimental/batchmap/sumdb/verify/verify.go (about)

     1  // Copyright 2020 Google LLC. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // verify confirms that all of the entries in a go.sum file are committed to
    16  // by the verifiable map created in this demo.
    17  package main
    18  
    19  import (
    20  	"bufio"
    21  	"bytes"
    22  	"crypto"
    23  	"flag"
    24  	"os"
    25  	"strings"
    26  
    27  	"github.com/golang/glog"
    28  
    29  	"github.com/google/trillian-examples/experimental/batchmap/sumdb/mapdb"
    30  	"github.com/google/trillian-examples/experimental/batchmap/sumdb/verification"
    31  
    32  	_ "github.com/mattn/go-sqlite3"
    33  )
    34  
    35  const hash = crypto.SHA512_256
    36  
    37  var (
    38  	sumFile      = flag.String("sum_file", "", "go.sum file to check for integrity.")
    39  	mapDB        = flag.String("map_db", "", "sqlite DB containing the map tiles.")
    40  	treeID       = flag.Int64("tree_id", 12345, "The ID of the tree. Used as a salt in hashing.")
    41  	prefixStrata = flag.Int("prefix_strata", 2, "The number of strata of 8-bit strata before the final strata.")
    42  )
    43  
    44  func main() {
    45  	flag.Parse()
    46  
    47  	if *mapDB == "" {
    48  		glog.Exitf("No map_dir provided")
    49  	}
    50  	if *sumFile == "" {
    51  		glog.Exitf("No sum_file provided")
    52  	}
    53  
    54  	tiledb, err := mapdb.NewTileDB(*mapDB)
    55  	if err != nil {
    56  		glog.Exitf("Failed to open map DB at %q: %v", *mapDB, err)
    57  	}
    58  	var rev int
    59  	var logRoot []byte
    60  	if rev, logRoot, _, err = tiledb.LatestRevision(); err != nil {
    61  		glog.Exitf("No revisions found in map DB at %q: %v", *mapDB, err)
    62  	}
    63  
    64  	mv := verification.NewMapVerifier(tiledb.Tile, *prefixStrata, *treeID, hash)
    65  
    66  	// Open the go.sum file for reading a line at a time.
    67  	file, err := os.Open(*sumFile)
    68  	if err != nil {
    69  		glog.Exitf("failed to read file %q: %q", *sumFile, err)
    70  	}
    71  	defer func() {
    72  		if err := file.Close(); err != nil {
    73  			glog.Errorf("file.Close(): %v", err)
    74  		}
    75  	}()
    76  	scanner := bufio.NewScanner(file)
    77  
    78  	var root []byte
    79  	var count int
    80  	// Confirm that each entry in the go.sum file is committed to by the map.
    81  	// NOTE:
    82  	// This is optimized for clarity of demonstrating inclusion, not for performance.
    83  	// This code leads to a lot of duplicate I/O and tile calculation, particularly
    84  	// for the higher tiles in the map (e.g. the root tile will be read and verified
    85  	// for every single line in the go.sum file).
    86  	for scanner.Scan() {
    87  		line := scanner.Text()
    88  		split := strings.LastIndex(line, " ")
    89  		key := line[:split]
    90  		expectedString := line[split+1:]
    91  		glog.V(1).Infof("checking key %q value %q", key, expectedString)
    92  		newRoot, err := mv.CheckInclusion(rev, key, []byte(expectedString))
    93  		if err != nil {
    94  			glog.Exitf("inclusion check failed for key %q value %q: %q", key, expectedString, err)
    95  		}
    96  		if root != nil && !bytes.Equal(root, newRoot) {
    97  			glog.Exitf("map root changed while verifying file")
    98  		}
    99  		root = newRoot
   100  		count++
   101  	}
   102  	glog.Infof("Verified %d entries committed to by map rev %d root %x. Log checkpoint:\n%s", count, rev, root, logRoot)
   103  }