2018-06-11 13:13:19 +00:00
|
|
|
package stacks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2018-09-10 10:01:38 +00:00
|
|
|
httperror "github.com/portainer/libhttp/error"
|
|
|
|
"github.com/portainer/libhttp/request"
|
|
|
|
"github.com/portainer/libhttp/response"
|
2018-06-11 13:13:19 +00:00
|
|
|
"github.com/portainer/portainer"
|
|
|
|
"github.com/portainer/portainer/http/proxy"
|
|
|
|
"github.com/portainer/portainer/http/security"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GET request on /api/stacks/:id
|
|
|
|
func (handler *Handler) stackInspect(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
|
2018-06-18 10:07:56 +00:00
|
|
|
stackID, err := request.RetrieveNumericRouteVariableValue(r, "id")
|
2018-06-11 13:13:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusBadRequest, "Invalid stack identifier route variable", err}
|
|
|
|
}
|
|
|
|
|
|
|
|
stack, err := handler.StackService.Stack(portainer.StackID(stackID))
|
2018-06-19 11:15:10 +00:00
|
|
|
if err == portainer.ErrObjectNotFound {
|
2018-06-11 13:13:19 +00:00
|
|
|
return &httperror.HandlerError{http.StatusNotFound, "Unable to find a stack with the specified identifier inside the database", err}
|
|
|
|
} else if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to find a stack with the specified identifier inside the database", err}
|
|
|
|
}
|
|
|
|
|
|
|
|
resourceControl, err := handler.ResourceControlService.ResourceControlByResourceID(stack.Name)
|
2018-06-19 11:15:10 +00:00
|
|
|
if err != nil && err != portainer.ErrObjectNotFound {
|
2018-06-11 13:13:19 +00:00
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve a resource control associated to the stack", err}
|
|
|
|
}
|
|
|
|
|
|
|
|
securityContext, err := security.RetrieveRestrictedRequestContext(r)
|
|
|
|
if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve info from request context", err}
|
|
|
|
}
|
|
|
|
|
|
|
|
extendedStack := proxy.ExtendedStack{*stack, portainer.ResourceControl{}}
|
2018-08-19 05:57:28 +00:00
|
|
|
if !securityContext.IsAdmin && resourceControl == nil {
|
|
|
|
return &httperror.HandlerError{http.StatusForbidden, "Access denied to resource", portainer.ErrResourceAccessDenied}
|
|
|
|
}
|
|
|
|
|
2018-06-11 13:13:19 +00:00
|
|
|
if resourceControl != nil {
|
|
|
|
if securityContext.IsAdmin || proxy.CanAccessStack(stack, resourceControl, securityContext.UserID, securityContext.UserMemberships) {
|
|
|
|
extendedStack.ResourceControl = *resourceControl
|
|
|
|
} else {
|
|
|
|
return &httperror.HandlerError{http.StatusForbidden, "Access denied to resource", portainer.ErrResourceAccessDenied}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return response.JSON(w, extendedStack)
|
|
|
|
}
|