From 6ef9359c5fca37a16c792c68e18382507a593eda Mon Sep 17 00:00:00 2001 From: Jimmi Dyson Date: Mon, 15 May 2023 18:03:32 +0100 Subject: [PATCH] fix: Support http registries for pushing bundles (#405) --- cmd/mindthegap/push/bundle/bundle.go | 14 ++++- .../e2e/imagebundle/push_image_bundle_test.go | 62 +++++++++++++++++++ 2 files changed, 73 insertions(+), 3 deletions(-) diff --git a/cmd/mindthegap/push/bundle/bundle.go b/cmd/mindthegap/push/bundle/bundle.go index b64a6493..5649540a 100644 --- a/cmd/mindthegap/push/bundle/bundle.go +++ b/cmd/mindthegap/push/bundle/bundle.go @@ -100,6 +100,11 @@ func NewCommand(out output.Output, bundleCmdName string) *cobra.Command { } destRemoteOpts := []remote.Option{remote.WithTransport(destTLSRoundTripper)} + var destNameOpts []name.Option + if flags.SkipTLSVerify(destRegistrySkipTLSVerify, destRegistryURI) { + destNameOpts = append(destNameOpts, name.Insecure) + } + keychain := authn.DefaultKeychain if destRegistryUsername != "" && destRegistryPassword != "" { keychain = authn.NewMultiKeychain( @@ -133,6 +138,7 @@ func NewCommand(out output.Output, bundleCmdName string) *cobra.Command { sourceRemoteOpts, destRegistryURI.Address(), destRemoteOpts, + destNameOpts, out, prePushFuncs..., ) @@ -190,7 +196,7 @@ type prePushFunc func(destRegistry, imageName string, imageTags ...string) error func pushImages( cfg config.ImagesConfig, sourceRegistry string, sourceRemoteOpts []remote.Option, - destRegistry string, destRemoteOpts []remote.Option, + destRegistry string, destRemoteOpts []remote.Option, destNameOpts []name.Option, out output.Output, prePushFuncs ...prePushFunc, ) error { @@ -221,14 +227,16 @@ func pushImages( ) srcImage := fmt.Sprintf("%s/%s:%s", sourceRegistry, imageName, imageTag) - srcRef, err := name.ParseReference(srcImage, name.StrictValidation) + srcRef, err := name.ParseReference(srcImage, name.Insecure, name.StrictValidation) if err != nil { out.EndOperation(false) return err } destImage := fmt.Sprintf("%s/%s:%s", destRegistry, imageName, imageTag) - dstRef, err := name.ParseReference(destImage, name.StrictValidation) + dstRef, err := name.ParseReference( + destImage, + append(destNameOpts, name.StrictValidation)...) if err != nil { out.EndOperation(false) return err diff --git a/test/e2e/imagebundle/push_image_bundle_test.go b/test/e2e/imagebundle/push_image_bundle_test.go index db4e22a3..43b036c1 100644 --- a/test/e2e/imagebundle/push_image_bundle_test.go +++ b/test/e2e/imagebundle/push_image_bundle_test.go @@ -244,6 +244,68 @@ var _ = Describe("Push Bundle", func() { Eventually(done).Should(BeClosed()) }) + It("With non-TLS", func() { + helpers.CreateBundle( + GinkgoT(), + bundleFile, + filepath.Join("testdata", "create-success.yaml"), + ) + + ipAddr := helpers.GetFirstNonLoopbackIP(GinkgoT()) + + port, err := freeport.GetFreePort() + Expect(err).NotTo(HaveOccurred()) + reg, err := registry.NewRegistry(registry.Config{ + StorageDirectory: filepath.Join(tmpDir, "registry"), + Host: ipAddr.String(), + Port: uint16(port), + }) + Expect(err).NotTo(HaveOccurred()) + + done := make(chan struct{}) + go func() { + defer GinkgoRecover() + + Expect(reg.ListenAndServe()).To(Succeed()) + + close(done) + }() + + helpers.WaitForTCPPort(GinkgoT(), ipAddr.String(), port) + + cmd.SetArgs([]string{ + "--image-bundle", bundleFile, + "--to-registry", fmt.Sprintf("http://%s:%d", ipAddr, port), + }) + + Expect(cmd.Execute()).To(Succeed()) + + testRoundTripper, err := httputils.TLSConfiguredRoundTripper( + remote.DefaultTransport, + net.JoinHostPort(ipAddr.String(), strconv.Itoa(port)), + true, + "", + ) + Expect(err).NotTo(HaveOccurred()) + + helpers.ValidateImageIsAvailable( + GinkgoT(), + ipAddr.String(), + port, + "stefanprodan/podinfo", + "6.2.0", + []*v1.Platform{{ + OS: "linux", + Architecture: runtime.GOARCH, + }}, + remote.WithTransport(testRoundTripper), + ) + + Expect(reg.Shutdown(context.Background())).To((Succeed())) + + Eventually(done).Should(BeClosed()) + }) + It("Bundle does not exist", func() { cmd.SetArgs([]string{ "--image-bundle", bundleFile,