2017-05-23 18:56:10 +00:00
|
|
|
package security
|
|
|
|
|
2018-08-19 05:57:28 +00:00
|
|
|
import (
|
2021-07-14 09:15:21 +00:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
portainer "github.com/portainer/portainer/api"
|
2018-08-19 05:57:28 +00:00
|
|
|
)
|
2017-05-23 18:56:10 +00:00
|
|
|
|
2021-07-14 09:15:21 +00:00
|
|
|
// IsAdmin returns true if the logged-in user is an admin
|
|
|
|
func IsAdmin(request *http.Request) (bool, error) {
|
|
|
|
tokenData, err := RetrieveTokenData(request)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tokenData.Role == portainer.AdministratorRole, nil
|
|
|
|
}
|
|
|
|
|
2018-06-11 15:58:46 +00:00
|
|
|
// AuthorizedResourceControlAccess checks whether the user can alter an existing resource control.
|
|
|
|
func AuthorizedResourceControlAccess(resourceControl *portainer.ResourceControl, context *RestrictedRequestContext) bool {
|
2018-08-19 05:57:28 +00:00
|
|
|
if context.IsAdmin || resourceControl.Public {
|
2018-06-11 15:58:46 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, access := range resourceControl.TeamAccesses {
|
|
|
|
for _, membership := range context.UserMemberships {
|
|
|
|
if membership.TeamID == access.TeamID {
|
2018-08-19 05:57:28 +00:00
|
|
|
return true
|
2018-06-11 15:58:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, access := range resourceControl.UserAccesses {
|
|
|
|
if context.UserID == access.UserID {
|
2018-08-19 05:57:28 +00:00
|
|
|
return true
|
2018-06-11 15:58:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-19 05:57:28 +00:00
|
|
|
return false
|
2018-06-11 15:58:46 +00:00
|
|
|
}
|
|
|
|
|
2017-05-23 18:56:10 +00:00
|
|
|
// AuthorizedResourceControlUpdate ensure that the user can update a resource control object.
|
|
|
|
// A non-administrator user cannot create a resource control where:
|
2018-08-19 05:57:28 +00:00
|
|
|
// * the Public flag is set false
|
2019-11-12 23:41:42 +00:00
|
|
|
// * the AdministatorsOnly flag is set to true
|
2018-06-11 15:58:46 +00:00
|
|
|
// * he wants to create a resource control without any user/team accesses
|
2017-05-23 18:56:10 +00:00
|
|
|
// * he wants to add more than one user in the user accesses
|
2019-05-24 06:04:58 +00:00
|
|
|
// * he wants to add a user in the user accesses that is not corresponding to its id
|
2017-05-23 18:56:10 +00:00
|
|
|
// * he wants to add a team he is not a member of
|
2019-11-12 23:41:42 +00:00
|
|
|
func AuthorizedResourceControlUpdate(resourceControl *portainer.ResourceControl, context *RestrictedRequestContext) bool {
|
2018-08-19 05:57:28 +00:00
|
|
|
if context.IsAdmin || resourceControl.Public {
|
2017-05-23 18:56:10 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-11-12 23:41:42 +00:00
|
|
|
if resourceControl.AdministratorsOnly {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-05-23 18:56:10 +00:00
|
|
|
userAccessesCount := len(resourceControl.UserAccesses)
|
|
|
|
teamAccessesCount := len(resourceControl.TeamAccesses)
|
2018-06-11 15:58:46 +00:00
|
|
|
|
|
|
|
if userAccessesCount == 0 && teamAccessesCount == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-05-23 18:56:10 +00:00
|
|
|
if userAccessesCount > 1 || (userAccessesCount == 1 && teamAccessesCount == 1) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if userAccessesCount == 1 {
|
|
|
|
access := resourceControl.UserAccesses[0]
|
|
|
|
if access.UserID == context.UserID {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if teamAccessesCount > 0 {
|
|
|
|
for _, access := range resourceControl.TeamAccesses {
|
|
|
|
for _, membership := range context.UserMemberships {
|
|
|
|
if membership.TeamID == access.TeamID {
|
2018-08-19 05:57:28 +00:00
|
|
|
return true
|
2017-05-23 18:56:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-19 05:57:28 +00:00
|
|
|
return false
|
2017-05-23 18:56:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AuthorizedTeamManagement ensure that access to the management of the specified team is granted.
|
|
|
|
// It will check if the user is either administrator or leader of that team.
|
|
|
|
func AuthorizedTeamManagement(teamID portainer.TeamID, context *RestrictedRequestContext) bool {
|
|
|
|
if context.IsAdmin {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, membership := range context.UserMemberships {
|
|
|
|
if membership.TeamID == teamID && membership.Role == portainer.TeamLeader {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
2018-02-23 02:10:26 +00:00
|
|
|
|
2021-09-20 00:14:22 +00:00
|
|
|
// authorizedEndpointAccess ensure that the user can access the specified environment(endpoint).
|
2018-02-23 02:10:26 +00:00
|
|
|
// It will check if the user is part of the authorized users or part of a team that is
|
2021-09-20 00:14:22 +00:00
|
|
|
// listed in the authorized teams of the environment(endpoint) and the associated group.
|
2018-06-18 09:56:31 +00:00
|
|
|
func authorizedEndpointAccess(endpoint *portainer.Endpoint, endpointGroup *portainer.EndpointGroup, userID portainer.UserID, memberships []portainer.TeamMembership) bool {
|
2021-07-14 09:15:21 +00:00
|
|
|
groupAccess := AuthorizedAccess(userID, memberships, endpointGroup.UserAccessPolicies, endpointGroup.TeamAccessPolicies)
|
2018-04-26 16:08:46 +00:00
|
|
|
if !groupAccess {
|
2021-07-14 09:15:21 +00:00
|
|
|
return AuthorizedAccess(userID, memberships, endpoint.UserAccessPolicies, endpoint.TeamAccessPolicies)
|
2018-02-23 02:10:26 +00:00
|
|
|
}
|
2018-04-26 16:08:46 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-09-20 00:14:22 +00:00
|
|
|
// authorizedEndpointGroupAccess ensure that the user can access the specified environment(endpoint) group.
|
2018-04-26 16:08:46 +00:00
|
|
|
// It will check if the user is part of the authorized users or part of a team that is
|
|
|
|
// listed in the authorized teams.
|
2019-02-25 00:02:49 +00:00
|
|
|
func authorizedEndpointGroupAccess(endpointGroup *portainer.EndpointGroup, userID portainer.UserID, memberships []portainer.TeamMembership) bool {
|
2021-07-14 09:15:21 +00:00
|
|
|
return AuthorizedAccess(userID, memberships, endpointGroup.UserAccessPolicies, endpointGroup.TeamAccessPolicies)
|
2018-02-23 02:10:26 +00:00
|
|
|
}
|
2018-03-22 22:44:43 +00:00
|
|
|
|
|
|
|
// AuthorizedRegistryAccess ensure that the user can access the specified registry.
|
|
|
|
// It will check if the user is part of the authorized users or part of a team that is
|
2021-09-20 00:14:22 +00:00
|
|
|
// listed in the authorized teams for a specified environment(endpoint),
|
2021-07-14 09:15:21 +00:00
|
|
|
func AuthorizedRegistryAccess(registry *portainer.Registry, user *portainer.User, teamMemberships []portainer.TeamMembership, endpointID portainer.EndpointID) bool {
|
|
|
|
if user.Role == portainer.AdministratorRole {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
registryEndpointAccesses := registry.RegistryAccesses[endpointID]
|
|
|
|
|
|
|
|
return AuthorizedAccess(user.ID, teamMemberships, registryEndpointAccesses.UserAccessPolicies, registryEndpointAccesses.TeamAccessPolicies)
|
2018-04-26 16:08:46 +00:00
|
|
|
}
|
|
|
|
|
2021-07-14 09:15:21 +00:00
|
|
|
// AuthorizedAccess verifies the userID or memberships are authorized to use an object per the supplied access policies
|
|
|
|
func AuthorizedAccess(userID portainer.UserID, memberships []portainer.TeamMembership, userAccessPolicies portainer.UserAccessPolicies, teamAccessPolicies portainer.TeamAccessPolicies) bool {
|
2019-05-24 06:04:58 +00:00
|
|
|
_, userAccess := userAccessPolicies[userID]
|
|
|
|
if userAccess {
|
|
|
|
return true
|
2018-03-22 22:44:43 +00:00
|
|
|
}
|
2019-05-24 06:04:58 +00:00
|
|
|
|
2018-03-22 22:44:43 +00:00
|
|
|
for _, membership := range memberships {
|
2019-05-24 06:04:58 +00:00
|
|
|
_, teamAccess := teamAccessPolicies[membership.TeamID]
|
|
|
|
if teamAccess {
|
|
|
|
return true
|
2018-03-22 22:44:43 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-24 06:04:58 +00:00
|
|
|
|
2018-03-22 22:44:43 +00:00
|
|
|
return false
|
|
|
|
}
|