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

Allow to use HTTPS protocol in URLs #379

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions cmd/helm-s3/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package main

import (
"context"
"fmt"
"net/url"
"os"
"path/filepath"

Expand Down Expand Up @@ -38,6 +40,7 @@ func newPushCommand(opts *options) *cobra.Command {
force: false,
ignoreIfExists: false,
relative: false,
https: false,
}

cmd := &cobra.Command{
Expand Down Expand Up @@ -69,6 +72,7 @@ func newPushCommand(opts *options) *cobra.Command {
flags.BoolVar(&act.force, "force", act.force, "Replace the chart if it already exists. This can cause the repository to lose existing chart; use it with care.")
flags.BoolVar(&act.ignoreIfExists, "ignore-if-exists", act.ignoreIfExists, "If the chart already exists, exit normally and do not trigger an error.")
flags.BoolVar(&act.relative, "relative", act.relative, "Use relative chart URL in the index instead of absolute.")
flags.BoolVar(&act.https, "https", act.https, "Use HTTPS protocol for chart URLs in the index instead of S3 protocol.")

return cmd
}
Expand All @@ -92,6 +96,7 @@ type pushAction struct {
force bool
ignoreIfExists bool
relative bool
https bool
}

func (act *pushAction) run(ctx context.Context) error {
Expand Down Expand Up @@ -205,6 +210,14 @@ func (act *pushAction) run(ctx context.Context) error {
baseURL = ""
}

if act.https {
parsedS3URL, err := url.Parse(baseURL)
if err != nil {
act.printer.PrintErrf("[ERROR] failed to parse baseURL: %s", err)
}
baseURL = fmt.Sprintf("https://%s/%s", parsedS3URL.Host, parsedS3URL.Path)
}

filename := escapeIfRelative(fname, act.relative)

if err := idx.AddOrReplace(chart.Metadata().Value(), filename, baseURL, hash); err != nil {
Expand Down
13 changes: 13 additions & 0 deletions cmd/helm-s3/reindex.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"fmt"
"net/url"

"github.com/pkg/errors"
"github.com/spf13/cobra"
Expand All @@ -27,6 +28,7 @@ func newReindexCommand(opts *options) *cobra.Command {
verbose: false,
repoName: "",
relative: false,
https: false,
}

cmd := &cobra.Command{
Expand All @@ -50,6 +52,7 @@ func newReindexCommand(opts *options) *cobra.Command {

flags := cmd.Flags()
flags.BoolVar(&act.relative, "relative", act.relative, "Use relative chart URLs in the index instead of absolute.")
flags.BoolVar(&act.https, "https", act.https, "Use HTTPS protocol for chart URLs in the index instead of S3 protocol.")

return cmd
}
Expand All @@ -69,6 +72,8 @@ type reindexAction struct {
// flags

relative bool

https bool
}

func (act *reindexAction) run(ctx context.Context) error {
Expand All @@ -94,6 +99,14 @@ func (act *reindexAction) run(ctx context.Context) error {
baseURL = ""
}

if act.https {
parsedS3URL, err := url.Parse(baseURL)
if err != nil {
act.printer.PrintErrf("[ERROR] failed to parse baseURL: %s", err)
}
baseURL = fmt.Sprintf("https://%s/%s", parsedS3URL.Host, parsedS3URL.Path)
}

if act.verbose {
act.printer.Printf("[DEBUG] Adding %s to index.\n", item.Filename)
}
Expand Down
52 changes: 52 additions & 0 deletions tests/e2e/push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,58 @@ func TestPushRelative(t *testing.T) {
assert.FileExists(t, filepath.Join(tmpdir, chartFilename))
}

func TestPushHttps(t *testing.T) {
t.Log("Test push action with --https flag")

const (
repoName = "test-push-https"
repoDir = "charts"
chartName = "foo"
chartVersion = "1.2.3"
chartFilename = "foo-1.2.3.tgz"
chartFilepath = "testdata/" + chartFilename
)

setupRepo(t, repoName, repoDir)
defer teardownRepo(t, repoName)

cmd, stdout, stderr := command(fmt.Sprintf("helm s3 push --https %s %s", chartFilepath, repoName))
err := cmd.Run()
assert.NoError(t, err)
assertEmptyOutput(t, nil, stderr)
assert.Contains(t, stdout.String(), "Successfully uploaded the chart to the repository.")

// Fetch the repo index and check that chart uri is relative.

tmpdir, err := os.MkdirTemp("", t.Name())
require.NoError(t, err)
defer os.RemoveAll(tmpdir)

indexFile := filepath.Join(tmpdir, "index.yaml")

err = mc.FGetObject(repoName, repoDir+"/index.yaml", indexFile, minio.GetObjectOptions{})
require.NoError(t, err)

idx, err := repo.LoadIndexFile(indexFile)
require.NoError(t, err)

cv, err := idx.Get(chartName, chartVersion)
require.NoError(t, err)

expected := []string{fmt.Sprintf("https://%s/%s/%s", repoName, repoDir, chartFilename)}
if diff := cmp.Diff(expected, cv.URLs); diff != "" {
t.Errorf("mismatch (-want +got):\n%s", diff)
}

// Check that chart can be successfully fetched.

cmd, stdout, stderr = command(fmt.Sprintf("helm fetch %s/%s --version %s --destination %s", repoName, chartName, chartVersion, tmpdir))
err = cmd.Run()
assert.NoError(t, err)
assertEmptyOutput(t, stdout, stderr)
assert.FileExists(t, filepath.Join(tmpdir, chartFilename))
}

func assertEmptyOutput(t *testing.T, stdout, stderr *bytes.Buffer) {
t.Helper()

Expand Down