2021-08-05 03:02:06 +00:00
|
|
|
package kubernetes
|
|
|
|
|
|
|
|
import (
|
2021-08-26 14:00:59 +00:00
|
|
|
"errors"
|
2021-08-05 03:02:06 +00:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
httperror "github.com/portainer/libhttp/error"
|
|
|
|
portainer "github.com/portainer/portainer/api"
|
2021-08-26 14:00:59 +00:00
|
|
|
"github.com/portainer/portainer/api/http/middlewares"
|
2021-08-05 03:02:06 +00:00
|
|
|
"github.com/portainer/portainer/api/http/security"
|
2021-08-26 14:00:59 +00:00
|
|
|
"github.com/portainer/portainer/api/internal/authorization"
|
|
|
|
"github.com/portainer/portainer/api/internal/endpointutils"
|
2021-08-05 03:02:06 +00:00
|
|
|
"github.com/portainer/portainer/api/kubernetes/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Handler is the HTTP handler which will natively deal with to external endpoints.
|
|
|
|
type Handler struct {
|
|
|
|
*mux.Router
|
2021-08-26 14:00:59 +00:00
|
|
|
dataStore portainer.DataStore
|
|
|
|
kubernetesClientFactory *cli.ClientFactory
|
|
|
|
authorizationService *authorization.Service
|
2021-08-31 21:23:21 +00:00
|
|
|
JwtService portainer.JWTService
|
2021-08-05 03:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewHandler creates a handler to process pre-proxied requests to external APIs.
|
2021-08-26 14:00:59 +00:00
|
|
|
func NewHandler(bouncer *security.RequestBouncer, authorizationService *authorization.Service, dataStore portainer.DataStore, kubernetesClientFactory *cli.ClientFactory) *Handler {
|
2021-08-05 03:02:06 +00:00
|
|
|
h := &Handler{
|
2021-08-26 14:00:59 +00:00
|
|
|
Router: mux.NewRouter(),
|
|
|
|
dataStore: dataStore,
|
|
|
|
kubernetesClientFactory: kubernetesClientFactory,
|
|
|
|
authorizationService: authorizationService,
|
2021-08-05 03:02:06 +00:00
|
|
|
}
|
2021-08-26 14:00:59 +00:00
|
|
|
|
|
|
|
kubeRouter := h.PathPrefix("/kubernetes/{id}").Subrouter()
|
|
|
|
|
|
|
|
kubeRouter.Use(bouncer.AuthenticatedAccess)
|
|
|
|
kubeRouter.Use(middlewares.WithEndpoint(dataStore.Endpoint(), "id"))
|
|
|
|
kubeRouter.Use(kubeOnlyMiddleware)
|
|
|
|
|
|
|
|
kubeRouter.PathPrefix("/config").Handler(
|
2021-08-05 03:02:06 +00:00
|
|
|
bouncer.AuthenticatedAccess(httperror.LoggerHandler(h.getKubernetesConfig))).Methods(http.MethodGet)
|
2021-08-31 21:08:01 +00:00
|
|
|
kubeRouter.PathPrefix("/nodes_limits").Handler(
|
|
|
|
bouncer.AuthenticatedAccess(httperror.LoggerHandler(h.getKubernetesNodesLimits))).Methods(http.MethodGet)
|
2021-08-26 14:00:59 +00:00
|
|
|
|
|
|
|
// namespaces
|
|
|
|
// in the future this piece of code might be in another package (or a few different packages - namespaces/namespace?)
|
|
|
|
// to keep it simple, we've decided to leave it like this.
|
|
|
|
namespaceRouter := kubeRouter.PathPrefix("/namespaces/{namespace}").Subrouter()
|
|
|
|
namespaceRouter.Handle("/system", bouncer.RestrictedAccess(httperror.LoggerHandler(h.namespacesToggleSystem))).Methods(http.MethodPut)
|
|
|
|
|
2021-08-05 03:02:06 +00:00
|
|
|
return h
|
|
|
|
}
|
2021-08-26 14:00:59 +00:00
|
|
|
|
|
|
|
func kubeOnlyMiddleware(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(rw http.ResponseWriter, request *http.Request) {
|
|
|
|
endpoint, err := middlewares.FetchEndpoint(request)
|
|
|
|
if err != nil {
|
|
|
|
httperror.WriteError(rw, http.StatusInternalServerError, "Unable to find an endpoint on request context", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !endpointutils.IsKubernetesEndpoint(endpoint) {
|
|
|
|
errMessage := "Endpoint is not a kubernetes endpoint"
|
|
|
|
httperror.WriteError(rw, http.StatusBadRequest, errMessage, errors.New(errMessage))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
next.ServeHTTP(rw, request)
|
|
|
|
})
|
|
|
|
}
|