...

Source file src/github.com/sigstore/rekor/cmd/rekor-cli/app/log_proof.go

Documentation: github.com/sigstore/rekor/cmd/rekor-cli/app

     1  //
     2  // Copyright 2021 The Sigstore Authors.
     3  //
     4  // Licensed under the Apache License, Version 2.0 (the "License");
     5  // you may not use this file except in compliance with the License.
     6  // You may obtain a copy of the License at
     7  //
     8  //     http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  // Unless required by applicable law or agreed to in writing, software
    11  // distributed under the License is distributed on an "AS IS" BASIS,
    12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  
    16  package app
    17  
    18  import (
    19  	"errors"
    20  	"fmt"
    21  	"os"
    22  
    23  	"github.com/spf13/cobra"
    24  	"github.com/spf13/viper"
    25  
    26  	"github.com/sigstore/rekor/cmd/rekor-cli/app/format"
    27  	"github.com/sigstore/rekor/pkg/client"
    28  	"github.com/sigstore/rekor/pkg/generated/client/tlog"
    29  	"github.com/sigstore/rekor/pkg/log"
    30  )
    31  
    32  type logProofOutput struct {
    33  	RootHash string
    34  	Hashes   []string
    35  }
    36  
    37  func (l *logProofOutput) String() string {
    38  	s := fmt.Sprintf("Current Root Hash: %v\n", l.RootHash)
    39  	s += "Hashes: ["
    40  	for i, hash := range l.Hashes {
    41  		if i+1 == len(l.Hashes) {
    42  			s += hash
    43  		} else {
    44  			s += fmt.Sprintf("%v,", hash)
    45  		}
    46  	}
    47  	s += "]\n"
    48  	return s
    49  }
    50  
    51  // logProof represents the consistency proof
    52  var logProofCmd = &cobra.Command{
    53  	Use:   "logproof",
    54  	Short: "Rekor logproof command",
    55  	Long:  `Prints information required to compute the consistency proof of the transparency log`,
    56  	PreRunE: func(cmd *cobra.Command, _ []string) error {
    57  		// these are bound here so that they are not overwritten by other commands
    58  		if err := viper.BindPFlags(cmd.Flags()); err != nil {
    59  			return fmt.Errorf("error initializing cmd line args: %w", err)
    60  		}
    61  		if viper.GetUint64("first-size") == 0 {
    62  			return errors.New("first-size must be > 0")
    63  		}
    64  		if !viper.IsSet("last-size") {
    65  			return errors.New("last-size must be specified")
    66  		}
    67  		if viper.GetUint64("last-size") == 0 {
    68  			return errors.New("last-size must be > 0")
    69  		}
    70  		if viper.GetUint64("first-size") > viper.GetUint64("last-size") {
    71  			return errors.New("last-size must be >= to first-size")
    72  		}
    73  		return nil
    74  	},
    75  	Run: format.WrapCmd(func(_ []string) (interface{}, error) {
    76  		rekorClient, err := client.GetRekorClient(viper.GetString("rekor_server"), client.WithUserAgent(UserAgent()), client.WithRetryCount(viper.GetUint("retry")), client.WithLogger(log.CliLogger))
    77  		if err != nil {
    78  			return nil, err
    79  		}
    80  
    81  		firstSize := int64(viper.GetUint64("first-size"))
    82  		lastSize := int64(viper.GetUint64("last-size"))
    83  		treeID := viper.GetString("tree-id")
    84  
    85  		params := tlog.NewGetLogProofParams()
    86  		params.FirstSize = &firstSize
    87  		params.LastSize = lastSize
    88  		params.TreeID = &treeID
    89  		params.SetTimeout(viper.GetDuration("timeout"))
    90  
    91  		result, err := rekorClient.Tlog.GetLogProof(params)
    92  		if err != nil {
    93  			return nil, err
    94  		}
    95  
    96  		consistencyProof := result.GetPayload()
    97  		return &logProofOutput{
    98  			RootHash: *consistencyProof.RootHash,
    99  			Hashes:   consistencyProof.Hashes,
   100  		}, nil
   101  	}),
   102  }
   103  
   104  func init() {
   105  	initializePFlagMap()
   106  	logProofCmd.Flags().Uint64("first-size", 1, "the size of the log where the proof should begin")
   107  	logProofCmd.Flags().Uint64("last-size", 0, "the size of the log where the proof should end")
   108  	logProofCmd.Flags().String("tree-id", "", "the tree id of the log (defaults to active tree)")
   109  
   110  	if err := logProofCmd.MarkFlagRequired("last-size"); err != nil {
   111  		fmt.Println(err)
   112  		os.Exit(1)
   113  	}
   114  
   115  	rootCmd.AddCommand(logProofCmd)
   116  }
   117  

View as plain text