mirror of https://github.com/portainer/portainer
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
357 lines
11 KiB
357 lines
11 KiB
8 years ago
|
package handler
|
||
8 years ago
|
|
||
|
import (
|
||
|
"github.com/portainer/portainer"
|
||
8 years ago
|
httperror "github.com/portainer/portainer/http/error"
|
||
|
"github.com/portainer/portainer/http/proxy"
|
||
|
"github.com/portainer/portainer/http/security"
|
||
8 years ago
|
|
||
|
"encoding/json"
|
||
|
"log"
|
||
|
"net/http"
|
||
|
"os"
|
||
|
"strconv"
|
||
|
|
||
|
"github.com/asaskevich/govalidator"
|
||
|
"github.com/gorilla/mux"
|
||
|
)
|
||
|
|
||
|
// EndpointHandler represents an HTTP API handler for managing Docker endpoints.
|
||
|
type EndpointHandler struct {
|
||
|
*mux.Router
|
||
8 years ago
|
Logger *log.Logger
|
||
|
authorizeEndpointManagement bool
|
||
|
EndpointService portainer.EndpointService
|
||
|
FileService portainer.FileService
|
||
8 years ago
|
ProxyManager *proxy.Manager
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
const (
|
||
|
// ErrEndpointManagementDisabled is an error raised when trying to access the endpoints management endpoints
|
||
|
// when the server has been started with the --external-endpoints flag
|
||
|
ErrEndpointManagementDisabled = portainer.Error("Endpoint management is disabled")
|
||
|
)
|
||
|
|
||
8 years ago
|
// NewEndpointHandler returns a new instance of EndpointHandler.
|
||
8 years ago
|
func NewEndpointHandler(bouncer *security.RequestBouncer, authorizeEndpointManagement bool) *EndpointHandler {
|
||
8 years ago
|
h := &EndpointHandler{
|
||
8 years ago
|
Router: mux.NewRouter(),
|
||
|
Logger: log.New(os.Stderr, "", log.LstdFlags),
|
||
8 years ago
|
authorizeEndpointManagement: authorizeEndpointManagement,
|
||
8 years ago
|
}
|
||
|
h.Handle("/endpoints",
|
||
8 years ago
|
bouncer.AdministratorAccess(http.HandlerFunc(h.handlePostEndpoints))).Methods(http.MethodPost)
|
||
8 years ago
|
h.Handle("/endpoints",
|
||
8 years ago
|
bouncer.RestrictedAccess(http.HandlerFunc(h.handleGetEndpoints))).Methods(http.MethodGet)
|
||
8 years ago
|
h.Handle("/endpoints/{id}",
|
||
8 years ago
|
bouncer.AdministratorAccess(http.HandlerFunc(h.handleGetEndpoint))).Methods(http.MethodGet)
|
||
8 years ago
|
h.Handle("/endpoints/{id}",
|
||
8 years ago
|
bouncer.AdministratorAccess(http.HandlerFunc(h.handlePutEndpoint))).Methods(http.MethodPut)
|
||
8 years ago
|
h.Handle("/endpoints/{id}/access",
|
||
8 years ago
|
bouncer.AdministratorAccess(http.HandlerFunc(h.handlePutEndpointAccess))).Methods(http.MethodPut)
|
||
8 years ago
|
h.Handle("/endpoints/{id}",
|
||
8 years ago
|
bouncer.AdministratorAccess(http.HandlerFunc(h.handleDeleteEndpoint))).Methods(http.MethodDelete)
|
||
8 years ago
|
|
||
8 years ago
|
return h
|
||
|
}
|
||
|
|
||
|
// handleGetEndpoints handles GET requests on /endpoints
|
||
|
func (handler *EndpointHandler) handleGetEndpoints(w http.ResponseWriter, r *http.Request) {
|
||
8 years ago
|
securityContext, err := security.RetrieveRestrictedRequestContext(r)
|
||
8 years ago
|
if err != nil {
|
||
8 years ago
|
httperror.WriteErrorResponse(w, err, http.StatusInternalServerError, handler.Logger)
|
||
8 years ago
|
return
|
||
|
}
|
||
8 years ago
|
|
||
8 years ago
|
endpoints, err := handler.EndpointService.Endpoints()
|
||
8 years ago
|
if err != nil {
|
||
8 years ago
|
httperror.WriteErrorResponse(w, err, http.StatusInternalServerError, handler.Logger)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
filteredEndpoints, err := security.FilterEndpoints(endpoints, securityContext)
|
||
|
if err != nil {
|
||
|
httperror.WriteErrorResponse(w, err, http.StatusInternalServerError, handler.Logger)
|
||
|
return
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
encodeJSON(w, filteredEndpoints, handler.Logger)
|
||
8 years ago
|
}
|
||
|
|
||
|
// handlePostEndpoints handles POST requests on /endpoints
|
||
|
func (handler *EndpointHandler) handlePostEndpoints(w http.ResponseWriter, r *http.Request) {
|
||
8 years ago
|
if !handler.authorizeEndpointManagement {
|
||
8 years ago
|
httperror.WriteErrorResponse(w, ErrEndpointManagementDisabled, http.StatusServiceUnavailable, handler.Logger)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
var req postEndpointsRequest
|
||
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||
8 years ago
|
httperror.WriteErrorResponse(w, ErrInvalidJSON, http.StatusBadRequest, handler.Logger)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
|
||
|
_, err := govalidator.ValidateStruct(req)
|
||
|
if err != nil {
|
||
8 years ago
|
httperror.WriteErrorResponse(w, ErrInvalidRequestFormat, http.StatusBadRequest, handler.Logger)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
|
||
|
endpoint := &portainer.Endpoint{
|
||
8 years ago
|
Name: req.Name,
|
||
|
URL: req.URL,
|
||
8 years ago
|
PublicURL: req.PublicURL,
|
||
8 years ago
|
TLS: req.TLS,
|
||
|
AuthorizedUsers: []portainer.UserID{},
|
||
8 years ago
|
AuthorizedTeams: []portainer.TeamID{},
|
||
8 years ago
|
}
|
||
|
|
||
|
err = handler.EndpointService.CreateEndpoint(endpoint)
|
||
|
if err != nil {
|
||
8 years ago
|
httperror.WriteErrorResponse(w, err, http.StatusInternalServerError, handler.Logger)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
|
||
|
if req.TLS {
|
||
|
caCertPath, _ := handler.FileService.GetPathForTLSFile(endpoint.ID, portainer.TLSFileCA)
|
||
|
endpoint.TLSCACertPath = caCertPath
|
||
|
certPath, _ := handler.FileService.GetPathForTLSFile(endpoint.ID, portainer.TLSFileCert)
|
||
|
endpoint.TLSCertPath = certPath
|
||
|
keyPath, _ := handler.FileService.GetPathForTLSFile(endpoint.ID, portainer.TLSFileKey)
|
||
|
endpoint.TLSKeyPath = keyPath
|
||
|
err = handler.EndpointService.UpdateEndpoint(endpoint.ID, endpoint)
|
||
|
if err != nil {
|
||
8 years ago
|
httperror.WriteErrorResponse(w, err, http.StatusInternalServerError, handler.Logger)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
}
|
||
|
|
||
|
encodeJSON(w, &postEndpointsResponse{ID: int(endpoint.ID)}, handler.Logger)
|
||
|
}
|
||
|
|
||
|
type postEndpointsRequest struct {
|
||
8 years ago
|
Name string `valid:"required"`
|
||
|
URL string `valid:"required"`
|
||
|
PublicURL string `valid:"-"`
|
||
|
TLS bool
|
||
8 years ago
|
}
|
||
|
|
||
|
type postEndpointsResponse struct {
|
||
|
ID int `json:"Id"`
|
||
|
}
|
||
|
|
||
|
// handleGetEndpoint handles GET requests on /endpoints/:id
|
||
|
func (handler *EndpointHandler) handleGetEndpoint(w http.ResponseWriter, r *http.Request) {
|
||
|
vars := mux.Vars(r)
|
||
|
id := vars["id"]
|
||
|
|
||
|
endpointID, err := strconv.Atoi(id)
|
||
|
if err != nil {
|
||
8 years ago
|
httperror.WriteErrorResponse(w, err, http.StatusBadRequest, handler.Logger)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
endpoint, err := handler.EndpointService.Endpoint(portainer.EndpointID(endpointID))
|
||
|
if err == portainer.ErrEndpointNotFound {
|
||
8 years ago
|
httperror.WriteErrorResponse(w, err, http.StatusNotFound, handler.Logger)
|
||
8 years ago
|
return
|
||
|
} else if err != nil {
|
||
8 years ago
|
httperror.WriteErrorResponse(w, err, http.StatusInternalServerError, handler.Logger)
|
||
8 years ago
|
return
|
||
8 years ago
|
}
|
||
|
|
||
|
encodeJSON(w, endpoint, handler.Logger)
|
||
|
}
|
||
|
|
||
8 years ago
|
// handlePutEndpointAccess handles PUT requests on /endpoints/:id/access
|
||
|
func (handler *EndpointHandler) handlePutEndpointAccess(w http.ResponseWriter, r *http.Request) {
|
||
8 years ago
|
vars := mux.Vars(r)
|
||
|
id := vars["id"]
|
||
|
|
||
|
endpointID, err := strconv.Atoi(id)
|
||
|
if err != nil {
|
||
8 years ago
|
httperror.WriteErrorResponse(w, err, http.StatusBadRequest, handler.Logger)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
var req putEndpointAccessRequest
|
||
|
if err = json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||
8 years ago
|
httperror.WriteErrorResponse(w, ErrInvalidJSON, http.StatusBadRequest, handler.Logger)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
|
||
|
_, err = govalidator.ValidateStruct(req)
|
||
|
if err != nil {
|
||
8 years ago
|
httperror.WriteErrorResponse(w, ErrInvalidRequestFormat, http.StatusBadRequest, handler.Logger)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
endpoint, err := handler.EndpointService.Endpoint(portainer.EndpointID(endpointID))
|
||
|
if err == portainer.ErrEndpointNotFound {
|
||
8 years ago
|
httperror.WriteErrorResponse(w, err, http.StatusNotFound, handler.Logger)
|
||
8 years ago
|
return
|
||
|
} else if err != nil {
|
||
8 years ago
|
httperror.WriteErrorResponse(w, err, http.StatusInternalServerError, handler.Logger)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
if req.AuthorizedUsers != nil {
|
||
|
authorizedUserIDs := []portainer.UserID{}
|
||
|
for _, value := range req.AuthorizedUsers {
|
||
|
authorizedUserIDs = append(authorizedUserIDs, portainer.UserID(value))
|
||
|
}
|
||
|
endpoint.AuthorizedUsers = authorizedUserIDs
|
||
|
}
|
||
|
|
||
|
if req.AuthorizedTeams != nil {
|
||
|
authorizedTeamIDs := []portainer.TeamID{}
|
||
|
for _, value := range req.AuthorizedTeams {
|
||
|
authorizedTeamIDs = append(authorizedTeamIDs, portainer.TeamID(value))
|
||
|
}
|
||
|
endpoint.AuthorizedTeams = authorizedTeamIDs
|
||
8 years ago
|
}
|
||
|
|
||
|
err = handler.EndpointService.UpdateEndpoint(endpoint.ID, endpoint)
|
||
8 years ago
|
if err != nil {
|
||
8 years ago
|
httperror.WriteErrorResponse(w, err, http.StatusInternalServerError, handler.Logger)
|
||
8 years ago
|
return
|
||
8 years ago
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
type putEndpointAccessRequest struct {
|
||
8 years ago
|
AuthorizedUsers []int `valid:"-"`
|
||
8 years ago
|
AuthorizedTeams []int `valid:"-"`
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
// handlePutEndpoint handles PUT requests on /endpoints/:id
|
||
|
func (handler *EndpointHandler) handlePutEndpoint(w http.ResponseWriter, r *http.Request) {
|
||
8 years ago
|
if !handler.authorizeEndpointManagement {
|
||
8 years ago
|
httperror.WriteErrorResponse(w, ErrEndpointManagementDisabled, http.StatusServiceUnavailable, handler.Logger)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
vars := mux.Vars(r)
|
||
|
id := vars["id"]
|
||
|
|
||
|
endpointID, err := strconv.Atoi(id)
|
||
|
if err != nil {
|
||
8 years ago
|
httperror.WriteErrorResponse(w, err, http.StatusBadRequest, handler.Logger)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
|
||
|
var req putEndpointsRequest
|
||
|
if err = json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||
8 years ago
|
httperror.WriteErrorResponse(w, ErrInvalidJSON, http.StatusBadRequest, handler.Logger)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
|
||
|
_, err = govalidator.ValidateStruct(req)
|
||
|
if err != nil {
|
||
8 years ago
|
httperror.WriteErrorResponse(w, ErrInvalidRequestFormat, http.StatusBadRequest, handler.Logger)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
endpoint, err := handler.EndpointService.Endpoint(portainer.EndpointID(endpointID))
|
||
|
if err == portainer.ErrEndpointNotFound {
|
||
8 years ago
|
httperror.WriteErrorResponse(w, err, http.StatusNotFound, handler.Logger)
|
||
8 years ago
|
return
|
||
|
} else if err != nil {
|
||
8 years ago
|
httperror.WriteErrorResponse(w, err, http.StatusInternalServerError, handler.Logger)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
|
||
|
if req.Name != "" {
|
||
|
endpoint.Name = req.Name
|
||
|
}
|
||
|
|
||
|
if req.URL != "" {
|
||
|
endpoint.URL = req.URL
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
if req.PublicURL != "" {
|
||
|
endpoint.PublicURL = req.PublicURL
|
||
|
}
|
||
|
|
||
8 years ago
|
if req.TLS {
|
||
8 years ago
|
endpoint.TLS = true
|
||
8 years ago
|
caCertPath, _ := handler.FileService.GetPathForTLSFile(endpoint.ID, portainer.TLSFileCA)
|
||
|
endpoint.TLSCACertPath = caCertPath
|
||
|
certPath, _ := handler.FileService.GetPathForTLSFile(endpoint.ID, portainer.TLSFileCert)
|
||
|
endpoint.TLSCertPath = certPath
|
||
|
keyPath, _ := handler.FileService.GetPathForTLSFile(endpoint.ID, portainer.TLSFileKey)
|
||
|
endpoint.TLSKeyPath = keyPath
|
||
|
} else {
|
||
8 years ago
|
endpoint.TLS = false
|
||
|
endpoint.TLSCACertPath = ""
|
||
|
endpoint.TLSCertPath = ""
|
||
|
endpoint.TLSKeyPath = ""
|
||
8 years ago
|
err = handler.FileService.DeleteTLSFiles(endpoint.ID)
|
||
|
if err != nil {
|
||
8 years ago
|
httperror.WriteErrorResponse(w, err, http.StatusInternalServerError, handler.Logger)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
_, err = handler.ProxyManager.CreateAndRegisterProxy(endpoint)
|
||
8 years ago
|
if err != nil {
|
||
8 years ago
|
httperror.WriteErrorResponse(w, err, http.StatusInternalServerError, handler.Logger)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
err = handler.EndpointService.UpdateEndpoint(endpoint.ID, endpoint)
|
||
|
if err != nil {
|
||
8 years ago
|
httperror.WriteErrorResponse(w, err, http.StatusInternalServerError, handler.Logger)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
}
|
||
|
|
||
|
type putEndpointsRequest struct {
|
||
8 years ago
|
Name string `valid:"-"`
|
||
|
URL string `valid:"-"`
|
||
|
PublicURL string `valid:"-"`
|
||
|
TLS bool `valid:"-"`
|
||
8 years ago
|
}
|
||
|
|
||
|
// handleDeleteEndpoint handles DELETE requests on /endpoints/:id
|
||
|
func (handler *EndpointHandler) handleDeleteEndpoint(w http.ResponseWriter, r *http.Request) {
|
||
8 years ago
|
if !handler.authorizeEndpointManagement {
|
||
8 years ago
|
httperror.WriteErrorResponse(w, ErrEndpointManagementDisabled, http.StatusServiceUnavailable, handler.Logger)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
vars := mux.Vars(r)
|
||
|
id := vars["id"]
|
||
|
|
||
|
endpointID, err := strconv.Atoi(id)
|
||
|
if err != nil {
|
||
8 years ago
|
httperror.WriteErrorResponse(w, err, http.StatusBadRequest, handler.Logger)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
endpoint, err := handler.EndpointService.Endpoint(portainer.EndpointID(endpointID))
|
||
8 years ago
|
|
||
8 years ago
|
if err == portainer.ErrEndpointNotFound {
|
||
8 years ago
|
httperror.WriteErrorResponse(w, err, http.StatusNotFound, handler.Logger)
|
||
8 years ago
|
return
|
||
|
} else if err != nil {
|
||
8 years ago
|
httperror.WriteErrorResponse(w, err, http.StatusInternalServerError, handler.Logger)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
handler.ProxyManager.DeleteProxy(string(endpointID))
|
||
8 years ago
|
|
||
8 years ago
|
err = handler.EndpointService.DeleteEndpoint(portainer.EndpointID(endpointID))
|
||
|
if err != nil {
|
||
8 years ago
|
httperror.WriteErrorResponse(w, err, http.StatusInternalServerError, handler.Logger)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
|
||
|
if endpoint.TLS {
|
||
|
err = handler.FileService.DeleteTLSFiles(portainer.EndpointID(endpointID))
|
||
|
if err != nil {
|
||
8 years ago
|
httperror.WriteErrorResponse(w, err, http.StatusInternalServerError, handler.Logger)
|
||
|
return
|
||
8 years ago
|
}
|
||
|
}
|
||
|
}
|