Skip to content

Commit a3624a3

Browse files
gcp-cherry-pick-bot[bot]andrii-korotkov-verkada
andauthoredDec 10, 2024··
fix: Allow to delete repos with invalid urls (#20921) (#20975) (#21116)
* fix: Allow to delete repos with invalid urls (#20921) Fixes #20921 Normalization of repo url is attempted during repo deletion before comparison with existing repos. Before this change, it'd fail on invalid urls and return "", resulting in permission denied. This ended up in a state where people can add repos with invalid urls but couldn't delete them. Return raw repo url if url parsing failed. Add a test to validate the deletion can be performed and make sure it fails without the main change. This needs to be cherry-picked to v2.11-2.13 * Don't modify the original NormalizeGitURL --------- Signed-off-by: Andrii Korotkov <andrii.korotkov@verkada.com> Co-authored-by: Andrii Korotkov <137232734+andrii-korotkov-verkada@users.noreply.github.com>

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed
 

‎server/repository/repository_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121

2222
"github.com/argoproj/argo-cd/v2/common"
2323
"github.com/argoproj/argo-cd/v2/pkg/apiclient/repository"
24+
repositorypkg "github.com/argoproj/argo-cd/v2/pkg/apiclient/repository"
2425
appsv1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
2526
fakeapps "github.com/argoproj/argo-cd/v2/pkg/client/clientset/versioned/fake"
2627
appinformer "github.com/argoproj/argo-cd/v2/pkg/client/informers/externalversions"
@@ -1057,3 +1058,35 @@ func TestGetRepository(t *testing.T) {
10571058
})
10581059
}
10591060
}
1061+
1062+
func TestDeleteRepository(t *testing.T) {
1063+
repositories := map[string]string{
1064+
"valid": "https://bitbucket.org/workspace/repo.git",
1065+
// Check a wrongly formatter repo as well, see https://github.com/argoproj/argo-cd/issues/20921
1066+
"invalid": "git clone https://bitbucket.org/workspace/repo.git",
1067+
}
1068+
1069+
kubeclientset := fake.NewSimpleClientset(&argocdCM, &argocdSecret)
1070+
settingsMgr := settings.NewSettingsManager(context.Background(), kubeclientset, testNamespace)
1071+
1072+
for name, repo := range repositories {
1073+
t.Run(name, func(t *testing.T) {
1074+
repoServerClient := mocks.RepoServerServiceClient{}
1075+
repoServerClient.On("TestRepository", mock.Anything, mock.Anything).Return(&apiclient.TestRepositoryResponse{}, nil)
1076+
1077+
repoServerClientset := mocks.Clientset{RepoServerServiceClient: &repoServerClient}
1078+
enforcer := newEnforcer(kubeclientset)
1079+
1080+
db := &dbmocks.ArgoDB{}
1081+
db.On("DeleteRepository", context.TODO(), repo, "default").Return(nil)
1082+
db.On("ListRepositories", context.TODO()).Return([]*appsv1.Repository{{Repo: repo, Project: "default"}}, nil)
1083+
db.On("GetRepository", context.TODO(), repo, "default").Return(&appsv1.Repository{Repo: repo, Project: "default"}, nil)
1084+
appLister, projLister := newAppAndProjLister(defaultProj)
1085+
1086+
s := NewServer(&repoServerClientset, db, enforcer, newFixtures().Cache, appLister, projLister, testNamespace, settingsMgr)
1087+
resp, err := s.DeleteRepository(context.TODO(), &repository.RepoQuery{Repo: repo, AppProject: "default"})
1088+
require.NoError(t, err)
1089+
assert.Equal(t, repositorypkg.RepoResponse{}, *resp)
1090+
})
1091+
}
1092+
}

‎util/git/git.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,21 @@ func IsTruncatedCommitSHA(sha string) bool {
3535

3636
// SameURL returns whether or not the two repository URLs are equivalent in location
3737
func SameURL(leftRepo, rightRepo string) bool {
38-
normalLeft := NormalizeGitURL(leftRepo)
39-
normalRight := NormalizeGitURL(rightRepo)
38+
normalLeft := NormalizeGitURLAllowInvalid(leftRepo)
39+
normalRight := NormalizeGitURLAllowInvalid(rightRepo)
4040
return normalLeft != "" && normalRight != "" && normalLeft == normalRight
4141
}
4242

43+
// Similar to NormalizeGitURL, except returning an original url if the url is invalid.
44+
// Needed to allow a deletion of repos with invalid urls. See https://github.com/argoproj/argo-cd/issues/20921.
45+
func NormalizeGitURLAllowInvalid(repo string) string {
46+
normalized := NormalizeGitURL(repo)
47+
if normalized == "" {
48+
return repo
49+
}
50+
return normalized
51+
}
52+
4353
// NormalizeGitURL normalizes a git URL for purposes of comparison, as well as preventing redundant
4454
// local clones (by normalizing various forms of a URL to a consistent location).
4555
// Prefer using SameURL() over this function when possible. This algorithm may change over time

0 commit comments

Comments
 (0)
Please sign in to comment.