Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update find-images output to contain the images' digest #2327

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 8 additions & 9 deletions src/pkg/packager/prepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,14 @@ func (p *Packager) FindImages() (imgMap map[string][]string, err error) {
for _, image := range sortedImages {
// Use print because we want this dumped to stdout
imagesMap[component.Name] = append(imagesMap[component.Name], image)
componentDefinition += fmt.Sprintf(" - %s\n", image)
imageWithSha, err := utils.GetImageWithSha(image)
if err != nil {
message.WarnErrf(err, "Problem getting image with sha for %s: %s", image, err.Error())
erroredCosignLookups = append(erroredCosignLookups, image)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cosign signatures are different than sha signatures. I recommend creating a new array for erroredShaLookups. You can place a message near erroredCosginLookups in the below block further down in the file

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added a check with erroredShaLookups

continue
}
message.Debugf("Image with sha: %s", imageWithSha)
componentDefinition += fmt.Sprintf(" - %s\n", imageWithSha)
}
}

Expand All @@ -228,14 +235,6 @@ func (p *Packager) FindImages() (imgMap map[string][]string, err error) {
validImages = append(validImages, image)
}
}

if len(validImages) > 0 {
componentDefinition += fmt.Sprintf(" # Possible images - %s - %s\n", p.cfg.Pkg.Metadata.Name, component.Name)
for _, image := range validImages {
imagesMap[component.Name] = append(imagesMap[component.Name], image)
componentDefinition += fmt.Sprintf(" - %s\n", image)
}
}
waveywaves marked this conversation as resolved.
Show resolved Hide resolved
}

spinner.Success()
Expand Down
17 changes: 17 additions & 0 deletions src/pkg/utils/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,32 @@ package utils
import (
"encoding/json"
"fmt"
"github.com/google/go-containerregistry/pkg/crane"
"os"
"path/filepath"
"regexp"

"github.com/defenseunicorns/zarf/src/pkg/transform"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/layout"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
)

// GetImageWithSha returns the image reference and the sha256 digest of the image.
func GetImageWithSha(imgSrc string) (string, error) {
regex := regexp.MustCompile(`^.*@sha256:[a-f0-9]{64}$`)
if regex.MatchString(imgSrc) {
return imgSrc, nil
}

imgDescriptor, err := crane.Head(imgSrc)
if err != nil {
return "", fmt.Errorf("unable to pull image (%s) to get the sha256 digest: %w", imgSrc, err)
waveywaves marked this conversation as resolved.
Show resolved Hide resolved
}

return fmt.Sprintf("%s@%s", imgSrc, imgDescriptor.Digest), nil
}

// LoadOCIImage returns a v1.Image with the image ref specified from a location provided, or an error if the image cannot be found.
func LoadOCIImage(imgPath string, refInfo transform.Image) (v1.Image, error) {
// Use the manifest within the index.json to load the specific image we want
Expand Down