Skip to content

Commit e375eb4

Browse files
authored
name stored attestations by digest instead of UUID (sigstore#769)
* store attestations by digest instead of by UUID Signed-off-by: Bob Callaway <bcallaway@google.com> * fix typo Signed-off-by: Bob Callaway <bcallaway@google.com> * remove github.com/pkg/errors Signed-off-by: Bob Callaway <bcallaway@google.com>
1 parent f61a71e commit e375eb4

File tree

13 files changed

+114
-44
lines changed

13 files changed

+114
-44
lines changed

pkg/api/entries.go

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"strconv"
2727

2828
"github.com/cyberphone/json-canonicalization/go/src/webpki.org/jsoncanonicalizer"
29+
"github.com/go-openapi/runtime"
2930
"github.com/go-openapi/runtime/middleware"
3031
"github.com/go-openapi/strfmt"
3132
"github.com/go-openapi/swag"
@@ -97,12 +98,35 @@ func logEntryFromLeaf(ctx context.Context, signer signature.Signer, tc TrillianC
9798

9899
uuid := hex.EncodeToString(leaf.MerkleLeafHash)
99100
if viper.GetBool("enable_attestation_storage") {
100-
att, err := storageClient.FetchAttestation(ctx, uuid)
101+
pe, err := models.UnmarshalProposedEntry(bytes.NewReader(leaf.LeafValue), runtime.JSONConsumer())
101102
if err != nil {
102-
log.Logger.Errorf("error fetching attestation: %s %s", uuid, err)
103-
} else {
104-
logEntryAnon.Attestation = &models.LogEntryAnonAttestation{
105-
Data: att,
103+
return nil, err
104+
}
105+
eimpl, err := types.NewEntry(pe)
106+
if err != nil {
107+
return nil, err
108+
}
109+
attKey := eimpl.AttestationKey()
110+
if attKey != "" {
111+
att, err := storageClient.FetchAttestation(ctx, attKey)
112+
if err != nil {
113+
log.Logger.Errorf("error fetching attestation by key, trying by UUID: %s %s", attKey, err)
114+
// the original attestation implementation stored this by uuid instead of by digest
115+
activeTree := fmt.Sprintf("%x", tc.logID)
116+
entryIDstruct, err := sharding.CreateEntryIDFromParts(activeTree, uuid)
117+
if err != nil {
118+
err := fmt.Errorf("error creating EntryID from active treeID %v and uuid %v: %w", activeTree, uuid, err)
119+
return nil, err
120+
}
121+
att, err = storageClient.FetchAttestation(ctx, entryIDstruct.UUID)
122+
if err != nil {
123+
log.Logger.Errorf("error fetching attestation by uuid: %s %s", entryIDstruct.UUID, err)
124+
}
125+
}
126+
if err == nil {
127+
logEntryAnon.Attestation = &models.LogEntryAnonAttestation{
128+
Data: att,
129+
}
106130
}
107131
}
108132
}
@@ -224,14 +248,16 @@ func createLogEntry(params entries.CreateLogEntryParams) (models.LogEntry, middl
224248
if viper.GetBool("enable_attestation_storage") {
225249

226250
go func() {
227-
attestation := entry.Attestation()
228-
if attestation == nil {
229-
log.RequestIDLogger(params.HTTPRequest).Infof("no attestation for %s", entryID)
251+
attKey, attVal := entry.AttestationKeyValue()
252+
if attVal == nil {
253+
log.RequestIDLogger(params.HTTPRequest).Infof("no attestation for %s", uuid)
230254
return
231255
}
232-
// TODO stop using uuid and use attestation hash
233-
if err := storeAttestation(context.Background(), entryIDstruct.UUID, attestation); err != nil {
256+
if err := storeAttestation(context.Background(), attKey, attVal); err != nil {
257+
// entryIDstruct.UUID
234258
log.RequestIDLogger(params.HTTPRequest).Errorf("error storing attestation: %s", err)
259+
} else {
260+
log.RequestIDLogger(params.HTTPRequest).Infof("stored attestation for uuid %s with filename %s", entryIDstruct.UUID, attKey)
235261
}
236262
}()
237263
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,12 @@ func (v V001Entry) validate() error {
287287
return nil
288288
}
289289

290-
func (v V001Entry) Attestation() []byte {
291-
return nil
290+
func (v V001Entry) AttestationKey() string {
291+
return ""
292+
}
293+
294+
func (v V001Entry) AttestationKeyValue() (string, []byte) {
295+
return "", nil
292296
}
293297

294298
func (v V001Entry) CreateFromArtifactProperties(ctx context.Context, props types.ArtifactProperties) (models.ProposedEntry, error) {

pkg/types/entries.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ type EntryImpl interface {
3535
IndexKeys() ([]string, error) // the keys that should be added to the external index for this entry
3636
Canonicalize(ctx context.Context) ([]byte, error) // marshal the canonical entry to be put into the tlog
3737
Unmarshal(e models.ProposedEntry) error // unmarshal the abstract entry into the specific struct for this versioned type
38-
Attestation() []byte
38+
AttestationKey() string // returns the key used to look up the attestation from storage (should be sha256:digest)
39+
AttestationKeyValue() (string, []byte) // returns the key to be used when storing the attestation as well as the attestation itself
3940
CreateFromArtifactProperties(context.Context, ArtifactProperties) (models.ProposedEntry, error)
4041
}
4142

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,12 @@ func (v *V001Entry) validate() (pki.Signature, pki.PublicKey, error) {
183183
return sigObj, keyObj, nil
184184
}
185185

186-
func (v V001Entry) Attestation() []byte {
187-
return nil
186+
func (v V001Entry) AttestationKey() string {
187+
return ""
188+
}
189+
190+
func (v V001Entry) AttestationKeyValue() (string, []byte) {
191+
return "", nil
188192
}
189193

190194
func (v V001Entry) CreateFromArtifactProperties(ctx context.Context, props types.ArtifactProperties) (models.ProposedEntry, error) {

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,12 @@ func (v V001Entry) validate() error {
280280
return nil
281281
}
282282

283-
func (v V001Entry) Attestation() []byte {
284-
return nil
283+
func (v V001Entry) AttestationKey() string {
284+
return ""
285+
}
286+
287+
func (v V001Entry) AttestationKeyValue() (string, []byte) {
288+
return "", nil
285289
}
286290

287291
func (v V001Entry) CreateFromArtifactProperties(ctx context.Context, props types.ArtifactProperties) (models.ProposedEntry, error) {

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

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -182,16 +182,11 @@ func (v *V001Entry) Canonicalize(ctx context.Context) ([]byte, error) {
182182
},
183183
},
184184
}
185-
attestation := v.Attestation()
186-
if attestation != nil {
187-
decodedAttestation, err := base64.StdEncoding.DecodeString(string(attestation))
188-
if err != nil {
189-
return nil, fmt.Errorf("decoding attestation: %w", err)
190-
}
191-
attH := sha256.Sum256(decodedAttestation)
185+
attKey, attValue := v.AttestationKeyValue()
186+
if attValue != nil {
192187
canonicalEntry.Content.PayloadHash = &models.IntotoV001SchemaContentPayloadHash{
193188
Algorithm: swag.String(models.IntotoV001SchemaContentHashAlgorithmSha256),
194-
Value: swag.String(hex.EncodeToString(attH[:])),
189+
Value: swag.String(strings.Replace(attKey, fmt.Sprintf("%s:", models.IntotoV001SchemaContentHashAlgorithmSha256), "", 1)),
195190
}
196191
}
197192

@@ -237,13 +232,25 @@ func (v *V001Entry) validate() error {
237232
return nil
238233
}
239234

240-
func (v *V001Entry) Attestation() []byte {
235+
// AttestationKey returns the digest of the attestation that was uploaded, to be used to lookup the attestation from storage
236+
func (v *V001Entry) AttestationKey() string {
237+
if v.IntotoObj.Content != nil && v.IntotoObj.Content.PayloadHash != nil {
238+
return fmt.Sprintf("%s:%s", *v.IntotoObj.Content.PayloadHash.Algorithm, *v.IntotoObj.Content.PayloadHash.Value)
239+
}
240+
return ""
241+
}
242+
243+
// AttestationKeyValue returns both the key and value to be persisted into attestation storage
244+
func (v *V001Entry) AttestationKeyValue() (string, []byte) {
241245
storageSize := base64.StdEncoding.DecodedLen(len(v.env.Payload))
242246
if storageSize > viper.GetInt("max_attestation_size") {
243247
log.Logger.Infof("Skipping attestation storage, size %d is greater than max %d", storageSize, viper.GetInt("max_attestation_size"))
244-
return nil
248+
return "", nil
245249
}
246-
return []byte(v.env.Payload)
250+
attBytes, _ := base64.StdEncoding.DecodeString(v.env.Payload)
251+
attHash := sha256.Sum256(attBytes)
252+
attKey := fmt.Sprintf("%s:%s", models.IntotoV001SchemaContentHashAlgorithmSha256, hex.EncodeToString(attHash[:]))
253+
return attKey, attBytes
247254
}
248255

249256
type verifier struct {

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,12 @@ func extractPKCS7SignatureFromJAR(inz *zip.Reader) ([]byte, error) {
271271
return nil, errors.New("unable to locate signature in JAR file")
272272
}
273273

274-
func (v *V001Entry) Attestation() []byte {
275-
return nil
274+
func (v V001Entry) AttestationKey() string {
275+
return ""
276+
}
277+
278+
func (v V001Entry) AttestationKeyValue() (string, []byte) {
279+
return "", nil
276280
}
277281

278282
func (v *V001Entry) CreateFromArtifactProperties(ctx context.Context, props types.ArtifactProperties) (models.ProposedEntry, error) {

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,8 +332,12 @@ func (v V001Entry) validate() error {
332332
return nil
333333
}
334334

335-
func (v V001Entry) Attestation() []byte {
336-
return nil
335+
func (v V001Entry) AttestationKey() string {
336+
return ""
337+
}
338+
339+
func (v V001Entry) AttestationKeyValue() (string, []byte) {
340+
return "", nil
337341
}
338342

339343
func (v V001Entry) CreateFromArtifactProperties(ctx context.Context, props types.ArtifactProperties) (models.ProposedEntry, error) {

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,12 @@ func (v V001Entry) validate() error {
173173
return nil
174174
}
175175

176-
func (v V001Entry) Attestation() []byte {
177-
return nil
176+
func (v V001Entry) AttestationKey() string {
177+
return ""
178+
}
179+
180+
func (v V001Entry) AttestationKeyValue() (string, []byte) {
181+
return "", nil
178182
}
179183

180184
func (v V001Entry) CreateFromArtifactProperties(_ context.Context, props types.ArtifactProperties) (models.ProposedEntry, error) {

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,12 @@ func (v V001Entry) validate() error {
307307
return nil
308308
}
309309

310-
func (v V001Entry) Attestation() []byte {
311-
return nil
310+
func (v V001Entry) AttestationKey() string {
311+
return ""
312+
}
313+
314+
func (v V001Entry) AttestationKeyValue() (string, []byte) {
315+
return "", nil
312316
}
313317

314318
func (v V001Entry) CreateFromArtifactProperties(ctx context.Context, props types.ArtifactProperties) (models.ProposedEntry, error) {

0 commit comments

Comments
 (0)