Skip to content

Commit

Permalink
Reject create/update with a URL for upload repos
Browse files Browse the repository at this point in the history
  • Loading branch information
jlsherrill committed Jul 11, 2024
1 parent 0b6bead commit f58b96d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
10 changes: 7 additions & 3 deletions pkg/dao/repository_configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (r repositoryConfigDaoImpl) Create(ctx context.Context, newRepoReq api.Repo

ApiFieldsToModel(newRepoReq, &newRepoConfig, &newRepo)

if newRepo.URL == "" {
if newRepo.URL == "" || newRepo.Origin == config.OriginUpload {
if err := r.db.WithContext(ctx).Create(&newRepo).Error; err != nil {
return api.RepositoryResponse{}, DBErrorToApi(err)
}
Expand Down Expand Up @@ -609,10 +609,14 @@ func (r repositoryConfigDaoImpl) Update(ctx context.Context, orgID, uuid string,
}
ApiUpdateFieldsToModel(repoParams, &repoConfig, &repo)

// If URL is included in params, search for existing
if repoConfig.Repository.Origin == config.OriginUpload && repoParams.URL != nil && *repoParams.URL != "" {
return &ce.DaoError{BadValidation: true, Message: "Cannot set URL on upload repositories"}
}

// If URL is included in params, and not an upload repo, search for existing
// Repository record, or create a new one.
// Then replace existing Repository/RepoConfig association.
if repoParams.URL != nil {
if repoParams.URL != nil && repoConfig.Repository.Origin != config.OriginUpload {
cleanedUrl := models.CleanupURL(*repoParams.URL)
err = tx.FirstOrCreate(&repo, "url = ?", cleanedUrl).Error
if err != nil {
Expand Down
29 changes: 29 additions & 0 deletions pkg/dao/repository_configs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,35 @@ func (suite *RepositoryConfigSuite) TestCreateUploadNoSnap() {
assert.ErrorContains(suite.T(), err, "URL cannot be specified for upload repositories.")
}

func (suite *RepositoryConfigSuite) TestCreateUpdateUploadWithExistingURL() {
rcDao := GetRepositoryConfigDao(suite.tx, suite.mockPulpClient)
url := "http://example.com/testcreateuploadexistingurl/"
err := suite.tx.Create(&models.Repository{URL: url}).Error
require.NoError(suite.T(), err)

repo, err := rcDao.Create(context.Background(), api.RepositoryRequest{
OrgID: pointy.Pointer("123"),
Origin: pointy.Pointer("upload"),
Name: pointy.Pointer(url),
URL: pointy.Pointer(url),
Snapshot: pointy.Pointer(true),
})
assert.NotNil(suite.T(), err)
assert.Empty(suite.T(), repo.UUID)

repo, err = rcDao.Create(context.Background(), api.RepositoryRequest{
OrgID: pointy.Pointer("123"),
Origin: pointy.Pointer("upload"),
Name: pointy.Pointer(url),
Snapshot: pointy.Pointer(true),
})
assert.Nil(suite.T(), err)
assert.NotEmpty(suite.T(), repo.UUID)

_, err = rcDao.Update(context.Background(), repo.OrgID, repo.UUID, api.RepositoryUpdateRequest{URL: &url})
assert.NotNil(suite.T(), err)
}

func (suite *RepositoryConfigSuite) TestCreateTwiceWithNoSlash() {
toCreate := api.RepositoryRequest{
Name: pointy.String(""),
Expand Down

0 comments on commit f58b96d

Please sign in to comment.