Files
upstream-corteza/server/system/automation/rbac_handler.go
upstream-mirror 7e385fc126
Some checks failed
SonarQube / SonarQube (push) Failing after 5m28s
E2e test on main branch push / server and client test (map[name:admin port:8080]) (push) Failing after 3m33s
Server and lib tests / Library test (js) (push) Failing after 1m36s
Server and lib tests / Library test (vue) (push) Successful in 2m24s
Server and lib tests / Web client test (admin) (push) Successful in 2m52s
Server and lib tests / Web client test (compose) (push) Successful in 3m23s
Server and lib tests / Web client test (discovery) (push) Successful in 2m49s
Server and lib tests / Web client test (one) (push) Successful in 2m52s
Server and lib tests / Web client test (privacy) (push) Successful in 2m44s
Server and lib tests / Web client test (reporter) (push) Successful in 2m56s
Server and lib tests / Web client test (workflow) (push) Successful in 3m0s
Server and lib tests / Server tests (push) Failing after 4m55s
E2e test on main branch push / notify-failure (push) Failing after 9s
Server and lib tests / Send matrix testing notification (push) Successful in 2s
Mark stale issues and pull requests / stale (push) Successful in 40s
snapshot of https://github.com/cortezaproject/corteza.git (2024.9.x, 2026-09-18; blobs > 131072B omitted)
2026-09-18 01:22:23 +05:00

139 lines
3.3 KiB
Go

package automation
import (
"context"
"fmt"
"github.com/cortezaproject/corteza/server/pkg/rbac"
"github.com/cortezaproject/corteza/server/system/types"
)
type (
rbacService interface {
Can(ses rbac.Session, op string, res rbac.Resource) bool
Grant(ctx context.Context, rules ...*rbac.Rule) (err error)
}
rbacUserService interface {
FindByAny(ctx context.Context, identifier interface{}) (*types.User, error)
}
rbacHandler struct {
reg rbacHandlerRegistry
rbac rbacService
user rbacUserService
role roleService
}
auxResource struct{ res string }
)
var _ rbac.Resource = &auxResource{}
func (r auxResource) RbacResource() string {
return r.res
}
func RbacHandler(reg rbacHandlerRegistry, rbac rbacService, user rbacUserService, role roleService) *rbacHandler {
h := &rbacHandler{
reg: reg,
rbac: rbac,
user: user,
role: role,
}
h.register()
return h
}
func (h rbacHandler) allow(ctx context.Context, args *rbacAllowArgs) (err error) {
r, err := lookupRole(ctx, h.role, rolesLookupArgs{
hasLookup: args.hasRole,
Lookup: args.Role,
lookupID: args.roleID,
lookupHandle: args.roleHandle,
lookupRes: args.roleRes,
})
if err != nil {
return
}
return h.grant(ctx, rbac.AllowRule(r.ID, args.Resource.RbacResource(), args.Operation))
}
func (h rbacHandler) deny(ctx context.Context, args *rbacDenyArgs) (err error) {
r, err := lookupRole(ctx, h.role, rolesLookupArgs{
hasLookup: args.hasRole,
Lookup: args.Role,
lookupID: args.roleID,
lookupHandle: args.roleHandle,
lookupRes: args.roleRes,
})
if err != nil {
return
}
return h.grant(ctx, rbac.DenyRule(r.ID, args.Resource.RbacResource(), args.Operation))
}
func (h rbacHandler) inherit(ctx context.Context, args *rbacInheritArgs) (err error) {
r, err := lookupRole(ctx, h.role, rolesLookupArgs{
hasLookup: args.hasRole,
Lookup: args.Role,
lookupID: args.roleID,
lookupHandle: args.roleHandle,
lookupRes: args.roleRes,
})
if err != nil {
return
}
return h.grant(ctx, rbac.InheritRule(r.ID, args.Resource.RbacResource(), args.Operation))
}
// verifies grant op (granter needs to be allowed to grant on the component
func (h rbacHandler) grant(ctx context.Context, r *rbac.Rule) (err error) {
// we can safely create a fake component resource since there are
// no extra metadata hidden under it,
cmpRes := &auxResource{rbac.ResourceComponent(r.Resource)}
if !h.rbac.Can(rbac.ContextToSession(ctx), "grant", cmpRes) {
return fmt.Errorf("not allowed to grant %s", r.String())
}
return h.rbac.Grant(ctx, r)
}
func (h rbacHandler) check(ctx context.Context, args *rbacCheckArgs) (b *rbacCheckResults, err error) {
var secses rbac.Session
b = &rbacCheckResults{}
if args.hasUser {
// user was explicitly set to rbac checking function
//
// load all roles that this user is member of
// and create new security session out of it
var (
mm types.RoleMemberSet
ids []uint64
)
mm, err = h.role.Membership(ctx, args.User.ID)
_ = mm.Walk(func(m *types.RoleMember) error {
ids = append(ids, m.RoleID)
return nil
})
args.User.SetRoles(ids...)
secses = rbac.NewSession(ctx, args.User)
} else {
secses = rbac.ContextToSession(ctx)
}
b.Can = h.rbac.Can(secses, args.Operation, args.Resource)
return
}