-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create SBOMs for images while creating packages
Issue #22 Uses in-toto/witness#167
- Loading branch information
1 parent
bea1002
commit 014ff1c
Showing
7 changed files
with
1,455 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package sbom | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/defenseunicorns/zarf/src/internal/images" | ||
"github.com/defenseunicorns/zarf/src/internal/message" | ||
"github.com/google/go-containerregistry/pkg/name" | ||
v1 "github.com/google/go-containerregistry/pkg/v1" | ||
"github.com/testifysec/witness/pkg/attestation" | ||
"github.com/testifysec/witness/pkg/attestation/syft" | ||
) | ||
|
||
func CatalogImages(tagToImage map[name.Tag]v1.Image, sbomDir string) { | ||
imageCount := len(tagToImage) | ||
spinner := message.NewProgressSpinner("Creating SBOMs for %d images.", imageCount) | ||
actx, err := attestation.NewContext([]attestation.Attestor{}) | ||
if err != nil { | ||
spinner.Fatalf(err, "Unable to make attestation context") | ||
} | ||
|
||
cachePath := images.CachePath() | ||
currImage := 1 | ||
for tag, img := range tagToImage { | ||
spinner.Updatef("Creating image SBOMs (%d of %d): %s", currImage, imageCount, tag) | ||
sbomAttestor := syft.New(syft.WithImageSource(img, cachePath, tag.String())) | ||
if err := sbomAttestor.Attest(actx); err != nil { | ||
spinner.Fatalf(err, "Unable to build sbom for image %s", tag.String()) | ||
} | ||
|
||
sbomFile, err := os.Create(filepath.Join(sbomDir, fmt.Sprintf("%s.json", sbomAttestor.SBOM.Source.ImageMetadata.ID))) | ||
if err != nil { | ||
spinner.Fatalf(err, "Unable to create SBOM file for image %s", tag.String()) | ||
} | ||
|
||
defer sbomFile.Close() | ||
enc := json.NewEncoder(sbomFile) | ||
if err := enc.Encode(sbomAttestor); err != nil { | ||
spinner.Fatalf(err, "Unable to write SBOM file for image %s", tag.String()) | ||
} | ||
|
||
currImage++ | ||
} | ||
|
||
spinner.Success() | ||
} |