2016-12-18 05:21:29 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
2017-03-12 16:24:15 +00:00
|
|
|
"strconv"
|
|
|
|
|
2016-12-18 05:21:29 +00:00
|
|
|
"github.com/portainer/portainer"
|
|
|
|
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2016-12-25 20:34:02 +00:00
|
|
|
|
|
|
|
"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
|
2017-03-12 16:24:15 +00:00
|
|
|
Logger *log.Logger
|
|
|
|
EndpointService portainer.EndpointService
|
2017-04-09 18:38:41 +00:00
|
|
|
ProxyService *ProxyService
|
2016-12-18 05:21:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewDockerHandler returns a new instance of DockerHandler.
|
2017-03-12 16:24:15 +00:00
|
|
|
func NewDockerHandler(mw *middleWareService, resourceControlService portainer.ResourceControlService) *DockerHandler {
|
2016-12-18 05:21:29 +00:00
|
|
|
h := &DockerHandler{
|
2017-03-12 16:24:15 +00:00
|
|
|
Router: mux.NewRouter(),
|
|
|
|
Logger: log.New(os.Stderr, "", log.LstdFlags),
|
2016-12-18 05:21:29 +00:00
|
|
|
}
|
2017-03-12 16:24:15 +00:00
|
|
|
h.PathPrefix("/{id}/").Handler(
|
|
|
|
mw.authenticated(http.HandlerFunc(h.proxyRequestsToDockerAPI)))
|
2016-12-18 05:21:29 +00:00
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
2017-03-12 16:24:15 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2017-03-12 16:24:15 +00:00
|
|
|
return false
|
2016-12-18 05:21:29 +00:00
|
|
|
}
|
|
|
|
|
2017-03-12 16:24:15 +00:00
|
|
|
func (handler *DockerHandler) proxyRequestsToDockerAPI(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
id := vars["id"]
|
2016-12-24 04:49:29 +00:00
|
|
|
|
2017-03-12 16:24:15 +00:00
|
|
|
parsedID, err := strconv.Atoi(id)
|
|
|
|
if err != nil {
|
|
|
|
Error(w, err, http.StatusBadRequest, handler.Logger)
|
|
|
|
return
|
2016-12-24 04:49:29 +00:00
|
|
|
}
|
|
|
|
|
2017-03-12 16:24:15 +00:00
|
|
|
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
|
|
|
|
2017-03-12 16:24:15 +00:00
|
|
|
tokenData, err := extractTokenDataFromRequestContext(r)
|
2016-12-18 05:21:29 +00:00
|
|
|
if err != nil {
|
2017-03-12 16:24:15 +00:00
|
|
|
Error(w, err, http.StatusInternalServerError, handler.Logger)
|
2016-12-18 05:21:29 +00:00
|
|
|
}
|
2017-03-12 16:24:15 +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
|
|
|
}
|
|
|
|
|
2017-03-28 12:28:17 +00:00
|
|
|
var proxy http.Handler
|
2017-04-09 18:38:41 +00:00
|
|
|
proxy = handler.ProxyService.GetProxy(string(endpointID))
|
|
|
|
if proxy == nil {
|
|
|
|
proxy, err = handler.ProxyService.CreateAndRegisterProxy(endpoint)
|
2017-03-12 16:24:15 +00:00
|
|
|
if err != nil {
|
|
|
|
Error(w, err, http.StatusBadRequest, handler.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2016-12-18 05:21:29 +00:00
|
|
|
|
2017-04-09 18:38:41 +00:00
|
|
|
http.StripPrefix("/"+id, proxy).ServeHTTP(w, r)
|
2016-12-18 05:21:29 +00:00
|
|
|
}
|