forked from sigstore/rekor
/
upload.go
159 lines (143 loc) · 4.52 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
//
// 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
}
var entry models.ProposedEntry
params := entries.NewCreateLogEntryParams()
params.SetTimeout(viper.GetDuration("timeout"))
entryStr := viper.GetString("entry")
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)
}
} else {
typeStr, versionStr, err := ParseTypeFlag(viper.GetString("type"))
if err != nil {
return nil, err
}
props := CreatePropsFromPflags()
entry, err = types.NewProposedEntry(context.Background(), typeStr, versionStr, *props)
if err != nil {
return nil, fmt.Errorf("error: %w", err)
}
}
params.SetProposedEntry(entry)
resp, 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
}
}
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)
}
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)
}