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

Fix potential for double transform of images (i.e with operators that self-reference) #1989

Merged
merged 10 commits into from
Aug 28, 2023
12 changes: 12 additions & 0 deletions src/pkg/transform/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package transform

import (
"fmt"
"strings"

"github.com/defenseunicorns/zarf/src/pkg/utils/helpers"
"github.com/distribution/distribution/reference"
Expand All @@ -29,6 +30,11 @@ func ImageTransformHost(targetHost, srcReference string) (string, error) {
return "", err
}

// check if image has already been transformed
if strings.HasPrefix(targetHost, image.Host) {
return srcReference, nil
}

// Generate a crc32 hash of the image host + name
checksum := helpers.GetCRCHash(image.Name)

Expand All @@ -46,6 +52,12 @@ func ImageTransformHostWithoutChecksum(targetHost, srcReference string) (string,
if err != nil {
return "", err
}

// check if image has already been transformed
if strings.HasPrefix(targetHost, image.Host) {
return srcReference, nil
}

return fmt.Sprintf("%s/%s%s", targetHost, image.Path, image.TagOrDigest), nil
}

Expand Down
4 changes: 4 additions & 0 deletions src/pkg/transform/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var imageRefs = []string{
"defenseunicorns/zarf-agent@sha256:84605f731c6a18194794c51e70021c671ab064654b751aa57e905bce55be13de",
"ghcr.io/stefanprodan/podinfo:6.3.3",
"registry1.dso.mil/ironbank/opensource/defenseunicorns/zarf/zarf-agent:v0.25.0",
"gitlab.com/project/gitea/gitea:1.19.3-rootless-zarf-3431384023",
}

var badImageRefs = []string{
Expand All @@ -34,6 +35,7 @@ func TestImageTransformHost(t *testing.T) {
"gitlab.com/project/defenseunicorns/zarf-agent@sha256:84605f731c6a18194794c51e70021c671ab064654b751aa57e905bce55be13de",
"gitlab.com/project/stefanprodan/podinfo:6.3.3-zarf-2985051089",
"gitlab.com/project/ironbank/opensource/defenseunicorns/zarf/zarf-agent:v0.25.0-zarf-2003217571",
"gitlab.com/project/gitea/gitea:1.19.3-rootless-zarf-3431384023",
}

for idx, ref := range imageRefs {
Expand All @@ -56,6 +58,7 @@ func TestImageTransformHostWithoutChecksum(t *testing.T) {
"gitlab.com/project/defenseunicorns/zarf-agent@sha256:84605f731c6a18194794c51e70021c671ab064654b751aa57e905bce55be13de",
"gitlab.com/project/stefanprodan/podinfo:6.3.3",
"gitlab.com/project/ironbank/opensource/defenseunicorns/zarf/zarf-agent:v0.25.0",
"gitlab.com/project/gitea/gitea:1.19.3-rootless-zarf-3431384023",
}

for idx, ref := range imageRefs {
Expand All @@ -78,6 +81,7 @@ func TestParseImageRef(t *testing.T) {
{"docker.io/", "defenseunicorns/zarf-agent", "", "sha256:84605f731c6a18194794c51e70021c671ab064654b751aa57e905bce55be13de"},
{"ghcr.io/", "stefanprodan/podinfo", "6.3.3", ""},
{"registry1.dso.mil/", "ironbank/opensource/defenseunicorns/zarf/zarf-agent", "v0.25.0", ""},
{"gitlab.com/", "project/gitea/gitea", "1.19.3-rootless-zarf-3431384023", ""},
}

for idx, ref := range imageRefs {
Expand Down