From 47dae4d35a66b668e370d7cbf10a184a4133c329 Mon Sep 17 00:00:00 2001 From: Harshit Mehta Date: Thu, 9 Mar 2023 19:04:05 +0530 Subject: [PATCH] Add username and password support to Repo add feature (#228) * Add username and password support to Repo add in UI * Add support for Username and Passowrd in Add Repo API --- pkg/dashboard/handlers/helmHandlers.go | 11 ++++++----- pkg/dashboard/objects/repos.go | 14 +++++++++----- pkg/dashboard/objects/repos_test.go | 2 +- pkg/dashboard/static/index.html | 22 ++++++++++++++++++++-- pkg/dashboard/static/styles-base.css | 5 +++++ 5 files changed, 41 insertions(+), 13 deletions(-) diff --git a/pkg/dashboard/handlers/helmHandlers.go b/pkg/dashboard/handlers/helmHandlers.go index 08b6162..79ff5b5 100644 --- a/pkg/dashboard/handlers/helmHandlers.go +++ b/pkg/dashboard/handlers/helmHandlers.go @@ -4,6 +4,11 @@ import ( "encoding/json" "errors" "fmt" + "net/http" + "sort" + "strconv" + "strings" + "github.com/gin-gonic/gin" "github.com/hexops/gotextdiff" "github.com/hexops/gotextdiff/myers" @@ -19,10 +24,6 @@ import ( "helm.sh/helm/v3/pkg/repo" helmtime "helm.sh/helm/v3/pkg/time" "k8s.io/utils/strings/slices" - "net/http" - "sort" - "strconv" - "strings" ) type HelmHandler struct { @@ -494,7 +495,7 @@ func (h *HelmHandler) RepoAdd(c *gin.Context) { } // TODO: more repo options to accept - err := app.Repositories.Add(c.PostForm("name"), c.PostForm("url")) + err := app.Repositories.Add(c.PostForm("name"), c.PostForm("url"), c.PostForm("username"), c.PostForm("password")) if err != nil { _ = c.AbortWithError(http.StatusInternalServerError, err) return diff --git a/pkg/dashboard/objects/repos.go b/pkg/dashboard/objects/repos.go index 250a91b..02358bd 100644 --- a/pkg/dashboard/objects/repos.go +++ b/pkg/dashboard/objects/repos.go @@ -65,11 +65,15 @@ func (r *Repositories) List() ([]Repository, error) { return res, nil } -func (r *Repositories) Add(name string, url string) error { +func (r *Repositories) Add(name string, url string, username string, password string) error { if name == "" || url == "" { return errors.New("Name and URL are required parameters to add the repository") } + if (username != "" && password == "") || (username == "" && password != "") { + return errors.New("Username and Password, both are required parameters to add the repository with authentication") + } + // copied from cmd/helm/repo_add.go repoFile := r.Settings.RepositoryConfig @@ -88,10 +92,10 @@ func (r *Repositories) Add(name string, url string) error { defer r.mx.Unlock() c := repo.Entry{ - Name: name, - URL: url, - //Username: o.username, - //Password: o.password, + Name: name, + URL: url, + Username: username, + Password: password, //PassCredentialsAll: o.passCredentialsAll, //CertFile: o.certFile, //KeyFile: o.keyFile, diff --git a/pkg/dashboard/objects/repos_test.go b/pkg/dashboard/objects/repos_test.go index d02256d..cb6fd76 100644 --- a/pkg/dashboard/objects/repos_test.go +++ b/pkg/dashboard/objects/repos_test.go @@ -75,7 +75,7 @@ func TestFlow(t *testing.T) { testRepoUrl := "https://helm.github.io/examples" // add repo - err = testRepository.Add(testRepoName, testRepoUrl) + err = testRepository.Add(testRepoName, testRepoUrl, "", "") assert.NilError(t, err) // get repo diff --git a/pkg/dashboard/static/index.html b/pkg/dashboard/static/index.html index f67b0b2..f33e8fa 100644 --- a/pkg/dashboard/static/index.html +++ b/pkg/dashboard/static/index.html @@ -362,8 +362,26 @@