fix(helm): helm pull --repo to use repo creds from helm repos file

Up to this point, helm pull --repo required --username and --password
even if the repository had already been added via helm repo add.

From now on, check repositories in the repositories file and if the URL
matches, pull the chart with the username and password from the entry.

Fixes #9599

Signed-off-by: Andreas Karis <ak.karis@gmail.com>
pull/9760/head
Andreas Karis 3 years ago
parent eb99434597
commit 8af97d560d

@ -119,7 +119,19 @@ func (p *Pull) Run(chartRef string) (string, error) {
}
if p.RepoURL != "" {
chartURL, err := repo.FindChartInAuthAndTLSAndPassRepoURL(p.RepoURL, p.Username, p.Password, chartRef, p.Version, p.CertFile, p.KeyFile, p.CaFile, p.InsecureSkipTLSverify, p.PassCredentialsAll, getter.All(p.Settings))
username := p.Username
password := p.Password
if username == "" && password == "" {
repoEntry, err := repo.FindRepoEntry(p.RepoURL, p.Settings.RepositoryConfig)
if err != nil {
return out.String(), err
}
if repoEntry != nil {
username = repoEntry.Username
password = repoEntry.Password
}
}
chartURL, err := repo.FindChartInAuthAndTLSAndPassRepoURL(p.RepoURL, username, password, chartRef, p.Version, p.CertFile, p.KeyFile, p.CaFile, p.InsecureSkipTLSverify, p.PassCredentialsAll, getter.All(p.Settings))
if err != nil {
return out.String(), err
}

@ -95,6 +95,28 @@ func (r *File) Get(name string) *Entry {
return nil
}
// Get returns an entry with the given URL if it exists, otherwise returns nil
func (r *File) GetURL(url string) *Entry {
for _, entry := range r.Repositories {
if entry.URL == url {
return entry
}
}
return nil
}
func FindRepoEntry(url string, repositoryConfig string) (*Entry, error) {
f, err := LoadFile(repositoryConfig)
if err != nil {
return nil, err
}
entry := f.GetURL(url)
if entry == nil {
return nil, nil
}
return entry, nil
}
// Remove removes the entry from the list of repositories.
func (r *File) Remove(name string) bool {
cp := []*Entry{}

@ -129,6 +129,56 @@ func TestRepoFile_Get(t *testing.T) {
}
}
func TestRepoFile_GetURL(t *testing.T) {
repo := NewFile()
repo.Add(
&Entry{
Name: "first",
URL: "https://example.com/first",
},
&Entry{
Name: "second",
URL: "https://example.com/second",
},
&Entry{
Name: "third",
URL: "https://example.com/third",
},
&Entry{
Name: "fourth",
URL: "https://example.com/fourth",
},
)
url := "https://example.com/second"
entry := repo.GetURL(url)
if entry == nil {
t.Fatalf("Expected repo entry %q to be found", url)
}
if entry.Name != "second" {
t.Errorf("Expected repo Name to be %q but got %q", "second", entry.Name)
}
entry = repo.GetURL("http://nonexistent.example.com/nonexistent")
if entry != nil {
t.Errorf("Got unexpected entry %+v", entry)
}
}
func TestFindRepoEntry(t *testing.T) {
url := "https://example.com/incubator"
repoEntry, err := FindRepoEntry(url, testRepositoriesFile)
if err != nil {
t.Errorf("%q could not be loaded: %s", testRepositoriesFile, err)
}
if repoEntry == nil {
t.Fatalf("Could not find repository with URL: %q", url)
}
}
func TestRemoveRepository(t *testing.T) {
sampleRepository := NewFile()
sampleRepository.Add(

Loading…
Cancel
Save