Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/webhook/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func checkIfRepoExist(pac pac.RepositoryLister, repo *v1alpha1.Repository, ns st
}
for i := len(repositories) - 1; i >= 0; i-- {
repoFromCluster := repositories[i]
if repoFromCluster.Spec.URL == repo.Spec.URL &&
if strings.TrimRight(strings.TrimSpace(repoFromCluster.Spec.URL), "/") == strings.TrimRight(strings.TrimSpace(repo.Spec.URL), "/") &&
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zakisk ah I think this is a likely culprit for the issue you linked.

We could probably make this normalization a bit more consistent using something like the below (playground link), but this should work as well I think. Though if strings.TrimSpace() is necessary then it would still be necessary in addition to path.Clean().

func normalizeUrl(repoUrl string) (string, err) {
    u, err := url.Parse(repoUrl)
    if err != nil {
        return "", err
    }
    u.Path = path.Clean(u.Path)
    return u.String(), nil
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, @aThorp96 makes sense! feel free to raise PR or you want me to do?

(repoFromCluster.Name != repo.Name || repoFromCluster.Namespace != repo.Namespace) {
return true, nil
}
Expand Down
20 changes: 20 additions & 0 deletions pkg/webhook/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,26 @@ func TestReconcilerAdmit(t *testing.T) {
}),
allowed: true,
},
{
name: "reject repository URL with trailing slash",
repo: testnewrepo.NewRepo(testnewrepo.RepoTestcreationOpts{
Name: "test-run",
InstallNamespace: "namespace",
URL: "https://pac.test/already/installed/",
}),
allowed: false,
result: "repository already exists with URL: https://pac.test/already/installed/",
},
{
name: "reject repository URL with multiple trailing slashes",
repo: testnewrepo.NewRepo(testnewrepo.RepoTestcreationOpts{
Name: "test-run",
InstallNamespace: "namespace",
URL: "https://pac.test/already/installed//////",
}),
allowed: false,
result: "repository already exists with URL: https://pac.test/already/installed//////",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
34 changes: 34 additions & 0 deletions test/repository_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,37 @@ func TestOthersRepositoryCreation(t *testing.T) {
assert.Assert(t, err != nil)
assert.Equal(t, err.Error(), "admission webhook \"validation.pipelinesascode.tekton.dev\" denied the request: repository already exists with URL: https://pac.test/pac/app")
}

func TestOthersRepositoryCreationWithTrailingSlash(t *testing.T) {
ctx := context.TODO()
ctx, runcnx, _, _, err := ghtest.Setup(ctx, false, false)
assert.NilError(t, err)

targetNs := names.SimpleNameGenerator.RestrictLengthWithRandomSuffix("test-repo")
repo := &v1alpha1.Repository{
ObjectMeta: metav1.ObjectMeta{
Name: targetNs,
},
Spec: v1alpha1.RepositorySpec{
URL: "https://pac.test/pac/app",
},
}

defer repository.NSTearDown(ctx, t, runcnx, targetNs)
err = repository.CreateNS(ctx, targetNs, runcnx)
assert.NilError(t, err)
err = repository.CreateRepo(ctx, targetNs, runcnx, repo)
assert.NilError(t, err)

// create a new cr with same git url
targetNsNew := names.SimpleNameGenerator.RestrictLengthWithRandomSuffix("test-repo-new")
repo.Name = "test-repo-new"
repo.Spec.URL = "https://pac.test/pac/app/"

defer repository.NSTearDown(ctx, t, runcnx, targetNsNew)
err = repository.CreateNS(ctx, targetNsNew, runcnx)
assert.NilError(t, err)
err = repository.CreateRepo(ctx, targetNsNew, runcnx, repo)
assert.Assert(t, err != nil)
assert.Equal(t, err.Error(), "admission webhook \"validation.pipelinesascode.tekton.dev\" denied the request: repository already exists with URL: https://pac.test/pac/app/")
Comment thread
zakisk marked this conversation as resolved.
}
Loading