From dc8a93b3f61a47fabd24e407b439b2bddda07de4 Mon Sep 17 00:00:00 2001 From: Tomas Hozza Date: Mon, 2 Aug 2021 13:00:10 +0200 Subject: [PATCH] Weldr API: rename `checkImageTypeDenylist()` and return bool Rename the `checkImageTypeDenylist()` method to `isImageTypeAllowed()` and return boolean value instead of error. Signed-off-by: Tomas Hozza --- internal/weldr/api.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/internal/weldr/api.go b/internal/weldr/api.go index f47c410d4..4fa359bcb 100644 --- a/internal/weldr/api.go +++ b/internal/weldr/api.go @@ -385,11 +385,11 @@ func (api *API) openImageFile(composeId uuid.UUID, compose store.Compose) (io.Re return reader, size, nil } -// checkImageTypeDenylist checks the given ImageType and Distro names against -//the distro-specific ImageType Denylist provided to the API from configuration. -// If the given ImageType is not allowed the method returns an `error`. -// Otherwise `nil` is returned. -func (api *API) checkImageTypeDenylist(distroName, imageType string) error { +// isImageTypeAllowed checks the given ImageType and Distro names against +// the distro-specific ImageType Denylist provided to the API from configuration. +// If the given ImageType is not allowed the method returns an `false`. +// Otherwise `true` is returned. +func (api *API) isImageTypeAllowed(distroName, imageType string) bool { anyImageType := "*" anyDistro := "*" @@ -397,20 +397,20 @@ func (api *API) checkImageTypeDenylist(distroName, imageType string) error { if distroName == deniedDistro || deniedDistro == anyDistro { for _, deniedImgType := range deniedImgTypes { if imageType == deniedImgType || deniedImgType == anyImageType { - return fmt.Errorf("image type denied by configuration: %q", imageType) + return false } } } } - return nil + return true } // getImageType returns the ImageType for the selected distro // This is necessary because different distros support different image types, and the image // type may have a different package set than other distros. func (api *API) getImageType(distroName, imageType string) (distro.ImageType, error) { - if err := api.checkImageTypeDenylist(distroName, imageType); err != nil { - return nil, err + if !api.isImageTypeAllowed(distroName, imageType) { + return nil, fmt.Errorf("image type %q for distro %q is denied by configuration", imageType, distroName) } distro := api.getDistro(distroName) @@ -2522,7 +2522,7 @@ func (api *API) composeTypesHandler(writer http.ResponseWriter, request *http.Re } for _, format := range arch.ListImageTypes() { - if err := api.checkImageTypeDenylist(distroName, format); err != nil { + if !api.isImageTypeAllowed(distroName, format) { continue } reply.Types = append(reply.Types, composeType{format, true})