Skip to content
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
14 changes: 10 additions & 4 deletions pkg/app/pipedv1/plugin/kubernetes/provider/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ func (h *Helm) LoginToOCIRegistry(ctx context.Context, address, username, passwo
"login",
"-u",
username,
"-p",
password,
"--password-stdin",
address,
}

var stderr bytes.Buffer
cmd := exec.CommandContext(ctx, h.execPath, args...)
cmd.Stdin = strings.NewReader(password)
cmd.Stderr = &stderr

h.logger.Info("login to oci registry", zap.String("address", address))
Expand Down Expand Up @@ -138,10 +138,16 @@ func (h *Helm) AddRepository(ctx context.Context, repo config.HelmChartRepositor
if repo.Insecure {
args = append(args, "--insecure-skip-tls-verify")
}
if repo.Username != "" || repo.Password != "" {
args = append(args, "--username", repo.Username, "--password", repo.Password)
if repo.Username != "" {
args = append(args, "--username", repo.Username)
}
if repo.Password != "" {
args = append(args, "--password-stdin")
}
cmd := exec.CommandContext(ctx, h.execPath, args...)
if repo.Password != "" {
cmd.Stdin = strings.NewReader(repo.Password)
}
out, err := cmd.CombinedOutput()
if err != nil {
h.logger.Error("failed to add chart repository", zap.String("name", repo.Name), zap.Error(err))
Expand Down
66 changes: 66 additions & 0 deletions pkg/app/pipedv1/plugin/kubernetes/provider/helm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ package provider

import (
"context"
"os"
"path/filepath"
"strings"
"testing"

Expand All @@ -25,6 +27,7 @@ import (

"github.com/pipe-cd/piped-plugin-sdk-go/toolregistry/toolregistrytest"

"github.com/pipe-cd/pipecd/pkg/app/pipedv1/plugin/kubernetes/config"
"github.com/pipe-cd/pipecd/pkg/app/pipedv1/plugin/kubernetes/toolregistry"
)

Expand Down Expand Up @@ -84,6 +87,43 @@ func TestTemplateLocalChart_WithNamespace(t *testing.T) {
}
}

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

execPath, argsPath, stdinPath := newFakeHelm(t)
helm := NewHelm(execPath, zaptest.NewLogger(t))

err := helm.LoginToOCIRegistry(t.Context(), "ghcr.io", "test-user", "super-secret")
require.NoError(t, err)

args := readFakeHelmOutput(t, argsPath)
assert.NotContains(t, args, "super-secret")
assert.Contains(t, args, "--password-stdin")
assert.Equal(t, "super-secret", strings.TrimSpace(readFakeHelmOutput(t, stdinPath)))
}

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

execPath, argsPath, stdinPath := newFakeHelm(t)
helm := NewHelm(execPath, zaptest.NewLogger(t))

err := helm.AddRepository(t.Context(), config.HelmChartRepository{
Name: "test-repo",
Address: "https://example.com/charts",
Username: "test-user",
Password: "super-secret",
})
require.NoError(t, err)

args := readFakeHelmOutput(t, argsPath)
assert.NotContains(t, args, "super-secret")
assert.Contains(t, args, "--username")
assert.Contains(t, args, "test-user")
assert.Contains(t, args, "--password-stdin")
assert.Equal(t, "super-secret", strings.TrimSpace(readFakeHelmOutput(t, stdinPath)))
}

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

Expand Down Expand Up @@ -210,3 +250,29 @@ func TestTemplateRemoteChart(t *testing.T) {
require.Equal(t, "testapp-helloworld", name)
}
}

func newFakeHelm(t *testing.T) (execPath, argsPath, stdinPath string) {
t.Helper()

dir := t.TempDir()
argsPath = filepath.Join(dir, "args.txt")
stdinPath = filepath.Join(dir, "stdin.txt")
execPath = filepath.Join(dir, "helm")

script := strings.Join([]string{
"#!/bin/sh",
"printf '%s\\n' \"$@\" > \"" + argsPath + "\"",
"cat > \"" + stdinPath + "\"",
}, "\n")

require.NoError(t, os.WriteFile(execPath, []byte(script), 0o755))
return execPath, argsPath, stdinPath
}

func readFakeHelmOutput(t *testing.T, path string) string {
t.Helper()

data, err := os.ReadFile(path)
require.NoError(t, err)
return string(data)
}
Loading