/
get.go
209 lines (180 loc) · 5.75 KB
/
get.go
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
//
// Copyright 2021 The Sigstore Authors.
//
// 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.
package app
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"strconv"
"time"
"github.com/go-openapi/runtime"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/sigstore/rekor/cmd/rekor-cli/app/format"
"github.com/sigstore/rekor/pkg/client"
"github.com/sigstore/rekor/pkg/generated/client/entries"
"github.com/sigstore/rekor/pkg/generated/models"
"github.com/sigstore/rekor/pkg/log"
"github.com/sigstore/rekor/pkg/sharding"
"github.com/sigstore/rekor/pkg/types"
)
type getCmdOutput struct {
Attestation string
AttestationType string
Body interface{}
LogIndex int
IntegratedTime int64
UUID string
LogID string
}
func (g *getCmdOutput) String() string {
s := fmt.Sprintf("LogID: %v\n", g.LogID)
if g.Attestation != "" {
s += fmt.Sprintf("Attestation: %s\n", g.Attestation)
}
s += fmt.Sprintf("Index: %d\n", g.LogIndex)
dt := time.Unix(g.IntegratedTime, 0).UTC().Format(time.RFC3339)
s += fmt.Sprintf("IntegratedTime: %s\n", dt)
s += fmt.Sprintf("Entry UUID: %s\n", g.UUID)
var b bytes.Buffer
e := json.NewEncoder(&b)
e.SetIndent("", " ")
_ = e.Encode(g.Body)
s += fmt.Sprintf("Body: %s\n", b.Bytes())
return s
}
// getCmd represents the get command
var getCmd = &cobra.Command{
Use: "get",
Short: "Rekor get command",
Long: `Get information regarding entries in the transparency log`,
PreRun: func(cmd *cobra.Command, args []string) {
// these are bound here so that they are not overwritten by other commands
if err := viper.BindPFlags(cmd.Flags()); err != nil {
log.CliLogger.Fatal("Error initializing cmd line args: ", err)
}
},
Run: format.WrapCmd(func(args []string) (interface{}, error) {
rekorClient, err := client.GetRekorClient(viper.GetString("rekor_server"), client.WithUserAgent(UserAgent()))
if err != nil {
return nil, err
}
logIndex := viper.GetString("log-index")
uuid := viper.GetString("uuid")
if logIndex == "" && uuid == "" {
return nil, errors.New("either --uuid or --log-index must be specified")
}
if logIndex != "" {
params := entries.NewGetLogEntryByIndexParams()
params.SetTimeout(viper.GetDuration("timeout"))
logIndexInt, err := strconv.ParseInt(logIndex, 10, 0)
if err != nil {
return nil, fmt.Errorf("error parsing --log-index: %w", err)
}
params.LogIndex = logIndexInt
resp, err := rekorClient.Entries.GetLogEntryByIndex(params)
if err != nil {
return nil, err
}
for ix, entry := range resp.Payload {
if verified, err := verifyLogEntry(context.Background(), rekorClient, entry); err != nil || !verified {
return nil, fmt.Errorf("unable to verify entry was added to log %w", err)
}
return parseEntry(ix, entry)
}
}
// Note: this UUID may be an EntryID
if uuid != "" {
params := entries.NewGetLogEntryByUUIDParams()
params.SetTimeout(viper.GetDuration("timeout"))
// NOTE: This undoes the change that let people pass in longer UUIDs without
// trouble even if their client is old, a.k.a. it will be able to use the TreeID
// (if present) for routing in the GetLogEntryByUUIDHandler
params.EntryUUID = uuid
resp, err := rekorClient.Entries.GetLogEntryByUUID(params)
if err != nil {
return nil, err
}
paramsUUID, err := sharding.GetUUIDFromIDString(params.EntryUUID)
if err != nil {
return nil, err
}
paramsTreeID, err := sharding.GetTreeIDFromIDString(params.EntryUUID)
if !errors.Is(err, sharding.ErrPlainUUID) {
return nil, err
}
for k, entry := range resp.Payload {
outputUUID, err := sharding.GetUUIDFromIDString(k)
if err != nil {
return nil, err
}
outTreeID, err := sharding.GetTreeIDFromIDString(k)
if !errors.Is(err, sharding.ErrPlainUUID) {
return nil, err
}
// Compare against expected UUID and Tree ID (if present).
if outputUUID != paramsUUID {
continue
}
if paramsTreeID != "" && outTreeID != "" && paramsTreeID != outTreeID {
continue
}
if verified, err := verifyLogEntry(context.Background(), rekorClient, entry); err != nil || !verified {
return nil, fmt.Errorf("unable to verify entry was added to log %w", err)
}
return parseEntry(k, entry)
}
}
return nil, errors.New("entry not found")
}),
}
func parseEntry(uuid string, e models.LogEntryAnon) (interface{}, error) {
b, err := base64.StdEncoding.DecodeString(e.Body.(string))
if err != nil {
return nil, err
}
pe, err := models.UnmarshalProposedEntry(bytes.NewReader(b), runtime.JSONConsumer())
if err != nil {
return nil, err
}
eimpl, err := types.UnmarshalEntry(pe)
if err != nil {
return nil, err
}
obj := getCmdOutput{
Body: eimpl,
UUID: uuid,
IntegratedTime: *e.IntegratedTime,
LogIndex: int(*e.LogIndex),
LogID: *e.LogID,
}
if e.Attestation != nil {
obj.Attestation = string(e.Attestation.Data)
}
return &obj, nil
}
func init() {
initializePFlagMap()
if err := addUUIDPFlags(getCmd, false); err != nil {
log.CliLogger.Fatal("Error parsing cmd line args: ", err)
}
if err := addLogIndexFlag(getCmd, false); err != nil {
log.CliLogger.Fatal("Error parsing cmd line args: ", err)
}
rootCmd.AddCommand(getCmd)
}