From 11738dde51447c7bfd1ef0c97cd2bd8fb5e3bfa1 Mon Sep 17 00:00:00 2001 From: Soule BA Date: Mon, 19 Dec 2022 22:52:20 +0100 Subject: [PATCH] Provide a helper to set the registryClient in cmd If enabled the registryClient is set using a helper that accepts the TLS flags. This keeps the client creation consistent accross the different commands. Signed-off-by: Soule BA --- cmd/helm/install.go | 8 +++++++- cmd/helm/pull.go | 8 +++++++- cmd/helm/push.go | 5 +++++ cmd/helm/root.go | 30 ++++++++++++++++++++++++++++-- cmd/helm/show.go | 43 ++++++++++++++++++++++++++++++++++++------- cmd/helm/template.go | 6 ++++++ cmd/helm/upgrade.go | 8 +++++++- pkg/action/install.go | 25 +++++++++---------------- pkg/action/pull.go | 24 +++++------------------- pkg/action/push.go | 12 +----------- pkg/action/show.go | 6 ++++++ pkg/action/upgrade.go | 6 ++++++ 12 files changed, 123 insertions(+), 58 deletions(-) diff --git a/cmd/helm/install.go b/cmd/helm/install.go index dc7018089..13c674066 100644 --- a/cmd/helm/install.go +++ b/cmd/helm/install.go @@ -136,6 +136,12 @@ func newInstallCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { return compInstall(args, toComplete, client) }, RunE: func(_ *cobra.Command, args []string) error { + registryClient, err := newRegistryClient(client.CertFile, client.KeyFile, client.CaFile, client.InsecureSkipTLSverify) + if err != nil { + return fmt.Errorf("missing registry client: %w", err) + } + client.SetRegistryClient(registryClient) + rel, err := runInstall(args, client, valueOpts, out) if err != nil { return errors.Wrap(err, "INSTALLATION FAILED") @@ -203,7 +209,7 @@ func runInstall(args []string, client *action.Install, valueOpts *values.Options } client.ReleaseName = name - cp, err := client.ChartPathOptions.LocateChart(chart, out, settings) + cp, err := client.ChartPathOptions.LocateChart(chart, settings) if err != nil { return nil, err } diff --git a/cmd/helm/pull.go b/cmd/helm/pull.go index 238550250..2d3747f28 100644 --- a/cmd/helm/pull.go +++ b/cmd/helm/pull.go @@ -43,7 +43,7 @@ result in an error, and the chart will not be saved locally. ` func newPullCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { - client := action.NewPullWithOpts(action.WithConfig(cfg), action.WithPullOptWriter(out)) + client := action.NewPullWithOpts(action.WithConfig(cfg)) cmd := &cobra.Command{ Use: "pull [chart URL | repo/chartname] [...]", @@ -64,6 +64,12 @@ func newPullCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { client.Version = ">0.0.0-0" } + registryClient, err := newRegistryClient(client.CertFile, client.KeyFile, client.CaFile, client.InsecureSkipTLSverify) + if err != nil { + return fmt.Errorf("missing registry client: %w", err) + } + client.SetRegistryClient(registryClient) + for i := 0; i < len(args); i++ { output, err := client.Run(args[i]) if err != nil { diff --git a/cmd/helm/push.go b/cmd/helm/push.go index dffe8f4ea..b1e3e60af 100644 --- a/cmd/helm/push.go +++ b/cmd/helm/push.go @@ -67,6 +67,11 @@ func newPushCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { return nil, cobra.ShellCompDirectiveNoFileComp }, RunE: func(cmd *cobra.Command, args []string) error { + registryClient, err := newRegistryClient(o.certFile, o.keyFile, o.caFile, o.insecureSkipTLSverify) + if err != nil { + return fmt.Errorf("missing registry client: %w", err) + } + cfg.RegistryClient = registryClient chartRef := args[0] remote := args[1] client := action.NewPushWithOpts(action.WithPushConfig(cfg), diff --git a/cmd/helm/root.go b/cmd/helm/root.go index c4f439511..5bccdf5bf 100644 --- a/cmd/helm/root.go +++ b/cmd/helm/root.go @@ -152,7 +152,7 @@ func newRootCmd(actionConfig *action.Configuration, out io.Writer, args []string flags.ParseErrorsWhitelist.UnknownFlags = true flags.Parse(args) - registryClient, err := newRegistryClient(out) + registryClient, err := newDefaultRegistryClient() if err != nil { return nil, err } @@ -257,7 +257,22 @@ func checkForExpiredRepos(repofile string) { } -func newRegistryClient(out io.Writer) (*registry.Client, error) { +func newRegistryClient(certFile, keyFile, caFile string, insecureSkipTLSverify bool) (*registry.Client, error) { + if certFile != "" && keyFile != "" || caFile != "" || insecureSkipTLSverify { + registryClient, err := newRegistryClientWithTLS(certFile, keyFile, caFile, insecureSkipTLSverify) + if err != nil { + return nil, err + } + return registryClient, nil + } + registryClient, err := newDefaultRegistryClient() + if err != nil { + return nil, err + } + return registryClient, nil +} + +func newDefaultRegistryClient() (*registry.Client, error) { // Create a new registry client registryClient, err := registry.NewClient( registry.ClientOptDebug(settings.Debug), @@ -270,3 +285,14 @@ func newRegistryClient(out io.Writer) (*registry.Client, error) { } return registryClient, nil } + +func newRegistryClientWithTLS(certFile, keyFile, caFile string, insecureSkipTLSverify bool) (*registry.Client, error) { + // Create a new registry client + registryClient, err := registry.NewRegistryClientWithTLS(os.Stderr, certFile, keyFile, caFile, insecureSkipTLSverify, + settings.RegistryConfig, settings.Debug, + ) + if err != nil { + return nil, err + } + return registryClient, nil +} diff --git a/cmd/helm/show.go b/cmd/helm/show.go index e9b17a8cb..a2edd1931 100644 --- a/cmd/helm/show.go +++ b/cmd/helm/show.go @@ -84,7 +84,11 @@ func newShowCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { ValidArgsFunction: validArgsFunc, RunE: func(cmd *cobra.Command, args []string) error { client.OutputFormat = action.ShowAll - output, err := runShow(args, client, out) + err := addRegistryClient(client) + if err != nil { + return err + } + output, err := runShow(args, client) if err != nil { return err } @@ -101,7 +105,11 @@ func newShowCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { ValidArgsFunction: validArgsFunc, RunE: func(cmd *cobra.Command, args []string) error { client.OutputFormat = action.ShowValues - output, err := runShow(args, client, out) + err := addRegistryClient(client) + if err != nil { + return err + } + output, err := runShow(args, client) if err != nil { return err } @@ -118,7 +126,11 @@ func newShowCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { ValidArgsFunction: validArgsFunc, RunE: func(cmd *cobra.Command, args []string) error { client.OutputFormat = action.ShowChart - output, err := runShow(args, client, out) + err := addRegistryClient(client) + if err != nil { + return err + } + output, err := runShow(args, client) if err != nil { return err } @@ -135,7 +147,11 @@ func newShowCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { ValidArgsFunction: validArgsFunc, RunE: func(cmd *cobra.Command, args []string) error { client.OutputFormat = action.ShowReadme - output, err := runShow(args, client, out) + err := addRegistryClient(client) + if err != nil { + return err + } + output, err := runShow(args, client) if err != nil { return err } @@ -152,7 +168,11 @@ func newShowCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { ValidArgsFunction: validArgsFunc, RunE: func(cmd *cobra.Command, args []string) error { client.OutputFormat = action.ShowCRDs - output, err := runShow(args, client, out) + err := addRegistryClient(client) + if err != nil { + return err + } + output, err := runShow(args, client) if err != nil { return err } @@ -191,16 +211,25 @@ func addShowFlags(subCmd *cobra.Command, client *action.Show) { } } -func runShow(args []string, client *action.Show, out io.Writer) (string, error) { +func runShow(args []string, client *action.Show) (string, error) { debug("Original chart version: %q", client.Version) if client.Version == "" && client.Devel { debug("setting version to >0.0.0-0") client.Version = ">0.0.0-0" } - cp, err := client.ChartPathOptions.LocateChart(args[0], out, settings) + cp, err := client.ChartPathOptions.LocateChart(args[0], settings) if err != nil { return "", err } return client.Run(cp) } + +func addRegistryClient(client *action.Show) error { + registryClient, err := newRegistryClient(client.CertFile, client.KeyFile, client.CaFile, client.InsecureSkipTLSverify) + if err != nil { + return fmt.Errorf("missing registry client: %w", err) + } + client.SetRegistryClient(registryClient) + return nil +} diff --git a/cmd/helm/template.go b/cmd/helm/template.go index 0ddd0c551..3bc70f995 100644 --- a/cmd/helm/template.go +++ b/cmd/helm/template.go @@ -73,6 +73,12 @@ func newTemplateCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { client.KubeVersion = parsedKubeVersion } + registryClient, err := newRegistryClient(client.CertFile, client.KeyFile, client.CaFile, client.InsecureSkipTLSverify) + if err != nil { + return fmt.Errorf("missing registry client: %w", err) + } + client.SetRegistryClient(registryClient) + client.DryRun = true client.ReleaseName = "release-name" client.Replace = true // Skip the name check diff --git a/cmd/helm/upgrade.go b/cmd/helm/upgrade.go index bf9b30ffb..145d342b7 100644 --- a/cmd/helm/upgrade.go +++ b/cmd/helm/upgrade.go @@ -90,6 +90,12 @@ func newUpgradeCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { RunE: func(cmd *cobra.Command, args []string) error { client.Namespace = settings.Namespace() + registryClient, err := newRegistryClient(client.CertFile, client.KeyFile, client.CaFile, client.InsecureSkipTLSverify) + if err != nil { + return fmt.Errorf("missing registry client: %w", err) + } + client.SetRegistryClient(registryClient) + // Fixes #7002 - Support reading values from STDIN for `upgrade` command // Must load values AFTER determining if we have to call install so that values loaded from stdin are are not read twice if client.Install { @@ -136,7 +142,7 @@ func newUpgradeCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { client.Version = ">0.0.0-0" } - chartPath, err := client.ChartPathOptions.LocateChart(args[1], out, settings) + chartPath, err := client.ChartPathOptions.LocateChart(args[1], settings) if err != nil { return err } diff --git a/pkg/action/install.go b/pkg/action/install.go index bc228e0c0..5af61b932 100644 --- a/pkg/action/install.go +++ b/pkg/action/install.go @@ -20,7 +20,6 @@ import ( "bytes" "context" "fmt" - "io" "io/ioutil" "net/url" "os" @@ -137,6 +136,11 @@ func NewInstall(cfg *Configuration) *Install { return in } +// SetRegistryClient sets the registry client for the install action +func (i *Install) SetRegistryClient(registryClient *registry.Client) { + i.ChartPathOptions.registryClient = registryClient +} + func (i *Install) installCRDs(crds []chart.CRD) error { // We do these one file at a time in the order they were read. totalItems := []*resource.Info{} @@ -676,22 +680,11 @@ OUTER: // - URL // // If 'verify' was set on ChartPathOptions, this will attempt to also verify the chart. -func (c *ChartPathOptions) LocateChart(name string, out io.Writer, settings *cli.EnvSettings) (string, error) { - // If there is no registry client and the name is in an OCI registry return - // an error and a lookup will not occur. - if registry.IsOCI(name) { - if (c.CertFile != "" && c.KeyFile != "") || c.CaFile != "" || c.InsecureSkipTLSverify { - registryClient, err := registry.NewRegistryClientWithTLS(out, c.CertFile, c.KeyFile, c.CaFile, - c.InsecureSkipTLSverify, settings.RegistryConfig, settings.Debug) - if err != nil { - return "", err - } - c.registryClient = registryClient - } - if c.registryClient == nil { - return "", fmt.Errorf("unable to lookup chart %q, missing registry client", name) - } +func (c *ChartPathOptions) LocateChart(name string, settings *cli.EnvSettings) (string, error) { + if registry.IsOCI(name) && c.registryClient == nil { + return "", fmt.Errorf("unable to lookup chart %q, missing registry client", name) } + name = strings.TrimSpace(name) version := strings.TrimSpace(c.Version) diff --git a/pkg/action/pull.go b/pkg/action/pull.go index 102e4a588..9952fb2ed 100644 --- a/pkg/action/pull.go +++ b/pkg/action/pull.go @@ -18,7 +18,6 @@ package action import ( "fmt" - "io" "io/ioutil" "os" "path/filepath" @@ -48,7 +47,6 @@ type Pull struct { UntarDir string DestDir string cfg *Configuration - out io.Writer } type PullOpt func(*Pull) @@ -59,13 +57,6 @@ func WithConfig(cfg *Configuration) PullOpt { } } -// WithOptWriter sets the registryOut field on the push configuration object. -func WithPullOptWriter(out io.Writer) PullOpt { - return func(p *Pull) { - p.out = out - } -} - // NewPull creates a new Pull object. func NewPull() *Pull { return NewPullWithOpts() @@ -81,6 +72,11 @@ func NewPullWithOpts(opts ...PullOpt) *Pull { return p } +// SetRegistryClient sets the registry client on the pull configuration object. +func (p *Pull) SetRegistryClient(client *registry.Client) { + p.cfg.RegistryClient = client +} + // Run executes 'helm pull' against the given release. func (p *Pull) Run(chartRef string) (string, error) { var out strings.Builder @@ -102,16 +98,6 @@ func (p *Pull) Run(chartRef string) (string, error) { } if registry.IsOCI(chartRef) { - // Provide a tls enabled client for the pull command if the user has - // specified the cert file or key file or ca file. - if (p.ChartPathOptions.CertFile != "" && p.ChartPathOptions.KeyFile != "") || p.ChartPathOptions.CaFile != "" || p.ChartPathOptions.InsecureSkipTLSverify { - registryClient, err := registry.NewRegistryClientWithTLS(p.out, p.ChartPathOptions.CertFile, p.ChartPathOptions.KeyFile, p.ChartPathOptions.CaFile, - p.ChartPathOptions.InsecureSkipTLSverify, p.Settings.RegistryConfig, p.Settings.Debug) - if err != nil { - return out.String(), err - } - p.cfg.RegistryClient = registryClient - } c.Options = append(c.Options, getter.WithRegistryClient(p.cfg.RegistryClient)) c.RegistryClient = p.cfg.RegistryClient diff --git a/pkg/action/push.go b/pkg/action/push.go index 21f6fb947..892006406 100644 --- a/pkg/action/push.go +++ b/pkg/action/push.go @@ -90,21 +90,11 @@ func (p *Push) Run(chartRef string, remote string) (string, error) { Pushers: pusher.All(p.Settings), Options: []pusher.Option{ pusher.WithTLSClientConfig(p.certFile, p.keyFile, p.caFile), + pusher.WithInsecureSkipTLSVerify(p.insecureSkipTLSverify), }, } if registry.IsOCI(remote) { - // Provide a tls enabled client for the pull command if the user has - // specified the cert file or key file or ca file. - if (p.certFile != "" && p.keyFile != "") || p.caFile != "" || p.insecureSkipTLSverify { - registryClient, err := registry.NewRegistryClientWithTLS(p.out, p.certFile, p.keyFile, p.caFile, - p.insecureSkipTLSverify, p.Settings.RegistryConfig, p.Settings.Debug) - if err != nil { - return out.String(), err - } - p.cfg.RegistryClient = registryClient - } - // Don't use the default registry client if tls options are set. c.Options = append(c.Options, pusher.WithRegistryClient(p.cfg.RegistryClient)) } diff --git a/pkg/action/show.go b/pkg/action/show.go index 9ba85234d..8cf231593 100644 --- a/pkg/action/show.go +++ b/pkg/action/show.go @@ -28,6 +28,7 @@ import ( "helm.sh/helm/v3/pkg/chart" "helm.sh/helm/v3/pkg/chart/loader" "helm.sh/helm/v3/pkg/chartutil" + "helm.sh/helm/v3/pkg/registry" ) // ShowOutputFormat is the format of the output of `helm show` @@ -82,6 +83,11 @@ func NewShowWithConfig(output ShowOutputFormat, cfg *Configuration) *Show { return sh } +// SetRegistryClient sets the registry client to use when pulling a chart from a registry. +func (s *Show) SetRegistryClient(client *registry.Client) { + s.ChartPathOptions.registryClient = client +} + // Run executes 'helm show' against the given release. func (s *Show) Run(chartpath string) (string, error) { if s.chart == nil { diff --git a/pkg/action/upgrade.go b/pkg/action/upgrade.go index 8fb895df0..829be51df 100644 --- a/pkg/action/upgrade.go +++ b/pkg/action/upgrade.go @@ -32,6 +32,7 @@ import ( "helm.sh/helm/v3/pkg/chartutil" "helm.sh/helm/v3/pkg/kube" "helm.sh/helm/v3/pkg/postrender" + "helm.sh/helm/v3/pkg/registry" "helm.sh/helm/v3/pkg/release" "helm.sh/helm/v3/pkg/releaseutil" "helm.sh/helm/v3/pkg/storage/driver" @@ -122,6 +123,11 @@ func NewUpgrade(cfg *Configuration) *Upgrade { return up } +// SetRegistryClient sets the registry client to use when fetching charts. +func (u *Upgrade) SetRegistryClient(client *registry.Client) { + u.ChartPathOptions.registryClient = client +} + // Run executes the upgrade on the given release. func (u *Upgrade) Run(name string, chart *chart.Chart, vals map[string]interface{}) (*release.Release, error) { ctx := context.Background()