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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
package policies
import (
"embed"
_ "embed"
"fmt"
"io/fs"
"strings"
"github.com/cedar-policy/cedar-go"
"github.com/cedar-policy/cedar-go/types"
xlog "gitlab.com/mokhax/spike/pkg/log"
)
//go:embed *.cedar *.json
var files embed.FS
var All *cedar.PolicySet = cedar.NewPolicySet()
var Entities cedar.EntityMap = cedar.EntityMap{}
func init() {
err := fs.WalkDir(files, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil
}
if strings.HasSuffix(path, ".cedar") {
content, err := fs.ReadFile(files, path)
if err != nil {
return err
}
policy := cedar.Policy{}
if err := policy.UnmarshalCedar(content); err != nil {
return err
}
policy.SetFilename(path)
All.Add(cedar.PolicyID(path), &policy)
} else if strings.HasSuffix(path, ".json") {
content, err := fs.ReadFile(files, path)
if err != nil {
return err
}
if err := Entities.UnmarshalJSON(content); err != nil {
return err
}
}
return nil
})
if err != nil {
xlog.Default.Printf("error: %v\n", err)
}
}
func Allowed(request cedar.Request) bool {
ok, diagnostic := All.IsAuthorized(Entities, request)
fmt.Printf("%v: %v -> %v %v%v\n", ok, request.Principal, request.Action, request.Context.Map(), request.Resource)
if len(diagnostic.Errors) > 0 {
for err := range diagnostic.Errors {
xlog.Default.Printf("%v\n", err)
}
}
if len(diagnostic.Reasons) > 0 {
for reason := range diagnostic.Reasons {
xlog.Default.Printf("%v\n", reason)
}
}
return ok == types.Allow
}
|