2017-05-23 18:56:10 +00:00
|
|
|
package security
|
|
|
|
|
|
|
|
import "github.com/portainer/portainer"
|
|
|
|
|
|
|
|
// AuthorizedResourceControlDeletion ensure that the user can delete a resource control object.
|
|
|
|
// A non-administrator user cannot delete a resource control where:
|
|
|
|
// * the AdministratorsOnly flag is set
|
|
|
|
// * he is not one of the users in the user accesses
|
|
|
|
// * he is not a member of any team within the team accesses
|
|
|
|
func AuthorizedResourceControlDeletion(resourceControl *portainer.ResourceControl, context *RestrictedRequestContext) bool {
|
|
|
|
if context.IsAdmin {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if resourceControl.AdministratorsOnly {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
userAccessesCount := len(resourceControl.UserAccesses)
|
|
|
|
teamAccessesCount := len(resourceControl.TeamAccesses)
|
|
|
|
|
|
|
|
if teamAccessesCount > 0 {
|
|
|
|
for _, access := range resourceControl.TeamAccesses {
|
|
|
|
for _, membership := range context.UserMemberships {
|
2017-07-13 07:12:06 +00:00
|
|
|
if membership.TeamID == access.TeamID {
|
2017-05-23 18:56:10 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if userAccessesCount > 0 {
|
|
|
|
for _, access := range resourceControl.UserAccesses {
|
|
|
|
if access.UserID == context.UserID {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
if context.IsAdmin {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if resourceControl.AdministratorsOnly {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
authorizedTeamAccess := false
|
|
|
|
for _, access := range resourceControl.TeamAccesses {
|
|
|
|
for _, membership := range context.UserMemberships {
|
|
|
|
if membership.TeamID == access.TeamID {
|
|
|
|
authorizedTeamAccess = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !authorizedTeamAccess {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
authorizedUserAccess := false
|
|
|
|
for _, access := range resourceControl.UserAccesses {
|
|
|
|
if context.UserID == access.UserID {
|
|
|
|
authorizedUserAccess = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !authorizedUserAccess {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-05-23 18:56:10 +00:00
|
|
|
// AuthorizedResourceControlUpdate ensure that the user can update a resource control object.
|
|
|
|
// It reuses the creation restrictions and adds extra checks.
|
|
|
|
// A non-administrator user cannot update a resource control where:
|
|
|
|
// * he wants to put one or more user in the user accesses
|
|
|
|
func AuthorizedResourceControlUpdate(resourceControl *portainer.ResourceControl, context *RestrictedRequestContext) bool {
|
|
|
|
userAccessesCount := len(resourceControl.UserAccesses)
|
|
|
|
if !context.IsAdmin && userAccessesCount > 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return AuthorizedResourceControlCreation(resourceControl, context)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AuthorizedResourceControlCreation ensure that the user can create a resource control object.
|
|
|
|
// A non-administrator user cannot create a resource control where:
|
|
|
|
// * the AdministratorsOnly flag is set
|
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
|
2018-06-11 15:58:46 +00:00
|
|
|
// * he wants tp 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
|
|
|
|
func AuthorizedResourceControlCreation(resourceControl *portainer.ResourceControl, context *RestrictedRequestContext) bool {
|
|
|
|
if context.IsAdmin {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if resourceControl.AdministratorsOnly {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
isMember := false
|
|
|
|
for _, membership := range context.UserMemberships {
|
|
|
|
if membership.TeamID == access.TeamID {
|
|
|
|
isMember = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !isMember {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// AuthorizedUserManagement ensure that access to the management of the specified user is granted.
|
|
|
|
// It will check if the user is either administrator or the owner of the user account.
|
|
|
|
func AuthorizedUserManagement(userID portainer.UserID, context *RestrictedRequestContext) bool {
|
|
|
|
if context.IsAdmin || context.UserID == userID {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2018-02-23 02:10:26 +00:00
|
|
|
|
2018-06-18 09:56:31 +00:00
|
|
|
// authorizedEndpointAccess ensure that the user can access the specified 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
|
2018-04-26 16:08:46 +00:00
|
|
|
// listed in the authorized teams of the 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 {
|
2018-04-26 16:08:46 +00:00
|
|
|
groupAccess := authorizedAccess(userID, memberships, endpointGroup.AuthorizedUsers, endpointGroup.AuthorizedTeams)
|
|
|
|
if !groupAccess {
|
|
|
|
return authorizedAccess(userID, memberships, endpoint.AuthorizedUsers, endpoint.AuthorizedTeams)
|
2018-02-23 02:10:26 +00:00
|
|
|
}
|
2018-04-26 16:08:46 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// AuthorizedEndpointGroupAccess ensure that the user can access the specified endpoint group.
|
|
|
|
// It will check if the user is part of the authorized users or part of a team that is
|
|
|
|
// listed in the authorized teams.
|
|
|
|
func AuthorizedEndpointGroupAccess(endpointGroup *portainer.EndpointGroup, userID portainer.UserID, memberships []portainer.TeamMembership) bool {
|
|
|
|
return authorizedAccess(userID, memberships, endpointGroup.AuthorizedUsers, endpointGroup.AuthorizedTeams)
|
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
|
|
|
|
// listed in the authorized teams.
|
|
|
|
func AuthorizedRegistryAccess(registry *portainer.Registry, userID portainer.UserID, memberships []portainer.TeamMembership) bool {
|
2018-04-26 16:08:46 +00:00
|
|
|
return authorizedAccess(userID, memberships, registry.AuthorizedUsers, registry.AuthorizedTeams)
|
|
|
|
}
|
|
|
|
|
|
|
|
func authorizedAccess(userID portainer.UserID, memberships []portainer.TeamMembership, authorizedUsers []portainer.UserID, authorizedTeams []portainer.TeamID) bool {
|
|
|
|
for _, authorizedUserID := range authorizedUsers {
|
2018-03-22 22:44:43 +00:00
|
|
|
if authorizedUserID == userID {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, membership := range memberships {
|
2018-04-26 16:08:46 +00:00
|
|
|
for _, authorizedTeamID := range authorizedTeams {
|
2018-03-22 22:44:43 +00:00
|
|
|
if membership.TeamID == authorizedTeamID {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|