Add additional utilities to work with resources
This commit is contained in:
parent
e7a36e26c5
commit
f5a4bd9a30
@ -1,6 +1,8 @@
|
||||
package envoy
|
||||
|
||||
import "github.com/cortezaproject/corteza-server/pkg/envoy/resource"
|
||||
import (
|
||||
"github.com/cortezaproject/corteza-server/pkg/envoy/resource"
|
||||
)
|
||||
|
||||
// NormalizeResourceTranslations takes the provided resource.ResourceTranslation
|
||||
// and merges duplicates based on the Priority parameter
|
||||
@ -63,3 +65,35 @@ func NormalizeResourceTranslations(rr ...resource.Interface) []resource.Interfac
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
func appendRefSet(a resource.RefSet, b *resource.Ref) resource.RefSet {
|
||||
return append(a, b)
|
||||
}
|
||||
|
||||
// FilterRequiredResourceTranslations returns only resource translations relevant for the given resources
|
||||
func FilterRequiredResourceTranslations(request resource.InterfaceSet, translations []*resource.ResourceTranslation) (out []*resource.ResourceTranslation) {
|
||||
out = make([]*resource.ResourceTranslation, 0, 100)
|
||||
|
||||
// Filter
|
||||
procResSet(request, func(r resource.Interface) {
|
||||
localeRes, ok := r.(resource.LocaleInterface)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
_, ref, pp := localeRes.ResourceTranslationParts()
|
||||
resourceRefSet := appendRefSet(pp, ref)
|
||||
|
||||
for _, t := range translations {
|
||||
translationRefSet := appendRefSet(t.RefPath, t.RefRes)
|
||||
// Res. tr. use strict equality to determine where it falls into
|
||||
if !translationRefSet.Equals(resourceRefSet) {
|
||||
continue
|
||||
}
|
||||
|
||||
out = append(out, t)
|
||||
}
|
||||
})
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
41
pkg/envoy/rbac.go
Normal file
41
pkg/envoy/rbac.go
Normal file
@ -0,0 +1,41 @@
|
||||
package envoy
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/cortezaproject/corteza-server/pkg/envoy/resource"
|
||||
)
|
||||
|
||||
// FilterRequestedRBACRules returns only RBAC rules relevant for the given resources
|
||||
func FilterRequestedRBACRules(request resource.InterfaceSet, rules []*resource.RbacRule) (out []*resource.RbacRule) {
|
||||
out = make([]*resource.RbacRule, 0, 10)
|
||||
|
||||
// Filter
|
||||
dupRuleIndex := make(map[string]bool)
|
||||
procResSet(request, func(r resource.Interface) {
|
||||
rbacRes, ok := r.(resource.RBACInterface)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
_, ref, pp := rbacRes.RBACParts()
|
||||
resourceRefSet := appendRefSet(pp, ref)
|
||||
|
||||
for _, rule := range rules {
|
||||
k := fmt.Sprintf("%s, %s, %d; %d", rule.Res.Resource, rule.Res.Operation, rule.Res.Access, rule.Res.RoleID)
|
||||
if dupRuleIndex[k] {
|
||||
continue
|
||||
}
|
||||
dupRuleIndex[k] = true
|
||||
ruleRefSet := appendRefSet(rule.RefPath, rule.RefRes)
|
||||
// Checking if rule is <= resource since wildflags can be used
|
||||
if !ruleRefSet.IsSubset(resourceRefSet) {
|
||||
continue
|
||||
}
|
||||
|
||||
out = append(out, rule)
|
||||
}
|
||||
})
|
||||
|
||||
return
|
||||
}
|
||||
19
pkg/envoy/shared.go
Normal file
19
pkg/envoy/shared.go
Normal file
@ -0,0 +1,19 @@
|
||||
package envoy
|
||||
|
||||
import "github.com/cortezaproject/corteza-server/pkg/envoy/resource"
|
||||
|
||||
// procResSet is a little utility to run some op over given resources
|
||||
//
|
||||
// Helps cover special cases such as modules & module fields
|
||||
func procResSet(resources resource.InterfaceSet, fn func(r resource.Interface)) {
|
||||
for _, res := range resources {
|
||||
fn(res)
|
||||
|
||||
// Special case for modules since it has
|
||||
if modR, ok := res.(*resource.ComposeModule); ok {
|
||||
for _, f := range modR.ResFields {
|
||||
fn(f)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user