Skip to content

Commit 1ae39db

Browse files
authored
Push attestation from memory (#407)
This commit modifies the attestation push logic to ship the attestations from memory instead of the bolted-on mechanism we had earlier where we would write to disk, reparse and push. Signed-off-by: Adolfo García Veytia (Puerco) <puerco@carabiner.dev>
1 parent 427185e commit 1ae39db

1 file changed

Lines changed: 23 additions & 19 deletions

File tree

pkg/sourcetool/tool.go

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package sourcetool
77

88
import (
9+
"bytes"
910
"context"
1011
"errors"
1112
"fmt"
@@ -14,7 +15,9 @@ import (
1415
"strings"
1516
"time"
1617

18+
"github.com/carabiner-dev/attestation"
1719
"github.com/carabiner-dev/collector"
20+
"github.com/carabiner-dev/collector/envelope"
1821
cgithub "github.com/carabiner-dev/collector/repository/github"
1922
"github.com/carabiner-dev/collector/repository/note"
2023
"google.golang.org/protobuf/encoding/protojson"
@@ -501,30 +504,31 @@ func (t *Tool) AttestRevision(
501504
fmt.Printf("%s\n%s\n", string(provenanceData), vsaData)
502505
}
503506

504-
fpath := opts.OutputPath
505-
if fpath == "" {
506-
f, err := os.CreateTemp("", "attestations-")
507-
if err != nil {
508-
return nil, fmt.Errorf("opening tmp file: %w", err)
507+
// Write the bundle to disk only when an output path was requested. The push
508+
// below works from memory, so there is no longer a need for a temp file.
509+
if opts.OutputPath != "" {
510+
if err := os.WriteFile(
511+
opts.OutputPath, fmt.Appendf(nil, "%s\n%s\n", string(provenanceData), vsaData), os.FileMode(0o600),
512+
); err != nil {
513+
return nil, fmt.Errorf("writing attestations: %w", err)
509514
}
510-
f.Close() //nolint:errcheck,gosec
511-
fpath = f.Name()
512515
}
513516

514-
defer func() {
515-
if opts.OutputPath == "" {
516-
os.Remove(fpath) //nolint:errcheck,gosec
517+
if opts.Push {
518+
// Push the attestations from memory rather than re-reading the bundle
519+
// file: the file holds two records (provenance + VSA) as JSONL, which the
520+
// storer's single-document parser can't ingest. Parse each signed bundle
521+
// individually and store the envelopes directly.
522+
var envelopes []attestation.Envelope
523+
for _, data := range [][]byte{provenanceData, []byte(vsaData)} {
524+
parsed, err := envelope.Parsers.Parse(bytes.NewReader(data))
525+
if err != nil {
526+
return nil, fmt.Errorf("parsing attestation to push: %w", err)
527+
}
528+
envelopes = append(envelopes, parsed...)
517529
}
518-
}()
519-
520-
if err := os.WriteFile(
521-
fpath, fmt.Appendf(nil, "%s\n%s\n", string(provenanceData), vsaData), os.FileMode(0o600),
522-
); err != nil {
523-
return nil, fmt.Errorf("writing attestations: %w", err)
524-
}
525530

526-
if opts.Push {
527-
if err := agent.StoreFromFiles(ctx, []string{fpath}); err != nil {
531+
if err := agent.Store(ctx, envelopes); err != nil {
528532
return nil, fmt.Errorf("pushing attestations: %w", err)
529533
}
530534
}

0 commit comments

Comments
 (0)