Files
portainer/api/http/handler/users/user_namespaces.go
T
Yi Chen 2247d8c3a2 (feat)k8s/RBAC: Provide Portainer RBAC functionality for Kubernetes endpoints (#35)
* + endpoint and namespace level authorizations
+ user namespace authorization API
+ k8s client setup service account with k8s roles and policies by portainer role
* User authorization changes refresh token cache
* rbac authorizes k8s requests
* CE to EE migrator to include new authorizations

* code clean up
* comments

* * merge in the RestrictDefaultNamespace changes

* - remove unnecessary check for default namespace

* + updates namespace access policies when generating token

* * updates namespace access policies when querying the user namespace endpoint

* + k8s rule in rbac.go for endpoint access test
+ missing k8s cluster rules for different roles

* feat(rbac): update kube rbac

* feat(rbac): use the authorization directive

* feat(rbac): Update namespace access policies when user/team is deleted

* refactor(app): use new angular-multi-select capabilities

* feat(rbac): fix authorizations

* feat(rbac): fix userAccessPolicies update bug

* feat(rbac): add W applications authorizations

* feat(rbac): add application details W authorizations

* feat(rbac): add configurations W autohorizations

* feat(rbac): add configuration details W authorizations

* feat(rbac): add volumes W authorizations

* feat(rbac): add volume details W authorizations

* feat(rbac): add componentstatus to portainer-view role and add cluster/node authorizations

* fix(rbac): disable application note for non authorized user

* fix(rbac): add endpoints list and components status to portainer-basic

* fix(rbac): allow user to access default namespace when restrict default namespace isn't activated

* fix(rbac): remove default namespace from useraccesspolicies when restrict default namespace isn't activated

* fix(rbac): change some things

* fix(rbac): allow standard user to access container console

* - removed unused parameter

* fix(rbac): fix team authorizations

Co-authored-by: Maxime Bajeux <max.bajeux@gmail.com>
Co-authored-by: xAt0mZ <baron_l@epitech.eu>
2020-11-03 22:08:09 +13:00

99 lines
3.0 KiB
Go

package users
import (
"net/http"
httperror "github.com/portainer/libhttp/error"
"github.com/portainer/libhttp/request"
"github.com/portainer/libhttp/response"
portainer "github.com/portainer/portainer/api"
"github.com/portainer/portainer/api/http/errors"
"github.com/portainer/portainer/api/http/security"
)
// GET request on /api/users/:id/namespaces
// returns user's role authorizations of all namespaces in all k8s endpoints
func (handler *Handler) userNamespaces(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
userID, err := request.RetrieveNumericRouteVariableValue(r, "id")
if err != nil {
return &httperror.HandlerError{http.StatusBadRequest, "Invalid user identifier route variable", err}
}
tokenData, err := security.RetrieveTokenData(r)
if err != nil {
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve user authentication token", err}
}
if tokenData.Role != portainer.AdministratorRole && tokenData.ID != portainer.UserID(userID) {
return &httperror.HandlerError{http.StatusForbidden, "Permission denied to retrieve user namespaces", errors.ErrUnauthorized}
}
endpoints, err := handler.DataStore.Endpoint().Endpoints()
if err != nil {
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve user namespace data", err}
}
// key: endpointID, value: a map between namespace and user's role authorizations
results := make(map[int]map[string]portainer.Authorizations)
for _, endpoint := range endpoints {
// skip non k8s endpoints
if endpoint.Type != portainer.KubernetesLocalEnvironment ||
endpoint.Type != portainer.AgentOnKubernetesEnvironment ||
endpoint.Type != portainer.EdgeAgentOnKubernetesEnvironment {
continue
}
endpointRole, err := handler.AuthorizationService.GetUserEndpointRole(userID, int(endpoint.ID))
if err != nil {
break
}
// no endpoint role for the user, continue
if endpointRole == nil {
continue
}
kcl, err := handler.K8sClientFactory.GetKubeClient(&endpoint)
if err != nil {
break
}
namespaces, err := kcl.GetNamespaces()
if err != nil {
break
}
accessPolicies, err := kcl.GetNamespaceAccessPolicies()
if err != nil {
break
}
// update the namespace access policies based on user's role, also in configmap.
accessPolicies, hasChange, err := handler.AuthorizationService.UpdateUserNamespaceAccessPolicies(
userID, &endpoint, accessPolicies,
)
if hasChange {
err = kcl.UpdateNamespaceAccessPolicies(accessPolicies)
if err != nil {
break
}
}
namespaceAuthorizations, err := handler.AuthorizationService.GetUserNamespaceAuthorizations(
userID, int(endpoint.ID), accessPolicies, namespaces, endpointRole.Authorizations,
endpoint.Kubernetes.Configuration,
)
if err != nil {
break
}
results[int(endpoint.ID)] = namespaceAuthorizations
}
if err != nil {
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve user namespace data", err}
}
return response.JSON(w, results)
}