weldr: Check source POST for valid distros

This commit is contained in:
Brian C. Lane 2021-05-20 15:24:44 -07:00 committed by Ondřej Budai
parent 2b56e4d8e9
commit e2b170e754
2 changed files with 19 additions and 1 deletions

View file

@ -802,7 +802,6 @@ func (api *API) sourceNewHandler(writer http.ResponseWriter, request *http.Reque
err = errors_package.New("'url' field is missing from request")
}
}
if err != nil {
errors := responseError{
ID: "ProjectsError",
@ -812,6 +811,22 @@ func (api *API) sourceNewHandler(writer http.ResponseWriter, request *http.Reque
return
}
// If there is a list of distros, check to make sure they are valid
invalid := []string{}
for _, d := range source.SourceConfig().Distros {
if !common.IsStringInSortedSlice(api.distros.List(), d) {
invalid = append(invalid, d)
}
}
if len(invalid) > 0 {
errors := responseError{
ID: "ProjectsError",
Msg: "Invalid distributions: " + strings.Join(invalid, ","),
}
statusResponseError(writer, http.StatusBadRequest, errors)
return
}
// Is there an existing System Repo using this id?
for _, n := range api.systemRepoNames() {
if n == source.GetKey() {

View file

@ -186,6 +186,7 @@ func NewSourceConfigV1(id string, s store.SourceConfig) SourceConfigV1 {
sc.CheckGPG = s.CheckGPG
sc.CheckSSL = s.CheckSSL
sc.System = s.System
sc.Distros = s.Distros
return sc
}
@ -201,6 +202,7 @@ type SourceConfigV1 struct {
System bool `json:"system" toml:"system"`
Proxy string `json:"proxy,omitempty" toml:"proxy,omitempty"`
GPGUrls []string `json:"gpgkey_urls,omitempty" toml:"gpgkey_urls,omitempty"`
Distros []string `json:"distros,omitempty" toml:"distros,omitempty"`
}
// Key returns the key, .ID in this case
@ -226,6 +228,7 @@ func (s SourceConfigV1) SourceConfig() (ssc store.SourceConfig) {
ssc.URL = s.URL
ssc.CheckGPG = s.CheckGPG
ssc.CheckSSL = s.CheckSSL
ssc.Distros = s.Distros
return ssc
}