weldr: Add check for deleting system sources

It was passing it through to the non-system delete function
and not returning an error. This checks for system repos first and
returns a 400, SystemSource error response if it is in the system list.
This commit is contained in:
Brian C. Lane 2020-05-27 15:37:42 -07:00 committed by Tom Gundersen
parent 37258803b4
commit e37b513560
2 changed files with 24 additions and 6 deletions

View file

@ -526,7 +526,7 @@ func TestDeleteSystemSourcesV0(t *testing.T) {
// try removing system source
resp, err := DeleteSourceV0(testState.socket, repo_name)
require.NoError(t, err, "DELETE source failed with a client error")
require.True(t, resp.Status, "DELETE source failed: %#v", resp)
require.False(t, resp.Status, "DELETE system source test failed: %#v", resp)
// verify that system sources are still there
list, api, err := ListSourcesV0(testState.socket)
@ -552,7 +552,7 @@ func TestDeleteSystemSourcesV1(t *testing.T) {
// try removing system source
resp, err := DeleteSourceV1(testState.socket, repo_name)
require.NoError(t, err, "DELETE source failed with a client error")
require.True(t, resp.Status, "DELETE source failed: %#v", resp)
require.False(t, resp.Status, "DELETE system source test failed: %#v", resp)
// verify that system sources are still there
list, api, err := ListSourcesV1(testState.socket)

View file

@ -48,6 +48,15 @@ type API struct {
compatOutputDir string
}
// systemRepoIDs returns a list of the system repos
// NOTE: The system repos have no concept of id vs. name so the id is returned
func (api *API) systemRepoNames() (names []string) {
for _, repo := range api.repos {
names = append(names, repo.Name)
}
return names
}
var ValidBlueprintName = regexp.MustCompile(`^[a-zA-Z0-9._-]+$`)
func New(rpmmd rpmmd.RPMMD, arch distro.Arch, distro distro.Distro, repos []rpmmd.RepoConfig, logger *log.Logger, store *store.Store, workers *worker.Server, compatOutputDir string) *API {
@ -356,10 +365,7 @@ func (api *API) sourceListHandler(writer http.ResponseWriter, request *http.Requ
} else {
names = api.store.ListSourcesByName()
}
for _, repo := range api.repos {
names = append(names, repo.Name)
}
names = append(names, api.systemRepoNames()...)
err := json.NewEncoder(writer).Encode(reply{
Sources: names,
@ -625,6 +631,18 @@ func (api *API) sourceDeleteHandler(writer http.ResponseWriter, request *http.Re
return
}
// Check for system repos and return an error
for _, id := range api.systemRepoNames() {
if id == name[0][1:] {
errors := responseError{
ID: "SystemSource",
Msg: id + " is a system source, it cannot be deleted.",
}
statusResponseError(writer, http.StatusBadRequest, errors)
return
}
}
// Only delete the first name, which will have a / at the start because of the /*source route
if isRequestVersionAtLeast(params, 1) {
api.store.DeleteSourceByID(name[0][1:])