-
Notifications
You must be signed in to change notification settings - Fork 0
/
rbac.go
64 lines (53 loc) · 1.12 KB
/
rbac.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main
import (
"encoding/json"
"fmt"
"os"
)
type Permission struct {
Verb string `json:"verb"`
}
type Resource struct {
Name string
Permissions []string
}
type Service struct {
Name string
Resources []*Resource
WildcardResource *Resource
}
func LoadService(rbacPath, svcName string) (*Service, error) {
data := make(map[string][]Permission)
path := fmt.Sprintf("%s/%s.json", rbacPath, svcName)
reader, err := os.OpenFile(path, os.O_RDONLY, 0600)
if err != nil {
return nil, err
}
dec := json.NewDecoder(reader)
err = dec.Decode(&data)
if err != nil {
return nil, err
}
service := &Service{
Name: svcName,
Resources: make([]*Resource, 0, len(data)),
}
for resName, perms := range data {
res := &Resource{
Name: resName,
Permissions: make([]string, 0, len(perms)),
}
for _, perm := range perms {
if perm.Verb == "*" {
continue
}
res.Permissions = append(res.Permissions, perm.Verb)
}
if resName == "*" {
service.WildcardResource = res
} else {
service.Resources = append(service.Resources, res)
}
}
return service, nil
}