2018-06-11 13:13:19 +00:00
|
|
|
package stacks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2018-09-10 10:01:38 +00:00
|
|
|
httperror "github.com/portainer/libhttp/error"
|
2019-03-21 01:20:14 +00:00
|
|
|
"github.com/portainer/portainer/api"
|
|
|
|
"github.com/portainer/portainer/api/http/security"
|
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
|
|
|
|
FileService portainer.FileService
|
|
|
|
GitService portainer.GitService
|
|
|
|
StackService portainer.StackService
|
|
|
|
EndpointService portainer.EndpointService
|
|
|
|
ResourceControlService portainer.ResourceControlService
|
|
|
|
RegistryService portainer.RegistryService
|
|
|
|
DockerHubService portainer.DockerHubService
|
|
|
|
SwarmStackManager portainer.SwarmStackManager
|
|
|
|
ComposeStackManager portainer.ComposeStackManager
|
2019-10-07 03:12:21 +00:00
|
|
|
SettingsService portainer.SettingsService
|
2019-11-12 23:41:42 +00:00
|
|
|
UserService portainer.UserService
|
|
|
|
ExtensionService portainer.ExtensionService
|
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)
|
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) {
|
|
|
|
if securityContext.IsAdmin {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
userTeamIDs := make([]portainer.TeamID, 0)
|
|
|
|
for _, membership := range securityContext.UserMemberships {
|
|
|
|
userTeamIDs = append(userTeamIDs, membership.TeamID)
|
|
|
|
}
|
|
|
|
|
|
|
|
if resourceControl != nil && portainer.UserCanAccessResource(securityContext.UserID, userTeamIDs, resourceControl) {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := handler.ExtensionService.Extension(portainer.RBACExtension)
|
|
|
|
if err == portainer.ErrObjectNotFound {
|
|
|
|
return false, nil
|
|
|
|
} else if err != nil && err != portainer.ErrObjectNotFound {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := handler.UserService.User(securityContext.UserID)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, ok := user.EndpointAuthorizations[endpointID][portainer.EndpointResourcesAccess]
|
|
|
|
if ok {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|