Skip to content

Commit

Permalink
Fix the password display problem when passing the chart link (#1281)
Browse files Browse the repository at this point in the history
* Fix the password display problem when passing the
chart link

Signed-off-by: Eduardo Naves <eduardonaves41@gmail.com>
  • Loading branch information
NavesEdu committed Jan 24, 2024
1 parent 6707a3d commit 430677d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
20 changes: 19 additions & 1 deletion pkg/state/state.go
Expand Up @@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"io"
"net/url"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -3192,6 +3193,18 @@ func renderValsSecrets(e vals.Evaluator, input ...string) ([]string, error) {
return output, nil
}

func hideChartCredentials(chartCredentials string) (string, error) {
u, err := url.Parse(chartCredentials)
if err != nil {
return "", err
}
if u.User != nil {
u.User = url.UserPassword("---", "---")
}
modifiedURL := u.String()
return modifiedURL, nil
}

// DisplayAffectedReleases logs the upgraded, deleted and in error releases
func (ar *AffectedReleases) DisplayAffectedReleases(logger *zap.SugaredLogger) {
if ar.Upgraded != nil && len(ar.Upgraded) > 0 {
Expand All @@ -3203,7 +3216,12 @@ func (ar *AffectedReleases) DisplayAffectedReleases(logger *zap.SugaredLogger) {
)
tbl.Separator = " "
for _, release := range ar.Upgraded {
err := tbl.AddRow(release.Name, release.Chart, release.installedVersion, release.duration.Round(time.Second))
modifiedChart, modErr := hideChartCredentials(release.Chart)
if modErr != nil {
logger.Warn("Could not modify chart credentials, %v", modErr)
continue
}
err := tbl.AddRow(release.Name, modifiedChart, release.installedVersion, release.duration.Round(time.Second))
if err != nil {
logger.Warn("Could not add row, %v", err)
}
Expand Down
22 changes: 22 additions & 0 deletions pkg/state/state_test.go
Expand Up @@ -3455,3 +3455,25 @@ func TestAppendChartDownloadTLSFlags(t *testing.T) {
})
}
}

func TestHideChartURL(t *testing.T) {
tests := []struct {
input string
expected string
}{
{"http://username:password@example.com/", "http://---:---@example.com/"},
{"http://example.com@", "http://---:---@"},
{"https://username:password@example.com/", "https://---:---@example.com/"},
{"https://username:@password@example.com/", "https://---:---@example.com/"},
{"https://username::password@example.com/", "https://---:---@example.com/"},
{"https://username:httpd@example.com/", "https://---:---@example.com/"},
{"https://username:httpsd@example.com/", "https://---:---@example.com/"},
{"https://example.com/", "https://example.com/"},
}
for _, test := range tests {
result, _ := hideChartCredentials(test.input)
if result != test.expected {
t.Errorf("For input '%s', expected '%s', but got '%s'", test.input, test.expected, result)
}
}
}

0 comments on commit 430677d

Please sign in to comment.