Skip to content

Commit 9c18e20

Browse files
committed
Warn when Helm credentials are set but helmRepoURLRegex is empty
When Helm credentials are configured in an Auth object but helmRepoURLRegex is empty, addRemoteCharts silently drops the credentials. Log a Warn-level message so operators know credentials are not forwarded and how to fix it by setting spec.helmRepoURLRegex.
1 parent 2451e2d commit 9c18e20

2 files changed

Lines changed: 78 additions & 0 deletions

File tree

internal/bundlereader/resources.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ func mergeGenericMap(first, second *fleet.GenericMap) *fleet.GenericMap {
214214
// For every chart that is not on disk, create a directory struct that contains the charts URL as path.
215215
// This adds one directory per HelmOption.
216216
func addRemoteCharts(ctx context.Context, directories []directory, base string, charts []*fleet.HelmOptions, auth Auth, helmRepoURLRegex string) ([]directory, error) {
217+
warnedOnce := false
217218
for _, chart := range charts {
218219
if _, err := os.Stat(filepath.Join(base, chart.Chart)); os.IsNotExist(err) || chart.Repo != "" {
219220
shouldAddAuthToRequest, err := shouldAddAuthToRequest(helmRepoURLRegex, chart.Repo, chart.Chart)
@@ -222,6 +223,10 @@ func addRemoteCharts(ctx context.Context, directories []directory, base string,
222223
}
223224
auth := auth // loop-scoped variable
224225
if !shouldAddAuthToRequest {
226+
if !warnedOnce && helmRepoURLRegex == "" && (auth.Username != "" || auth.Password != "" || auth.SSHPrivateKey != nil) {
227+
logrus.Warn("helmRepoURLRegex is empty: Helm credentials will not be forwarded to any repository; set spec.helmRepoURLRegex to enable credential forwarding")
228+
warnedOnce = true
229+
}
225230
// Only clear credentials; preserve transport settings (BasicHTTP, CABundle, InsecureSkipVerify)
226231
auth.Username = ""
227232
auth.Password = ""

internal/bundlereader/resources_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"context"
66
"testing"
77

8+
"github.com/sirupsen/logrus"
9+
810
fleet "github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1"
911

1012
"github.com/stretchr/testify/assert"
@@ -207,3 +209,74 @@ func TestAddRemoteChartsStripsCredentials(t *testing.T) {
207209
assert.True(t, got.BasicHTTP, "BasicHTTP must be preserved when stripping credentials")
208210
assert.True(t, got.InsecureSkipVerify, "InsecureSkipVerify must be preserved when stripping credentials")
209211
}
212+
213+
func TestAddRemoteChartsWarnsMissingRegex(t *testing.T) {
214+
remoteChart := []*fleet.HelmOptions{{Chart: "/nonexistent/chart"}}
215+
216+
tests := []struct {
217+
name string
218+
charts []*fleet.HelmOptions
219+
auth Auth
220+
regex string
221+
wantWarning bool
222+
}{
223+
{
224+
name: "credentials set, regex empty — warn",
225+
charts: remoteChart,
226+
auth: Auth{Username: "user", Password: "secret"},
227+
regex: "",
228+
wantWarning: true,
229+
},
230+
{
231+
name: "SSH key set, regex empty — warn",
232+
charts: remoteChart,
233+
auth: Auth{SSHPrivateKey: []byte("key")},
234+
regex: "",
235+
wantWarning: true,
236+
},
237+
{
238+
name: "no credentials, regex empty — no warn",
239+
charts: remoteChart,
240+
auth: Auth{BasicHTTP: true},
241+
regex: "",
242+
wantWarning: false,
243+
},
244+
{
245+
name: "credentials set, regex provided — no warn",
246+
charts: remoteChart,
247+
auth: Auth{Username: "user", Password: "secret"},
248+
regex: "https://charts\\.example\\.com.*",
249+
wantWarning: false,
250+
},
251+
{
252+
name: "credentials set, regex empty, no charts — no warn",
253+
charts: nil,
254+
auth: Auth{Username: "user", Password: "secret"},
255+
regex: "",
256+
wantWarning: false,
257+
},
258+
}
259+
260+
for _, tt := range tests {
261+
t.Run(tt.name, func(t *testing.T) {
262+
var buf bytes.Buffer
263+
oldOut := logrus.StandardLogger().Out
264+
oldLevel := logrus.GetLevel()
265+
logrus.SetOutput(&buf)
266+
logrus.SetLevel(logrus.WarnLevel)
267+
t.Cleanup(func() {
268+
logrus.SetOutput(oldOut)
269+
logrus.SetLevel(oldLevel)
270+
})
271+
272+
_, err := addRemoteCharts(context.Background(), nil, t.TempDir(), tt.charts, tt.auth, tt.regex)
273+
require.NoError(t, err)
274+
275+
if tt.wantWarning {
276+
assert.Contains(t, buf.String(), "helmRepoURLRegex")
277+
} else {
278+
assert.NotContains(t, buf.String(), "helmRepoURLRegex")
279+
}
280+
})
281+
}
282+
}

0 commit comments

Comments
 (0)