Files
upstream-corteza/server/pkg/envoy/resource/rbac_rule.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

123 lines
2.5 KiB
Go

package resource
import (
"fmt"
composeTypes "github.com/cortezaproject/corteza/server/compose/types"
"github.com/cortezaproject/corteza/server/pkg/rbac"
)
type (
RbacRule struct {
*base
Res *rbac.Rule
RefResource string
RefRes *Ref
RefRole *Ref
RefPath []*Ref
}
)
func NewRbacRule(res *rbac.Rule, refRole, refRes *Ref, refResource string, refPath ...*Ref) *RbacRule {
r := &RbacRule{base: &base{}}
r.SetResourceType(RbacResourceType)
r.Res = res
r.RefRole = r.addRef(refRole)
r.RefResource = refResource
if refRes != nil {
r.RefRes = r.addRef(refRes)
}
// any additional constraints
for i, rp := range refPath {
ref := MakeRef(rp.ResourceType, rp.Identifiers)
if r.RefRes != nil {
r.RefRes.Constraint(ref)
}
if i > 0 {
for j := i - 1; j < i; j++ {
aux := refPath[j]
ref = ref.Constraint(MakeRef(aux.ResourceType, aux.Identifiers))
}
}
r.RefPath = append(r.RefPath, r.addRef(ref))
}
// Handle cases for nested resources
if refRes != nil {
switch refRes.ResourceType {
case composeTypes.RecordResourceType:
r.handleComposeRecord(res, refPath)
case composeTypes.ModuleFieldResourceType:
r.handleComposeModuleField(res, refPath)
}
}
return r
}
func (r *RbacRule) WrapError(act string, err error) error {
return fmt.Errorf("%s %s %v: %s",
act,
r.ResourceType(),
fmt.Sprintf("{ role: %s, resource: %s, operation: %s, access: %s }", r.RefRole.Identifiers.First(), r.RefResource, r.Res.Operation, r.Res.Access),
err,
)
}
func (r *RbacRule) Resource() interface{} {
return r.Res
}
func (r *RbacRule) ReRef(old RefSet, new RefSet) {
r.base.ReRef(old, new)
// Care for wildcards
if r.RefRes != nil {
for i, o := range old {
if o.equals(r.RefRes) {
r.RefRes = new[i]
break
}
}
}
for i, o := range old {
if RefSet(r.RefPath).findRef(o) > -1 {
r.RefPath = RefSet(r.RefPath).replaceRef(o, new[i])
}
}
}
func (r *RbacRule) handleComposeRecord(res *rbac.Rule, refPath []*Ref) {
// records are grouped under module
if len(refPath) < 2 {
return
}
r.AddRef(composeTypes.RecordResourceType, refPath[1].Identifiers.StringSlice()...)
}
func (r *RbacRule) handleComposeModuleField(res *rbac.Rule, refPath []*Ref) {
// module fields are grouped under module
if len(refPath) < 2 {
return
}
r.AddRef(composeTypes.ModuleFieldResourceTranslationType, refPath[1].Identifiers.StringSlice()...)
}
func RbacResourceErrNotFound(ii Identifiers) error {
return fmt.Errorf("rbac resource not found %v", ii.StringSlice())
}