Skip to content

Commit

Permalink
Fix search without sha prefix (#767)
Browse files Browse the repository at this point in the history
Signed-off-by: Eddie Zaneski <eddiezane@gmail.com>
  • Loading branch information
eddiezane authored Apr 11, 2022
1 parent 3b9511b commit 72add53
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
11 changes: 2 additions & 9 deletions cmd/rekor-cli/app/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion pkg/api/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down
33 changes: 33 additions & 0 deletions pkg/util/sha.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 72add53

Please sign in to comment.