2019-01-01 08:23:01 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2022-03-02 23:47:27 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/daemons/config"
|
2022-04-27 09:09:58 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/generated/clientset/versioned/scheme"
|
2019-01-01 08:23:01 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2022-04-27 09:09:58 +00:00
|
|
|
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
|
|
|
"k8s.io/apiserver/pkg/endpoints/handlers/responsewriters"
|
2019-01-01 08:23:01 +00:00
|
|
|
"k8s.io/apiserver/pkg/endpoints/request"
|
|
|
|
)
|
|
|
|
|
2019-10-27 05:53:25 +00:00
|
|
|
func hasRole(mustRoles []string, roles []string) bool {
|
|
|
|
for _, check := range roles {
|
|
|
|
for _, role := range mustRoles {
|
|
|
|
if role == check {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func doAuth(roles []string, serverConfig *config.Control, next http.Handler, rw http.ResponseWriter, req *http.Request) {
|
2020-09-21 16:56:03 +00:00
|
|
|
switch {
|
|
|
|
case serverConfig == nil:
|
|
|
|
logrus.Errorf("Authenticate not initialized: serverConfig is nil")
|
2022-04-27 09:09:58 +00:00
|
|
|
unauthorized(rw, req)
|
2019-01-01 08:23:01 +00:00
|
|
|
return
|
2020-09-21 16:56:03 +00:00
|
|
|
case serverConfig.Runtime.Authenticator == nil:
|
|
|
|
logrus.Errorf("Authenticate not initialized: serverConfig.Runtime.Authenticator is nil")
|
2022-04-27 09:09:58 +00:00
|
|
|
unauthorized(rw, req)
|
2020-09-21 16:56:03 +00:00
|
|
|
return
|
2019-01-01 08:23:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resp, ok, err := serverConfig.Runtime.Authenticator.AuthenticateRequest(req)
|
|
|
|
if err != nil {
|
2020-09-21 16:56:03 +00:00
|
|
|
logrus.Errorf("Failed to authenticate request from %s: %v", req.RemoteAddr, err)
|
2022-04-27 09:09:58 +00:00
|
|
|
unauthorized(rw, req)
|
2019-01-01 08:23:01 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-27 05:53:25 +00:00
|
|
|
if !ok || !hasRole(roles, resp.User.GetGroups()) {
|
2022-04-27 09:09:58 +00:00
|
|
|
forbidden(rw, req)
|
2019-01-01 08:23:01 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := request.WithUser(req.Context(), resp.User)
|
|
|
|
req = req.WithContext(ctx)
|
|
|
|
next.ServeHTTP(rw, req)
|
|
|
|
}
|
|
|
|
|
2019-10-27 05:53:25 +00:00
|
|
|
func authMiddleware(serverConfig *config.Control, roles ...string) mux.MiddlewareFunc {
|
2019-01-01 08:23:01 +00:00
|
|
|
return func(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
2019-10-27 05:53:25 +00:00
|
|
|
doAuth(roles, serverConfig, next, rw, req)
|
2019-01-01 08:23:01 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-04-27 09:09:58 +00:00
|
|
|
|
|
|
|
func unauthorized(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
responsewriters.ErrorNegotiated(
|
|
|
|
&apierrors.StatusError{ErrStatus: metav1.Status{
|
|
|
|
Status: metav1.StatusFailure,
|
|
|
|
Code: http.StatusUnauthorized,
|
|
|
|
Reason: metav1.StatusReasonUnauthorized,
|
|
|
|
Message: "not authorized",
|
|
|
|
}},
|
|
|
|
scheme.Codecs.WithoutConversion(), schema.GroupVersion{}, resp, req,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func forbidden(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
responsewriters.ErrorNegotiated(
|
|
|
|
&apierrors.StatusError{ErrStatus: metav1.Status{
|
|
|
|
Status: metav1.StatusFailure,
|
|
|
|
Code: http.StatusForbidden,
|
|
|
|
Reason: metav1.StatusReasonForbidden,
|
|
|
|
Message: "forbidden",
|
|
|
|
}},
|
|
|
|
scheme.Codecs.WithoutConversion(), schema.GroupVersion{}, resp, req,
|
|
|
|
)
|
|
|
|
}
|