Skip to content

Commit

Permalink
feat: support passing optional scheme in --to-registry (#252)
Browse files Browse the repository at this point in the history
Co-authored-by: Jimmi Dyson <jimmidyson@gmail.com>
  • Loading branch information
dkoshkin and jimmidyson authored Nov 7, 2022
1 parent 35a0746 commit 6983970
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 220 deletions.
60 changes: 60 additions & 0 deletions cmd/mindthegap/flags/custom_flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright 2021 D2iQ, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package flags

import (
"fmt"
"net/url"
"path"
)

const (
httpScheme = "http"
)

type RegistryURI struct {
raw string
scheme string
address string
}

func (v RegistryURI) String() string {
return v.raw
}

func (v *RegistryURI) Set(value string) (err error) {
v.raw = value
v.scheme, v.address, err = parsePossibleURI(value)

return
}

func parsePossibleURI(raw string) (scheme, address string, err error) {
u, err := url.ParseRequestURI(raw)
if err != nil || u.Host == "" {
// parse again with a scheme to make it a valid URI
u, err = url.ParseRequestURI("unused://" + raw)
if err != nil {
return "", "", fmt.Errorf("could not parse raw url: %q, error: %w", raw, err)
}
} else {
// only set the scheme when set by the caller
scheme = u.Scheme
}

address = path.Join(u.Host, u.Path)
return
}

func (v RegistryURI) Scheme() string {
return v.scheme
}

func (v RegistryURI) Address() string {
return v.address
}

func (*RegistryURI) Type() string {
return "string"
}
65 changes: 65 additions & 0 deletions cmd/mindthegap/flags/custom_flags_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2021 D2iQ, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package flags

import (
"testing"

"github.com/stretchr/testify/require"
)

func Test_parsePossibleURI(t *testing.T) {
t.Parallel()

tests := []struct {
name string
in string
expectedScheme string
expectedAddress string
}{
{
name: "no scheme",
in: "0.0.0.0:5000",
expectedAddress: "0.0.0.0:5000",
},
{
name: "http scheme",
in: "http://0.0.0.0:5000",
expectedScheme: "http",
expectedAddress: "0.0.0.0:5000",
},
{
name: "https scheme",
in: "https://0.0.0.0:5000",
expectedScheme: "https",
expectedAddress: "0.0.0.0:5000",
},
{
name: "no scheme with path",
in: "0.0.0.0:5000/dkp",
expectedAddress: "0.0.0.0:5000/dkp",
},
{
name: "http scheme with path",
in: "https://0.0.0.0:5000/dkp",
expectedScheme: "https",
expectedAddress: "0.0.0.0:5000/dkp",
},
{
name: "https scheme with path",
in: "https://0.0.0.0:5000/dkp",
expectedScheme: "https",
expectedAddress: "0.0.0.0:5000/dkp",
},
}
for ti := range tests {
tt := tests[ti]
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
scheme, address, _ := parsePossibleURI(tt.in)
require.Equal(t, tt.expectedScheme, scheme)
require.Equal(t, tt.expectedAddress, address)
})
}
}
10 changes: 10 additions & 0 deletions cmd/mindthegap/flags/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright 2021 D2iQ, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package flags

// SkipTLSVerify returns true if registrySkipTLSVerify is true
// or registryURI URI is http.
func SkipTLSVerify(registrySkipTLSVerify bool, registryURI RegistryURI) bool {
return registrySkipTLSVerify || registryURI.Scheme() == httpScheme
}
11 changes: 8 additions & 3 deletions cmd/mindthegap/push/helmbundle/helm_bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/mesosphere/dkp-cli-runtime/core/output"

"github.com/mesosphere/mindthegap/cleanup"
"github.com/mesosphere/mindthegap/cmd/mindthegap/flags"
"github.com/mesosphere/mindthegap/cmd/mindthegap/utils"
"github.com/mesosphere/mindthegap/config"
"github.com/mesosphere/mindthegap/docker/ecr"
Expand All @@ -23,7 +24,7 @@ import (
func NewCommand(out output.Output) *cobra.Command {
var (
helmBundleFiles []string
destRegistry string
destRegistryURI flags.RegistryURI
destRegistrySkipTLSVerify bool
destRegistryUsername string
destRegistryPassword string
Expand Down Expand Up @@ -70,6 +71,9 @@ func NewCommand(out output.Output) *cobra.Command {
}()
out.EndOperation(true)

// do not include the scheme
destRegistry := destRegistryURI.Address()

skopeoRunner, skopeoCleanup := skopeo.NewRunner()
cleaner.AddCleanupFn(func() { _ = skopeoCleanup() })

Expand Down Expand Up @@ -112,7 +116,7 @@ func NewCommand(out output.Output) *cobra.Command {
fmt.Sprintf("%s/charts", reg.Address()),
destRegistry,
skopeoOpts,
destRegistrySkipTLSVerify,
flags.SkipTLSVerify(destRegistrySkipTLSVerify, destRegistryURI),
out,
skopeoRunner,
prePushFuncs...,
Expand All @@ -123,7 +127,8 @@ func NewCommand(out output.Output) *cobra.Command {
cmd.Flags().StringSliceVar(&helmBundleFiles, "helm-bundle", nil,
"Tarball containing list of Helm charts to push. Can also be a glob pattern.")
_ = cmd.MarkFlagRequired("helm-bundle")
cmd.Flags().StringVar(&destRegistry, "to-registry", "", "Registry to push images to")
cmd.Flags().Var(&destRegistryURI, "to-registry", "Registry to push images to. "+
"TLS verification will be skipped when using an http:// registry.")
_ = cmd.MarkFlagRequired("to-registry")
cmd.Flags().BoolVar(&destRegistrySkipTLSVerify, "to-registry-insecure-skip-tls-verify", false,
"Skip TLS verification of registry to push images to (also use for non-TLS http registries)")
Expand Down
13 changes: 10 additions & 3 deletions cmd/mindthegap/push/imagebundle/image_bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/mesosphere/dkp-cli-runtime/core/output"

"github.com/mesosphere/mindthegap/cleanup"
"github.com/mesosphere/mindthegap/cmd/mindthegap/flags"
"github.com/mesosphere/mindthegap/cmd/mindthegap/utils"
"github.com/mesosphere/mindthegap/config"
"github.com/mesosphere/mindthegap/docker/ecr"
Expand All @@ -24,7 +25,7 @@ import (
func NewCommand(out output.Output) *cobra.Command {
var (
imageBundleFiles []string
destRegistry string
destRegistryURI flags.RegistryURI
destRegistryCACertificateFile string
destRegistrySkipTLSVerify bool
destRegistryUsername string
Expand Down Expand Up @@ -73,6 +74,9 @@ func NewCommand(out output.Output) *cobra.Command {
}()
out.EndOperation(true)

// do not include the scheme
destRegistry := destRegistryURI.Address()

skopeoRunner, skopeoCleanup := skopeo.NewRunner()
cleaner.AddCleanupFn(func() { _ = skopeoCleanup() })

Expand Down Expand Up @@ -116,7 +120,9 @@ func NewCommand(out output.Output) *cobra.Command {
destRegistry,
skopeoOpts,
destRegistryCACertificateFile,
destRegistrySkipTLSVerify,
// use either user provided --to-registry-insecure-skip-tls-verify flag
// or scheme from --to-registry flag
flags.SkipTLSVerify(destRegistrySkipTLSVerify, destRegistryURI),
out,
skopeoRunner,
prePushFuncs...,
Expand All @@ -127,7 +133,8 @@ func NewCommand(out output.Output) *cobra.Command {
cmd.Flags().StringSliceVar(&imageBundleFiles, "image-bundle", nil,
"Tarball containing list of images to push. Can also be a glob pattern.")
_ = cmd.MarkFlagRequired("image-bundle")
cmd.Flags().StringVar(&destRegistry, "to-registry", "", "Registry to push images to")
cmd.Flags().Var(&destRegistryURI, "to-registry", "Registry to push images to. "+
"TLS verification will be skipped when using an http:// registry.")
_ = cmd.MarkFlagRequired("to-registry")
cmd.Flags().StringVar(&destRegistryCACertificateFile, "to-registry-ca-cert-file", "",
"CA certificate file used to verify TLS verification of registry to push images to")
Expand Down
Loading

0 comments on commit 6983970

Please sign in to comment.