Skip to content

Commit

Permalink
add timeout flag to rekor-cli (#390)
Browse files Browse the repository at this point in the history
Signed-off-by: Bob Callaway <bob.callaway@gmail.com>
  • Loading branch information
bobcallaway authored Jul 28, 2021
1 parent 70eed2f commit 464970c
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 1 deletion.
2 changes: 2 additions & 0 deletions cmd/rekor-cli/app/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ var getCmd = &cobra.Command{
logIndex := viper.GetString("log-index")
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)
Expand All @@ -108,6 +109,7 @@ var getCmd = &cobra.Command{
uuid := viper.GetString("uuid")
if uuid != "" {
params := entries.NewGetLogEntryByUUIDParams()
params.SetTimeout(viper.GetDuration("timeout"))
params.EntryUUID = uuid

resp, err := rekorClient.Entries.GetLogEntryByUUID(params)
Expand Down
4 changes: 3 additions & 1 deletion cmd/rekor-cli/app/log_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ var logInfoCmd = &cobra.Command{
return nil, err
}

result, err := rekorClient.Tlog.GetLogInfo(nil)
params := tlog.GetLogInfoParams{}
params.SetTimeout(viper.GetDuration("timeout"))
result, err := rekorClient.Tlog.GetLogInfo(&params)
if err != nil {
return nil, err
}
Expand Down
1 change: 1 addition & 0 deletions cmd/rekor-cli/app/log_proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ var logProofCmd = &cobra.Command{
params := tlog.NewGetLogProofParams()
params.FirstSize = &firstSize
params.LastSize = lastSize
params.SetTimeout(viper.GetDuration("timeout"))

result, err := rekorClient.Tlog.GetLogProof(params)
if err != nil {
Expand Down
18 changes: 18 additions & 0 deletions cmd/rekor-cli/app/pflags.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"log"
"strconv"
"strings"
"time"

"github.com/sigstore/rekor/pkg/pki"
"github.com/spf13/pflag"
Expand All @@ -42,6 +43,7 @@ const (
fileOrURLFlag FlagType = "fileOrURL"
oidFlag FlagType = "oid"
formatFlag FlagType = "format"
timeoutFlag FlagType = "timeout"
)

type newPFlagValueFunc func() pflag.Value
Expand Down Expand Up @@ -95,6 +97,10 @@ func initializePFlagMap() {
// this validates the output format requested
return valueFactory(formatFlag, validateString("required,oneof=json default"), "")
},
timeoutFlag: func() pflag.Value {
// this validates the timeout is >= 0
return valueFactory(formatFlag, validateTimeout, "")
},
}
}

Expand Down Expand Up @@ -210,6 +216,18 @@ func validateOID(v string) error {
return useValidator(oidFlag, o)
}

// validateTimeout ensures that the supplied string is a valid time.Duration value >= 0
func validateTimeout(v string) error {
duration, err := time.ParseDuration(v)
if err != nil {
return err
}
d := struct {
Duration time.Duration `validate:"min=0"`
}{duration}
return useValidator(timeoutFlag, d)
}

// validateTypeFlag ensures that the string is in the format type(\.version)? and
// that one of the types requested is implemented
func validateTypeFlag(v string) error {
Expand Down
1 change: 1 addition & 0 deletions cmd/rekor-cli/app/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func init() {

rootCmd.PersistentFlags().Var(NewFlagValue(urlFlag, "https://rekor.sigstore.dev"), "rekor_server", "Server address:port")
rootCmd.PersistentFlags().Var(NewFlagValue(formatFlag, "default"), "format", "Command output format")
rootCmd.PersistentFlags().Var(NewFlagValue(timeoutFlag, "30s"), "timeout", "HTTP timeout")

rootCmd.PersistentFlags().String("api-key", "", "API key for rekor.sigstore.dev")

Expand Down
1 change: 1 addition & 0 deletions cmd/rekor-cli/app/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ var searchCmd = &cobra.Command{
}

params := index.NewSearchIndexParams()
params.SetTimeout(viper.GetDuration("timeout"))
params.Query = &models.SearchIndex{}

artifactStr := viper.GetString("artifact")
Expand Down
1 change: 1 addition & 0 deletions cmd/rekor-cli/app/timestamp.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ var timestampCmd = &cobra.Command{
}

params := timestamp.NewGetTimestampResponseParams()
params.SetTimeout(viper.GetDuration("timeout"))
params.Request = ioutil.NopCloser(bytes.NewReader(requestBytes))

var respBytes bytes.Buffer
Expand Down
1 change: 1 addition & 0 deletions cmd/rekor-cli/app/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ var uploadCmd = &cobra.Command{
}
var entry models.ProposedEntry
params := entries.NewCreateLogEntryParams()
params.SetTimeout(viper.GetDuration("timeout"))

entryStr := viper.GetString("entry")
if entryStr != "" {
Expand Down
1 change: 1 addition & 0 deletions cmd/rekor-cli/app/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ var verifyCmd = &cobra.Command{
}

searchParams := entries.NewSearchLogQueryParams()
searchParams.SetTimeout(viper.GetDuration("timeout"))
searchLogQuery := models.SearchLogQuery{}

uuid := viper.GetString("uuid")
Expand Down

0 comments on commit 464970c

Please sign in to comment.