Skip to content

Commit

Permalink
Cherry[2.7] Helm repository might affect manifest generation of not r…
Browse files Browse the repository at this point in the history
…elated helm charts (#14528)

* Merge pull request from GHSA-94mc-2ch7-r5r5

Signed-off-by: Alexander Matyushentsev <AMatyushentsev@gmail.com>

* fix: fix broken helm repo alias/name support (#13647)

Signed-off-by: Alexander Matyushentsev <AMatyushentsev@gmail.com>

---------

Signed-off-by: Alexander Matyushentsev <AMatyushentsev@gmail.com>
  • Loading branch information
alexmt committed Jul 15, 2023
1 parent fe4ba23 commit 490fb79
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 52 deletions.
94 changes: 48 additions & 46 deletions reposerver/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -929,12 +929,40 @@ func (s *Service) getManifestCacheEntry(cacheKey string, q *apiclient.ManifestRe
return false, nil, nil
}

func getHelmRepos(repositories []*v1alpha1.Repository) []helm.HelmRepository {
repos := make([]helm.HelmRepository, 0)
func getHelmRepos(appPath string, repositories []*v1alpha1.Repository, helmRepoCreds []*v1alpha1.RepoCreds) ([]helm.HelmRepository, error) {
dependencies, err := getHelmDependencyRepos(appPath)
if err != nil {
return nil, err
}
reposByName := make(map[string]*v1alpha1.Repository)
reposByUrl := make(map[string]*v1alpha1.Repository)
for _, repo := range repositories {
reposByUrl[repo.Repo] = repo
if repo.Name != "" {
reposByName[repo.Name] = repo
}
}

repos := make([]helm.HelmRepository, 0)
for _, dep := range dependencies {
repo, ok := reposByUrl[dep.Repo]
if !ok && dep.Name != "" {
repo, ok = reposByName[dep.Name]
}
if !ok {
repo = &v1alpha1.Repository{Repo: dep.Repo, Name: dep.Name, EnableOCI: dep.EnableOCI}
if repositoryCredential := getRepoCredential(helmRepoCreds, dep.Repo); repositoryCredential != nil {
repo.EnableOCI = repositoryCredential.EnableOCI
repo.Password = repositoryCredential.Password
repo.Username = repositoryCredential.Username
repo.SSHPrivateKey = repositoryCredential.SSHPrivateKey
repo.TLSClientCertData = repositoryCredential.TLSClientCertData
repo.TLSClientCertKey = repositoryCredential.TLSClientCertKey
}
}
repos = append(repos, helm.HelmRepository{Name: repo.Name, Repo: repo.Repo, Creds: repo.GetHelmCreds(), EnableOci: repo.EnableOCI})
}
return repos
return repos, nil
}

type dependencies struct {
Expand All @@ -958,9 +986,14 @@ func getHelmDependencyRepos(appPath string) ([]*v1alpha1.Repository, error) {
}

for _, r := range d.Dependencies {
if u, err := url.Parse(r.Repository); err == nil && (u.Scheme == "https" || u.Scheme == "oci") {
if strings.HasPrefix(r.Repository, "@") {
repos = append(repos, &v1alpha1.Repository{
Name: r.Repository[1:],
})
} else if u, err := url.Parse(r.Repository); err == nil && (u.Scheme == "https" || u.Scheme == "oci") {
repo := &v1alpha1.Repository{
Repo: r.Repository,
// trimming oci:// prefix since it is currently not supported by Argo CD (OCI repos just have no scheme)
Repo: strings.TrimPrefix(r.Repository, "oci://"),
Name: sanitizeRepoName(r.Repository),
EnableOCI: u.Scheme == "oci",
}
Expand All @@ -975,15 +1008,6 @@ func sanitizeRepoName(repoName string) string {
return strings.ReplaceAll(repoName, "/", "-")
}

func repoExists(repo string, repos []*v1alpha1.Repository) bool {
for _, r := range repos {
if strings.TrimPrefix(repo, ociPrefix) == strings.TrimPrefix(r.Repo, ociPrefix) {
return true
}
}
return false
}

func isConcurrencyAllowed(appPath string) bool {
if _, err := os.Stat(path.Join(appPath, allowConcurrencyFile)); err == nil {
return true
Expand Down Expand Up @@ -1019,32 +1043,6 @@ func runHelmBuild(appPath string, h helm.Helm) error {
return os.WriteFile(markerFile, []byte("marker"), 0644)
}

func populateRequestRepos(appPath string, q *apiclient.ManifestRequest) error {
repos, err := getHelmDependencyRepos(appPath)
if err != nil {
return err
}

for _, r := range repos {
if !repoExists(r.Repo, q.Repos) {
repositoryCredential := getRepoCredential(q.HelmRepoCreds, r.Repo)
if repositoryCredential != nil {
if repositoryCredential.EnableOCI {
r.Repo = strings.TrimPrefix(r.Repo, ociPrefix)
}
r.EnableOCI = repositoryCredential.EnableOCI
r.Password = repositoryCredential.Password
r.Username = repositoryCredential.Username
r.SSHPrivateKey = repositoryCredential.SSHPrivateKey
r.TLSClientCertData = repositoryCredential.TLSClientCertData
r.TLSClientCertKey = repositoryCredential.TLSClientCertKey
}
q.Repos = append(q.Repos, r)
}
}
return nil
}

func helmTemplate(appPath string, repoRoot string, env *v1alpha1.Env, q *apiclient.ManifestRequest, isLocal bool, gitRepoPaths io.TempPaths) ([]*unstructured.Unstructured, error) {
concurrencyAllowed := isConcurrencyAllowed(appPath)
if !concurrencyAllowed {
Expand Down Expand Up @@ -1132,16 +1130,16 @@ func helmTemplate(appPath string, repoRoot string, env *v1alpha1.Env, q *apiclie
templateOpts.SetString[i] = env.Envsubst(j)
}

if err := populateRequestRepos(appPath, q); err != nil {
return nil, fmt.Errorf("failed parsing dependencies: %v", err)
}

var proxy string
if q.Repo != nil {
proxy = q.Repo.Proxy
}

h, err := helm.NewHelmApp(appPath, getHelmRepos(q.Repos), isLocal, version, proxy, passCredentials)
helmRepos, err := getHelmRepos(appPath, q.Repos, q.HelmRepoCreds)
if err != nil {
return nil, err
}
h, err := helm.NewHelmApp(appPath, helmRepos, isLocal, version, proxy, passCredentials)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -2043,7 +2041,11 @@ func populateHelmAppDetails(res *apiclient.RepoAppDetailsResponse, appPath strin
}
passCredentials = q.Source.Helm.PassCredentials
}
h, err := helm.NewHelmApp(appPath, getHelmRepos(q.Repos), false, version, q.Repo.Proxy, passCredentials)
helmRepos, err := getHelmRepos(appPath, q.Repos, nil)
if err != nil {
return err
}
h, err := helm.NewHelmApp(appPath, helmRepos, false, version, q.Repo.Proxy, passCredentials)
if err != nil {
return err
}
Expand Down
29 changes: 23 additions & 6 deletions reposerver/repository/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1277,6 +1277,7 @@ func TestListApps(t *testing.T) {
"oci-dependencies": "Helm",
"out-of-bounds-values-file-link": "Helm",
"values-files": "Helm",
"helm-with-dependencies": "Helm",
}
assert.Equal(t, expectedApps, res.Apps)
}
Expand Down Expand Up @@ -2603,19 +2604,35 @@ func Test_populateHelmAppDetails_values_symlinks(t *testing.T) {
})
}

func TestOCIDependencies(t *testing.T) {
func TestGetHelmRepos_OCIDependencies(t *testing.T) {
src := argoappv1.ApplicationSource{Path: "."}
q := apiclient.ManifestRequest{Repo: &argoappv1.Repository{}, ApplicationSource: &src, HelmRepoCreds: []*argoappv1.RepoCreds{
{URL: "example.com", Username: "test", Password: "test", EnableOCI: true},
}}

err := populateRequestRepos("./testdata/oci-dependencies", &q)
helmRepos, err := getHelmRepos("./testdata/oci-dependencies", q.Repos, q.HelmRepoCreds)
assert.Nil(t, err)

assert.Equal(t, len(q.Repos), 1)
assert.Equal(t, q.Repos[0].Username, "test")
assert.Equal(t, q.Repos[0].EnableOCI, true)
assert.Equal(t, q.Repos[0].Repo, "example.com")
assert.Equal(t, len(helmRepos), 1)
assert.Equal(t, helmRepos[0].Username, "test")
assert.Equal(t, helmRepos[0].EnableOci, true)
assert.Equal(t, helmRepos[0].Repo, "example.com")
}

func TestGetHelmRepo_NamedRepos(t *testing.T) {
src := argoappv1.ApplicationSource{Path: "."}
q := apiclient.ManifestRequest{Repo: &argoappv1.Repository{}, ApplicationSource: &src, Repos: []*argoappv1.Repository{{
Name: "custom-repo",
Repo: "https://example.com",
Username: "test",
}}}

helmRepos, err := getHelmRepos("./testdata/helm-with-dependencies", q.Repos, q.HelmRepoCreds)
assert.Nil(t, err)

assert.Equal(t, len(helmRepos), 1)
assert.Equal(t, helmRepos[0].Username, "test")
assert.Equal(t, helmRepos[0].Repo, "https://example.com")
}

func Test_getResolvedValueFiles(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: v2
name: helm-with-dependencies
version: v1.0.0
dependencies:
- name: helm
repository: "@custom-repo"
version: v1.0.0

0 comments on commit 490fb79

Please sign in to comment.