feat: output attestation to file (#1087)

This commit is contained in:
Batuhan Apaydın 2022-07-08 20:05:20 +03:00 committed by GitHub
parent c7fa498a1b
commit 5206193b23
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -102,7 +102,7 @@ func Run(ctx context.Context, app *config.Application, ko sigopts.KeyOpts, args
subscription := eventBus.Subscribe() subscription := eventBus.Subscribe()
return eventloop.EventLoop( return eventloop.EventLoop(
execWorker(app, *si, format, predicateType, sv), execWorker(app, *si, format, predicateType, sv, app.File),
eventloop.SetupSignals(), eventloop.SetupSignals(),
subscription, subscription,
stereoscope.Cleanup, stereoscope.Cleanup,
@ -137,7 +137,7 @@ func parseImageSource(userInput string, app *config.Application) (s *source.Inpu
return si, nil return si, nil
} }
func execWorker(app *config.Application, sourceInput source.Input, format sbom.Format, predicateType string, sv *sign.SignerVerifier) <-chan error { func execWorker(app *config.Application, sourceInput source.Input, format sbom.Format, predicateType string, sv *sign.SignerVerifier, file string) <-chan error {
errs := make(chan error) errs := make(chan error)
go func() { go func() {
defer close(errs) defer close(errs)
@ -163,7 +163,7 @@ func execWorker(app *config.Application, sourceInput source.Input, format sbom.F
return return
} }
err = generateAttestation(app, sbomBytes, src, sv, predicateType) err = generateAttestation(app, sbomBytes, src, sv, predicateType, file)
if err != nil { if err != nil {
errs <- err errs <- err
return return
@ -172,7 +172,7 @@ func execWorker(app *config.Application, sourceInput source.Input, format sbom.F
return errs return errs
} }
func generateAttestation(app *config.Application, predicate []byte, src *source.Source, sv *sign.SignerVerifier, predicateType string) error { func generateAttestation(app *config.Application, predicate []byte, src *source.Source, sv *sign.SignerVerifier, predicateType string, file string) error {
switch len(src.Image.Metadata.RepoDigests) { switch len(src.Image.Metadata.RepoDigests) {
case 0: case 0:
return fmt.Errorf("cannot generate attestation since no repo digests were found; make sure you're passing an OCI registry source for the attest command") return fmt.Errorf("cannot generate attestation since no repo digests were found; make sure you're passing an OCI registry source for the attest command")
@ -219,7 +219,12 @@ func generateAttestation(app *config.Application, predicate []byte, src *source.
bus.Publish(partybus.Event{ bus.Publish(partybus.Event{
Type: event.Exit, Type: event.Exit,
Value: func() error { Value: func() error {
_, err := os.Stdout.Write(signedPayload) var err error
if file != "" {
err = os.WriteFile(file, signedPayload, 0600)
} else {
_, err = os.Stdout.Write(signedPayload)
}
return err return err
}, },
}) })