portainer/api/http/docker_handler.go

81 lines
2.1 KiB
Go
Raw Normal View History

2016-12-18 05:21:29 +00:00
package http
import (
"strconv"
2016-12-18 05:21:29 +00:00
"github.com/portainer/portainer"
"log"
"net/http"
"os"
"github.com/gorilla/mux"
2016-12-18 05:21:29 +00:00
)
// DockerHandler represents an HTTP API handler for proxying requests to the Docker API.
type DockerHandler struct {
*mux.Router
Logger *log.Logger
EndpointService portainer.EndpointService
ProxyService *ProxyService
2016-12-18 05:21:29 +00:00
}
// NewDockerHandler returns a new instance of DockerHandler.
func NewDockerHandler(mw *middleWareService, resourceControlService portainer.ResourceControlService) *DockerHandler {
2016-12-18 05:21:29 +00:00
h := &DockerHandler{
Router: mux.NewRouter(),
Logger: log.New(os.Stderr, "", log.LstdFlags),
2016-12-18 05:21:29 +00:00
}
h.PathPrefix("/{id}/").Handler(
mw.authenticated(http.HandlerFunc(h.proxyRequestsToDockerAPI)))
2016-12-18 05:21:29 +00:00
return h
}
func checkEndpointAccessControl(endpoint *portainer.Endpoint, userID portainer.UserID) bool {
for _, authorizedUserID := range endpoint.AuthorizedUsers {
if authorizedUserID == userID {
return true
2016-12-18 05:21:29 +00:00
}
}
return false
2016-12-18 05:21:29 +00:00
}
func (handler *DockerHandler) proxyRequestsToDockerAPI(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
parsedID, err := strconv.Atoi(id)
if err != nil {
Error(w, err, http.StatusBadRequest, handler.Logger)
return
}
endpointID := portainer.EndpointID(parsedID)
endpoint, err := handler.EndpointService.Endpoint(endpointID)
if err != nil {
Error(w, err, http.StatusInternalServerError, handler.Logger)
return
}
2016-12-18 05:21:29 +00:00
tokenData, err := extractTokenDataFromRequestContext(r)
2016-12-18 05:21:29 +00:00
if err != nil {
Error(w, err, http.StatusInternalServerError, handler.Logger)
2016-12-18 05:21:29 +00:00
}
if tokenData.Role != portainer.AdministratorRole && !checkEndpointAccessControl(endpoint, tokenData.ID) {
Error(w, portainer.ErrEndpointAccessDenied, http.StatusForbidden, handler.Logger)
return
2016-12-18 05:21:29 +00:00
}
var proxy http.Handler
proxy = handler.ProxyService.GetProxy(string(endpointID))
if proxy == nil {
proxy, err = handler.ProxyService.CreateAndRegisterProxy(endpoint)
if err != nil {
Error(w, err, http.StatusBadRequest, handler.Logger)
return
}
}
2016-12-18 05:21:29 +00:00
http.StripPrefix("/"+id, proxy).ServeHTTP(w, r)
2016-12-18 05:21:29 +00:00
}