Support context roles support in rbac pkg

This commit is contained in:
Denis Arh
2021-07-08 11:22:11 +02:00
parent 6a6f74d4a6
commit b3da377c2d
19 changed files with 344 additions and 139 deletions
@@ -10,7 +10,6 @@ import (
"context"
"github.com/cortezaproject/corteza-server/pkg/rbac"
"github.com/cortezaproject/corteza-server/pkg/actionlog"
internalAuth "github.com/cortezaproject/corteza-server/pkg/auth"
{{- range .Imports }}
{{ normalizeImport . }}
{{- end }}
@@ -22,7 +21,7 @@ type (
actionlog actionlog.Recorder
rbac interface {
Can([]uint64, string, rbac.Resource) bool
Can(rbac.Session, string, rbac.Resource) bool
Grant(context.Context, ...*rbac.Rule) error
FindRulesByRoleID(roleID uint64) (rr rbac.RuleSet)
}
@@ -39,15 +38,7 @@ func AccessControl() *accessControl {
func (svc accessControl) can(ctx context.Context, op string, res rbac.Resource) bool {
var (
identity = internalAuth.GetIdentityFromContext(ctx)
)
if identity == nil {
panic("expecting identity in context")
}
return svc.rbac.Can(identity.Roles(), op, res)
return svc.rbac.Can(rbac.ContextToSession(ctx), op, res)
}
// Effective returns a list of effective permissions for all given resource
@@ -32,7 +32,7 @@ const (
//
// This function is auto-generated
func (r {{ .Resource }}) RbacResource() string {
return {{ .Resource }}RbacResource({{ if .RBAC.Resource }}{{ range .RBAC.Resource.Elements }}r.{{ unexport . }},{{ end }}{{ end }})
return {{ .Resource }}RbacResource({{ if .RBAC.Resource }}{{ range .RBAC.Resource.Elements }}r.{{ export . }},{{ end }}{{ end }})
}
// {{ .Resource }}RbacResource returns string representation of RBAC resource for {{ .Resource }}
@@ -53,4 +53,31 @@ func {{ .Resource }}RbacResource({{ if .RBAC.Resource }}{{ range .RBAC.Resource.
{{- end }}
return out
}
{{ if .RBAC.Resource.Attributes }}
// RbacAttributes returns resource attributes used for generating list of contextual roles
//
// This function is auto-generated
func (r {{ .Resource }}) RbacAttributes() map[string]interface{} {
return {{ unexport .Resource }}RbacAttributes(r)
}
{{ if .RBAC.Resource.Attributes.Fields }}
// {{ .Resource }}RbacResource returns string representation of RBAC resource for {{ .Resource }}
//
// RBAC resource is in the {{ .RBAC.Schema }}:/... format
//
// This function is auto-generated
func {{ unexport .Resource }}RbacAttributes(r {{ .Resource }}) map[string]interface{} {
return map[string]interface{}{
{{- range .RBAC.Resource.Attributes.Fields }}
{{ printf "%q" . }}: r.{{ export . }},
{{- end }}
}
}
{{- end }}
{{- end }}
{{- end }}
+16 -1
View File
@@ -27,7 +27,8 @@ type (
}
rbacResource struct {
Elements []string
Elements []string
Attributes *rbacAttributes
}
rbacOperations []*rbacOperation
@@ -37,6 +38,10 @@ type (
CanFnName string `yaml:"canFnName"`
Description string
}
rbacAttributes struct {
Fields []string `yaml:"-"`
}
)
func (set *rbacOperations) UnmarshalYAML(n *yaml.Node) error {
@@ -64,6 +69,16 @@ func (op *rbacOperation) UnmarshalYAML(n *yaml.Node) error {
return n.Decode(aux)
}
func (a *rbacAttributes) UnmarshalYAML(n *yaml.Node) error {
if y7s.IsKind(n, yaml.ScalarNode) {
return nil
}
// if not scalar, assume we will get list of fields
a.Fields = make([]string, 0)
return n.Decode(&a.Fields)
}
func RbacOperationCanFnName(res, op string) string {
// when check function name is not explicitly defined we try
// to use resource and operation name and generate easy-to-read name
+1 -1
View File
@@ -44,7 +44,7 @@ func Export(pp ...string) (out string) {
func Unexport(pp ...string) (out string) {
out = Export(pp...)
if len(out) > 0 {
if len(out) == 0 {
return
}