RBAC: Add function to generate wildcards from prefix (#54275)

* RBAC: Move metadata to own file

* RBAC: Rename test files

* RBAC: Add wildcard structure and helper function to generate wildcards
from prefix

* RBAC: Refactor filter to use WildcardsFromPrefix

* RBAC: Refactor GetResourceMetadata to use WildcardsFromPrefix
This commit is contained in:
Karl Persson
2022-08-26 17:10:35 +02:00
committed by GitHub
parent 6227528ea0
commit 9d2f5ef62f
7 changed files with 128 additions and 91 deletions
+29
View File
@@ -141,3 +141,32 @@ func (s scopeProviderImpl) GetResourceAllScope() string {
func (s scopeProviderImpl) GetResourceAllIDScope() string {
return GetResourceAllIDScope(s.root)
}
// WildcardsFromPrefix generates valid wildcards from prefix
// datasource:uid: => "*", "datasource:*", "datasource:uid:*"
func WildcardsFromPrefix(prefix string) Wildcards {
var b strings.Builder
wildcards := Wildcards{"*"}
parts := strings.Split(prefix, ":")
for _, p := range parts {
if p == "" {
continue
}
b.WriteString(p)
b.WriteRune(':')
wildcards = append(wildcards, b.String()+"*")
}
return wildcards
}
type Wildcards []string
// Contains check if wildcards contains scope
func (wildcards Wildcards) Contains(scope string) bool {
for _, w := range wildcards {
if scope == w {
return true
}
}
return false
}