mirror of
https://github.com/argoproj/argo-cd.git
synced 2026-03-27 10:58:49 +01:00
fix: wrong installation id returned from cache (#26969)
Signed-off-by: Zach Aller <zach_aller@intuit.com>
This commit is contained in:
@@ -685,10 +685,11 @@ func DiscoverGitHubAppInstallationID(ctx context.Context, appId int64, privateKe
|
||||
opts.Page = resp.NextPage
|
||||
}
|
||||
|
||||
// Cache all installation IDs
|
||||
// Cache each installation under its account's key so multiple orgs do not overwrite each other.
|
||||
for _, installation := range allInstallations {
|
||||
if installation.Account != nil && installation.Account.Login != nil && installation.ID != nil {
|
||||
githubInstallationIdCache.Set(cacheKey, *installation.ID, gocache.DefaultExpiration)
|
||||
instKey := fmt.Sprintf("%s:%s:%d", strings.ToLower(*installation.Account.Login), domain, appId)
|
||||
githubInstallationIdCache.Set(instKey, *installation.ID, gocache.DefaultExpiration)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -600,6 +600,35 @@ func TestDiscoverGitHubAppInstallationID(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, int64(98765), actualId)
|
||||
})
|
||||
|
||||
t.Run("returns correct installation ID when app is installed on multiple orgs", func(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if strings.HasSuffix(r.URL.Path, "/app/installations") {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
//nolint:errcheck
|
||||
json.NewEncoder(w).Encode([]map[string]any{
|
||||
{"id": 11111, "account": map[string]any{"login": "org-alpha"}},
|
||||
{"id": 22222, "account": map[string]any{"login": "target-org"}},
|
||||
{"id": 33333, "account": map[string]any{"login": "org-gamma"}},
|
||||
})
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
t.Cleanup(func() {
|
||||
domain, _ := domainFromBaseURL(server.URL)
|
||||
for _, org := range []string{"org-alpha", "target-org", "org-gamma"} {
|
||||
githubInstallationIdCache.Delete(fmt.Sprintf("%s:%s:%d", org, domain, 12345))
|
||||
}
|
||||
})
|
||||
|
||||
ctx := context.Background()
|
||||
actualId, err := DiscoverGitHubAppInstallationID(ctx, 12345, fakeGitHubAppPrivateKey, server.URL, "target-org")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, int64(22222), actualId, "should return the installation ID for the requested org, not the last one in the list")
|
||||
})
|
||||
}
|
||||
|
||||
func TestExtractOrgFromRepoURL(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user