2018-06-11 13:13:19 +00:00
|
|
|
package stacks
|
|
|
|
|
|
|
|
import (
|
2020-07-07 21:57:52 +00:00
|
|
|
"errors"
|
2018-06-11 13:13:19 +00:00
|
|
|
"net/http"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2018-09-10 10:01:38 +00:00
|
|
|
httperror "github.com/portainer/libhttp/error"
|
2020-07-07 21:57:52 +00:00
|
|
|
"github.com/portainer/portainer/api"
|
|
|
|
bolterrors "github.com/portainer/portainer/api/bolt/errors"
|
2019-03-21 01:20:14 +00:00
|
|
|
"github.com/portainer/portainer/api/http/security"
|
2020-06-16 07:58:16 +00:00
|
|
|
"github.com/portainer/portainer/api/internal/authorization"
|
2018-06-11 13:13:19 +00:00
|
|
|
)
|
|
|
|
|
2020-07-07 21:57:52 +00:00
|
|
|
var (
|
|
|
|
errStackAlreadyExists = errors.New("A stack already exists with this name")
|
|
|
|
errStackNotExternal = errors.New("Not an external stack")
|
|
|
|
)
|
|
|
|
|
2018-06-11 13:13:19 +00:00
|
|
|
// Handler is the HTTP handler used to handle stack operations.
|
|
|
|
type Handler struct {
|
|
|
|
stackCreationMutex *sync.Mutex
|
|
|
|
stackDeletionMutex *sync.Mutex
|
2018-06-18 09:56:31 +00:00
|
|
|
requestBouncer *security.RequestBouncer
|
2018-06-11 13:13:19 +00:00
|
|
|
*mux.Router
|
2020-05-20 05:23:15 +00:00
|
|
|
DataStore portainer.DataStore
|
|
|
|
FileService portainer.FileService
|
|
|
|
GitService portainer.GitService
|
|
|
|
SwarmStackManager portainer.SwarmStackManager
|
|
|
|
ComposeStackManager portainer.ComposeStackManager
|
2020-07-05 23:21:03 +00:00
|
|
|
KubernetesDeployer portainer.KubernetesDeployer
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewHandler creates a handler to manage stack operations.
|
|
|
|
func NewHandler(bouncer *security.RequestBouncer) *Handler {
|
|
|
|
h := &Handler{
|
|
|
|
Router: mux.NewRouter(),
|
|
|
|
stackCreationMutex: &sync.Mutex{},
|
|
|
|
stackDeletionMutex: &sync.Mutex{},
|
2018-06-18 09:56:31 +00:00
|
|
|
requestBouncer: bouncer,
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
h.Handle("/stacks",
|
2019-10-07 03:10:51 +00:00
|
|
|
bouncer.AuthenticatedAccess(httperror.LoggerHandler(h.stackCreate))).Methods(http.MethodPost)
|
2018-06-11 13:13:19 +00:00
|
|
|
h.Handle("/stacks",
|
2019-10-07 03:10:51 +00:00
|
|
|
bouncer.AuthenticatedAccess(httperror.LoggerHandler(h.stackList))).Methods(http.MethodGet)
|
2018-06-11 13:13:19 +00:00
|
|
|
h.Handle("/stacks/{id}",
|
2019-10-07 03:10:51 +00:00
|
|
|
bouncer.AuthenticatedAccess(httperror.LoggerHandler(h.stackInspect))).Methods(http.MethodGet)
|
2018-06-11 13:13:19 +00:00
|
|
|
h.Handle("/stacks/{id}",
|
2019-10-07 03:10:51 +00:00
|
|
|
bouncer.AuthenticatedAccess(httperror.LoggerHandler(h.stackDelete))).Methods(http.MethodDelete)
|
2018-06-11 13:13:19 +00:00
|
|
|
h.Handle("/stacks/{id}",
|
2019-10-07 03:10:51 +00:00
|
|
|
bouncer.AuthenticatedAccess(httperror.LoggerHandler(h.stackUpdate))).Methods(http.MethodPut)
|
2018-06-11 13:13:19 +00:00
|
|
|
h.Handle("/stacks/{id}/file",
|
2019-10-07 03:10:51 +00:00
|
|
|
bouncer.AuthenticatedAccess(httperror.LoggerHandler(h.stackFile))).Methods(http.MethodGet)
|
2018-06-19 15:28:40 +00:00
|
|
|
h.Handle("/stacks/{id}/migrate",
|
2019-10-07 03:10:51 +00:00
|
|
|
bouncer.AuthenticatedAccess(httperror.LoggerHandler(h.stackMigrate))).Methods(http.MethodPost)
|
2020-08-03 22:18:53 +00:00
|
|
|
h.Handle("/stacks/{id}/start",
|
|
|
|
bouncer.AuthenticatedAccess(httperror.LoggerHandler(h.stackStart))).Methods(http.MethodPost)
|
|
|
|
h.Handle("/stacks/{id}/stop",
|
|
|
|
bouncer.AuthenticatedAccess(httperror.LoggerHandler(h.stackStop))).Methods(http.MethodPost)
|
2018-06-11 13:13:19 +00:00
|
|
|
return h
|
|
|
|
}
|
2019-11-12 23:41:42 +00:00
|
|
|
|
|
|
|
func (handler *Handler) userCanAccessStack(securityContext *security.RestrictedRequestContext, endpointID portainer.EndpointID, resourceControl *portainer.ResourceControl) (bool, error) {
|
2020-07-27 07:11:32 +00:00
|
|
|
user, err := handler.DataStore.User().User(securityContext.UserID)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
2019-11-12 23:41:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
userTeamIDs := make([]portainer.TeamID, 0)
|
|
|
|
for _, membership := range securityContext.UserMemberships {
|
|
|
|
userTeamIDs = append(userTeamIDs, membership.TeamID)
|
|
|
|
}
|
|
|
|
|
2020-06-16 07:58:16 +00:00
|
|
|
if resourceControl != nil && authorization.UserCanAccessResource(securityContext.UserID, userTeamIDs, resourceControl) {
|
2019-11-12 23:41:42 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2020-07-27 07:11:32 +00:00
|
|
|
return handler.userIsAdminOrEndpointAdmin(user, endpointID)
|
2019-11-12 23:41:42 +00:00
|
|
|
}
|
2020-07-22 18:38:45 +00:00
|
|
|
|
|
|
|
func (handler *Handler) userIsAdminOrEndpointAdmin(user *portainer.User, endpointID portainer.EndpointID) (bool, error) {
|
|
|
|
isAdmin := user.Role == portainer.AdministratorRole
|
|
|
|
if isAdmin {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
rbacExtension, err := handler.DataStore.Extension().Extension(portainer.RBACExtension)
|
|
|
|
if err != nil && err != bolterrors.ErrObjectNotFound {
|
|
|
|
return false, errors.New("Unable to verify if RBAC extension is loaded")
|
|
|
|
}
|
|
|
|
|
|
|
|
if rbacExtension == nil {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
_, endpointResourceAccess := user.EndpointAuthorizations[portainer.EndpointID(endpointID)][portainer.EndpointResourcesAccess]
|
|
|
|
|
|
|
|
return endpointResourceAccess, nil
|
|
|
|
}
|
2020-07-27 07:11:32 +00:00
|
|
|
|
|
|
|
func (handler *Handler) userCanCreateStack(securityContext *security.RestrictedRequestContext, endpointID portainer.EndpointID) (bool, error) {
|
|
|
|
user, err := handler.DataStore.User().User(securityContext.UserID)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return handler.userIsAdminOrEndpointAdmin(user, endpointID)
|
|
|
|
}
|