0
Fork 0
mirror of https://github.com/project-zot/zot.git synced 2024-12-30 22:34:13 -05:00
zot/pkg/extensions/extension_userprefs.go
Andrei Aaron 77149aa85c
refactor(extensions)!: refactor the extensions URLs and errors (#1636)
BREAKING CHANGE: The functionality provided by the mgmt endpoint has beed redesigned - see details below
BREAKING CHANGE: The API keys endpoint has been moved -  see details below
BREAKING CHANGE: The mgmt extension config has been removed - endpoint is now enabled by having both the search and the ui extensions enabled
BREAKING CHANGE: The API keys configuration has been moved from extensions to http>auth>apikey

mgmt and imagetrust extensions:
- separate the _zot/ext/mgmt into 3 separate endpoints: _zot/ext/auth, _zot/ext/notation, _zot/ext/cosign
- signature verification logic is in a separate `imagetrust` extension
- better hanling or errors in case of signature uploads: logging and error codes (more 400 and less 500 errors)
- add authz on signature uploads (and add a new middleware in common for this purpose)
- remove the mgmt extension configuration - it is now enabled if the UI and the search extensions are enabled

userprefs estension:
- userprefs are enabled if both search and ui extensions are enabled (as opposed to just search)

apikey extension is removed and logic moved into the api folder
- Move apikeys code out of pkg/extensions and into pkg/api
- Remove apikey configuration options from the extensions configuration and move it inside the http auth section
- remove the build label apikeys

other changes:
- move most of the logic adding handlers to the extensions endpoints out of routes.go and into the extensions files.
- add warnings in case the users are still using configurations with the obsolete settings for mgmt and api keys
- add a new function in the extension package which could be a single point of starting backgroud tasks for all extensions
- more clear methods for verifying specific extensions are enabled
- fix http methods paired with the UI handlers
- rebuild swagger docs

Signed-off-by: Andrei Aaron <aaaron@luxoft.com>
2023-08-02 21:58:34 +03:00

159 lines
3.9 KiB
Go

//go:build userprefs
// +build userprefs
package extensions
import (
"errors"
"net/http"
"github.com/gorilla/mux"
zerr "zotregistry.io/zot/errors"
"zotregistry.io/zot/pkg/api/config"
"zotregistry.io/zot/pkg/api/constants"
zcommon "zotregistry.io/zot/pkg/common"
"zotregistry.io/zot/pkg/log"
mTypes "zotregistry.io/zot/pkg/meta/types"
)
const (
ToggleRepoBookmarkAction = "toggleBookmark"
ToggleRepoStarAction = "toggleStar"
)
func IsBuiltWithUserPrefsExtension() bool {
return true
}
func SetupUserPreferencesRoutes(conf *config.Config, router *mux.Router,
metaDB mTypes.MetaDB, log log.Logger,
) {
if !conf.AreUserPrefsEnabled() {
log.Info().Msg("skip enabling the user preferences route as the config prerequisites are not met")
return
}
log.Info().Msg("setting up user preferences routes")
allowedMethods := zcommon.AllowedMethods(http.MethodPut)
userPrefsRouter := router.PathPrefix(constants.ExtUserPrefs).Subrouter()
userPrefsRouter.Use(zcommon.CORSHeadersMiddleware(conf.HTTP.AllowOrigin))
userPrefsRouter.Use(zcommon.AddExtensionSecurityHeaders())
userPrefsRouter.Use(zcommon.ACHeadersMiddleware(conf, allowedMethods...))
userPrefsRouter.Methods(allowedMethods...).Handler(HandleUserPrefs(metaDB, log))
log.Info().Msg("finished setting up user preferences routes")
}
// Repo preferences godoc
// @Summary Add bookmarks/stars info
// @Description Add bookmarks/stars info
// @Router /v2/_zot/ext/userprefs [put]
// @Accept json
// @Produce json
// @Param action query string true "specify action" Enums(toggleBookmark, toggleStar)
// @Param repo query string true "repository name"
// @Success 200 {string} string "ok"
// @Failure 404 {string} string "not found"
// @Failure 403 {string} string "forbidden"
// @Failure 500 {string} string "internal server error"
// @Failure 400 {string} string "bad request".
func HandleUserPrefs(metaDB mTypes.MetaDB, log log.Logger) http.Handler {
return http.HandlerFunc(func(rsp http.ResponseWriter, req *http.Request) {
if !zcommon.QueryHasParams(req.URL.Query(), []string{"action"}) {
rsp.WriteHeader(http.StatusBadRequest)
return
}
action := req.URL.Query().Get("action")
switch action {
case ToggleRepoBookmarkAction:
PutBookmark(rsp, req, metaDB, log) //nolint:contextcheck
return
case ToggleRepoStarAction:
PutStar(rsp, req, metaDB, log) //nolint:contextcheck
return
default:
rsp.WriteHeader(http.StatusBadRequest)
return
}
})
}
func PutStar(rsp http.ResponseWriter, req *http.Request, metaDB mTypes.MetaDB, log log.Logger) {
if !zcommon.QueryHasParams(req.URL.Query(), []string{"repo"}) {
rsp.WriteHeader(http.StatusBadRequest)
return
}
repo := req.URL.Query().Get("repo")
if repo == "" {
rsp.WriteHeader(http.StatusNotFound)
return
}
_, err := metaDB.ToggleStarRepo(req.Context(), repo)
if err != nil {
if errors.Is(err, zerr.ErrRepoMetaNotFound) {
rsp.WriteHeader(http.StatusNotFound)
return
} else if errors.Is(err, zerr.ErrUserDataNotAllowed) {
rsp.WriteHeader(http.StatusForbidden)
return
}
rsp.WriteHeader(http.StatusInternalServerError)
return
}
rsp.WriteHeader(http.StatusOK)
}
func PutBookmark(rsp http.ResponseWriter, req *http.Request, metaDB mTypes.MetaDB, log log.Logger) {
if !zcommon.QueryHasParams(req.URL.Query(), []string{"repo"}) {
rsp.WriteHeader(http.StatusBadRequest)
return
}
repo := req.URL.Query().Get("repo")
if repo == "" {
rsp.WriteHeader(http.StatusNotFound)
return
}
_, err := metaDB.ToggleBookmarkRepo(req.Context(), repo)
if err != nil {
if errors.Is(err, zerr.ErrRepoMetaNotFound) {
rsp.WriteHeader(http.StatusNotFound)
return
} else if errors.Is(err, zerr.ErrUserDataNotAllowed) {
rsp.WriteHeader(http.StatusForbidden)
return
}
rsp.WriteHeader(http.StatusInternalServerError)
return
}
rsp.WriteHeader(http.StatusOK)
}