From f6b83e1c272a22ffd1815b7d38fb6c5f0f1003f9 Mon Sep 17 00:00:00 2001 From: Vibhav Bobade Date: Tue, 27 Feb 2024 02:01:01 +0530 Subject: [PATCH] fix: allow host+subpath as the source registry for registry-override (#2306) ## Description Instead of looking for refInfo.Host in the override map loop through the keys and values in i.RegistryOverrides, check if the refInfo.Reference begins with an override key and, if it does, replace that override text with the override value and set it back to actualSrc. Do not use ImageTransformHostWithoutChecksum since we already have the parsed ref and all the info we need to do the replacement. ## Related Issue Fixes #2135 ## Type of change - [x] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Other (security config, docs update, etc) ## Checklist before merging - [x] Test, docs, adr added or updated as needed - [x] [Contributor Guide Steps](https://github.com/defenseunicorns/zarf/blob/main/CONTRIBUTING.md#developer-workflow) followed --------- Signed-off-by: Vibhav Bobade Co-authored-by: Wayne Starr Co-authored-by: Austin Abro <37223396+AustinAbro321@users.noreply.github.com> --- src/internal/packager/images/pull.go | 9 +++------ src/test/e2e/25_helm_test.go | 18 +++++++++++++++++- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/internal/packager/images/pull.go b/src/internal/packager/images/pull.go index 92ed0d5775..a8bb98d327 100644 --- a/src/internal/packager/images/pull.go +++ b/src/internal/packager/images/pull.go @@ -84,12 +84,9 @@ func (i *ImageConfig) PullAll() ([]ImgInfo, error) { } actualSrc := refInfo.Reference - if overrideHost, present := i.RegistryOverrides[refInfo.Host]; present { - var err error - actualSrc, err = transform.ImageTransformHostWithoutChecksum(overrideHost, refInfo.Reference) - if err != nil { - metadataImageConcurrency.ErrorChan <- fmt.Errorf("failed to swap override host %s for %s: %w", overrideHost, refInfo.Reference, err) - return + for k, v := range i.RegistryOverrides { + if strings.HasPrefix(refInfo.Reference, k) { + actualSrc = strings.Replace(refInfo.Reference, k, v, 1) } } diff --git a/src/test/e2e/25_helm_test.go b/src/test/e2e/25_helm_test.go index 38478d710c..fef3433e76 100644 --- a/src/test/e2e/25_helm_test.go +++ b/src/test/e2e/25_helm_test.go @@ -55,7 +55,23 @@ func testHelmChartsExample(t *testing.T) { require.Contains(t, e2e.StripMessageFormatting(stdErr), "chart \"asdf\" version \"6.4.0\" not found") require.Contains(t, e2e.StripMessageFormatting(stdErr), "Available charts and versions from \"https://stefanprodan.github.io/podinfo\":") - // Create the package (with a registry override to test that as well) + // Create a test package (with a registry override (host+subpath to host+subpath) to test that as well) + stdOut, stdErr, err = e2e.Zarf("package", "create", "examples/helm-charts", "-o", "build", "--registry-override", "ghcr.io/stefanprodan=docker.io/stefanprodan", "--tmpdir", tmpdir, "--confirm") + require.NoError(t, err, stdOut, stdErr) + + // Create a test package (with a registry override (host to host+subpath) to test that as well) + // expect to fail as ghcr.io is overriden and the expected final image doesn't exist but the override works well based on the error message in the output + stdOut, stdErr, err = e2e.Zarf("package", "create", "examples/helm-charts", "-o", "build", "--registry-override", "ghcr.io=localhost:555/noway", "--tmpdir", tmpdir, "--confirm") + require.Error(t, err, stdOut, stdErr) + require.Contains(t, string(stdErr), "localhost:555/noway") + + // Create a test package (with a registry override (host+subpath to host) to test that as well) + // works same as the above failing test + stdOut, stdErr, err = e2e.Zarf("package", "create", "examples/helm-charts", "-o", "build", "--registry-override", "ghcr.io/stefanprodan=localhost:555", "--tmpdir", tmpdir, "--confirm") + require.Error(t, err, stdOut, stdErr) + require.Contains(t, string(stdErr), "localhost:555") + + // Create the package (with a registry override (host to host) to test that as well) stdOut, stdErr, err = e2e.Zarf("package", "create", "examples/helm-charts", "-o", "build", "--registry-override", "ghcr.io=docker.io", "--tmpdir", tmpdir, "--confirm") require.NoError(t, err, stdOut, stdErr)