2020-06-16 07:58:16 +00:00
|
|
|
package authorization
|
|
|
|
|
2020-07-06 23:18:39 +00:00
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/portainer/portainer/api"
|
|
|
|
)
|
2019-11-12 23:41:42 +00:00
|
|
|
|
|
|
|
// NewPrivateResourceControl will create a new private resource control associated to the resource specified by the
|
|
|
|
// identifier and type parameters. It automatically assigns it to the user specified by the userID parameter.
|
2020-06-16 07:58:16 +00:00
|
|
|
func NewPrivateResourceControl(resourceIdentifier string, resourceType portainer.ResourceControlType, userID portainer.UserID) *portainer.ResourceControl {
|
|
|
|
return &portainer.ResourceControl{
|
2019-11-12 23:41:42 +00:00
|
|
|
Type: resourceType,
|
|
|
|
ResourceID: resourceIdentifier,
|
|
|
|
SubResourceIDs: []string{},
|
2020-06-16 07:58:16 +00:00
|
|
|
UserAccesses: []portainer.UserResourceAccess{
|
2019-11-12 23:41:42 +00:00
|
|
|
{
|
|
|
|
UserID: userID,
|
2020-06-16 07:58:16 +00:00
|
|
|
AccessLevel: portainer.ReadWriteAccessLevel,
|
2019-11-12 23:41:42 +00:00
|
|
|
},
|
|
|
|
},
|
2020-06-16 07:58:16 +00:00
|
|
|
TeamAccesses: []portainer.TeamResourceAccess{},
|
2019-11-12 23:41:42 +00:00
|
|
|
AdministratorsOnly: false,
|
|
|
|
Public: false,
|
|
|
|
System: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewSystemResourceControl will create a new public resource control with the System flag set to true.
|
|
|
|
// These kind of resource control are not persisted and are created on the fly by the Portainer API.
|
2020-06-16 07:58:16 +00:00
|
|
|
func NewSystemResourceControl(resourceIdentifier string, resourceType portainer.ResourceControlType) *portainer.ResourceControl {
|
|
|
|
return &portainer.ResourceControl{
|
2019-11-12 23:41:42 +00:00
|
|
|
Type: resourceType,
|
|
|
|
ResourceID: resourceIdentifier,
|
|
|
|
SubResourceIDs: []string{},
|
2020-06-16 07:58:16 +00:00
|
|
|
UserAccesses: []portainer.UserResourceAccess{},
|
|
|
|
TeamAccesses: []portainer.TeamResourceAccess{},
|
2019-11-12 23:41:42 +00:00
|
|
|
AdministratorsOnly: false,
|
|
|
|
Public: true,
|
|
|
|
System: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewPublicResourceControl will create a new public resource control.
|
2020-06-16 07:58:16 +00:00
|
|
|
func NewPublicResourceControl(resourceIdentifier string, resourceType portainer.ResourceControlType) *portainer.ResourceControl {
|
|
|
|
return &portainer.ResourceControl{
|
2019-11-12 23:41:42 +00:00
|
|
|
Type: resourceType,
|
|
|
|
ResourceID: resourceIdentifier,
|
|
|
|
SubResourceIDs: []string{},
|
2020-06-16 07:58:16 +00:00
|
|
|
UserAccesses: []portainer.UserResourceAccess{},
|
|
|
|
TeamAccesses: []portainer.TeamResourceAccess{},
|
2019-11-12 23:41:42 +00:00
|
|
|
AdministratorsOnly: false,
|
|
|
|
Public: true,
|
|
|
|
System: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewRestrictedResourceControl will create a new resource control with user and team accesses restrictions.
|
2020-06-16 07:58:16 +00:00
|
|
|
func NewRestrictedResourceControl(resourceIdentifier string, resourceType portainer.ResourceControlType, userIDs []portainer.UserID, teamIDs []portainer.TeamID) *portainer.ResourceControl {
|
|
|
|
userAccesses := make([]portainer.UserResourceAccess, 0)
|
|
|
|
teamAccesses := make([]portainer.TeamResourceAccess, 0)
|
2019-11-12 23:41:42 +00:00
|
|
|
|
|
|
|
for _, id := range userIDs {
|
2020-06-16 07:58:16 +00:00
|
|
|
access := portainer.UserResourceAccess{
|
2019-11-12 23:41:42 +00:00
|
|
|
UserID: id,
|
2020-06-16 07:58:16 +00:00
|
|
|
AccessLevel: portainer.ReadWriteAccessLevel,
|
2019-11-12 23:41:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
userAccesses = append(userAccesses, access)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, id := range teamIDs {
|
2020-06-16 07:58:16 +00:00
|
|
|
access := portainer.TeamResourceAccess{
|
2019-11-12 23:41:42 +00:00
|
|
|
TeamID: id,
|
2020-06-16 07:58:16 +00:00
|
|
|
AccessLevel: portainer.ReadWriteAccessLevel,
|
2019-11-12 23:41:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
teamAccesses = append(teamAccesses, access)
|
|
|
|
}
|
|
|
|
|
2020-06-16 07:58:16 +00:00
|
|
|
return &portainer.ResourceControl{
|
2019-11-12 23:41:42 +00:00
|
|
|
Type: resourceType,
|
|
|
|
ResourceID: resourceIdentifier,
|
|
|
|
SubResourceIDs: []string{},
|
|
|
|
UserAccesses: userAccesses,
|
|
|
|
TeamAccesses: teamAccesses,
|
|
|
|
AdministratorsOnly: false,
|
|
|
|
Public: false,
|
|
|
|
System: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecorateStacks will iterate through a list of stacks, check for an associated resource control for each
|
|
|
|
// stack and decorate the stack element if a resource control is found.
|
2020-06-16 07:58:16 +00:00
|
|
|
func DecorateStacks(stacks []portainer.Stack, resourceControls []portainer.ResourceControl) []portainer.Stack {
|
2019-11-12 23:41:42 +00:00
|
|
|
for idx, stack := range stacks {
|
|
|
|
|
2020-06-16 07:58:16 +00:00
|
|
|
resourceControl := GetResourceControlByResourceIDAndType(stack.Name, portainer.StackResourceControl, resourceControls)
|
2019-11-12 23:41:42 +00:00
|
|
|
if resourceControl != nil {
|
|
|
|
stacks[idx].ResourceControl = resourceControl
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return stacks
|
|
|
|
}
|
|
|
|
|
2020-07-06 23:18:39 +00:00
|
|
|
// DecorateCustomTemplates will iterate through a list of custom templates, check for an associated resource control for each
|
|
|
|
// template and decorate the template element if a resource control is found.
|
|
|
|
func DecorateCustomTemplates(templates []portainer.CustomTemplate, resourceControls []portainer.ResourceControl) []portainer.CustomTemplate {
|
|
|
|
for idx, template := range templates {
|
|
|
|
|
|
|
|
resourceControl := GetResourceControlByResourceIDAndType(strconv.Itoa(int(template.ID)), portainer.CustomTemplateResourceControl, resourceControls)
|
|
|
|
if resourceControl != nil {
|
|
|
|
templates[idx].ResourceControl = resourceControl
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return templates
|
|
|
|
}
|
|
|
|
|
2019-11-12 23:41:42 +00:00
|
|
|
// FilterAuthorizedStacks returns a list of decorated stacks filtered through resource control access checks.
|
2020-06-16 07:58:16 +00:00
|
|
|
func FilterAuthorizedStacks(stacks []portainer.Stack, user *portainer.User, userTeamIDs []portainer.TeamID, rbacEnabled bool) []portainer.Stack {
|
|
|
|
authorizedStacks := make([]portainer.Stack, 0)
|
2019-11-12 23:41:42 +00:00
|
|
|
|
|
|
|
for _, stack := range stacks {
|
2020-06-16 07:58:16 +00:00
|
|
|
_, ok := user.EndpointAuthorizations[stack.EndpointID][portainer.EndpointResourcesAccess]
|
2019-11-12 23:41:42 +00:00
|
|
|
if rbacEnabled && ok {
|
|
|
|
authorizedStacks = append(authorizedStacks, stack)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if stack.ResourceControl != nil && UserCanAccessResource(user.ID, userTeamIDs, stack.ResourceControl) {
|
|
|
|
authorizedStacks = append(authorizedStacks, stack)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return authorizedStacks
|
|
|
|
}
|
|
|
|
|
2020-07-06 23:18:39 +00:00
|
|
|
// FilterAuthorizedCustomTemplates returns a list of decorated custom templates filtered through resource control access checks.
|
|
|
|
func FilterAuthorizedCustomTemplates(customTemplates []portainer.CustomTemplate, user *portainer.User, userTeamIDs []portainer.TeamID) []portainer.CustomTemplate {
|
|
|
|
authorizedTemplates := make([]portainer.CustomTemplate, 0)
|
|
|
|
|
|
|
|
for _, customTemplate := range customTemplates {
|
|
|
|
if customTemplate.CreatedByUserID == user.ID || (customTemplate.ResourceControl != nil && UserCanAccessResource(user.ID, userTeamIDs, customTemplate.ResourceControl)) {
|
|
|
|
authorizedTemplates = append(authorizedTemplates, customTemplate)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return authorizedTemplates
|
|
|
|
}
|
|
|
|
|
2019-11-12 23:41:42 +00:00
|
|
|
// UserCanAccessResource will valide that a user has permissions defined in the specified resource control
|
|
|
|
// based on its identifier and the team(s) he is part of.
|
2020-06-16 07:58:16 +00:00
|
|
|
func UserCanAccessResource(userID portainer.UserID, userTeamIDs []portainer.TeamID, resourceControl *portainer.ResourceControl) bool {
|
2019-11-12 23:41:42 +00:00
|
|
|
for _, authorizedUserAccess := range resourceControl.UserAccesses {
|
|
|
|
if userID == authorizedUserAccess.UserID {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, authorizedTeamAccess := range resourceControl.TeamAccesses {
|
|
|
|
for _, userTeamID := range userTeamIDs {
|
|
|
|
if userTeamID == authorizedTeamAccess.TeamID {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return resourceControl.Public
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetResourceControlByResourceIDAndType retrieves the first matching resource control in a set of resource controls
|
|
|
|
// based on the specified id and resource type parameters.
|
2020-06-16 07:58:16 +00:00
|
|
|
func GetResourceControlByResourceIDAndType(resourceID string, resourceType portainer.ResourceControlType, resourceControls []portainer.ResourceControl) *portainer.ResourceControl {
|
2019-11-12 23:41:42 +00:00
|
|
|
for _, resourceControl := range resourceControls {
|
|
|
|
if resourceID == resourceControl.ResourceID && resourceType == resourceControl.Type {
|
|
|
|
return &resourceControl
|
|
|
|
}
|
|
|
|
for _, subResourceID := range resourceControl.SubResourceIDs {
|
|
|
|
if resourceID == subResourceID {
|
|
|
|
return &resourceControl
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|