From 72add538eb79dd1180e73ba3acd3b1301f91e126 Mon Sep 17 00:00:00 2001 From: Eddie Zaneski Date: Mon, 11 Apr 2022 06:26:29 -0600 Subject: [PATCH] Fix search without sha prefix (#767) Signed-off-by: Eddie Zaneski --- cmd/rekor-cli/app/search.go | 11 ++--------- pkg/api/index.go | 3 ++- pkg/util/sha.go | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 10 deletions(-) create mode 100644 pkg/util/sha.go diff --git a/cmd/rekor-cli/app/search.go b/cmd/rekor-cli/app/search.go index c9123e28c..f6e3fc428 100644 --- a/cmd/rekor-cli/app/search.go +++ b/cmd/rekor-cli/app/search.go @@ -37,6 +37,7 @@ import ( "github.com/sigstore/rekor/pkg/generated/client/index" "github.com/sigstore/rekor/pkg/generated/models" "github.com/sigstore/rekor/pkg/log" + "github.com/sigstore/rekor/pkg/util" ) type searchCmdOutput struct { @@ -108,15 +109,7 @@ var searchCmd = &cobra.Command{ artifactStr := viper.GetString("artifact") sha := viper.GetString("sha") if sha != "" { - var prefix string - if !strings.HasPrefix(sha, "sha256:") && !strings.HasPrefix(sha, "sha1:") { - if len(sha) == 40 { - prefix = "sha1:" - } else { - prefix = "sha256:" - } - } - params.Query.Hash = fmt.Sprintf("%v%v", prefix, sha) + params.Query.Hash = util.PrefixSHA(sha) } else if artifactStr != "" { hasher := sha256.New() var tee io.Reader diff --git a/pkg/api/index.go b/pkg/api/index.go index 27a9f5c82..11eb42f83 100644 --- a/pkg/api/index.go +++ b/pkg/api/index.go @@ -38,8 +38,9 @@ func SearchIndexHandler(params index.SearchIndexParams) middleware.Responder { var result []string if params.Query.Hash != "" { // This must be a valid sha256 hash + sha := util.PrefixSHA(params.Query.Hash) var resultUUIDs []string - if err := redisClient.Do(httpReqCtx, radix.Cmd(&resultUUIDs, "LRANGE", strings.ToLower(params.Query.Hash), "0", "-1")); err != nil { + if err := redisClient.Do(httpReqCtx, radix.Cmd(&resultUUIDs, "LRANGE", strings.ToLower(sha), "0", "-1")); err != nil { return handleRekorAPIError(params, http.StatusInternalServerError, err, redisUnexpectedResult) } result = append(result, resultUUIDs...) diff --git a/pkg/util/sha.go b/pkg/util/sha.go new file mode 100644 index 000000000..4ea65d270 --- /dev/null +++ b/pkg/util/sha.go @@ -0,0 +1,33 @@ +// Copyright 2022 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 util + +import ( + "fmt" + "strings" +) + +// PrefixSHA sets the prefix of a sha hash to match how it is stored based on the length. +func PrefixSHA(sha string) string { + var prefix string + if !strings.HasPrefix(sha, "sha256:") && !strings.HasPrefix(sha, "sha1:") { + if len(sha) == 40 { + prefix = "sha1:" + } else { + prefix = "sha256:" + } + } + return fmt.Sprintf("%v%v", prefix, sha) +}