Skip to content

Commit b163de0

Browse files
authored
fix: remove project from cache key for project scoped credentials (#22816)
Signed-off-by: Peter Jiang <[email protected]>
1 parent 8283115 commit b163de0

File tree

6 files changed

+77
-50
lines changed

6 files changed

+77
-50
lines changed

reposerver/repository/repository.go

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ func (s *Service) runRepoOperation(
345345

346346
if source.IsHelm() {
347347
if settings.noCache {
348-
err = helmClient.CleanChartCache(source.Chart, revision, repo.Project)
348+
err = helmClient.CleanChartCache(source.Chart, revision)
349349
if err != nil {
350350
return err
351351
}
@@ -354,7 +354,7 @@ func (s *Service) runRepoOperation(
354354
if source.Helm != nil {
355355
helmPassCredentials = source.Helm.PassCredentials
356356
}
357-
chartPath, closer, err := helmClient.ExtractChart(source.Chart, revision, repo.Project, helmPassCredentials, s.initConstants.HelmManifestMaxExtractedSize, s.initConstants.DisableHelmManifestMaxExtractedSize)
357+
chartPath, closer, err := helmClient.ExtractChart(source.Chart, revision, helmPassCredentials, s.initConstants.HelmManifestMaxExtractedSize, s.initConstants.DisableHelmManifestMaxExtractedSize)
358358
if err != nil {
359359
return err
360360
}
@@ -1180,7 +1180,7 @@ func helmTemplate(appPath string, repoRoot string, env *v1alpha1.Env, q *apiclie
11801180
referencedSource := getReferencedSource(p.Path, q.RefSources)
11811181
if referencedSource != nil {
11821182
// If the $-prefixed path appears to reference another source, do env substitution _after_ resolving the source
1183-
resolvedPath, err = getResolvedRefValueFile(p.Path, env, q.GetValuesFileSchemes(), referencedSource.Repo.Repo, gitRepoPaths, referencedSource.Repo.Project)
1183+
resolvedPath, err = getResolvedRefValueFile(p.Path, env, q.GetValuesFileSchemes(), referencedSource.Repo.Repo, gitRepoPaths)
11841184
if err != nil {
11851185
return nil, "", fmt.Errorf("error resolving set-file path: %w", err)
11861186
}
@@ -1306,7 +1306,7 @@ func getResolvedValueFiles(
13061306
referencedSource := getReferencedSource(rawValueFile, refSources)
13071307
if referencedSource != nil {
13081308
// If the $-prefixed path appears to reference another source, do env substitution _after_ resolving that source.
1309-
resolvedPath, err = getResolvedRefValueFile(rawValueFile, env, allowedValueFilesSchemas, referencedSource.Repo.Repo, gitRepoPaths, referencedSource.Repo.Project)
1309+
resolvedPath, err = getResolvedRefValueFile(rawValueFile, env, allowedValueFilesSchemas, referencedSource.Repo.Repo, gitRepoPaths)
13101310
if err != nil {
13111311
return nil, fmt.Errorf("error resolving value file path: %w", err)
13121312
}
@@ -1339,15 +1339,9 @@ func getResolvedRefValueFile(
13391339
allowedValueFilesSchemas []string,
13401340
refSourceRepo string,
13411341
gitRepoPaths io.TempPaths,
1342-
project string,
13431342
) (pathutil.ResolvedFilePath, error) {
13441343
pathStrings := strings.Split(rawValueFile, "/")
1345-
1346-
keyData, err := json.Marshal(map[string]string{"url": git.NormalizeGitURL(refSourceRepo), "project": project})
1347-
if err != nil {
1348-
return "", err
1349-
}
1350-
repoPath := gitRepoPaths.GetPathIfExists(string(keyData))
1344+
repoPath := gitRepoPaths.GetPathIfExists(git.NormalizeGitURL(refSourceRepo))
13511345
if repoPath == "" {
13521346
return "", fmt.Errorf("failed to find repo %q", refSourceRepo)
13531347
}
@@ -2382,7 +2376,7 @@ func (s *Service) GetRevisionChartDetails(ctx context.Context, q *apiclient.Repo
23822376
if err != nil {
23832377
return nil, fmt.Errorf("helm client error: %w", err)
23842378
}
2385-
chartPath, closer, err := helmClient.ExtractChart(q.Name, revision, q.Repo.Project, false, s.initConstants.HelmManifestMaxExtractedSize, s.initConstants.DisableHelmManifestMaxExtractedSize)
2379+
chartPath, closer, err := helmClient.ExtractChart(q.Name, revision, false, s.initConstants.HelmManifestMaxExtractedSize, s.initConstants.DisableHelmManifestMaxExtractedSize)
23862380
if err != nil {
23872381
return nil, fmt.Errorf("error extracting chart: %w", err)
23882382
}
@@ -2412,11 +2406,7 @@ func fileParameters(q *apiclient.RepoServerAppDetailsQuery) []v1alpha1.HelmFileP
24122406
}
24132407

24142408
func (s *Service) newClient(repo *v1alpha1.Repository, opts ...git.ClientOpts) (git.Client, error) {
2415-
keyData, err := json.Marshal(map[string]string{"url": git.NormalizeGitURL(repo.Repo), "project": repo.Project})
2416-
if err != nil {
2417-
return nil, err
2418-
}
2419-
repoPath, err := s.gitRepoPaths.GetPath(string(keyData))
2409+
repoPath, err := s.gitRepoPaths.GetPath(git.NormalizeGitURL(repo.Repo))
24202410
if err != nil {
24212411
return nil, err
24222412
}

reposerver/repository/repository_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,10 @@ func newServiceWithMocks(t *testing.T, root string, signed bool) (*Service, *git
127127
chart: {{Version: "1.0.0"}, {Version: version}},
128128
oobChart: {{Version: "1.0.0"}, {Version: version}},
129129
}}, nil)
130-
helmClient.On("ExtractChart", chart, version, "", false, int64(0), false).Return("./testdata/my-chart", io.NopCloser, nil)
131-
helmClient.On("ExtractChart", oobChart, version, "", false, int64(0), false).Return("./testdata2/out-of-bounds-chart", io.NopCloser, nil)
132-
helmClient.On("CleanChartCache", chart, version, "").Return(nil)
133-
helmClient.On("CleanChartCache", oobChart, version, "").Return(nil)
130+
helmClient.On("ExtractChart", chart, version, false, int64(0), false).Return("./testdata/my-chart", io.NopCloser, nil)
131+
helmClient.On("ExtractChart", oobChart, version, false, int64(0), false).Return("./testdata2/out-of-bounds-chart", io.NopCloser, nil)
132+
helmClient.On("CleanChartCache", chart, version).Return(nil)
133+
helmClient.On("CleanChartCache", oobChart, version).Return(nil)
134134
helmClient.On("DependencyBuild").Return(nil)
135135

136136
paths.On("Add", mock.Anything, mock.Anything).Return(root, nil)
@@ -3283,8 +3283,7 @@ func Test_getResolvedValueFiles(t *testing.T) {
32833283
tempDir := t.TempDir()
32843284
paths := io.NewRandomizedTempPaths(tempDir)
32853285

3286-
key, _ := json.Marshal(map[string]string{"url": git.NormalizeGitURL("https://github.com/org/repo1"), "project": ""})
3287-
paths.Add(string(key), path.Join(tempDir, "repo1"))
3286+
paths.Add(git.NormalizeGitURL("https://github.com/org/repo1"), path.Join(tempDir, "repo1"))
32883287

32893288
testCases := []struct {
32903289
name string

test/e2e/scoped_repository_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,41 @@ func TestGetRepoCLIOutput(t *testing.T) {
232232
git https://github.com/argoproj/argo-cd.git false false false false Successful argo-project`, output)
233233
})
234234
}
235+
236+
func TestCreateRepoWithSameURLInTwoProjects(t *testing.T) {
237+
projectFixture.Given(t).
238+
When().
239+
Name("project-one").
240+
Create().
241+
Then()
242+
243+
projectFixture.Given(t).
244+
When().
245+
Name("project-two").
246+
Create().
247+
Then()
248+
249+
path := "https://github.com/argoproj/argo-cd.git"
250+
251+
// Create repository in first project
252+
repoFixture.GivenWithSameState(t).
253+
When().
254+
Path(path).
255+
Project("project-one").
256+
Create().
257+
Then().
258+
And(func(r *Repository, _ error) {
259+
assert.Equal(t, r.Repo, path)
260+
})
261+
262+
// Create repository with same URL in second project
263+
repoFixture.GivenWithSameState(t).
264+
When().
265+
Path(path).
266+
Project("project-two").
267+
Create().
268+
Then().
269+
And(func(r *Repository, _ error) {
270+
assert.Equal(t, r.Repo, path)
271+
})
272+
}

util/helm/client.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ type indexCache interface {
5555
}
5656

5757
type Client interface {
58-
CleanChartCache(chart string, version string, project string) error
59-
ExtractChart(chart string, version string, project string, passCredentials bool, manifestMaxExtractedSize int64, disableManifestMaxExtractedSize bool) (string, argoio.Closer, error)
58+
CleanChartCache(chart string, version string) error
59+
ExtractChart(chart string, version string, passCredentials bool, manifestMaxExtractedSize int64, disableManifestMaxExtractedSize bool) (string, argoio.Closer, error)
6060
GetIndex(noCache bool, maxIndexSize int64) (*Index, error)
6161
GetTags(chart string, noCache bool) (*TagsList, error)
6262
TestHelmOCI() (bool, error)
@@ -120,8 +120,8 @@ func fileExist(filePath string) (bool, error) {
120120
return true, nil
121121
}
122122

123-
func (c *nativeHelmChart) CleanChartCache(chart string, version string, project string) error {
124-
cachePath, err := c.getCachedChartPath(chart, version, project)
123+
func (c *nativeHelmChart) CleanChartCache(chart string, version string) error {
124+
cachePath, err := c.getCachedChartPath(chart, version)
125125
if err != nil {
126126
return fmt.Errorf("error getting cached chart path: %w", err)
127127
}
@@ -148,7 +148,7 @@ func untarChart(tempDir string, cachedChartPath string, manifestMaxExtractedSize
148148
return files.Untgz(tempDir, reader, manifestMaxExtractedSize, false)
149149
}
150150

151-
func (c *nativeHelmChart) ExtractChart(chart string, version string, project string, passCredentials bool, manifestMaxExtractedSize int64, disableManifestMaxExtractedSize bool) (string, argoio.Closer, error) {
151+
func (c *nativeHelmChart) ExtractChart(chart string, version string, passCredentials bool, manifestMaxExtractedSize int64, disableManifestMaxExtractedSize bool) (string, argoio.Closer, error) {
152152
// always use Helm V3 since we don't have chart content to determine correct Helm version
153153
helmCmd, err := NewCmdWithVersion("", c.enableOci, c.proxy, c.noProxy)
154154
if err != nil {
@@ -162,7 +162,7 @@ func (c *nativeHelmChart) ExtractChart(chart string, version string, project str
162162
return "", nil, fmt.Errorf("error creating temporary directory: %w", err)
163163
}
164164

165-
cachedChartPath, err := c.getCachedChartPath(chart, version, project)
165+
cachedChartPath, err := c.getCachedChartPath(chart, version)
166166
if err != nil {
167167
_ = os.RemoveAll(tempDir)
168168
return "", nil, fmt.Errorf("error getting cached chart path: %w", err)
@@ -385,8 +385,8 @@ func normalizeChartName(chart string) string {
385385
return nc
386386
}
387387

388-
func (c *nativeHelmChart) getCachedChartPath(chart string, version string, project string) (string, error) {
389-
keyData, err := json.Marshal(map[string]string{"url": c.repoURL, "chart": chart, "version": version, "project": project})
388+
func (c *nativeHelmChart) getCachedChartPath(chart string, version string) (string, error) {
389+
keyData, err := json.Marshal(map[string]string{"url": c.repoURL, "chart": chart, "version": version})
390390
if err != nil {
391391
return "", fmt.Errorf("error marshaling cache key data: %w", err)
392392
}

util/helm/client_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func TestIndex(t *testing.T) {
7979

8080
func Test_nativeHelmChart_ExtractChart(t *testing.T) {
8181
client := NewClient("https://argoproj.github.io/argo-helm", Creds{}, false, "", "")
82-
path, closer, err := client.ExtractChart("argo-cd", "0.7.1", "", false, math.MaxInt64, true)
82+
path, closer, err := client.ExtractChart("argo-cd", "0.7.1", false, math.MaxInt64, true)
8383
require.NoError(t, err)
8484
defer io.Close(closer)
8585
info, err := os.Stat(path)
@@ -89,13 +89,13 @@ func Test_nativeHelmChart_ExtractChart(t *testing.T) {
8989

9090
func Test_nativeHelmChart_ExtractChartWithLimiter(t *testing.T) {
9191
client := NewClient("https://argoproj.github.io/argo-helm", Creds{}, false, "", "")
92-
_, _, err := client.ExtractChart("argo-cd", "0.7.1", "", false, 100, false)
92+
_, _, err := client.ExtractChart("argo-cd", "0.7.1", false, 100, false)
9393
require.Error(t, err, "error while iterating on tar reader: unexpected EOF")
9494
}
9595

9696
func Test_nativeHelmChart_ExtractChart_insecure(t *testing.T) {
9797
client := NewClient("https://argoproj.github.io/argo-helm", Creds{InsecureSkipVerify: true}, false, "", "")
98-
path, closer, err := client.ExtractChart("argo-cd", "0.7.1", "", false, math.MaxInt64, true)
98+
path, closer, err := client.ExtractChart("argo-cd", "0.7.1", false, math.MaxInt64, true)
9999
require.NoError(t, err)
100100
defer io.Close(closer)
101101
info, err := os.Stat(path)

util/helm/mocks/Client.go

Lines changed: 16 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)