Skip to content

Commit 51c1261

Browse files
authored
Merge pull request #179 from projectsyn/report-error-invalid-url
Report error if tenant registration contains an invalid git url
2 parents 9d36cb1 + 939dfab commit 51c1261

2 files changed

Lines changed: 28 additions & 19 deletions

File tree

pkg/api/utils.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,11 @@ func NewCRDFromAPITenant(apiTenant Tenant) (*synv1alpha1.Tenant, error) {
131131
}
132132

133133
if apiTenant.GitRepo != nil {
134-
tenant.Spec.GitRepoTemplate = newGitRepoTemplate(&apiTenant.GitRepo.GitRepo, string(apiTenant.Id))
134+
tmpl, err := newGitRepoTemplate(&apiTenant.GitRepo.GitRepo, string(apiTenant.Id))
135+
if err != nil {
136+
return nil, fmt.Errorf("failed to create git repo template: %w", err)
137+
}
138+
tenant.Spec.GitRepoTemplate = tmpl
135139
}
136140

137141
SyncCRDFromAPITenant(apiTenant.TenantProperties, tenant)
@@ -276,13 +280,17 @@ func NewCRDFromAPICluster(apiCluster Cluster) (*synv1alpha1.Cluster, error) {
276280
},
277281
}
278282

279-
cluster.Spec.GitRepoTemplate = newGitRepoTemplate(apiCluster.GitRepo, string(apiCluster.Id))
283+
tmpl, err := newGitRepoTemplate(apiCluster.GitRepo, string(apiCluster.Id))
284+
if err != nil {
285+
return nil, fmt.Errorf("failed to create git repo template: %w", err)
286+
}
287+
cluster.Spec.GitRepoTemplate = tmpl
280288

281289
if apiCluster.GitRepo != nil && apiCluster.GitRepo.HostKeys != nil {
282290
cluster.Spec.GitHostKeys = *apiCluster.GitRepo.HostKeys
283291
}
284292

285-
err := SyncCRDFromAPICluster(apiCluster.ClusterProperties, cluster)
293+
err = SyncCRDFromAPICluster(apiCluster.ClusterProperties, cluster)
286294
return cluster, err
287295
}
288296

@@ -366,23 +374,23 @@ func SyncCRDFromAPICluster(source ClusterProperties, target *synv1alpha1.Cluster
366374
return nil
367375
}
368376

369-
func newGitRepoTemplate(repo *GitRepo, name string) *synv1alpha1.GitRepoTemplate {
377+
func newGitRepoTemplate(repo *GitRepo, name string) (*synv1alpha1.GitRepoTemplate, error) {
370378
if repo == nil {
371379
// No git info was specified
372-
return nil
380+
return nil, nil
373381
}
374382

375383
if repo.Type == nil || *repo.Type != string(synv1alpha1.UnmanagedRepoType) {
376384
if repo.Url != nil && len(*repo.Url) > 0 {
377385
// It's not unmanaged and the URL was specified, take it apart
378386
url, err := url.Parse(*repo.Url)
379387
if err != nil {
380-
return nil
388+
return nil, fmt.Errorf("failed to parse git repo URL: %w", err)
381389
}
382390
pathParts := strings.Split(url.Path, "/")
383391
pathParts = pathParts[1:]
384392
if len(pathParts) < 2 {
385-
return nil
393+
return nil, fmt.Errorf("failed to parse git repo URL, expected 2+ path elements in '%s'", url.Path)
386394
}
387395
// remove .git extension
388396
repoName := strings.ReplaceAll(pathParts[len(pathParts)-1], ".git", "")
@@ -391,13 +399,13 @@ func newGitRepoTemplate(repo *GitRepo, name string) *synv1alpha1.GitRepoTemplate
391399
RepoType: synv1alpha1.AutoRepoType,
392400
Path: repoPath,
393401
RepoName: repoName,
394-
}
402+
}, nil
395403
}
396404
} else if repo.Type != nil {
397405
// Repo is unmanaged, remove name and path
398406
return &synv1alpha1.GitRepoTemplate{
399407
RepoType: synv1alpha1.UnmanagedRepoType,
400-
}
408+
}, nil
401409
}
402-
return nil
410+
return nil, nil
403411
}

pkg/api/utils_test.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,18 @@ func TestRepoConversionDefaultAuto(t *testing.T) {
2121
Url: nil,
2222
}
2323
repoName := "c-dshfjuhrtu"
24-
repoTemplate := newGitRepoTemplate(apiRepo, repoName)
24+
repoTemplate, _ := newGitRepoTemplate(apiRepo, repoName)
25+
assert.Nil(t, repoTemplate)
26+
repoTemplate, _ = newGitRepoTemplate(apiRepo, repoName)
2527
assert.Nil(t, repoTemplate)
26-
assert.Nil(t, newGitRepoTemplate(nil, repoName))
2728
}
2829

2930
func TestRepoConversionUnmanagedo(t *testing.T) {
3031
apiRepo := &GitRepo{
3132
Type: pointer.ToString("unmanaged"),
3233
Url: pointer.ToString("ssh://git@some.host/path/to/repo.git"),
3334
}
34-
repoTemplate := newGitRepoTemplate(apiRepo, "some-name")
35+
repoTemplate, _ := newGitRepoTemplate(apiRepo, "some-name")
3536
assert.Empty(t, repoTemplate.RepoName)
3637
assert.Empty(t, repoTemplate.Path)
3738
}
@@ -43,7 +44,7 @@ func TestRepoConversionSpecSubGroupPath(t *testing.T) {
4344
Type: pointer.ToString("auto"),
4445
Url: pointer.ToString("ssh://git@some.host/" + repoPath + "/" + repoName + ".git"),
4546
}
46-
repoTemplate := newGitRepoTemplate(apiRepo, "some-name")
47+
repoTemplate, _ := newGitRepoTemplate(apiRepo, "some-name")
4748
assert.Equal(t, repoName, repoTemplate.RepoName)
4849
assert.Equal(t, repoPath, repoTemplate.Path)
4950
assert.Empty(t, repoTemplate.APISecretRef.Name)
@@ -56,7 +57,7 @@ func TestRepoConversionSpecPath(t *testing.T) {
5657
Type: pointer.ToString("auto"),
5758
Url: pointer.ToString("ssh://git@some.host/" + repoPath + "/" + repoName + ".git"),
5859
}
59-
repoTemplate := newGitRepoTemplate(apiRepo, "some-name")
60+
repoTemplate, _ := newGitRepoTemplate(apiRepo, "some-name")
6061
assert.Equal(t, repoName, repoTemplate.RepoName)
6162
assert.Equal(t, repoPath, repoTemplate.Path)
6263
assert.Empty(t, repoTemplate.APISecretRef.Name)
@@ -66,13 +67,13 @@ func TestRepoConversionFail(t *testing.T) {
6667
apiRepo := &GitRepo{
6768
Url: pointer.ToString("://git@some.host/group/example.git"),
6869
}
69-
repoTemplate := newGitRepoTemplate(apiRepo, "some-name")
70-
assert.Nil(t, repoTemplate)
70+
_, err := newGitRepoTemplate(apiRepo, "some-name")
71+
assert.Error(t, err)
7172

72-
repoTemplate = newGitRepoTemplate(&GitRepo{
73+
_, err = newGitRepoTemplate(&GitRepo{
7374
Url: pointer.ToString("ssh://git@some.host/example.git"),
7475
}, "test")
75-
assert.Nil(t, repoTemplate)
76+
assert.Error(t, err)
7677
}
7778

7879
func TestGenerateClusterID(t *testing.T) {

0 commit comments

Comments
 (0)