2017-05-23 18:56:10 +00:00
|
|
|
package security
|
|
|
|
|
|
|
|
import (
|
2020-05-14 02:14:28 +00:00
|
|
|
"errors"
|
2020-07-07 21:57:52 +00:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
2020-05-14 02:14:28 +00:00
|
|
|
|
2018-09-10 10:01:38 +00:00
|
|
|
httperror "github.com/portainer/libhttp/error"
|
2021-04-06 10:08:43 +00:00
|
|
|
portainer "github.com/portainer/portainer/api"
|
2020-07-07 21:57:52 +00:00
|
|
|
bolterrors "github.com/portainer/portainer/api/bolt/errors"
|
|
|
|
httperrors "github.com/portainer/portainer/api/http/errors"
|
2017-05-23 18:56:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
// RequestBouncer represents an entity that manages API request accesses
|
|
|
|
RequestBouncer struct {
|
2020-08-11 05:41:37 +00:00
|
|
|
dataStore portainer.DataStore
|
|
|
|
jwtService portainer.JWTService
|
2018-06-18 09:56:31 +00:00
|
|
|
}
|
|
|
|
|
2017-05-23 18:56:10 +00:00
|
|
|
// RestrictedRequestContext is a data structure containing information
|
2019-10-07 03:10:51 +00:00
|
|
|
// used in AuthenticatedAccess
|
2017-05-23 18:56:10 +00:00
|
|
|
RestrictedRequestContext struct {
|
|
|
|
IsAdmin bool
|
|
|
|
IsTeamLeader bool
|
|
|
|
UserID portainer.UserID
|
|
|
|
UserMemberships []portainer.TeamMembership
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewRequestBouncer initializes a new RequestBouncer
|
2020-08-11 05:41:37 +00:00
|
|
|
func NewRequestBouncer(dataStore portainer.DataStore, jwtService portainer.JWTService) *RequestBouncer {
|
2017-05-23 18:56:10 +00:00
|
|
|
return &RequestBouncer{
|
2020-08-11 05:41:37 +00:00
|
|
|
dataStore: dataStore,
|
|
|
|
jwtService: jwtService,
|
2017-05-23 18:56:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-24 06:04:58 +00:00
|
|
|
// PublicAccess defines a security check for public API endpoints.
|
2017-05-23 18:56:10 +00:00
|
|
|
// No authentication is required to access these endpoints.
|
|
|
|
func (bouncer *RequestBouncer) PublicAccess(h http.Handler) http.Handler {
|
|
|
|
h = mwSecureHeaders(h)
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
2019-10-07 03:10:51 +00:00
|
|
|
// AdminAccess defines a security check for API endpoints that require an authorization check.
|
2017-05-23 18:56:10 +00:00
|
|
|
// Authentication is required to access these endpoints.
|
2020-08-11 05:41:37 +00:00
|
|
|
// The administrator role is required to use these endpoints.
|
2019-10-07 03:10:51 +00:00
|
|
|
// The request context will be enhanced with a RestrictedRequestContext object
|
|
|
|
// that might be used later to inside the API operation for extra authorization validation
|
|
|
|
// and resource filtering.
|
|
|
|
func (bouncer *RequestBouncer) AdminAccess(h http.Handler) http.Handler {
|
2019-05-24 06:04:58 +00:00
|
|
|
h = bouncer.mwUpgradeToRestrictedRequest(h)
|
2019-10-07 03:10:51 +00:00
|
|
|
h = bouncer.mwCheckPortainerAuthorizations(h, true)
|
2019-05-24 06:04:58 +00:00
|
|
|
h = bouncer.mwAuthenticatedUser(h)
|
2017-05-23 18:56:10 +00:00
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
2019-05-24 06:04:58 +00:00
|
|
|
// RestrictedAccess defines a security check for restricted API endpoints.
|
2017-05-23 18:56:10 +00:00
|
|
|
// Authentication is required to access these endpoints.
|
|
|
|
// The request context will be enhanced with a RestrictedRequestContext object
|
2019-10-07 03:10:51 +00:00
|
|
|
// that might be used later to inside the API operation for extra authorization validation
|
|
|
|
// and resource filtering.
|
2017-05-23 18:56:10 +00:00
|
|
|
func (bouncer *RequestBouncer) RestrictedAccess(h http.Handler) http.Handler {
|
2019-10-07 03:10:51 +00:00
|
|
|
h = bouncer.mwUpgradeToRestrictedRequest(h)
|
|
|
|
h = bouncer.mwCheckPortainerAuthorizations(h, false)
|
|
|
|
h = bouncer.mwAuthenticatedUser(h)
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
|
|
|
// AuthenticatedAccess defines a security check for restricted API endpoints.
|
|
|
|
// Authentication is required to access these endpoints.
|
|
|
|
// The request context will be enhanced with a RestrictedRequestContext object
|
|
|
|
// that might be used later to inside the API operation for extra authorization validation
|
|
|
|
// and resource filtering.
|
|
|
|
func (bouncer *RequestBouncer) AuthenticatedAccess(h http.Handler) http.Handler {
|
2017-05-23 18:56:10 +00:00
|
|
|
h = bouncer.mwUpgradeToRestrictedRequest(h)
|
2019-05-24 06:04:58 +00:00
|
|
|
h = bouncer.mwAuthenticatedUser(h)
|
2017-05-23 18:56:10 +00:00
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
2019-05-24 06:04:58 +00:00
|
|
|
// AuthorizedEndpointOperation retrieves the JWT token from the request context and verifies
|
2018-06-18 09:56:31 +00:00
|
|
|
// that the user can access the specified endpoint.
|
2019-05-24 06:04:58 +00:00
|
|
|
// An error is returned when access to the endpoint is denied or if the user do not have the required
|
|
|
|
// authorization to execute the operation.
|
2020-08-11 05:41:37 +00:00
|
|
|
func (bouncer *RequestBouncer) AuthorizedEndpointOperation(r *http.Request, endpoint *portainer.Endpoint) error {
|
2018-06-18 09:56:31 +00:00
|
|
|
tokenData, err := RetrieveTokenData(r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if tokenData.Role == portainer.AdministratorRole {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-20 05:23:15 +00:00
|
|
|
memberships, err := bouncer.dataStore.TeamMembership().TeamMembershipsByUserID(tokenData.ID)
|
2018-06-18 09:56:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-20 05:23:15 +00:00
|
|
|
group, err := bouncer.dataStore.EndpointGroup().EndpointGroup(endpoint.GroupID)
|
2018-06-18 09:56:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !authorizedEndpointAccess(endpoint, group, tokenData.ID, memberships) {
|
2020-07-07 21:57:52 +00:00
|
|
|
return httperrors.ErrEndpointAccessDenied
|
2018-06-18 09:56:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-14 02:14:28 +00:00
|
|
|
// AuthorizedEdgeEndpointOperation verifies that the request was received from a valid Edge endpoint
|
|
|
|
func (bouncer *RequestBouncer) AuthorizedEdgeEndpointOperation(r *http.Request, endpoint *portainer.Endpoint) error {
|
2020-07-05 23:21:03 +00:00
|
|
|
if endpoint.Type != portainer.EdgeAgentOnKubernetesEnvironment && endpoint.Type != portainer.EdgeAgentOnDockerEnvironment {
|
2020-05-14 02:14:28 +00:00
|
|
|
return errors.New("Invalid endpoint type")
|
|
|
|
}
|
|
|
|
|
|
|
|
edgeIdentifier := r.Header.Get(portainer.PortainerAgentEdgeIDHeader)
|
|
|
|
if edgeIdentifier == "" {
|
|
|
|
return errors.New("missing Edge identifier")
|
|
|
|
}
|
|
|
|
|
|
|
|
if endpoint.EdgeID != "" && endpoint.EdgeID != edgeIdentifier {
|
|
|
|
return errors.New("invalid Edge identifier")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-06 10:08:43 +00:00
|
|
|
// handlers are applied backwards to the incoming request:
|
|
|
|
// - add secure handlers to the response
|
|
|
|
// - parse the JWT token and put it into the http context.
|
2019-05-24 06:04:58 +00:00
|
|
|
func (bouncer *RequestBouncer) mwAuthenticatedUser(h http.Handler) http.Handler {
|
|
|
|
h = bouncer.mwCheckAuthentication(h)
|
|
|
|
h = mwSecureHeaders(h)
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
|
|
|
// mwCheckPortainerAuthorizations will verify that the user has the required authorization to access
|
2020-08-11 05:41:37 +00:00
|
|
|
// a specific API endpoint.
|
|
|
|
// If the administratorOnly flag is specified, this will prevent non-admin
|
2019-10-07 03:10:51 +00:00
|
|
|
// users from accessing the endpoint.
|
|
|
|
func (bouncer *RequestBouncer) mwCheckPortainerAuthorizations(next http.Handler, administratorOnly bool) http.Handler {
|
2017-05-23 18:56:10 +00:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2019-05-24 06:04:58 +00:00
|
|
|
tokenData, err := RetrieveTokenData(r)
|
|
|
|
if err != nil {
|
2020-07-07 21:57:52 +00:00
|
|
|
httperror.WriteError(w, http.StatusForbidden, "Access denied", httperrors.ErrUnauthorized)
|
2019-05-24 06:04:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if tokenData.Role == portainer.AdministratorRole {
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-11 05:41:37 +00:00
|
|
|
if administratorOnly {
|
|
|
|
httperror.WriteError(w, http.StatusForbidden, "Access denied", httperrors.ErrUnauthorized)
|
2019-05-24 06:04:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-11 05:41:37 +00:00
|
|
|
_, err = bouncer.dataStore.User().User(tokenData.ID)
|
2020-07-07 21:57:52 +00:00
|
|
|
if err != nil && err == bolterrors.ErrObjectNotFound {
|
|
|
|
httperror.WriteError(w, http.StatusUnauthorized, "Unauthorized", httperrors.ErrUnauthorized)
|
2019-09-09 22:58:26 +00:00
|
|
|
return
|
|
|
|
} else if err != nil {
|
|
|
|
httperror.WriteError(w, http.StatusInternalServerError, "Unable to retrieve user details from the database", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-05-23 18:56:10 +00:00
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// mwUpgradeToRestrictedRequest will enhance the current request with
|
|
|
|
// a new RestrictedRequestContext object.
|
|
|
|
func (bouncer *RequestBouncer) mwUpgradeToRestrictedRequest(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
tokenData, err := RetrieveTokenData(r)
|
|
|
|
if err != nil {
|
2020-07-07 21:57:52 +00:00
|
|
|
httperror.WriteError(w, http.StatusForbidden, "Access denied", httperrors.ErrResourceAccessDenied)
|
2017-05-23 18:56:10 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
requestContext, err := bouncer.newRestrictedContextRequest(tokenData.ID, tokenData.Role)
|
|
|
|
if err != nil {
|
2018-06-11 13:13:19 +00:00
|
|
|
httperror.WriteError(w, http.StatusInternalServerError, "Unable to create restricted request context ", err)
|
2017-05-23 18:56:10 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-07-14 09:15:21 +00:00
|
|
|
ctx := StoreRestrictedRequestContext(r, requestContext)
|
2017-05-23 18:56:10 +00:00
|
|
|
next.ServeHTTP(w, r.WithContext(ctx))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// mwCheckAuthentication provides Authentication middleware for handlers
|
2021-04-06 10:08:43 +00:00
|
|
|
//
|
|
|
|
// It parses the JWT token and adds the parsed token data to the http context
|
2017-05-23 18:56:10 +00:00
|
|
|
func (bouncer *RequestBouncer) mwCheckAuthentication(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var tokenData *portainer.TokenData
|
2020-06-09 09:55:36 +00:00
|
|
|
var token string
|
2017-05-23 18:56:10 +00:00
|
|
|
|
2020-06-09 09:55:36 +00:00
|
|
|
// Optionally, token might be set via the "token" query parameter.
|
|
|
|
// For example, in websocket requests
|
|
|
|
token = r.URL.Query().Get("token")
|
2017-05-23 18:56:10 +00:00
|
|
|
|
2020-06-09 09:55:36 +00:00
|
|
|
// Get token from the Authorization header
|
|
|
|
tokens, ok := r.Header["Authorization"]
|
|
|
|
if ok && len(tokens) >= 1 {
|
|
|
|
token = tokens[0]
|
|
|
|
token = strings.TrimPrefix(token, "Bearer ")
|
|
|
|
}
|
2018-02-28 07:09:51 +00:00
|
|
|
|
2020-06-09 09:55:36 +00:00
|
|
|
if token == "" {
|
2020-07-07 21:57:52 +00:00
|
|
|
httperror.WriteError(w, http.StatusUnauthorized, "Unauthorized", httperrors.ErrUnauthorized)
|
2020-06-09 09:55:36 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
tokenData, err = bouncer.jwtService.ParseAndVerifyToken(token)
|
|
|
|
if err != nil {
|
|
|
|
httperror.WriteError(w, http.StatusUnauthorized, "Invalid JWT token", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = bouncer.dataStore.User().User(tokenData.ID)
|
2020-07-07 21:57:52 +00:00
|
|
|
if err != nil && err == bolterrors.ErrObjectNotFound {
|
|
|
|
httperror.WriteError(w, http.StatusUnauthorized, "Unauthorized", httperrors.ErrUnauthorized)
|
2020-06-09 09:55:36 +00:00
|
|
|
return
|
|
|
|
} else if err != nil {
|
|
|
|
httperror.WriteError(w, http.StatusInternalServerError, "Unable to retrieve user details from the database", err)
|
|
|
|
return
|
2017-05-23 18:56:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx := storeTokenData(r, tokenData)
|
|
|
|
next.ServeHTTP(w, r.WithContext(ctx))
|
|
|
|
return
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-05-24 06:04:58 +00:00
|
|
|
// mwSecureHeaders provides secure headers middleware for handlers.
|
|
|
|
func mwSecureHeaders(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Add("X-XSS-Protection", "1; mode=block")
|
|
|
|
w.Header().Add("X-Content-Type-Options", "nosniff")
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-05-23 18:56:10 +00:00
|
|
|
func (bouncer *RequestBouncer) newRestrictedContextRequest(userID portainer.UserID, userRole portainer.UserRole) (*RestrictedRequestContext, error) {
|
2021-04-06 10:08:43 +00:00
|
|
|
if userRole == portainer.AdministratorRole {
|
|
|
|
return &RestrictedRequestContext{
|
|
|
|
IsAdmin: true,
|
|
|
|
UserID: userID,
|
|
|
|
}, nil
|
2017-05-23 18:56:10 +00:00
|
|
|
}
|
|
|
|
|
2021-04-06 10:08:43 +00:00
|
|
|
memberships, err := bouncer.dataStore.TeamMembership().TeamMembershipsByUserID(userID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-05-23 18:56:10 +00:00
|
|
|
|
2021-04-06 10:08:43 +00:00
|
|
|
isTeamLeader := false
|
|
|
|
for _, membership := range memberships {
|
|
|
|
if membership.Role == portainer.TeamLeader {
|
|
|
|
isTeamLeader = true
|
2017-05-23 18:56:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-06 10:08:43 +00:00
|
|
|
return &RestrictedRequestContext{
|
|
|
|
IsAdmin: false,
|
|
|
|
UserID: userID,
|
|
|
|
IsTeamLeader: isTeamLeader,
|
|
|
|
UserMemberships: memberships,
|
|
|
|
}, nil
|
2017-05-23 18:56:10 +00:00
|
|
|
}
|
2020-06-25 03:25:51 +00:00
|
|
|
|
|
|
|
// EdgeComputeOperation defines a restriced edge compute operation.
|
|
|
|
// Use of this operation will only be authorized if edgeCompute is enabled in settings
|
|
|
|
func (bouncer *RequestBouncer) EdgeComputeOperation(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
settings, err := bouncer.dataStore.Settings().Settings()
|
|
|
|
if err != nil {
|
|
|
|
httperror.WriteError(w, http.StatusServiceUnavailable, "Unable to retrieve settings", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !settings.EnableEdgeComputeFeatures {
|
|
|
|
httperror.WriteError(w, http.StatusServiceUnavailable, "Edge compute features are disabled", errors.New("Edge compute features are disabled"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|