...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
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