Skip to content

Commit 72add53

Browse files
authored
Fix search without sha prefix (sigstore#767)
Signed-off-by: Eddie Zaneski <eddiezane@gmail.com>
1 parent 3b9511b commit 72add53

File tree

3 files changed

+37
-10
lines changed

3 files changed

+37
-10
lines changed

cmd/rekor-cli/app/search.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
"github.com/sigstore/rekor/pkg/generated/client/index"
3838
"github.com/sigstore/rekor/pkg/generated/models"
3939
"github.com/sigstore/rekor/pkg/log"
40+
"github.com/sigstore/rekor/pkg/util"
4041
)
4142

4243
type searchCmdOutput struct {
@@ -108,15 +109,7 @@ var searchCmd = &cobra.Command{
108109
artifactStr := viper.GetString("artifact")
109110
sha := viper.GetString("sha")
110111
if sha != "" {
111-
var prefix string
112-
if !strings.HasPrefix(sha, "sha256:") && !strings.HasPrefix(sha, "sha1:") {
113-
if len(sha) == 40 {
114-
prefix = "sha1:"
115-
} else {
116-
prefix = "sha256:"
117-
}
118-
}
119-
params.Query.Hash = fmt.Sprintf("%v%v", prefix, sha)
112+
params.Query.Hash = util.PrefixSHA(sha)
120113
} else if artifactStr != "" {
121114
hasher := sha256.New()
122115
var tee io.Reader

pkg/api/index.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ func SearchIndexHandler(params index.SearchIndexParams) middleware.Responder {
3838
var result []string
3939
if params.Query.Hash != "" {
4040
// This must be a valid sha256 hash
41+
sha := util.PrefixSHA(params.Query.Hash)
4142
var resultUUIDs []string
42-
if err := redisClient.Do(httpReqCtx, radix.Cmd(&resultUUIDs, "LRANGE", strings.ToLower(params.Query.Hash), "0", "-1")); err != nil {
43+
if err := redisClient.Do(httpReqCtx, radix.Cmd(&resultUUIDs, "LRANGE", strings.ToLower(sha), "0", "-1")); err != nil {
4344
return handleRekorAPIError(params, http.StatusInternalServerError, err, redisUnexpectedResult)
4445
}
4546
result = append(result, resultUUIDs...)

pkg/util/sha.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2022 The Sigstore Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package util
16+
17+
import (
18+
"fmt"
19+
"strings"
20+
)
21+
22+
// PrefixSHA sets the prefix of a sha hash to match how it is stored based on the length.
23+
func PrefixSHA(sha string) string {
24+
var prefix string
25+
if !strings.HasPrefix(sha, "sha256:") && !strings.HasPrefix(sha, "sha1:") {
26+
if len(sha) == 40 {
27+
prefix = "sha1:"
28+
} else {
29+
prefix = "sha256:"
30+
}
31+
}
32+
return fmt.Sprintf("%v%v", prefix, sha)
33+
}

0 commit comments

Comments
 (0)