Skip to content

Commit 2b4d1ba

Browse files
authored
Add sha256 prefix to index keys for artifact hashes (sigstore#290)
* Add sha256 prefix to index keys for artifact hashes This change adds the `sha256:` prefix to index values that are created to simplify searching the transparency log for artifacts. In case we shift to using a different hashing algorithm in the future, this will provide a way to specify it. Fixes sigstore#289 Signed-off-by: Bob Callaway <bob.callaway@gmail.com>
1 parent 9e3e56d commit 2b4d1ba

File tree

11 files changed

+70
-15
lines changed

11 files changed

+70
-15
lines changed

cmd/rekor-cli/app/pflags.go

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"os"
2828
"path/filepath"
2929
"strconv"
30+
"strings"
3031

3132
"github.com/go-openapi/strfmt"
3233
"github.com/go-openapi/swag"
@@ -47,7 +48,7 @@ func addSearchPFlags(cmd *cobra.Command) error {
4748

4849
cmd.Flags().Var(&fileOrURLFlag{}, "artifact", "path or URL to artifact file")
4950

50-
cmd.Flags().Var(&uuidFlag{}, "sha", "the SHA256 sum of the artifact")
51+
cmd.Flags().Var(&shaFlag{}, "sha", "the SHA256 sum of the artifact")
5152

5253
cmd.Flags().Var(&emailFlag{}, "email", "email associated with the public key's subject")
5354
return nil
@@ -468,6 +469,36 @@ func (f *pkiFormatFlag) Set(s string) error {
468469
return fmt.Errorf("value specified is invalid: [%s] supported values are: [pgp, minisign, x509, ssh]", s)
469470
}
470471

472+
type shaFlag struct {
473+
hash string
474+
}
475+
476+
func (s *shaFlag) String() string {
477+
return s.hash
478+
}
479+
480+
func (s *shaFlag) Set(v string) error {
481+
if v == "" {
482+
return errors.New("flag must be specified")
483+
}
484+
strToCheck := v
485+
if strings.HasPrefix(v, "sha256:") {
486+
strToCheck = strings.Replace(v, "sha256:", "", 1)
487+
}
488+
if _, err := hex.DecodeString(strToCheck); (err != nil) || (len(strToCheck) != 64) {
489+
if err == nil {
490+
err = errors.New("invalid length for value")
491+
}
492+
return fmt.Errorf("value specified is invalid: %w", err)
493+
}
494+
s.hash = v
495+
return nil
496+
}
497+
498+
func (s *shaFlag) Type() string {
499+
return "sha"
500+
}
501+
471502
type uuidFlag struct {
472503
hash string
473504
}

cmd/rekor-cli/app/search.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ var searchCmd = &cobra.Command{
116116
}
117117

118118
hashVal := strings.ToLower(hex.EncodeToString(hasher.Sum(nil)))
119-
params.Query.Hash = hashVal
119+
params.Query.Hash = "sha256:" + hashVal
120120
}
121121

122122
publicKeyStr := viper.GetString("public-key")

openapi.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ definitions:
338338
- "format"
339339
hash:
340340
type: string
341-
pattern: '^[0-9a-fA-F]{64}$'
341+
pattern: '^(sha256:)?[0-9a-fA-F]{64}$'
342342

343343
SearchLogQuery:
344344
type: object

pkg/api/entries.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -296,12 +296,14 @@ func SearchLogQueryHandler(params entries.SearchLogQueryParams) middleware.Respo
296296
}
297297

298298
for _, leafResp := range searchByHashResults {
299-
logEntry, err := logEntryFromLeaf(tc, leafResp.Leaf, leafResp.SignedLogRoot, leafResp.Proof)
300-
if err != nil {
301-
return handleRekorAPIError(params, code, err, err.Error())
302-
}
299+
if leafResp != nil {
300+
logEntry, err := logEntryFromLeaf(tc, leafResp.Leaf, leafResp.SignedLogRoot, leafResp.Proof)
301+
if err != nil {
302+
return handleRekorAPIError(params, code, err, err.Error())
303+
}
303304

304-
resultPayload = append(resultPayload, logEntry)
305+
resultPayload = append(resultPayload, logEntry)
306+
}
305307
}
306308
}
307309

pkg/generated/models/search_index.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/generated/restapi/embedded_spec.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/types/jar/v0.0.1/entry.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ func (v V001Entry) IndexKeys() []string {
9393
}
9494

9595
if v.JARModel.Archive.Hash != nil {
96-
result = append(result, strings.ToLower(swag.StringValue(v.JARModel.Archive.Hash.Value)))
96+
hashKey := strings.ToLower(fmt.Sprintf("%s:%s", *v.JARModel.Archive.Hash.Algorithm, *v.JARModel.Archive.Hash.Value))
97+
result = append(result, hashKey)
9798
}
9899

99100
return result

pkg/types/rekord/v0.0.1/entry.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ func (v V001Entry) IndexKeys() []string {
8888
result = append(result, v.keyObj.EmailAddresses()...)
8989

9090
if v.RekordObj.Data.Hash != nil {
91-
result = append(result, strings.ToLower(swag.StringValue(v.RekordObj.Data.Hash.Value)))
91+
hashKey := strings.ToLower(fmt.Sprintf("%s:%s", *v.RekordObj.Data.Hash.Algorithm, *v.RekordObj.Data.Hash.Value))
92+
result = append(result, hashKey)
9293
}
9394

9495
return result

pkg/types/rpm/v0.0.1/entry.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ func (v V001Entry) IndexKeys() []string {
9292
result = append(result, v.keyObj.EmailAddresses()...)
9393

9494
if v.RPMModel.Package.Hash != nil {
95-
result = append(result, strings.ToLower(swag.StringValue(v.RPMModel.Package.Hash.Value)))
95+
hashKey := strings.ToLower(fmt.Sprintf("%s:%s", *v.RPMModel.Package.Hash.Algorithm, *v.RPMModel.Package.Hash.Value))
96+
result = append(result, hashKey)
9697
}
9798

9899
return result

tests/e2e-test.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,9 @@ TMPDIR="$(mktemp -d -t rekor_test.XXXXXX)"
4545
touch $TMPDIR.rekor.yaml
4646
trap "rm -rf $TMPDIR" EXIT
4747
TMPDIR=$TMPDIR go test -tags=e2e ./tests/
48+
if docker-compose logs --no-color | grep -q "panic: runtime error:" ; then
49+
# if we're here, we found a panic
50+
echo "Failing due to panics detected in logs"
51+
docker-compose logs --no-color
52+
exit 1
53+
fi

0 commit comments

Comments
 (0)