/
upload.go
197 lines (181 loc) · 5.8 KB
/
upload.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
//
// 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 (
"context"
"fmt"
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"github.com/go-openapi/runtime"
"github.com/go-openapi/swag"
"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/types"
"github.com/sigstore/rekor/pkg/verify"
)
type uploadCmdOutput struct {
AlreadyExists bool
Location string
Index int64
}
func (u *uploadCmdOutput) String() string {
if u.AlreadyExists {
return fmt.Sprintf("Entry already exists; available at: %v%v\n", viper.GetString("rekor_server"), u.Location)
}
return fmt.Sprintf("Created entry at index %d, available at: %v%v\n", u.Index, viper.GetString("rekor_server"), u.Location)
}
// uploadCmd represents the upload command
var uploadCmd = &cobra.Command{
Use: "upload",
Short: "Upload an artifact to Rekor",
PreRunE: func(cmd *cobra.Command, args []string) error {
// these are bound here so that they are not overwritten by other commands
if err := viper.BindPFlags(cmd.Flags()); err != nil {
return err
}
if err := validateArtifactPFlags(false, false); err != nil {
return err
}
return nil
},
Long: `This command takes the public key, signature and URL of the release artifact and uploads it to the rekor server.`,
Run: format.WrapCmd(func(args []string) (interface{}, error) {
ctx := context.Background()
rekorClient, err := client.GetRekorClient(viper.GetString("rekor_server"), client.WithUserAgent(UserAgent()))
if err != nil {
return nil, err
}
params := entries.NewCreateLogEntryParams()
params.SetTimeout(viper.GetDuration("timeout"))
entryStr := viper.GetString("entry")
var resp *entries.CreateLogEntryCreated
if entryStr != "" {
var entryReader io.Reader
entryURL, err := url.Parse(entryStr)
if err == nil && entryURL.IsAbs() {
/* #nosec G107 */
entryResp, err := http.Get(entryStr)
if err != nil {
return nil, fmt.Errorf("error fetching entry: %w", err)
}
defer entryResp.Body.Close()
entryReader = entryResp.Body
} else {
entryReader, err = os.Open(filepath.Clean(entryStr))
if err != nil {
return nil, fmt.Errorf("error processing entry file: %w", err)
}
}
entry, err := models.UnmarshalProposedEntry(entryReader, runtime.JSONConsumer())
if err != nil {
return nil, fmt.Errorf("error parsing entry file: %w", err)
}
params.SetProposedEntry(entry)
r, err := rekorClient.Entries.CreateLogEntry(params)
if err != nil {
switch e := err.(type) {
case *entries.CreateLogEntryConflict:
return &uploadCmdOutput{
Location: e.Location.String(),
AlreadyExists: true,
}, nil
default:
return nil, err
}
}
resp = r
} else {
// Start with the default entry and work your way down
typeStr, _, err := ParseTypeFlag(viper.GetString("type"))
if err != nil {
return nil, err
}
supportedVersions, err := GetSupportedVersions(viper.GetString("type"))
if err != nil {
return nil, err
}
props := CreatePropsFromPflags()
for _, sv := range supportedVersions {
log.CliLogger.Infof("Trying to upload entry of type %s at version %s", typeStr, sv)
entry, err := types.NewProposedEntry(context.Background(), typeStr, sv, *props)
if err != nil {
return nil, fmt.Errorf("error: %w", err)
}
params.SetProposedEntry(entry)
r, err := rekorClient.Entries.CreateLogEntry(params)
if err == nil {
resp = r
break
}
switch e := err.(type) {
case *entries.CreateLogEntryConflict:
return &uploadCmdOutput{
Location: e.Location.String(),
AlreadyExists: true,
}, nil
default:
log.CliLogger.Warnf("Failed to upload entry, will try with another supported version if one exists: %v", err)
}
}
}
if resp == nil {
return nil, fmt.Errorf("no valid entry was created")
}
var newIndex int64
var logEntry models.LogEntryAnon
for _, entry := range resp.Payload {
newIndex = swag.Int64Value(entry.LogIndex)
logEntry = entry
}
// verify log entry
verifier, err := loadVerifier(rekorClient)
if err != nil {
return nil, fmt.Errorf("retrieving rekor public key")
}
if err := verify.VerifySignedEntryTimestamp(ctx, &logEntry, verifier); err != nil {
return nil, fmt.Errorf("unable to verify entry was added to log: %w", err)
}
// TODO: Remove conditional once inclusion proof/checkpoint is always returned by server.
if logEntry.Verification.InclusionProof != nil {
// verify inclusion proof
if err := verify.VerifyInclusion(ctx, &logEntry); err != nil {
return nil, fmt.Errorf("error verifying inclusion proof: %w", err)
}
// verify checkpoint
if err := verify.VerifyCheckpointSignature(&logEntry, verifier); err != nil {
return nil, err
}
}
return &uploadCmdOutput{
Location: string(resp.Location),
Index: newIndex,
}, nil
}),
}
func init() {
initializePFlagMap()
if err := addArtifactPFlags(uploadCmd); err != nil {
log.CliLogger.Fatal("Error parsing cmd line args:", err)
}
rootCmd.AddCommand(uploadCmd)
}